Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
Download

open-axiom repository from github

24005 views
1
// Copyright (C) 2010-2011, Gabriel Dos Reis.
2
// All rights reserved.
3
//
4
// Redistribution and use in source and binary forms, with or without
5
// modification, are permitted provided that the following conditions are
6
// met:
7
//
8
// - Redistributions of source code must retain the above copyright
9
// notice, this list of conditions and the following disclaimer.
10
//
11
// - Redistributions in binary form must reproduce the above copyright
12
// notice, this list of conditions and the following disclaimer in
13
// the documentation and/or other materials provided with the
14
// distribution.
15
//
16
// - Neither the name of The Numerical Algorithms Group Ltd. nor the
17
// names of its contributors may be used to endorse or promote products
18
// derived from this software without specific prior written permission.
19
//
20
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
21
// IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
22
// TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
23
// PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER
24
// OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
25
// EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
26
// PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
27
// PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
28
// LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
29
// NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
30
// SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31
32
// --% Author: Gabriel Dos Reis
33
34
#include <string.h>
35
#include <open-axiom/string-pool>
36
37
namespace OpenAxiom {
38
// ----------------
39
// -- StringItem --
40
// ----------------
41
bool
42
StringItem::equal(const Byte* str, size_t sz) const {
43
if (length != sz)
44
return false;
45
for (size_t i = 0; i < sz; ++i)
46
if (text[i] != str[i])
47
return false;
48
return true;
49
}
50
51
52
// ----------------
53
// -- StringPool --
54
// ----------------
55
StringPool::StringPool()
56
: BasicHashTable<StringItem>(109),
57
strings(2 * Memory::page_size())
58
{ }
59
60
// Return a hash for the string starting from `str'
61
// of length `sz'.
62
static size_t
63
hash(const Byte* str, size_t sz) {
64
size_t h = 0;
65
for(size_t i = 0; i < sz; ++i)
66
h = str[i] + (h << 6) + (h << 16) - h;
67
return h;
68
}
69
70
const Byte*
71
StringPool::make_copy(const Byte* f, size_t sz) {
72
Byte* s = strings.allocate(sz + 1);
73
memcpy(s, f, sz);
74
s[sz] = '\0';
75
return s;
76
}
77
78
StringPool::EntryType*
79
StringPool::intern(const Byte* src, size_t sz) {
80
const size_t h = hash(src, sz);
81
EntryType* e = hash_chain(h);
82
if (sz == 0)
83
return e;
84
for (; e->text != 0; e = e->chain) {
85
if (e->hash == h and e->equal(src, sz))
86
return e;
87
// If this is the last entry in this hash chain, allocate
88
// a new bucket to hold the information we want to store.
89
if (e->chain == 0)
90
e->chain = new_bucket();
91
}
92
e->text = make_copy(src, sz);
93
e->length = sz;
94
e->hash = h;
95
return e;
96
}
97
98
StringPool::EntryType*
99
StringPool::intern(const char* s) {
100
return intern(reinterpret_cast<const Byte*>(s), strlen(s));
101
}
102
}
103
104