Learn how easy it is to sync an existing GitHub or Google Code repo to a SourceForge project! See Demo

Close

[bf78f2]: src / libpanomatic / localfeatures / BoundedSet.h Maximize Restore History

Download this file

BoundedSet.h    114 lines (91 with data), 2.6 kB

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