open-axiom repository from github
// Copyright (C) 2010-2011, Gabriel Dos Reis.1// All rights reserved.2//3// Redistribution and use in source and binary forms, with or without4// modification, are permitted provided that the following conditions are5// met:6//7// - Redistributions of source code must retain the above copyright8// notice, this list of conditions and the following disclaimer.9//10// - Redistributions in binary form must reproduce the above copyright11// notice, this list of conditions and the following disclaimer in12// the documentation and/or other materials provided with the13// distribution.14//15// - Neither the name of The Numerical Algorithms Group Ltd. nor the16// names of its contributors may be used to endorse or promote products17// derived from this software without specific prior written permission.18//19// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS20// IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED21// TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A22// PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER23// OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,24// EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,25// PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR26// PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF27// LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING28// NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS29// SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.3031// --% Author: Gabriel Dos Reis3233#include <string.h>34#include <open-axiom/string-pool>3536namespace OpenAxiom {37// ----------------38// -- StringItem --39// ----------------40bool41StringItem::equal(const Byte* str, size_t sz) const {42if (length != sz)43return false;44for (size_t i = 0; i < sz; ++i)45if (text[i] != str[i])46return false;47return true;48}495051// ----------------52// -- StringPool --53// ----------------54StringPool::StringPool()55: BasicHashTable<StringItem>(109),56strings(2 * Memory::page_size())57{ }5859// Return a hash for the string starting from `str'60// of length `sz'.61static size_t62hash(const Byte* str, size_t sz) {63size_t h = 0;64for(size_t i = 0; i < sz; ++i)65h = str[i] + (h << 6) + (h << 16) - h;66return h;67}6869const Byte*70StringPool::make_copy(const Byte* f, size_t sz) {71Byte* s = strings.allocate(sz + 1);72memcpy(s, f, sz);73s[sz] = '\0';74return s;75}7677StringPool::EntryType*78StringPool::intern(const Byte* src, size_t sz) {79const size_t h = hash(src, sz);80EntryType* e = hash_chain(h);81if (sz == 0)82return e;83for (; e->text != 0; e = e->chain) {84if (e->hash == h and e->equal(src, sz))85return e;86// If this is the last entry in this hash chain, allocate87// a new bucket to hold the information we want to store.88if (e->chain == 0)89e->chain = new_bucket();90}91e->text = make_copy(src, sz);92e->length = sz;93e->hash = h;94return e;95}9697StringPool::EntryType*98StringPool::intern(const char* s) {99return intern(reinterpret_cast<const Byte*>(s), strlen(s));100}101}102103104