Lumiera  0.pre.03
»edit your freedom«
hash-indexed.hpp
Go to the documentation of this file.
1 /*
2  HASH-INDEXED.hpp - generic hash based and typed ID
3 
4  Copyright (C) Lumiera.org
5  2009, Hermann Vosseler <Ichthyostega@web.de>
6 
7  This program is free software; you can redistribute it and/or
8  modify it under the terms of the GNU General Public License as
9  published by the Free Software Foundation; either version 2 of
10  the License, or (at your option) any later version.
11 
12  This program is distributed in the hope that it will be useful,
13  but WITHOUT ANY WARRANTY; without even the implied warranty of
14  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15  GNU General Public License for more details.
16 
17  You should have received a copy of the GNU General Public License
18  along with this program; if not, write to the Free Software
19  Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
20 
21 */
22 
23 
54 #ifndef LIB_HASH_INDEXED_H
55 #define LIB_HASH_INDEXED_H
56 
57 #include "lib/hash-value.h"
58 #include "lib/hash-standard.hpp"
59 #include "lib/error.hpp"
60 
61 extern "C" {
62 #include "lib/luid.h"
63 }
64 
65 #include <functional>
66 
67 
68 namespace lib {
69 
72  namespace hash {
73 
79  class Plain
80  {
81  const HashVal hash_;
82 
83  public:
84  Plain (HashVal val)
85  : hash_(val)
86  { }
87 
88  template<typename TY>
89  Plain (TY const& something)
90  : hash_(hash_value (something)) // ADL
91  { }
92 
93  operator HashVal() const { return hash_; }
94  };
95 
100  class LuidH
101  {
102  lumiera_uid luid_;
103 
104  public:
105  LuidH ()
106  {
107  lumiera_uid_gen (&luid_);
108  ENSURE (0 < lumiera_uid_hash(&luid_));
109  }
110 
111  operator HashVal() const { return lumiera_uid_hash (get()); }
112  bool operator== (LuidH const& o) const { return lumiera_uid_eq (get(), o.get()); }
113  bool operator!= (LuidH const& o) const { return not operator== (o); }
114 
116  LUID get() const { return const_cast<LUID> (&luid_);}
117  };
118 
119 
120  /* === for use within unordered_map === */
121  inline HashVal hash_value (Plain const& plainHash) { return plainHash; }
122  inline HashVal hash_value (LuidH const& luid_Hash) { return luid_Hash; }
123 
124  } // namespace "hash"
125 
126 
127 
128  /********************************************************/
134  template<class BA, class IMP>
135  struct HashIndexed
136  {
137 
141  struct ID : IMP
142  {
143  ID () : IMP () {}
144  ID (BA const& ref) : IMP (ref.getID()) {} // note: automatic conversion (e.g. from PlacementMO&)
145  ID (IMP const& ir) : IMP (ir) {}
146  };
147 
151  template<typename T>
152  struct Id : ID
153  {
154  Id () : ID () {}
155  Id (T const& ref) : ID (ref) {}
156  };
157 
158 
161  : public std::unary_function<BA, HashVal>
162  {
163  HashVal operator() (BA const& obj) const { return obj.getID(); }
164  };
165 
167  struct UseHashID
168  : public std::unary_function<ID, HashVal>
169  {
170  HashVal operator() (ID const& id) const { return id; }
171  };
172 
173 
174  ID const&
175  getID () const
176  {
177  return id_;
178  }
179 
182  template<typename T>
183  Id<T> const&
184  recastID () const
185  {
186  return reinterpret_cast<Id<T> const&> (getID());
187  }
188 
189  void
190  assignID (HashIndexed const& ref)
191  {
192  this->id_ = ref.getID();
193  }
194 
196  friend bool operator== (HashIndexed const& hal, HashIndexed const& har) { return hal.id_==har.id_; }
197  friend bool operator!= (HashIndexed const& hal, HashIndexed const& har) { return hal.id_!=har.id_; }
198 
199 
200  protected:
201  HashIndexed () : id_() {}
202  HashIndexed (IMP const& iref) : id_(iref) {}
203 
204  private:
205  ID id_;
206  };
207 
208 
209 
210 
211 
212 } // namespace lib
213 #endif
enables use of BA objects as keys within std::unordered_map
size_t lumiera_uid_hash(const lumiera_uid *luid)
Generate a hash sum over an luid.
Definition: luid.c:112
Hash based ID, typed to a specific subclass of BA.
generic hash based ID, corresponding to the base class BA
lumiera_uid * LUID
a Lumiera UID
Definition: hash-value.h:59
simple Hash implementation directly incorporating the hash value.
A Mixin to add a private ID type to the target class, together with storage to hold an instance of th...
bool operator==(PtrDerefIter< I1 > const &il, PtrDerefIter< I2 > const &ir)
Supporting equality comparisons...
LUID get() const
for passing to C APIs
Implementation namespace for support and library code.
Lumiera unique object identifier.
void lumiera_uid_gen(lumiera_uid *luid)
Generate a new luid.
Definition: luid.c:57
trivial hash functor using the ID as hash
Helper to use a single extension point for specialised hash functions.
int lumiera_uid_eq(const lumiera_uid *luida, const lumiera_uid *luidb)
Test 2 luid&#39;s for equality.
Definition: luid.c:106
Lumiera error handling (C++ interface).
Hash implementation based on a lumiera unique object id (LUID) When invoking the default ctor...
Hash value types and utilities.
size_t HashVal
a STL compatible hash value
Definition: hash-value.h:56
unsigned char lumiera_uid[16]
storage for a Lumiera unique ID, based on a 128bit random number
Definition: hash-value.h:45
Id< T > const & recastID() const
redefining of the specific type info of the Id is allowed, as all share the same implementation ...