gfx/graphite2/src/Sparse.cpp

changeset 0
6474c204b198
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/gfx/graphite2/src/Sparse.cpp	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,62 @@
     1.4 +/*  GRAPHITE2 LICENSING
     1.5 +
     1.6 +    Copyright 2011, SIL International
     1.7 +    All rights reserved.
     1.8 +
     1.9 +    This library is free software; you can redistribute it and/or modify
    1.10 +    it under the terms of the GNU Lesser General Public License as published
    1.11 +    by the Free Software Foundation; either version 2.1 of License, or
    1.12 +    (at your option) any later version.
    1.13 +
    1.14 +    This program is distributed in the hope that it will be useful,
    1.15 +    but WITHOUT ANY WARRANTY; without even the implied warranty of
    1.16 +    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
    1.17 +    Lesser General Public License for more details.
    1.18 +
    1.19 +    You should also have received a copy of the GNU Lesser General Public
    1.20 +    License along with this library in the file named "LICENSE".
    1.21 +    If not, write to the Free Software Foundation, 51 Franklin Street,
    1.22 +    Suite 500, Boston, MA 02110-1335, USA or visit their web page on the
    1.23 +    internet at http://www.fsf.org/licenses/lgpl.html.
    1.24 +
    1.25 +Alternatively, the contents of this file may be used under the terms of the
    1.26 +Mozilla Public License (http://mozilla.org/MPL) or the GNU General Public
    1.27 +License, as published by the Free Software Foundation, either version 2
    1.28 +of the License or (at your option) any later version.
    1.29 +*/
    1.30 +#include <cassert>
    1.31 +#include "inc/Sparse.h"
    1.32 +#include "inc/bits.h"
    1.33 +
    1.34 +using namespace graphite2;
    1.35 +
    1.36 +sparse::chunk sparse::empty_chunk = {0,0};
    1.37 +
    1.38 +sparse::~sparse() throw()
    1.39 +{
    1.40 +    if (m_array.map == &empty_chunk) return;
    1.41 +    free(m_array.values);
    1.42 +}
    1.43 +
    1.44 +
    1.45 +sparse::mapped_type sparse::operator [] (const key_type k) const throw()
    1.46 +{
    1.47 +    mapped_type         g = key_type(k/SIZEOF_CHUNK - m_nchunks) >> (sizeof k*8 - 1);
    1.48 +    const chunk &       c = m_array.map[g*k/SIZEOF_CHUNK];
    1.49 +    const mask_t        m = c.mask >> (SIZEOF_CHUNK - 1 - (k%SIZEOF_CHUNK));
    1.50 +    g *= m & 1;
    1.51 +
    1.52 +    return g*m_array.values[g*(c.offset + bit_set_count(m >> 1))];
    1.53 +}
    1.54 +
    1.55 +
    1.56 +size_t sparse::capacity() const throw()
    1.57 +{
    1.58 +    size_t n = m_nchunks,
    1.59 +           s = 0;
    1.60 +
    1.61 +    for (const chunk *ci=m_array.map; n; --n, ++ci)
    1.62 +        s += bit_set_count(ci->mask);
    1.63 +
    1.64 +    return s;
    1.65 +}

mercurial