1 | // SPDX-License-Identifier: MIT OR MPL-2.0 OR LGPL-2.1-or-later OR GPL-2.0-or-later |
---|---|
2 | // Copyright 2011, SIL International, All rights reserved. |
3 | |
4 | #include <cassert> |
5 | #include "inc/Sparse.h" |
6 | #include "inc/bits.h" |
7 | |
8 | using namespace graphite2; |
9 | |
10 | const sparse::chunk sparse::empty_chunk = {0,0}; |
11 | |
12 | sparse::~sparse() throw() |
13 | { |
14 | if (m_array.map == &empty_chunk) return; |
15 | free(m_array.values); |
16 | } |
17 | |
18 | |
19 | sparse::mapped_type sparse::operator [] (const key_type k) const throw() |
20 | { |
21 | mapped_type g = key_type(k/SIZEOF_CHUNK - m_nchunks) >> (sizeof k*8 - 1); |
22 | const chunk & c = m_array.map[g*k/SIZEOF_CHUNK]; |
23 | const mask_t m = c.mask >> (SIZEOF_CHUNK - 1 - (k%SIZEOF_CHUNK)); |
24 | g *= m & 1; |
25 | |
26 | return g*m_array.values[g*(c.offset + bit_set_count(m >> 1))]; |
27 | } |
28 | |
29 | |
30 | size_t sparse::capacity() const throw() |
31 | { |
32 | size_t n = m_nchunks, |
33 | s = 0; |
34 | |
35 | for (const chunk *ci=m_array.map; n; --n, ++ci) |
36 | s += bit_set_count(ci->mask); |
37 | |
38 | return s; |
39 | } |
40 |