Thu, 22 Jan 2015 13:21:57 +0100
Incorporate requested changes from Mozilla in review:
https://bugzilla.mozilla.org/show_bug.cgi?id=1123480#c6
1 /* GRAPHITE2 LICENSING
3 Copyright 2011, SIL International
4 All rights reserved.
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.
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.
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.
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"
31 using namespace graphite2;
33 sparse::chunk sparse::empty_chunk = {0,0};
35 sparse::~sparse() throw()
36 {
37 if (m_array.map == &empty_chunk) return;
38 free(m_array.values);
39 }
42 sparse::mapped_type sparse::operator [] (const key_type k) const throw()
43 {
44 mapped_type g = key_type(k/SIZEOF_CHUNK - m_nchunks) >> (sizeof k*8 - 1);
45 const chunk & c = m_array.map[g*k/SIZEOF_CHUNK];
46 const mask_t m = c.mask >> (SIZEOF_CHUNK - 1 - (k%SIZEOF_CHUNK));
47 g *= m & 1;
49 return g*m_array.values[g*(c.offset + bit_set_count(m >> 1))];
50 }
53 size_t sparse::capacity() const throw()
54 {
55 size_t n = m_nchunks,
56 s = 0;
58 for (const chunk *ci=m_array.map; n; --n, ++ci)
59 s += bit_set_count(ci->mask);
61 return s;
62 }