aboutsummaryrefslogtreecommitdiff
path: root/libxsde/xsde/cxx/hashmap.cxx
blob: 6b22932f208ed2bb20bc68efdb447fadf1bca4c7 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
// file      : xsde/cxx/hashmap.cxx
// copyright : Copyright (c) 2005-2011 Code Synthesis Tools CC
// license   : GNU GPL v2 + exceptions; see accompanying LICENSE file

#include <string.h> // memset, memcpy, strlen, strcmp, strncmp

#include <xsde/cxx/hashmap.hxx>

#ifdef XSDE_CUSTOM_ALLOCATOR
#  include <xsde/cxx/allocator.hxx>
#endif

namespace xsde
{
  namespace cxx
  {
    // const_iterator
    //
    hashmap_const_iterator::
    hashmap_const_iterator (const hashmap& map, size_t b)
        : map_ (&map), bucket_ (b), element_ (0)
    {
      // Get it to the first actual element if any.
      //
      for (; bucket_ < map_->bcount_; ++bucket_)
      {
        hashmap::bucket* p = map_->buckets_[bucket_];
        if (p && p->size_)
          break;
      }
    }

    const void* hashmap_const_iterator::
    operator* () const
    {
      typedef hashmap::bucket bucket;
      typedef hashmap::element element;

      bucket* p = map_->buckets_[bucket_];
      const char* b = reinterpret_cast<const char*> (p) + sizeof (bucket);
      b += element_ * (sizeof (element) + map_->esize_);
      return b + sizeof (element);
    }

    hashmap_const_iterator& hashmap_const_iterator::
    operator++ ()
    {
      if (bucket_ < map_->bcount_)
      {
        hashmap::bucket* p = map_->buckets_[bucket_];

        if (p->size_ > element_ + 1)
          ++element_;
        else
        {
          element_ = 0;

          for (++bucket_; bucket_ < map_->bcount_; ++bucket_)
          {
            p = map_->buckets_[bucket_];
            if (p && p->size_)
              break;
          }
        }
      }

      return *this;
    }

    // hashmap
    //
    hashmap::
    ~hashmap ()
    {
#ifndef XSDE_EXCEPTIONS
      if (buckets_ != 0)
      {
#endif
        for (size_t i = 0; i < bcount_; ++i)
        {
          if (buckets_[i])
#ifndef XSDE_CUSTOM_ALLOCATOR
            operator delete (buckets_[i]);
#else
            cxx::free (buckets_[i]);
#endif
        }

#ifndef XSDE_CUSTOM_ALLOCATOR
        delete[] buckets_;
#else
        cxx::free (buckets_); // POD.
#endif

#ifndef XSDE_EXCEPTIONS
      }
#endif
    }


    hashmap::
    hashmap (size_t bcount, size_t esize)
        : esize_ (esize), ecount_ (0), bcount_ (bcount), buckets_ (0)
    {
#ifndef XSDE_EXCEPTIONS
      error_ = error_none;
#endif

#ifndef XSDE_CUSTOM_ALLOCATOR
      buckets_ = new bucket*[bcount_];
#else
      buckets_ = static_cast<bucket**> (alloc (bcount_ * sizeof (bucket*)));
#endif

#ifndef XSDE_EXCEPTIONS
      if (buckets_ == 0)
      {
        error_ = error_no_memory;
        return;
      }
#endif
      memset (buckets_, 0, sizeof (bucket*) * bcount_);
    }

    void hashmap::
    insert (const char* key, void* value)
    {
      size_t h = hash (key);
      bucket*& p = *(buckets_ + h % bcount_);

      if (p == 0)
      {
        // No elements in this bucket yet. Start with capacity for 2
        // elements.
        //
#ifndef XSDE_CUSTOM_ALLOCATOR
        p = static_cast<bucket*> (
          operator new (sizeof (bucket) + 2 * (sizeof (element) + esize_)));
#else
        p = static_cast<bucket*> (
          alloc (sizeof (bucket) + 2 * (sizeof (element) + esize_)));
#endif

#ifndef XSDE_EXCEPTIONS
        if (p == 0)
        {
          error_ = error_no_memory;
          return;
        }
#endif
        p->size_ = 0;
        p->capacity_ = 2;
      }

      if (p->size_ == p->capacity_)
      {
        // No more space in this bucket. Create a bigger bucket.
        //
        size_t c = p->size_ * 2;

#ifndef XSDE_CUSTOM_ALLOCATOR
        bucket* n = static_cast<bucket*> (
          operator new (sizeof (bucket) + c * (sizeof (element) + esize_)));
#else
        bucket* n = static_cast<bucket*> (
          alloc (sizeof (bucket) + c * (sizeof (element) + esize_)));
#endif

#ifndef XSDE_EXCEPTIONS
        if (n == 0)
        {
          error_ = error_no_memory;
          return;
        }
#endif
        n->size_ = p->size_;
        n->capacity_ = c;

        char* src = reinterpret_cast<char*> (p) + sizeof (bucket);
        char* dst = reinterpret_cast<char*> (n) + sizeof (bucket);

        memcpy (dst, src, p->size_ * (sizeof (element) + esize_));

#ifndef XSDE_CUSTOM_ALLOCATOR
        operator delete (p);
#else
        cxx::free (p);
#endif
        p = n;
      }

      char* data = reinterpret_cast<char*> (p) + sizeof (bucket) +
        p->size_ * (sizeof (element) + esize_);

      element* e = reinterpret_cast<element*> (data);
      e->hash_ = h;
      e->key_ = key;

      memcpy (data + sizeof (element), value, esize_);

      p->size_++;
      ecount_++;
    }

    const void* hashmap::
    find (const char* key) const
    {
      size_t h = hash (key);
      const bucket* p = *(buckets_ + h % bcount_);

      if (p)
      {
        const char* b = reinterpret_cast<const char*> (p) + sizeof (bucket);
        const char* e = b + p->size_ * (sizeof (element) + esize_);

        for (; b < e; b += sizeof (element) + esize_)
        {
          const element* e = reinterpret_cast<const element*> (b);

          if (e->hash_ == h && strcmp (e->key_, key) == 0)
            return b + sizeof (element);
        }
      }

      return 0;
    }

    size_t hashmap::
    max_bucket_size () const
    {
      size_t r = 0;
      for (size_t i = 0; i < bcount_; ++i)
      {
        if (buckets_[i] != 0 && buckets_[i]->size_ > r)
          r = buckets_[i]->size_;
      }
      return r;
    }
  }
}