Blame src/Sparse.cpp

Packit 955a04
/*  GRAPHITE2 LICENSING
Packit 955a04
Packit 955a04
    Copyright 2011, SIL International
Packit 955a04
    All rights reserved.
Packit 955a04
Packit 955a04
    This library is free software; you can redistribute it and/or modify
Packit 955a04
    it under the terms of the GNU Lesser General Public License as published
Packit 955a04
    by the Free Software Foundation; either version 2.1 of License, or
Packit 955a04
    (at your option) any later version.
Packit 955a04
Packit 955a04
    This program is distributed in the hope that it will be useful,
Packit 955a04
    but WITHOUT ANY WARRANTY; without even the implied warranty of
Packit 955a04
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
Packit 955a04
    Lesser General Public License for more details.
Packit 955a04
Packit 955a04
    You should also have received a copy of the GNU Lesser General Public
Packit 955a04
    License along with this library in the file named "LICENSE".
Packit 955a04
    If not, write to the Free Software Foundation, 51 Franklin Street,
Packit 955a04
    Suite 500, Boston, MA 02110-1335, USA or visit their web page on the
Packit 955a04
    internet at http://www.fsf.org/licenses/lgpl.html.
Packit 955a04
Packit 955a04
Alternatively, the contents of this file may be used under the terms of the
Packit 955a04
Mozilla Public License (http://mozilla.org/MPL) or the GNU General Public
Packit 955a04
License, as published by the Free Software Foundation, either version 2
Packit 955a04
of the License or (at your option) any later version.
Packit 955a04
*/
Packit 955a04
#include <cassert>
Packit 955a04
#include "inc/Sparse.h"
Packit 955a04
#include "inc/bits.h"
Packit 955a04
Packit 955a04
using namespace graphite2;
Packit 955a04
Packit 955a04
const sparse::chunk sparse::empty_chunk = {0,0};
Packit 955a04
Packit 955a04
sparse::~sparse() throw()
Packit 955a04
{
Packit 955a04
    if (m_array.map == &empty_chunk) return;
Packit 955a04
    free(m_array.values);
Packit 955a04
}
Packit 955a04
Packit 955a04
Packit 955a04
sparse::mapped_type sparse::operator [] (const key_type k) const throw()
Packit 955a04
{
Packit 955a04
    mapped_type         g = key_type(k/SIZEOF_CHUNK - m_nchunks) >> (sizeof k*8 - 1);
Packit 955a04
    const chunk &       c = m_array.map[g*k/SIZEOF_CHUNK];
Packit 955a04
    const mask_t        m = c.mask >> (SIZEOF_CHUNK - 1 - (k%SIZEOF_CHUNK));
Packit 955a04
    g *= m & 1;
Packit 955a04
Packit 955a04
    return g*m_array.values[g*(c.offset + bit_set_count(m >> 1))];
Packit 955a04
}
Packit 955a04
Packit 955a04
Packit 955a04
size_t sparse::capacity() const throw()
Packit 955a04
{
Packit 955a04
    size_t n = m_nchunks,
Packit 955a04
           s = 0;
Packit 955a04
Packit 955a04
    for (const chunk *ci=m_array.map; n; --n, ++ci)
Packit 955a04
        s += bit_set_count(ci->mask);
Packit 955a04
Packit 955a04
    return s;
Packit 955a04
}