17 #ifndef _SPARSE_ARRAY_H_
18 #define _SPARSE_ARRAY_H_
36 template <
typename T, u
int64_t SIZE,
unsigned PAGE_ADDR_BITS = 24>
class sparse_array {
38 static_assert(SIZE > 0,
"sparse_array size must be greater than 0");
40 const uint64_t page_addr_mask = (1 << PAGE_ADDR_BITS) - 1;
42 const uint64_t page_size = (1 << PAGE_ADDR_BITS);
44 const unsigned page_count = (SIZE + page_size - 1) / page_size;
46 const uint64_t page_addr_width = PAGE_ADDR_BITS;
48 using page_type = std::array<T, 1 << PAGE_ADDR_BITS>;
68 T nr = addr >> PAGE_ADDR_BITS;
69 if(arr[nr] ==
nullptr)
70 arr[nr] =
new page_type();
71 return arr[nr]->at(addr & page_addr_mask);
80 assert(page_nr < page_count);
81 if(arr[page_nr] ==
nullptr)
82 arr.at(page_nr) =
new page_type();
83 return *(arr[page_nr]);
93 T nr = addr >> PAGE_ADDR_BITS;
94 return arr.at(nr) !=
nullptr;
101 uint64_t
size() {
return SIZE; }
104 std::array<page_type*, SIZE / (1 << PAGE_ADDR_BITS) + 1> arr;
a sparse array suitable for large sizes
bool is_allocated(uint32_t addr)
page_type & operator()(uint32_t page_nr)
T & operator[](uint32_t addr)