Path: blob/master/thirdparty/graphite/src/Sparse.cpp
10279 views
// SPDX-License-Identifier: MIT OR MPL-2.0 OR LGPL-2.1-or-later OR GPL-2.0-or-later1// Copyright 2011, SIL International, All rights reserved.23#include <cassert>4#include "inc/Sparse.h"5#include "inc/bits.h"67using namespace graphite2;89const sparse::chunk sparse::empty_chunk = {0,0};1011sparse::~sparse() throw()12{13if (m_array.map == &empty_chunk) return;14free(m_array.values);15}161718sparse::mapped_type sparse::operator [] (const key_type k) const throw()19{20mapped_type g = key_type(k/SIZEOF_CHUNK - m_nchunks) >> (sizeof k*8 - 1);21const chunk & c = m_array.map[g*k/SIZEOF_CHUNK];22const mask_t m = c.mask >> (SIZEOF_CHUNK - 1 - (k%SIZEOF_CHUNK));23g *= m & 1;2425return g*m_array.values[g*(c.offset + bit_set_count(m >> 1))];26}272829size_t sparse::capacity() const throw()30{31size_t n = m_nchunks,32s = 0;3334for (const chunk *ci=m_array.map; n; --n, ++ci)35s += bit_set_count(ci->mask);3637return s;38}394041