OpendTect  6.6
toplist.h
Go to the documentation of this file.
1 #pragma once
2 
3 /*+
4 ________________________________________________________________________
5 
6  (C) dGB Beheer B.V.; (LICENSE) http://opendtect.org/OpendTect_license.txt
7  Author: K Tingdahl
8  Date: April 2003
9  RCS: $Id$
10 ________________________________________________________________________
11 
12 -*/
13 
14 #include "algomod.h"
15 #include "typeset.h"
16 
17 #include <utility>
18 
25 template <class VT, class AVT>
26 mClass(Algo) TopList
27 {
28 public:
29  TopList( int maxsize )
30  : maxsize_( maxsize ) {}
31 
32  virtual ~TopList() {}
33 
34  inline void reset();
37  inline VT getValue(int rank) const;
38  inline AVT getAssociatedValue(int rank) const;
39 
40  inline bool getHighestValue( VT& ) const;
41  inline bool getLowestValue( VT& ) const;
42 
43  inline int size() const;
44  inline int isEmpty() const { return !size(); }
45 
46  inline void addValue( VT val, AVT aval );
47 private:
48 
50  const int maxsize_;
51 };
52 
53 
54 template <class VT, class AVT> inline
56 {
57  values_.erase();
58 }
59 
60 
61 template <class VT, class AVT> inline
62 VT TopList<VT,AVT>::getValue(int pos) const
63 { return values_[pos].first; }
64 
65 
66 template <class VT, class AVT> inline
68 { return values_[pos].second; }
69 
70 
71 template <class VT, class AVT> inline
73 { return values_.size(); }
74 
75 
76 
77 template <class VT, class AVT> inline
78 bool TopList<VT,AVT>::getHighestValue( VT& res ) const
79 {
80  if ( size() )
81  {
82  res = values_[0].first;
83  return true;
84  }
85 
86  return false;
87 }
88 
89 
90 template <class VT, class AVT> inline
91 bool TopList<VT,AVT>::getLowestValue( VT& res ) const
92 {
93  const int sz = size();
94  if ( sz )
95  {
96  res = values_[sz-1].first;
97  return true;
98  }
99 
100  return false;
101 }
102 
103 
104 
105 template <class VT, class AVT> inline
106 void TopList<VT,AVT>::addValue( VT val, AVT aval )
107 {
108  int pos = 0;
109  const int mysize = size();
110 
111  while ( pos<mysize && values_[pos].first>val ) pos++;
112 
113  if ( pos==mysize )
114  {
115  if ( mysize>=maxsize_ )
116  return;
117 
118  values_ += std::pair<VT,AVT>( val, aval );
119  }
120  else
121  {
122  values_.insert( pos, std::pair<VT,AVT>(val,aval) );
123 
124  if ( mysize==maxsize_ )
125  values_.removeSingle(mysize);
126  }
127 }
TopList::isEmpty
int isEmpty() const
Definition: toplist.h:44
TopList::reset
void reset()
Definition: toplist.h:55
typeset.h
TopList::getValue
VT getValue(int rank) const
Definition: toplist.h:62
TopList::maxsize_
const int maxsize_
Definition: toplist.h:50
mClass
#define mClass(module)
Definition: commondefs.h:181
TopList::addValue
void addValue(VT val, AVT aval)
Definition: toplist.h:106
TopList::getAssociatedValue
AVT getAssociatedValue(int rank) const
Definition: toplist.h:67
TopList::size
int size() const
Definition: toplist.h:72
TopList::TopList
TopList(int maxsize)
Definition: toplist.h:29
TopList::values_
TypeSet< std::pair< VT, AVT > > values_
Definition: toplist.h:49
TopList::getHighestValue
bool getHighestValue(VT &) const
Definition: toplist.h:78
TopList::getLowestValue
bool getLowestValue(VT &) const
Definition: toplist.h:91
TopList::~TopList
virtual ~TopList()
Definition: toplist.h:32
TopList
Is a class that holds a "top N" list with the N highest (or lowest) values that is added....
Definition: houghtransform.h:22
TypeSet
Sets of (small) copyable elements.
Definition: commontypes.h:29

Generated at for the OpendTect seismic interpretation project. Copyright (C): dGB Beheer B.V. 1995-2021