Line data Source code
1 : /* GRAPHITE2 LICENSING
2 :
3 : Copyright 2011, SIL International
4 : All rights reserved.
5 :
6 : This library is free software; you can redistribute it and/or modify
7 : it under the terms of the GNU Lesser General Public License as published
8 : by the Free Software Foundation; either version 2.1 of License, or
9 : (at your option) any later version.
10 :
11 : This program is distributed in the hope that it will be useful,
12 : but WITHOUT ANY WARRANTY; without even the implied warranty of
13 : MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 : Lesser General Public License for more details.
15 :
16 : You should also have received a copy of the GNU Lesser General Public
17 : License along with this library in the file named "LICENSE".
18 : If not, write to the Free Software Foundation, 51 Franklin Street,
19 : Suite 500, Boston, MA 02110-1335, USA or visit their web page on the
20 : internet at http://www.fsf.org/licenses/lgpl.html.
21 :
22 : Alternatively, the contents of this file may be used under the terms of the
23 : Mozilla Public License (http://mozilla.org/MPL) or the GNU General Public
24 : License, as published by the Free Software Foundation, either version 2
25 : of the License or (at your option) any later version.
26 : */
27 : #include <cassert>
28 : #include "inc/Sparse.h"
29 : #include "inc/bits.h"
30 :
31 : using namespace graphite2;
32 :
33 : const sparse::chunk sparse::empty_chunk = {0,0};
34 :
35 0 : sparse::~sparse() throw()
36 : {
37 0 : if (m_array.map == &empty_chunk) return;
38 0 : free(m_array.values);
39 0 : }
40 :
41 :
42 0 : sparse::mapped_type sparse::operator [] (const key_type k) const throw()
43 : {
44 0 : mapped_type g = key_type(k/SIZEOF_CHUNK - m_nchunks) >> (sizeof k*8 - 1);
45 0 : const chunk & c = m_array.map[g*k/SIZEOF_CHUNK];
46 0 : const mask_t m = c.mask >> (SIZEOF_CHUNK - 1 - (k%SIZEOF_CHUNK));
47 0 : g *= m & 1;
48 :
49 0 : return g*m_array.values[g*(c.offset + bit_set_count(m >> 1))];
50 : }
51 :
52 :
53 0 : size_t sparse::capacity() const throw()
54 : {
55 0 : size_t n = m_nchunks,
56 0 : s = 0;
57 :
58 0 : for (const chunk *ci=m_array.map; n; --n, ++ci)
59 0 : s += bit_set_count(ci->mask);
60 :
61 0 : return s;
62 : }
|