Advertisement
Guest User

Untitled

a guest
May 22nd, 2019
83
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.32 KB | None | 0 0
  1. #pragma once
  2.  
  3. template<typename T, int items_per_bucket>
  4. struct Bucket_Array
  5. {
  6. struct Bucket
  7. {
  8. T items[items_per_bucket];
  9. };
  10.  
  11. int user_length = 0; // Users may set this if they want to keep track of how many items are in this instance in total. It doesn't affect bucket allocation though.
  12. int _bucket_count = 0; // Number of currently allocated buckets in _all_buckets. Changes when capacity is set.
  13. Bucket **_all_buckets = nullptr;
  14.  
  15. void clear()
  16. {
  17. if (this->_all_buckets == nullptr) { return; }
  18. for (int i = 0; i < this->_bucket_count; ++i)
  19. {
  20. delete this->_all_buckets[i];
  21. }
  22. delete this->_all_buckets;
  23. this->_all_buckets = nullptr;
  24. this->_bucket_count = 0;
  25. }
  26.  
  27. // Gets a pointer to the item at the specified absolute index.
  28. // Use this to read or write item values. Returns null if the requested index is out of range.
  29. T *get_slot(int index)
  30. {
  31. int bucket_index = index / items_per_bucket;
  32. if (bucket_index >= this->_bucket_count) return nullptr;
  33. int index_in_bucket = index % items_per_bucket;
  34. Bucket *bucket = _all_buckets[bucket_index];
  35. T *slot = &bucket->items[index_in_bucket];
  36. return slot;
  37. }
  38.  
  39. // Fancy C++ reference alternative for get_slot()
  40. T &operator[](int index) { return *this->get_slot(index); }
  41.  
  42. static int get_required_bucket_count(int capacity)
  43. {
  44. return (capacity + (items_per_bucket - 1)) / items_per_bucket;
  45. }
  46.  
  47. // If required, changes the number of buckets so that at least the specified number of array items can be stored.
  48. // Does not shrink the number of buckets.
  49. void ensure_capacity(int min_capacity)
  50. {
  51. int required_bucket_count = get_required_bucket_count(min_capacity);
  52. if (this->_bucket_count >= required_bucket_count) { return; }
  53. this->_change_bucket_count(required_bucket_count);
  54. }
  55.  
  56. // If required, changes the number of buckets so that at least the specified number of array items can be stored with minimum overhead,
  57. // and frees any superfluous buckets that may currently be allocated. Pointers to items in the freed buckets become invalid.
  58. void set_capacity(int capacity)
  59. {
  60. int required_bucket_count = get_required_bucket_count(capacity);
  61. if (this->_bucket_count == required_bucket_count) { return; }
  62. this->_change_bucket_count(required_bucket_count);
  63. }
  64.  
  65. void _change_bucket_count(int required_bucket_count)
  66. {
  67. int current_bucket_count = this->_bucket_count;
  68. Bucket **new_array = new Bucket *[required_bucket_count];
  69. int bucket_index = 0;
  70. if (_all_buckets != nullptr)
  71. {
  72. if (required_bucket_count >= current_bucket_count)
  73. {
  74. // Copy all existing buckets, no freeing, allocate new ones if necessary
  75. for (; bucket_index < current_bucket_count; ++bucket_index)
  76. {
  77. new_array[bucket_index] = this->_all_buckets[bucket_index];
  78. }
  79. }
  80. else
  81. {
  82. // Copy some buckets, free the rest
  83. for (; bucket_index < required_bucket_count; ++bucket_index)
  84. {
  85. new_array[bucket_index] = this->_all_buckets[bucket_index];
  86. }
  87. for (; bucket_index < current_bucket_count; ++bucket_index)
  88. {
  89. delete this->_all_buckets[bucket_index];
  90. }
  91. }
  92. }
  93.  
  94. for (; bucket_index < required_bucket_count; ++bucket_index)
  95. {
  96. new_array[bucket_index] = new Bucket();
  97. }
  98.  
  99. Bucket **old_array = this->_all_buckets;
  100. this->_all_buckets = new_array;
  101. this->_bucket_count = required_bucket_count;
  102. delete old_array;
  103. }
  104.  
  105. ~Bucket_Array()
  106. {
  107. this->clear();
  108. }
  109. };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement