BoundedSet.h

Go to the documentation of this file.
00001 /*
00002 * Copyright (C) 2007-2008 Anael Orlinski
00003 *
00004 * This file is part of Panomatic.
00005 *
00006 * Panomatic is free software; you can redistribute it and/or modify
00007 * it under the terms of the GNU General Public License as published by
00008 * the Free Software Foundation; either version 2 of the License, or
00009 * (at your option) any later version.
00010 *
00011 * Panomatic is distributed in the hope that it will be useful,
00012 * but WITHOUT ANY WARRANTY; without even the implied warranty of
00013 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00014 * GNU General Public License for more details.
00015 *
00016 * You should have received a copy of the GNU General Public License
00017 * along with Panomatic; if not, write to the Free Software
00018 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
00019 */
00020 
00021 #ifndef __detectpano_boundedset_h
00022 #define __detectpano_boundedset_h
00023 
00024 #include <set>
00025 #include <limits>
00026 
00027 // a container that keeps the N best elements during insertion.
00028 // then the container has a maximum size of N
00029 
00030 // TEMPLATE CLASS limited_multiset
00031 // by default will keep the N largest values.
00032 // to keep the N smallest values, use greater as comparator
00033 
00034 #ifdef _WIN32
00035 #undef max
00036 #undef min
00037 #endif
00038 
00039 namespace lfeat
00040 {
00041 
00042 template <typename _Key, typename _Compare = std::less<_Key> >
00043 class bounded_set
00044 {
00045 private:
00046     size_t                                              _maxSize;
00047     std::set< _Key, _Compare>   _set;
00048 
00049 public:
00050     typedef     typename std::set<_Key, _Compare>::iterator iterator;
00051 
00052     // define the constructors
00053     bounded_set() : _maxSize(std::numeric_limits<size_t>::max()), _set(std::set<_Key, _Compare>()) {}
00054 
00055     bounded_set(size_t iMaxSize) : _set(std::set<_Key, _Compare>()), _maxSize (iMaxSize) {}
00056 
00058     void setMaxSize(int iMax)
00059     {
00060         _maxSize = iMax;
00061     }
00062 
00064     size_t max_size() const
00065     {
00066         return _maxSize;
00067     }
00068 
00070     size_t size() const
00071     {
00072         return _set.size();
00073     }
00074 
00075     iterator begin()
00076     {
00077         return _set.begin();
00078     }
00079 
00080     iterator end()
00081     {
00082         return _set.end();
00083     }
00084 
00085     //  void swap(limited_multiset<_Key,_MaxLen ,_Compare, _Alloc>& __x)
00086     //  {
00087     //          multiset::swap(__x);
00088     //  }
00089 
00090     void truncate()
00091     {
00092         while (_set.size() > _maxSize)
00093         {
00094             _set.erase(_set.begin());
00095         }
00096     }
00097 
00098     // be careful, the returned iterator is always end !!!
00099     // in fact we don't know if the added value is truncated.
00100     void insert(const _Key& x)
00101     {
00102         _set.insert(x);
00103         truncate();
00104     }
00105 
00106     std::set< _Key, _Compare>& getSet()
00107     {
00108         return _set;
00109     }
00110 
00111 };
00112 
00113 }
00114 
00115 #endif // __detectpano_boundedset_h

Generated on Sun Apr 20 01:25:42 2014 for Hugintrunk by  doxygen 1.3.9.1