1
0

ProgressTracker.h 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. #include <map>
  2. #include "AutoLock.h"
  3. #include <bfc/platform/types.h>
  4. class ProgressTracker
  5. {
  6. public:
  7. static const uint64_t null_position;
  8. ProgressTracker()
  9. {
  10. current_position=0;
  11. current_chunk=0;
  12. chunks[0]=0;
  13. chunks[null_position]=null_position;
  14. };
  15. void Write(uint64_t bytes_written)
  16. {
  17. Nullsoft::Utility::AutoLock list_lock(list_guard);
  18. ChunkList::iterator next, itr = chunks.find(current_chunk);
  19. current_position += bytes_written;
  20. if (itr->second < current_position)
  21. itr->second = current_position;
  22. for (;;)
  23. {
  24. next = itr;
  25. ++next;
  26. if (next != chunks.end() && (next->first <= itr->second))
  27. {
  28. itr->second = next->second;
  29. chunks.erase(next);
  30. }
  31. else
  32. break;
  33. }
  34. }
  35. bool Valid(uint64_t requested_position, uint64_t requested_end)
  36. {
  37. Nullsoft::Utility::AutoLock list_lock(list_guard);
  38. for (ChunkList::iterator itr=chunks.begin();itr!=chunks.end();itr++)
  39. {
  40. if (requested_position >= itr->first)
  41. {
  42. if (requested_position < itr->second)
  43. {
  44. if (requested_end <= itr->second)
  45. {
  46. return true;
  47. }
  48. else
  49. {
  50. return false;
  51. }
  52. }
  53. }
  54. }
  55. return false;
  56. }
  57. bool Seek(uint64_t requested_position, uint64_t requested_end, uint64_t *new_start, uint64_t *new_end)
  58. {
  59. Nullsoft::Utility::AutoLock list_lock(list_guard);
  60. uint64_t last_good_start=0;
  61. ChunkList::iterator itr;
  62. for (itr=chunks.begin();itr!=chunks.end();itr++)
  63. {
  64. if (requested_position >= itr->first)
  65. {
  66. current_chunk = itr->first;
  67. if (requested_position <= itr->second)
  68. {
  69. ChunkList::iterator next = itr;
  70. ++next;
  71. *new_end = next->first;
  72. *new_start = current_position = itr->second;
  73. if (requested_end <= itr->second)
  74. {
  75. return true;
  76. }
  77. else
  78. {
  79. return false;
  80. }
  81. }
  82. else
  83. {
  84. last_good_start = itr->second;
  85. }
  86. }
  87. }
  88. if (last_good_start > requested_position)
  89. *new_start = current_position = last_good_start;
  90. else
  91. {
  92. *new_start = current_chunk = current_position = requested_position;
  93. chunks[current_chunk] = current_chunk;
  94. }
  95. *new_end = null_position;
  96. return false;
  97. }
  98. #if 0
  99. void Dump()
  100. {
  101. ChunkList::iterator itr;
  102. for (itr=chunks.begin();itr!=chunks.end();itr++)
  103. {
  104. printf("%I64u - %I64u\n", itr->first, itr->second);
  105. }
  106. }
  107. #endif
  108. typedef std::map<uint64_t, uint64_t> ChunkList;
  109. ChunkList chunks;
  110. uint64_t current_chunk;
  111. uint64_t current_position;
  112. Nullsoft::Utility::LockGuard list_guard;
  113. };
  114. const uint64_t ProgressTracker::null_position = (uint64_t)-1;