Home  · Classes  · Annotated Classes  · Modules  · Members  · Namespaces  · Related Pages
UniqueIdIndexer.h
Go to the documentation of this file.
1 // --------------------------------------------------------------------------
2 // OpenMS -- Open-Source Mass Spectrometry
3 // --------------------------------------------------------------------------
4 // Copyright The OpenMS Team -- Eberhard Karls University Tuebingen,
5 // ETH Zurich, and Freie Universitaet Berlin 2002-2017.
6 //
7 // This software is released under a three-clause BSD license:
8 // * Redistributions of source code must retain the above copyright
9 // notice, this list of conditions and the following disclaimer.
10 // * Redistributions in binary form must reproduce the above copyright
11 // notice, this list of conditions and the following disclaimer in the
12 // documentation and/or other materials provided with the distribution.
13 // * Neither the name of any author or any participating institution
14 // may be used to endorse or promote products derived from this software
15 // without specific prior written permission.
16 // For a full list of authors, refer to the file AUTHORS.
17 // --------------------------------------------------------------------------
18 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
19 // AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20 // IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21 // ARE DISCLAIMED. IN NO EVENT SHALL ANY OF THE AUTHORS OR THE CONTRIBUTING
22 // INSTITUTIONS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
23 // EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
24 // PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
25 // OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
26 // WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
27 // OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
28 // ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29 //
30 // --------------------------------------------------------------------------
31 // $Maintainer: Chris Bielow $
32 // $Authors: Clemens Groepl $
33 // --------------------------------------------------------------------------
34 
35 #ifndef OPENMS_CONCEPT_UNIQUEIDINDEXER_H
36 #define OPENMS_CONCEPT_UNIQUEIDINDEXER_H
37 
41 
42 #ifdef _MSC_VER // disable some BOOST warnings that distract from ours
43 # pragma warning( push ) // save warning state
44 # pragma warning( disable : 4396 )
45 #endif
46 
47 #include <boost/unordered_map.hpp>
48 
49 #ifdef _MSC_VER
50 # pragma warning( pop ) // restore old warning state
51 #endif
52 
53 
54 namespace OpenMS
55 {
56 
63  template <typename RandomAccessContainer>
65  {
66 public:
67 
68  typedef boost::unordered_map<UInt64, Size> UniqueIdMap;
69 
85  Size
86  uniqueIdToIndex(UInt64 unique_id) const
87  {
88  Size index;
89  try
90  {
91  index = uniqueid_to_index_.at(unique_id);
92  if (getBase_().at(index).getUniqueId() != unique_id)
93  {
94  throw std::out_of_range("unique_id_to_index_");
95  }
96  }
97  catch (std::out_of_range &)
98  {
99  try
100  {
101  this->updateUniqueIdToIndex();
102  index = uniqueid_to_index_.at(unique_id);
103  }
104  catch (std::out_of_range &)
105  {
106  index = -1; // which means: invalid
107  }
108  }
109  return index;
110  }
111 
116  void
118  {
119  Size num_valid_unique_id = 0;
120  // add or update unique id of existing features
121  for (Size index = 0; index < getBase_().size(); ++index)
122  {
123  UInt64 unique_id = getBase_()[index].getUniqueId();
124  if (UniqueIdInterface::isValid(unique_id))
125  {
126  uniqueid_to_index_[unique_id] = index;
127  ++num_valid_unique_id;
128  }
129  }
130  // remove invalid or outdated entries
132  for (UniqueIdMap::iterator iter = uniqueid_to_index_.begin(); iter != uniqueid_to_index_.end(); /* see loop */)
133  {
134  if (iter->second >= getBase_().size() || getBase_()[iter->second].getUniqueId() != iter->first)
135  {
136  iter = uniqueid_to_index_.erase(iter);
137  }
138  else
139  {
140  ++iter;
141  }
142  }
143  if (uniqueid_to_index_.size() != num_valid_unique_id)
144  {
145  std::stringstream ss;
146  ss << "Duplicate valid unique ids detected! RandomAccessContainer has size()==" << getBase_().size();
147  ss << ", num_valid_unique_id==" << num_valid_unique_id;
148  ss << ", uniqueid_to_index_.size()==" << uniqueid_to_index_.size();
149  throw Exception::Postcondition(__FILE__, __LINE__, OPENMS_PRETTY_FUNCTION, ss.str());
150  }
151  return;
152  }
153 
167  Size
169  {
170  Size invalid_uids(0);
171  uniqueid_to_index_.clear();
172  // add unique id of existing features
173  for (Size index = 0; index < getBase_().size(); ++index)
174  {
175  UInt64 unique_id = getBase_()[index].getUniqueId();
176  if (!UniqueIdInterface::isValid(unique_id))
177  {
178  getBase_()[index].ensureUniqueId();
179  unique_id = getBase_()[index].getUniqueId();
180  }
181 
182  // see if UID already present
183  while (uniqueid_to_index_.find(unique_id) != uniqueid_to_index_.end()) // double entry!
184  {
185  getBase_()[index].setUniqueId();
186  unique_id = getBase_()[index].getUniqueId();
187  ++invalid_uids;
188  }
189 
190  uniqueid_to_index_[unique_id] = index;
191 
192  }
193 
194  return invalid_uids;
195  }
196 
201  void
203  {
204  std::swap(uniqueid_to_index_, rhs.uniqueid_to_index_);
205  return;
206  }
207 
208 protected:
209 
214  const RandomAccessContainer &
215  getBase_() const
216  {
217  return *static_cast<const RandomAccessContainer *>(this);
218  }
219 
224  RandomAccessContainer &
226  {
227  return *static_cast<RandomAccessContainer *>(this);
228  }
229 
234  mutable UniqueIdMap uniqueid_to_index_;
235 
236  };
237 
238 } //namespace OpenMS
239 
240 #endif // OPENMS_CONCEPT_UNIQUEIDINDEXER_H
Size uniqueIdToIndex(UInt64 unique_id) const
Returns the index of the feature with the given unique id, or Size(-1) if none exists in this random ...
Definition: UniqueIdIndexer.h:86
RandomAccessContainer & getBase_()
A little helper to get access to the base (!) class RandomAccessContainer.
Definition: UniqueIdIndexer.h:225
Main OpenMS namespace.
Definition: FeatureDeconvolution.h:47
static bool isValid(UInt64 unique_id)
Returns true if the unique_id is valid, false otherwise.
Definition: UniqueIdInterface.h:70
Size resolveUniqueIdConflicts()
Assign new UID&#39;s to doubly occurring UID&#39;s.
Definition: UniqueIdIndexer.h:168
A base class for random access containers for classes derived from UniqueIdInterface that adds functi...
Definition: UniqueIdIndexer.h:64
OPENMS_UINT64_TYPE UInt64
Unsigned integer type (64bit)
Definition: Types.h:78
const RandomAccessContainer & getBase_() const
A little helper to get access to the base (!) class RandomAccessContainer.
Definition: UniqueIdIndexer.h:215
void updateUniqueIdToIndex() const
Updates the hash map from unique id to index.
Definition: UniqueIdIndexer.h:117
void swap(UniqueIdIndexer &rhs)
Swap.
Definition: UniqueIdIndexer.h:202
size_t Size
Size type e.g. used as variable which can hold result of size()
Definition: Types.h:128
Postcondition failed exception.
Definition: Exception.h:181
Definition: UniqueIdInterface.h:62
boost::unordered_map< UInt64, Size > UniqueIdMap
Definition: UniqueIdIndexer.h:68
UniqueIdMap uniqueid_to_index_
hash map from unique id to index of features
Definition: UniqueIdIndexer.h:234

OpenMS / TOPP release 2.3.0 Documentation generated on Tue Jan 9 2018 18:22:04 using doxygen 1.8.13