scc 2025.09
SystemC components library
sparse_array.h
1/*******************************************************************************
2 * Copyright 2017 MINRES Technologies GmbH
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 *******************************************************************************/
16
17#ifndef _SPARSE_ARRAY_H_
18#define _SPARSE_ARRAY_H_
19
20#include "ities.h"
21#include <array>
22#include <cassert>
23
29namespace util {
30
37template <typename T> class sparse_array_b {
38public:
39 const uint64_t mem_size;
40
41 const uint64_t page_addr_mask;
42
43 const uint64_t page_size;
44
45 const unsigned page_count;
46
47 const uint64_t page_addr_width;
48
49 using page_type = std::vector<T>;
53 sparse_array_b(uint64_t SIZE, unsigned PAGE_ADDR_BITS = 24)
54 : mem_size{SIZE}
55 , page_addr_mask{(1ULL << PAGE_ADDR_BITS) - 1}
56 , page_size{(1ULL << PAGE_ADDR_BITS)}
57 , page_count{(SIZE + page_size - 1) / page_size}
58 , page_addr_width{PAGE_ADDR_BITS}
59 , arr{SIZE / (1 << PAGE_ADDR_BITS) + 1} {
60 assert(SIZE > 0 && "sparse_array size must be greater than 0");
61 arr.fill(nullptr);
62 }
63
67 for(auto i : arr)
68 delete i;
69 }
70
76 T& operator[](uint32_t addr) {
77 assert(addr < mem_size);
78 T nr = addr >> page_addr_width;
79 if(arr[nr] == nullptr)
80 arr[nr] = new page_type();
81 return arr[nr]->at(addr & page_addr_mask);
82 }
83
89 page_type& operator()(uint32_t page_nr) {
90 assert(page_nr < page_count);
91 if(arr[page_nr] == nullptr)
92 arr.at(page_nr) = new page_type(page_size);
93 return *(arr[page_nr]);
94 }
95
101 bool is_allocated(uint32_t addr) {
102 assert(addr < mem_size);
103 T nr = addr >> page_addr_width;
104 return arr.at(nr) != nullptr;
105 }
106
111 uint64_t size() { return mem_size; }
112
113protected:
114 std::vector<page_type*> arr;
115};
116
123template <typename T, uint64_t SIZE, unsigned PAGE_ADDR_BITS = 24> class sparse_array {
124public:
125 static_assert(SIZE > 0, "sparse_array size must be greater than 0");
126
127 static constexpr uint64_t page_addr_mask = (1 << PAGE_ADDR_BITS) - 1;
128
129 static constexpr uint64_t page_size = (1 << PAGE_ADDR_BITS);
130
131 static constexpr unsigned page_count = (SIZE + page_size - 1) / page_size;
132
133 static constexpr uint64_t page_addr_width = PAGE_ADDR_BITS;
134
135 using page_type = std::array<T, 1 << PAGE_ADDR_BITS>;
139 sparse_array() { arr.fill(nullptr); }
144 for(auto i : arr)
145 delete i;
146 }
147
153 T& operator[](uint32_t addr) {
154 assert(addr < SIZE);
155 T nr = addr >> PAGE_ADDR_BITS;
156 if(arr[nr] == nullptr)
157 arr[nr] = new page_type();
158 return arr[nr]->at(addr & page_addr_mask);
159 }
160
166 page_type& operator()(uint32_t page_nr) {
167 assert(page_nr < page_count);
168 if(arr[page_nr] == nullptr)
169 arr.at(page_nr) = new page_type();
170 return *(arr[page_nr]);
171 }
172
178 bool is_allocated(uint32_t addr) {
179 assert(addr < SIZE);
180 T nr = addr >> PAGE_ADDR_BITS;
181 return arr.at(nr) != nullptr;
182 }
183
188 uint64_t size() { return SIZE; }
189
190protected:
191 std::array<page_type*, SIZE / (1 << PAGE_ADDR_BITS) + 1> arr;
192};
193} // namespace util
195#endif /* _SPARSE_ARRAY_H_ */
sparse_array_b(uint64_t SIZE, unsigned PAGE_ADDR_BITS=24)
T & operator[](uint32_t addr)
bool is_allocated(uint32_t addr)
page_type & operator()(uint32_t page_nr)
bool is_allocated(uint32_t addr)
page_type & operator()(uint32_t page_nr)
T & operator[](uint32_t addr)
SCC common utilities.
Definition bit_field.h:30