Hugintrunk  0.1
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
BoundedSet.h
Go to the documentation of this file.
1 /*
2 * Copyright (C) 2007-2008 Anael Orlinski
3 *
4 * This file is part of Panomatic.
5 *
6 * Panomatic is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
10 *
11 * Panomatic is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with Panomatic; if not, write to the Free Software
18 * <http://www.gnu.org/licenses/>.
19 */
20 
21 #ifndef __detectpano_boundedset_h
22 #define __detectpano_boundedset_h
23 
24 #include <set>
25 #include <limits>
26 
27 // a container that keeps the N best elements during insertion.
28 // then the container has a maximum size of N
29 
30 // TEMPLATE CLASS limited_multiset
31 // by default will keep the N largest values.
32 // to keep the N smallest values, use greater as comparator
33 
34 #ifdef _WIN32
35 #undef max
36 #undef min
37 #endif
38 
39 namespace lfeat
40 {
41 
42 template <typename _Key, typename _Compare = std::less<_Key> >
44 {
45 private:
46  size_t _maxSize;
47  std::set< _Key, _Compare> _set;
48 
49 public:
50  typedef typename std::set<_Key, _Compare>::iterator iterator;
51 
52  // define the constructors
53  bounded_set() : _maxSize(std::numeric_limits<size_t>::max()), _set(std::set<_Key, _Compare>()) {}
54 
55  bounded_set(size_t iMaxSize) : _set(std::set<_Key, _Compare>()), _maxSize (iMaxSize) {}
56 
58  void setMaxSize(int iMax)
59  {
60  _maxSize = iMax;
61  }
62 
64  size_t max_size() const
65  {
66  return _maxSize;
67  }
68 
70  size_t size() const
71  {
72  return _set.size();
73  }
74 
76  {
77  return _set.begin();
78  }
79 
81  {
82  return _set.end();
83  }
84 
85  // void swap(limited_multiset<_Key,_MaxLen ,_Compare, _Alloc>& __x)
86  // {
87  // multiset::swap(__x);
88  // }
89 
90  void truncate()
91  {
92  while (_set.size() > _maxSize)
93  {
94  _set.erase(_set.begin());
95  }
96  }
97 
98  // be careful, the returned iterator is always end !!!
99  // in fact we don't know if the added value is truncated.
100  void insert(const _Key& x)
101  {
102  _set.insert(x);
103  truncate();
104  }
105 
106  std::set< _Key, _Compare>& getSet()
107  {
108  return _set;
109  }
110 
111 };
112 
113 }
114 
115 #endif // __detectpano_boundedset_h
void setMaxSize(int iMax)
sets the max size of bounded set
Definition: BoundedSet.h:58
void insert(const _Key &x)
Definition: BoundedSet.h:100
size_t max_size() const
Returns the maximum size of the bounded_set.
Definition: BoundedSet.h:64
std::set< _Key, _Compare > & getSet()
Definition: BoundedSet.h:106
bounded_set(size_t iMaxSize)
Definition: BoundedSet.h:55
iterator end()
Definition: BoundedSet.h:80
size_t size() const
Returns the size of the limited_multiset.
Definition: BoundedSet.h:70
iterator begin()
Definition: BoundedSet.h:75
std::set< _Key, _Compare >::iterator iterator
Definition: BoundedSet.h:50
static T max(T x, T y)
Definition: svm.cpp:65
std::set< _Key, _Compare > _set
Definition: BoundedSet.h:47