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

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