LSSTApplications  1.1.2+25,10.0+13,10.0+132,10.0+133,10.0+224,10.0+41,10.0+8,10.0-1-g0f53050+14,10.0-1-g4b7b172+19,10.0-1-g61a5bae+98,10.0-1-g7408a83+3,10.0-1-gc1e0f5a+19,10.0-1-gdb4482e+14,10.0-11-g3947115+2,10.0-12-g8719d8b+2,10.0-15-ga3f480f+1,10.0-2-g4f67435,10.0-2-gcb4bc6c+26,10.0-28-gf7f57a9+1,10.0-3-g1bbe32c+14,10.0-3-g5b46d21,10.0-4-g027f45f+5,10.0-4-g86f66b5+2,10.0-4-gc4fccf3+24,10.0-40-g4349866+2,10.0-5-g766159b,10.0-5-gca2295e+25,10.0-6-g462a451+1
LSSTDataManagementBasePackage
Public Member Functions | Private Member Functions | Private Attributes | List of all members
lsst::ap::detail::HashedSet< EntryT, NumEntries > Class Template Reference

A set of up to NumEntries elements of type EntryT, hashed by an integer identifier. More...

#include <ChunkManagerImpl.h>

Inheritance diagram for lsst::ap::detail::HashedSet< EntryT, NumEntries >:

Public Member Functions

 HashedSet ()
 
EntryT * find (int const id)
 Returns a pointer to the entry with the given identifier, or null if there is no such entry. More...
 
EntryT const * find (int const id) const
 Returns a pointer to the entry with the given identifier, or null if there is no such entry. More...
 
EntryT * insert (int const id)
 
std::pair< EntryT *, bool > findOrInsert (int const id)
 
bool erase (int const id)
 
int size () const
 
int space () const
 
EntryT * begin ()
 
EntryT const * begin () const
 
EntryT * end ()
 
EntryT const * end () const
 

Private Member Functions

 BOOST_STATIC_ASSERT (NumEntries > 0 &&(NumEntries &(NumEntries-1))==0)
 
 BOOST_STATIC_ASSERT (NumEntries< INT_MAX)
 
EntryT const * doFind (int const id) const
 

Private Attributes

int _hashTable [2 *NumEntries]
 
int _free
 
int _size
 
EntryT _entries [NumEntries]
 

Detailed Description

template<typename EntryT, int NumEntries>
class lsst::ap::detail::HashedSet< EntryT, NumEntries >

A set of up to NumEntries elements of type EntryT, hashed by an integer identifier.

The hash table implementation is chained and intrusive – requirements follow:

Definition at line 72 of file ChunkManagerImpl.h.

Constructor & Destructor Documentation

template<typename EntryT , int NumEntries>
lsst::ap::detail::HashedSet< EntryT, NumEntries >::HashedSet ( )

Definition at line 71 of file ChunkManagerImpl.cc.

71  :
72  _free(0), _size(0)
73 {
74  for (int i = 0; i < 2*NumEntries; ++i) {
75  _hashTable[i] = -1;
76  }
77  // initialize linked list of free entries (embedded in the entries themselves)
78  for (int i = 0; i < NumEntries - 1; ++i) {
79  _entries[i].setId(-1);
80  _entries[i].setNextInChain(i + 1);
81  }
82  _entries[NumEntries - 1].setNextInChain(-1);
83 }
int _hashTable[2 *NumEntries]

Member Function Documentation

template<typename EntryT, int NumEntries>
EntryT* lsst::ap::detail::HashedSet< EntryT, NumEntries >::begin ( void  )
inline

Returns a pointer to the beginning of the underlying array of entries. Not all array entries will correspond to HashedSet entries : invalid entries - those that do not correspond to an entry that has been added to the set via insert() or findOrInsert() - are marked with an id value of -1.

Definition at line 112 of file ChunkManagerImpl.h.

112  {
113  return _entries;
114  }
template<typename EntryT, int NumEntries>
EntryT const* lsst::ap::detail::HashedSet< EntryT, NumEntries >::begin ( void  ) const
inline

Definition at line 115 of file ChunkManagerImpl.h.

115  {
116  return _entries;
117  }
template<typename EntryT, int NumEntries>
lsst::ap::detail::HashedSet< EntryT, NumEntries >::BOOST_STATIC_ASSERT ( NumEntries  ,
0 &&  NumEntries &(NumEntries-1) = =0 
)
private
template<typename EntryT, int NumEntries>
lsst::ap::detail::HashedSet< EntryT, NumEntries >::BOOST_STATIC_ASSERT ( )
private
template<typename EntryT , int NumEntries>
EntryT const * lsst::ap::detail::HashedSet< EntryT, NumEntries >::doFind ( int const  id) const
private

Returns a pointer to the entry with the given identifier, or null if there is no such entry.

Parameters
[in]idThe identifier of the entry to find.
Returns
A pointer to the entry with the given identifier, or null if no such entry was found.

Definition at line 93 of file ChunkManagerImpl.cc.

93  {
94  int i = _hashTable[hash(id) & (2*NumEntries - 1)];
95  while (i >= 0) {
96  if (id == _entries[i].getId()) {
97  return &_entries[i];
98  }
99  i = _entries[i].getNextInChain();
100  }
101  return 0;
102 }
int hash(boost::uint32_t key)
int _hashTable[2 *NumEntries]
template<typename EntryT, int NumEntries>
EntryT* lsst::ap::detail::HashedSet< EntryT, NumEntries >::end ( void  )
inline

Returns a pointer to the end of the underlying array of entries.

Definition at line 120 of file ChunkManagerImpl.h.

120  {
121  return _entries + NumEntries;
122  }
template<typename EntryT, int NumEntries>
EntryT const* lsst::ap::detail::HashedSet< EntryT, NumEntries >::end ( void  ) const
inline

Definition at line 123 of file ChunkManagerImpl.h.

123  {
124  return _entries + NumEntries;
125  }
template<typename EntryT , int NumEntries>
bool lsst::ap::detail::HashedSet< EntryT, NumEntries >::erase ( int const  id)

Erases the entry with the given id, returning true if an entry with the given id was found.

Parameters
[in]idThe id of the entry to erase.
Returns
true if an entry with the given id was found (and erased).

Definition at line 211 of file ChunkManagerImpl.cc.

211  {
212 
213  int const bucket = hash(id) & (2*NumEntries - 1);
214 
215  int i = _hashTable[bucket];
216  int last = -1;
217 
218  while (i >= 0) {
219  if (id == _entries[i].getId()) {
220  // found entry to erase, unlink it from the chain for its bucket
221  if (last < 0) {
222  _hashTable[bucket] = _entries[i].getNextInChain();
223  } else {
224  _entries[last].setNextInChain(_entries[i].getNextInChain());
225  }
226  // append the entry to the free list
227  _entries[i].setId(-1);
228  _entries[i].setNextInChain(_free);
229  _free = i;
230  --_size;
231  return true;
232  }
233  last = i;
234  i = _entries[i].getNextInChain();
235  }
236  return false;
237 }
int hash(boost::uint32_t key)
int _hashTable[2 *NumEntries]
template<typename EntryT, int NumEntries>
EntryT* lsst::ap::detail::HashedSet< EntryT, NumEntries >::find ( int const  id)
inline

Returns a pointer to the entry with the given identifier, or null if there is no such entry.

Definition at line 82 of file ChunkManagerImpl.h.

82  {
83  return const_cast<EntryT *>(doFind(id));
84  }
EntryT const * doFind(int const id) const
template<typename EntryT, int NumEntries>
EntryT const* lsst::ap::detail::HashedSet< EntryT, NumEntries >::find ( int const  id) const
inline

Returns a pointer to the entry with the given identifier, or null if there is no such entry.

Definition at line 87 of file ChunkManagerImpl.h.

87  {
88  return doFind(id);
89  }
EntryT const * doFind(int const id) const
template<typename EntryT , int NumEntries>
std::pair< EntryT *, bool > lsst::ap::detail::HashedSet< EntryT, NumEntries >::findOrInsert ( int const  id)

Returns a pointer to a preexisting or freshly default-constructed entry with the given identifier, along with a boolean indicating whether the entry was inserted (true) or found (false). The pointer returned is null if and only if a fresh entry was required but there were no free entries available.

Parameters
[in]idThe identifier (unique within this set) of the entry to find or insert.
Returns
A pointer to the entry with the given id along with a boolean indicating whether the entry was inserted (true) or found (false).

Definition at line 166 of file ChunkManagerImpl.cc.

166  {
167 
168  int const bucket = hash(id) & (2*NumEntries - 1);
169 
170  int i = _hashTable[bucket];
171  int last = -1;
172 
173  while (i >= 0) {
174  if (id == _entries[i].getId()) {
175  return std::pair<EntryT *, bool>(&_entries[i], false); // found an entry with the given id
176  }
177  last = i;
178  i = _entries[i].getNextInChain();
179  }
180 
181  // check that there is a free chunk, if so use it
182  int const c = _free;
183  if (c < 0) {
184  return std::pair<EntryT *, bool>(0, true);
185  }
186  _free = _entries[c].getNextInChain();
187 
188  if (last < 0) {
189  _hashTable[bucket] = c;
190  } else {
191  // hash collision - chain to the end of the bucket
192  _entries[last].setNextInChain(c);
193  }
194 
195  // basic chunk initialization, then return
196  new (&_entries[c]) EntryT();
197  _entries[c].setId(id);
198  _entries[c].setNextInChain(-1);
199  ++_size;
200  return std::pair<EntryT *, bool>(&_entries[c], true);
201 }
int hash(boost::uint32_t key)
int _hashTable[2 *NumEntries]
template<typename EntryT , int NumEntries>
EntryT * lsst::ap::detail::HashedSet< EntryT, NumEntries >::insert ( int const  id)

Returns a pointer to a freshly initialized entry with the given identifier, or null if an entry with the given identifier already exists.

Parameters
[in]idThe identifier (unique within this set) of the entry to insert.
Returns
A pointer to a freshly default-constructed entry with identifier set to id, or null if either a preexisting entry with the given identifier was found or no space for new entries remains.

Definition at line 115 of file ChunkManagerImpl.cc.

115  {
116 
117  // check that there is space for another entry
118  if (_free < 0) {
119  return 0;
120  }
121 
122  int const bucket = hash(id) & (2*NumEntries - 1);
123 
124  int i = _hashTable[bucket];
125  int last = -1;
126 
127  while (i >= 0) {
128  if (id == _entries[i].getId()) {
129  return 0; // already have an entry with the given id
130  }
131  last = i;
132  i = _entries[i].getNextInChain();
133  }
134 
135  // take an entry off the free list
136  int const c = _free;
137  _free = _entries[c].getNextInChain();
138 
139  if (last < 0) {
140  _hashTable[bucket] = c;
141  } else {
142  // hash collision - chain to the end of the bucket
143  _entries[last].setNextInChain(c);
144  }
145 
146  // basic entry initialization, then return
147  new (&_entries[c]) EntryT();
148  _entries[c].setId(id);
149  _entries[c].setNextInChain(-1);
150  ++_size;
151  return &_entries[c];
152 }
int hash(boost::uint32_t key)
int _hashTable[2 *NumEntries]
template<typename EntryT, int NumEntries>
int lsst::ap::detail::HashedSet< EntryT, NumEntries >::size ( ) const
inline

Returns the number of entries in the set

Definition at line 98 of file ChunkManagerImpl.h.

98  {
99  return _size;
100  }
template<typename EntryT, int NumEntries>
int lsst::ap::detail::HashedSet< EntryT, NumEntries >::space ( ) const
inline

Returns the number of additional entries there is space for in the set

Definition at line 103 of file ChunkManagerImpl.h.

103  {
104  return NumEntries - _size;
105  }

Member Data Documentation

template<typename EntryT, int NumEntries>
EntryT lsst::ap::detail::HashedSet< EntryT, NumEntries >::_entries[NumEntries]
private

Definition at line 131 of file ChunkManagerImpl.h.

template<typename EntryT, int NumEntries>
int lsst::ap::detail::HashedSet< EntryT, NumEntries >::_free
private

Definition at line 129 of file ChunkManagerImpl.h.

template<typename EntryT, int NumEntries>
int lsst::ap::detail::HashedSet< EntryT, NumEntries >::_hashTable[2 *NumEntries]
private

Definition at line 128 of file ChunkManagerImpl.h.

template<typename EntryT, int NumEntries>
int lsst::ap::detail::HashedSet< EntryT, NumEntries >::_size
private

Definition at line 130 of file ChunkManagerImpl.h.


The documentation for this class was generated from the following files: