iLab Neuromorphic Robotics Toolkit  0.1
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
BoundedSetImpl.H
Go to the documentation of this file.
1 /*! @file
2  @author Laurent Itti
3  @copyright GNU Public License (GPL v3)
4  @section License
5  @verbatim
6  // ////////////////////////////////////////////////////////////////////////
7  // The iLab Neuromorphic Robotics Toolkit (NRT) //
8  // Copyright 2010-2012 by the University of Southern California (USC) //
9  // and the iLab at USC. //
10  // //
11  // iLab - University of Southern California //
12  // Hedco Neurociences Building, Room HNB-10 //
13  // Los Angeles, Ca 90089-2520 - USA //
14  // //
15  // See http://ilab.usc.edu for information about this project. //
16  // ////////////////////////////////////////////////////////////////////////
17  // This file is part of The iLab Neuromorphic Robotics Toolkit. //
18  // //
19  // The iLab Neuromorphic Robotics Toolkit is free software: you can //
20  // redistribute it and/or modify it under the terms of the GNU General //
21  // Public License as published by the Free Software Foundation, either //
22  // version 3 of the License, or (at your option) any later version. //
23  // //
24  // The iLab Neuromorphic Robotics Toolkit is distributed in the hope //
25  // that it will be useful, but WITHOUT ANY WARRANTY; without even the //
26  // implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR //
27  // PURPOSE. See the GNU General Public License for more details. //
28  // //
29  // You should have received a copy of the GNU General Public License //
30  // along with The iLab Neuromorphic Robotics Toolkit. If not, see //
31  // <http://www.gnu.org/licenses/>. //
32  // ////////////////////////////////////////////////////////////////////////
33  @endverbatim */
34 
35 
36 
37 #ifndef INCLUDE_NRT_CORE_DESIGN_DETAILS_BOUNDEDSETIMPL_H
38 #define INCLUDE_NRT_CORE_DESIGN_DETAILS_BOUNDEDSETIMPL_H
39 
40 // ######################################################################
41 template <class T, typename EqualsTo> inline
42 nrt::BoundedSet<T, EqualsTo>::BoundedSet(size_t const siz, EqualsTo const & eq) :
43  itsSize(siz), itsEmptySemaphore(siz), itsFullSemaphore(0), itsEqualsTo(eq)
44 { }
45 
46 // ######################################################################
47 template <class T, typename EqualsTo> inline
48 void nrt::BoundedSet<T, EqualsTo>::push(T const & val, nrt::BoundedSetInsertPolicy const pol)
49 {
50  // First see if we can do a replacement, this will not change the size of the queue:
51  itsMutex.lock();
52  if (tryReplace(val, pol)) { itsMutex.unlock(); return; }
53  itsMutex.unlock();
54 
55  // We were not able to replace, so request a slot in the queue, potentially blocking until one is available:
56  itsEmptySemaphore.decrement(1);
57 
58  itsMutex.lock();
59  // all right, things may have changed now, so check again for duplicates and if none, push:
60  if (tryReplace(val, pol)) { itsMutex.unlock(); itsEmptySemaphore.increment(1); return; }
61 
62  itsQueue.push_back(val);
63  itsMutex.unlock();
64 
65  itsFullSemaphore.increment(1);
66 }
67 
68 // ######################################################################
69 template <class T, typename EqualsTo> inline
70 bool nrt::BoundedSet<T, EqualsTo>::tryReplace(T const & val, nrt::BoundedSetInsertPolicy const pol)
71 {
72  typename std::deque<T>::iterator itr = itsQueue.begin(), stop = itsQueue.end();
73  while (itr != stop)
74  if (itsEqualsTo(*itr, val))
75  switch(pol)
76  {
77  case nrt::BoundedSetInsertPolicy::Preserve: itsMutex.unlock(); return true;
78  case nrt::BoundedSetInsertPolicy::Replace: *itr = val; itsMutex.unlock(); return true;
79  }
80  else ++itr;
81  return false;
82 }
83 
84 // ######################################################################
85 template <class T, typename EqualsTo> inline
87 {
88  itsFullSemaphore.decrement(1);
89 
90  itsMutex.lock();
91  T val = itsQueue.front();
92  itsQueue.pop_front();
93  itsMutex.unlock();
94 
95  itsEmptySemaphore.increment(1);
96 
97  return val;
98 }
99 
100 // ######################################################################
101 template <class T, typename EqualsTo> inline
103 { return itsFullSemaphore.count(); }
104 
105 // ######################################################################
106 template <class T, typename EqualsTo> inline
108 { return itsSize; }
109 
110 #endif // INCLUDE_NRT_CORE_DESIGN_DETAILS_BOUNDEDSETIMPL_H
111