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)
5  2009, Hermann Vosseler <Ichthyostega@web.de>
6 
7   **Lumiera** is free software; you can redistribute it and/or modify it
8   under the terms of the GNU General Public License as published by the
9   Free Software Foundation; either version 2 of the License, or (at your
10   option) any later version. See the file COPYING for further details.
11 
12 */
13 
14 
45 #ifndef LIB_HASH_INDEXED_H
46 #define LIB_HASH_INDEXED_H
47 
48 #include "lib/hash-value.h"
49 #include "lib/hash-standard.hpp"
50 #include "lib/error.hpp"
51 
52 extern "C" {
53 #include "lib/luid.h"
54 }
55 
56 #include <functional>
57 
58 
59 namespace lib {
60 
63  namespace hash {
64 
70  class Plain
71  {
72  const HashVal hash_;
73 
74  public:
75  Plain (HashVal val)
76  : hash_(val)
77  { }
78 
79  template<typename TY>
80  Plain (TY const& something)
81  : hash_(hash_value (something)) // ADL
82  { }
83 
84  operator HashVal() const { return hash_; }
85  };
86 
91  class LuidH
92  {
93  lumiera_uid luid_;
94 
95  public:
96  LuidH ()
97  {
98  lumiera_uid_gen (&luid_);
99  ENSURE (0 < lumiera_uid_hash(&luid_));
100  }
101 
102  operator HashVal() const { return lumiera_uid_hash (get()); }
103  bool operator== (LuidH const& o) const { return lumiera_uid_eq (get(), o.get()); }
104  bool operator!= (LuidH const& o) const { return not operator== (o); }
105 
107  LUID get() const { return const_cast<LUID> (&luid_);}
108  };
109 
110 
111  /* === for use within unordered_map === */
112  inline HashVal hash_value (Plain const& plainHash) { return plainHash; }
113  inline HashVal hash_value (LuidH const& luid_Hash) { return luid_Hash; }
114 
115  } // namespace "hash"
116 
117 
118 
119  /********************************************************/
125  template<class BA, class IMP>
126  struct HashIndexed
127  {
128 
132  struct ID : IMP
133  {
134  ID () : IMP () {}
135  ID (BA const& ref) : IMP (ref.getID()) {} // note: automatic conversion (e.g. from PlacementMO&)
136  ID (IMP const& ir) : IMP (ir) {}
137  };
138 
142  template<typename T>
143  struct Id : ID
144  {
145  Id () : ID () {}
146  Id (T const& ref) : ID (ref) {}
147  };
148 
149 
152  : public std::unary_function<BA, HashVal>
153  {
154  HashVal operator() (BA const& obj) const { return obj.getID(); }
155  };
156 
158  struct UseHashID
159  : public std::unary_function<ID, HashVal>
160  {
161  HashVal operator() (ID const& id) const { return id; }
162  };
163 
164 
165  ID const&
166  getID () const
167  {
168  return id_;
169  }
170 
173  template<typename T>
174  Id<T> const&
175  recastID () const
176  {
177  return reinterpret_cast<Id<T> const&> (getID());
178  }
179 
180  void
181  assignID (HashIndexed const& ref)
182  {
183  this->id_ = ref.getID();
184  }
185 
187  friend bool operator== (HashIndexed const& hal, HashIndexed const& har) { return hal.id_==har.id_; }
188  friend bool operator!= (HashIndexed const& hal, HashIndexed const& har) { return hal.id_!=har.id_; }
189 
190 
191  protected:
192  HashIndexed () : id_() {}
193  HashIndexed (IMP const& iref) : id_(iref) {}
194 
195  private:
196  ID id_;
197  };
198 
199 
200 
201 
202 
203 } // namespace lib
204 #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:103
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:55
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:48
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:97
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:52
unsigned char lumiera_uid[16]
storage for a Lumiera unique ID, based on a 128bit random number
Definition: hash-value.h:40
Id< T > const & recastID() const
redefining of the specific type info of the Id is allowed, as all share the same implementation ...