17 #ifndef _SPARSE_ARRAY_H_
18 #define _SPARSE_ARRAY_H_
37 template <
typename T, u
int64_t SIZE,
unsigned PAGE_ADDR_BITS = 24>
class sparse_array {
39 static_assert(SIZE > 0,
"sparse_array size must be greater than 0");
41 const uint64_t page_addr_mask = (1 << PAGE_ADDR_BITS) - 1;
43 const uint64_t page_size = (1 << PAGE_ADDR_BITS);
45 const unsigned page_count = (SIZE + page_size - 1) / page_size;
47 const uint64_t page_addr_width = PAGE_ADDR_BITS;
49 using page_type = std::array<T, 1 << PAGE_ADDR_BITS>;
69 T nr = addr >> PAGE_ADDR_BITS;
70 if(arr[nr] ==
nullptr)
71 arr[nr] =
new page_type();
72 return arr[nr]->at(addr & page_addr_mask);
81 assert(page_nr < page_count);
82 if(arr[page_nr] ==
nullptr)
83 arr.at(page_nr) =
new page_type();
84 return *(arr[page_nr]);
94 T nr = addr >> PAGE_ADDR_BITS;
95 return arr.at(nr) !=
nullptr;
102 uint64_t
size() {
return SIZE; }
105 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)