OpenMS
Loading...
Searching...
No Matches
HashGrid.h
Go to the documentation of this file.
1// Copyright (c) 2002-present, OpenMS Inc. -- EKU Tuebingen, ETH Zurich, and FU Berlin
2// SPDX-License-Identifier: BSD-3-Clause
3//
4// --------------------------------------------------------------------------
5// $Maintainer: Lars Nilse $
6// $Authors: Bastian Blank $
7// --------------------------------------------------------------------------
8
11#include <boost/functional/hash.hpp>
12#include <boost/unordered/unordered_map.hpp>
13#include <cmath>
14#include <iterator>
15#include <limits>
16
17// Boost.Unordered, not std::unordered_map, on purpose: QTClusterFinder walks this grid in
18// container order and numbers its clusters from it, so the bucket order decides which of two
19// equally good clusters wins. Boost's bucket policy and boost::hash are the same on every
20// platform; the std containers' are not, and the regression references are shared across
21// platforms. That is why this header is internal (see the sources.cmake of this directory).
22
23#ifndef OPENMS_COMPARISON_CLUSTERING_HASHGRID_H
24 #define OPENMS_COMPARISON_CLUSTERING_HASHGRID_H
25
26namespace OpenMS
27{
38 template<typename Cluster>
40 {
41 public:
45 // XXX: Check is there is another type handy in OpenMS already
47
52
56 typedef typename boost::unordered_multimap<ClusterCenter, Cluster> CellContent;
57
61 typedef boost::unordered_map<CellIndex, CellContent> Grid;
62
63 typedef typename CellContent::key_type key_type;
64 typedef typename CellContent::mapped_type mapped_type;
65 typedef typename CellContent::value_type value_type;
66
67 private:
72 {
73 private:
74 friend class HashGrid;
75
76 typedef typename Grid::iterator grid_iterator;
77 typedef typename CellContent::iterator cell_iterator;
78
83 using iterator_category = std::input_iterator_tag;
91 using difference_type = std::ptrdiff_t;
93
94
98
99 // Search for next non-empty cell
101 {
102 while (cell_it_ == grid_it_->second.end())
103 {
104 grid_it_++;
105
106 // If we are at the last cell, set cell iterator to something well-known
107 if (grid_it_ == grid_.end())
108 {
110 return;
111 }
112
113 cell_it_ = grid_it_->second.begin();
114 }
115 }
116
117 public:
118 explicit Iterator(Grid& grid) : grid_(grid), grid_it_(grid.end())
119 {
120 }
121
122 Iterator(Grid& grid, grid_iterator grid_it, cell_iterator cell_it) : grid_(grid), grid_it_(grid_it), cell_it_(cell_it)
123 {
125 }
126
128 {
129 ++cell_it_;
131 return *this;
132 }
133
135 {
136 Iterator ret(*this);
137 operator++();
138 return ret;
139 }
140
141 bool operator==(const Iterator& rhs) const
142 {
143 return grid_it_ == rhs.grid_it_ && cell_it_ == rhs.cell_it_;
144 }
145
146 bool operator!=(const Iterator& rhs) const
147 {
148 return !(*this == rhs);
149 }
150
152 {
153 return *cell_it_;
154 }
155
157 {
158 return &*cell_it_;
159 }
160
161 const CellIndex index() const
162 {
163 return grid_it_->first;
164 }
165 };
166
171 {
172 private:
173 friend class HashGrid;
174
175 typedef typename Grid::const_iterator grid_iterator;
176 typedef typename CellContent::const_iterator cell_iterator;
177
182 using iterator_category = std::input_iterator_tag;
190 using difference_type = std::ptrdiff_t;
192
193 const Grid& grid_;
196
197 // Search for next non-empty cell
199 {
200 while (cell_it_ == grid_it_->second.end())
201 {
202 grid_it_++;
203
204 // If we are at the last cell, set cell iterator to something well-known
205 if (grid_it_ == grid_.end())
206 {
208 return;
209 }
210
211 cell_it_ = grid_it_->second.begin();
212 }
213 }
214
215 public:
216 ConstIterator(const Grid& grid) : grid_(grid), grid_it_(grid.end())
217 {
218 }
219
220 ConstIterator(const Grid& grid, grid_iterator grid_it, cell_iterator cell_it) : grid_(grid), grid_it_(grid_it), cell_it_(cell_it)
221 {
223 }
224
226 {
227 }
228
230 {
231 ++cell_it_;
233 return *this;
234 }
235
237 {
238 ConstIterator ret(*this);
239 operator++();
240 return ret;
241 }
242
243 bool operator==(const ConstIterator& rhs) const
244 {
245 return grid_it_ == rhs.grid_it_ && cell_it_ == rhs.cell_it_;
246 }
247
248 bool operator!=(const ConstIterator& rhs) const
249 {
250 return !(*this == rhs);
251 }
252
253 const value_type& operator*() const
254 {
255 return *cell_it_;
256 }
257
258 const value_type* operator->() const
259 {
260 return &*cell_it_;
261 }
262
263 const CellIndex index() const
264 {
265 return grid_it_->first;
266 }
267 };
268
269 public:
272 typedef typename Grid::const_iterator const_grid_iterator;
273 typedef typename Grid::iterator grid_iterator;
274 typedef typename CellContent::const_iterator const_cell_iterator;
275 typedef typename CellContent::iterator cell_iterator;
276 typedef typename CellContent::size_type size_type;
277
278 private:
281
282 public:
287
292
293 public:
295 {
296 }
297
304 {
305 const CellIndex cellkey = cellIndexAtClusterCenter(v.first);
306 CellContent& cell = cells_[cellkey];
307 updateGridDimension_(cellkey);
308 return cell.insert(v);
309 }
310
314 void erase(iterator pos)
315 {
316 CellContent& cell = pos.grid_it_->second;
317 cell.erase(pos.cell_it_);
318 }
319
326 {
327 const CellIndex cellkey = cellIndexAtClusterCenter(key);
328 auto cell = cells_.find(cellkey);
329 if (cell == cells_.end())
330 return 0;
331 return cell->second.erase(key);
332 }
333
337 void clear()
338 {
339 cells_.clear();
340 }
341
346 {
347 grid_iterator grid_it = cells_.begin();
348 if (grid_it == cells_.end())
349 return end();
350
351 cell_iterator cell_it = grid_it->second.begin();
352 return iterator(cells_, grid_it, cell_it);
353 }
354
359 {
360 const_grid_iterator grid_it = cells_.begin();
361 if (grid_it == cells_.end())
362 return end();
363
364 const_cell_iterator cell_it = grid_it->second.begin();
365 return const_iterator(cells_, grid_it, cell_it);
366 }
367
372 {
373 return iterator(cells_);
374 }
375
380 {
381 return const_iterator(cells_);
382 }
383
387 bool empty() const
388 {
389 return size() == 0;
390 }
391
396 {
397 size_type ret = 0;
398
399 for (const_grid_iterator it = grid_begin(); it != grid_end(); ++it)
400 {
401 ret += it->second.size();
402 }
403
404 return ret;
405 }
406
411 {
412 return cells_.begin();
413 }
414
419 {
420 return cells_.end();
421 }
422
426 const typename Grid::mapped_type& grid_at(const CellIndex& x) const
427 {
428 return cells_.at(x);
429 }
430
434 typename Grid::mapped_type& grid_at(const CellIndex& x)
435 {
436 return cells_.at(x);
437 }
438
443 {
444 return cells_.find(x);
445 }
446
451 {
452 return cells_.find(x);
453 }
454
455
460 {
461 return cells_.begin();
462 }
467 {
468 return cells_.end();
469 }
470
478 {
479 CellIndex ret;
480 typename CellIndex::iterator it = ret.begin();
481 typename ClusterCenter::const_iterator lit = key.begin(), rit = cell_dimension.begin();
482 for (; it != ret.end(); ++it, ++lit, ++rit)
483 {
484 double t = std::floor(*lit / *rit);
485 if (t < std::numeric_limits<Int64>::min() || t > std::numeric_limits<Int64>::max())
486 throw Exception::OutOfRange(__FILE__, __LINE__, OPENMS_PRETTY_FUNCTION);
487 *it = static_cast<Int64>(t);
488 }
489 return ret;
490 }
491
492 private:
494 {
495 typename CellIndex::const_iterator it_new = d.begin();
496 typename CellIndex::iterator it_cur = grid_dimension_.begin();
497 for (; it_new != d.end(); ++it_new, ++it_cur)
498 {
499 if (*it_cur < *it_new)
500 *it_cur = *it_new;
501 }
502 }
503 };
504
506 template<UInt N, typename T>
507 std::size_t hash_value(const DPosition<N, T>& b)
508 {
509 boost::hash<T> hasher;
510 std::size_t hash = 0;
511 for (typename DPosition<N, T>::const_iterator it = b.begin(); it != b.end(); ++it)
512 hash ^= hasher(*it);
513 return hash;
514 }
515
516} // namespace OpenMS
517
518#endif /* OPENMS_COMPARISON_CLUSTERING_HASHGRID_H */
Representation of a coordinate in D-dimensional space.
Definition DPosition.h:32
const_iterator begin() const
Non-mutable begin iterator.
Definition DPosition.h:348
CoordinateType * iterator
Definition DPosition.h:51
const CoordinateType * const_iterator
Definition DPosition.h:52
const_iterator end() const
Non-mutable end iterator.
Definition DPosition.h:354
Out of range exception.
Definition Exception.h:291
Constant element iterator for the hash grid.
Definition HashGrid.h:171
ConstIterator(const Grid &grid, grid_iterator grid_it, cell_iterator cell_it)
Definition HashGrid.h:220
const ConstIterator operator++(int)
Definition HashGrid.h:236
bool operator!=(const ConstIterator &rhs) const
Definition HashGrid.h:248
value_type * pointer
The pointer type as returned by operator->()
Definition HashGrid.h:188
const Grid & grid_
Definition HashGrid.h:193
const CellIndex index() const
Definition HashGrid.h:263
grid_iterator grid_it_
Definition HashGrid.h:194
const value_type * operator->() const
Definition HashGrid.h:258
HashGrid::value_type value_type
The iterator's value type.
Definition HashGrid.h:184
bool operator==(const ConstIterator &rhs) const
Definition HashGrid.h:243
value_type & reference
The reference type as returned by operator*()
Definition HashGrid.h:186
const value_type & operator*() const
Definition HashGrid.h:253
Grid::const_iterator grid_iterator
Definition HashGrid.h:175
cell_iterator cell_it_
Definition HashGrid.h:195
ConstIterator(const Iterator &it)
Definition HashGrid.h:225
ConstIterator(const Grid &grid)
Definition HashGrid.h:216
std::input_iterator_tag iterator_category
The iterator's category type.
Definition HashGrid.h:182
CellContent::const_iterator cell_iterator
Definition HashGrid.h:176
std::ptrdiff_t difference_type
The difference type.
Definition HashGrid.h:190
void searchNextCell_()
Definition HashGrid.h:198
ConstIterator & operator++()
Definition HashGrid.h:229
Element iterator for the hash grid.
Definition HashGrid.h:72
value_type & operator*() const
Definition HashGrid.h:151
CellContent::iterator cell_iterator
Definition HashGrid.h:77
value_type * pointer
The pointer type as returned by operator->()
Definition HashGrid.h:89
const CellIndex index() const
Definition HashGrid.h:161
grid_iterator grid_it_
Definition HashGrid.h:96
HashGrid::value_type value_type
The iterator's value type.
Definition HashGrid.h:85
value_type & reference
The reference type as returned by operator*()
Definition HashGrid.h:87
Grid::iterator grid_iterator
Definition HashGrid.h:76
bool operator==(const Iterator &rhs) const
Definition HashGrid.h:141
cell_iterator cell_it_
Definition HashGrid.h:97
Iterator(Grid &grid, grid_iterator grid_it, cell_iterator cell_it)
Definition HashGrid.h:122
value_type * operator->() const
Definition HashGrid.h:156
Grid & grid_
Definition HashGrid.h:95
std::input_iterator_tag iterator_category
The iterator's category type.
Definition HashGrid.h:83
Iterator(Grid &grid)
Definition HashGrid.h:118
std::ptrdiff_t difference_type
The difference type.
Definition HashGrid.h:91
bool operator!=(const Iterator &rhs) const
Definition HashGrid.h:146
void searchNextCell_()
Definition HashGrid.h:100
Iterator & operator++()
Definition HashGrid.h:127
const Iterator operator++(int)
Definition HashGrid.h:134
Container for (2-dimensional coordinate, value) pairs.
Definition HashGrid.h:40
CellContent::const_iterator const_cell_iterator
Definition HashGrid.h:274
grid_iterator grid_end()
Definition HashGrid.h:466
boost::unordered_multimap< ClusterCenter, Cluster > CellContent
Contents of a cell.
Definition HashGrid.h:56
const CellIndex & grid_dimension
Upper-right corner of key space for cells.
Definition HashGrid.h:291
Grid::const_iterator const_grid_iterator
Definition HashGrid.h:272
CellIndex grid_dimension_
Definition HashGrid.h:280
Iterator iterator
Definition HashGrid.h:271
const_iterator begin() const
Returns iterator to first element.
Definition HashGrid.h:358
CellContent::iterator cell_iterator
Definition HashGrid.h:275
const_grid_iterator grid_find(const CellIndex &x) const
Returns the grid cell at given index if present, otherwise the grid_end iterator.
Definition HashGrid.h:442
const_grid_iterator grid_end() const
Returns iterator to on after last grid cell.
Definition HashGrid.h:418
DPosition< 2, Int64 > CellIndex
Index for cells.
Definition HashGrid.h:51
DPosition< 2, double > ClusterCenter
Coordinate for stored pairs.
Definition HashGrid.h:46
boost::unordered_map< CellIndex, CellContent > Grid
Map of (cell-index, cell-content).
Definition HashGrid.h:61
cell_iterator insert(const value_type &v)
Inserts a (2-dimensional coordinate, value) pair.
Definition HashGrid.h:303
ConstIterator const_iterator
Definition HashGrid.h:270
size_type size() const
Return number of elements.
Definition HashGrid.h:395
CellContent::value_type value_type
Definition HashGrid.h:65
bool empty() const
Return true if HashGrid is empty.
Definition HashGrid.h:387
CellIndex cellIndexAtClusterCenter(const ClusterCenter &key) const
Computes the cell index for a given cluster center coordinate.
Definition HashGrid.h:477
Grid cells_
Definition HashGrid.h:279
CellContent::size_type size_type
Definition HashGrid.h:276
Grid::iterator grid_iterator
Definition HashGrid.h:273
HashGrid(const ClusterCenter &c_dimension)
Definition HashGrid.h:294
grid_iterator grid_find(const CellIndex &x)
Returns the grid cell at given index if present, otherwise the grid_end iterator.
Definition HashGrid.h:450
CellContent::key_type key_type
Definition HashGrid.h:63
const Grid::mapped_type & grid_at(const CellIndex &x) const
Returns the grid cell at given index.
Definition HashGrid.h:426
void updateGridDimension_(const CellIndex &d)
Definition HashGrid.h:493
grid_iterator grid_begin()
Definition HashGrid.h:459
void clear()
Clears the map.
Definition HashGrid.h:337
iterator end()
Returns iterator to first element.
Definition HashGrid.h:371
const_iterator end() const
Returns iterator to first element.
Definition HashGrid.h:379
void erase(iterator pos)
Erases element on given iterator.
Definition HashGrid.h:314
const ClusterCenter cell_dimension
Dimension of cells.
Definition HashGrid.h:286
iterator begin()
Returns iterator to first element.
Definition HashGrid.h:345
const_grid_iterator grid_begin() const
Returns iterator to first grid cell.
Definition HashGrid.h:410
size_type erase(const key_type &key)
Erases elements matching the 2-dimensional coordinate.
Definition HashGrid.h:325
Grid::mapped_type & grid_at(const CellIndex &x)
Definition HashGrid.h:434
CellContent::mapped_type mapped_type
Definition HashGrid.h:64
int64_t Int64
Signed integer type (64bit)
Definition Types.h:40
Main OpenMS namespace.
Definition openswathalgo/include/OpenMS/OPENSWATHALGO/DATAACCESS/ISpectrumAccess.h:19
std::size_t hash_value(const DPosition< N, T > &b)
Definition HashGrid.h:507