aboutsummaryrefslogtreecommitdiff
path: root/libxsde/xsde/cxx/hashmap.ixx
blob: 1fbff74271e1c3a80d1c90921a3f221919cfc06d (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
// file      : xsde/cxx/hashmap.ixx
// author    : Boris Kolpackov <boris@codesynthesis.com>
// copyright : Copyright (c) 2005-2011 Code Synthesis Tools CC
// license   : GNU GPL v2 + exceptions; see accompanying LICENSE file

namespace xsde
{
  namespace cxx
  {
    // const_iterator
    //
    inline hashmap_const_iterator hashmap_const_iterator::
    operator++ (int)
    {
      hashmap_const_iterator r (*this);
      ++(*this);
      return r;
    }

    inline bool
    operator== (const hashmap_const_iterator& i,
                const hashmap_const_iterator& j)
    {
      return i.map_ == j.map_ &&
        i.bucket_ == j.bucket_ &&
        i.element_ == j.element_;
    }

    inline bool
    operator!= (const hashmap_const_iterator& i,
                const hashmap_const_iterator& j)
    {
      return !(i == j);
    }

    // hashmap
    //

#ifndef XSDE_EXCEPTIONS
    inline hashmap::error hashmap::
    _error () const
    {
      return error_;
    }
#endif

    inline bool hashmap::
    empty () const
    {
      return ecount_ == 0;
    }

    inline size_t hashmap::
    size () const
    {
      return ecount_;
    }

    inline const hashmap::bucket* hashmap::
    find (size_t h) const
    {
      return *(buckets_ + h % bcount_);
    }

    inline hashmap::const_iterator hashmap::
    begin () const
    {
      return const_iterator (*this, ecount_ ? 0 : bcount_);
    }

    inline hashmap::const_iterator hashmap::
    end () const
    {
      return const_iterator (*this, bcount_);
    }

    // Fowler/Noll/Vo (FNV) hash (type FNV-1a)
    //
#if XSDE_ARCH_WIDTH == 32
    inline size_t hashmap::
    hash (const char* s)
    {
      size_t r = static_cast<size_t> (2166136261UL);
      for (; *s != 0; ++s)
      {
        r ^= static_cast<size_t> (*s);
        r *= static_cast<size_t> (16777619UL);
      }
      return r;
    }

    inline size_t hashmap::
    hash (const char* s, size_t n)
    {
      size_t r = static_cast<size_t> (2166136261UL);
      for (; n > 0; --n)
      {
        r ^= static_cast<size_t> (*s++);
        r *= static_cast<size_t> (16777619UL);
      }
      return r;
    }

    inline size_t hashmap::
    hash (size_t h, const char* s)
    {
      for (; *s != 0; ++s)
      {
        h ^= static_cast<size_t> (*s);
        h *= static_cast<size_t> (16777619UL);
      }
      return h;
    }

    inline size_t hashmap::
    hash (size_t h, const char* s, size_t n)
    {
      for (; n > 0; --n)
      {
        h ^= static_cast<size_t> (*s++);
        h *= static_cast<size_t> (16777619UL);
      }
      return h;
    }

#elif XSDE_ARCH_WIDTH == 64

    inline size_t hashmap::
    hash (const char* s)
    {
      size_t r = static_cast<size_t> (14695981039346656037ULL);
      for (; *s != 0; ++s)
      {
        r ^= static_cast<size_t> (*s);
        r *= static_cast<size_t> (1099511628211ULL);
      }
      return r;
    }

    inline size_t hashmap::
    hash (const char* s, size_t n)
    {
      size_t r = static_cast<size_t> (14695981039346656037ULL);
      for (; n > 0; --n)
      {
        r ^= static_cast<size_t> (*s++);
        r *= static_cast<size_t> (1099511628211ULL);
      }
      return r;
    }

    inline size_t hashmap::
    hash (size_t h, const char* s)
    {
      for (; *s != 0; ++s)
      {
        h ^= static_cast<size_t> (*s);
        h *= static_cast<size_t> (1099511628211ULL);
      }
      return h;
    }

    inline size_t hashmap::
    hash (size_t h, const char* s, size_t n)
    {
      for (; n > 0; --n)
      {
        h ^= static_cast<size_t> (*s++);
        h *= static_cast<size_t> (1099511628211ULL);
      }
      return h;
    }
#else
#error there is no suitable hash function for this architecture width
#endif // XSDE_ARCH_WIDTH
  }
}