MeshLib Documentation
Loading...
Searching...
No Matches
MRFewSmallest.h
Go to the documentation of this file.
1#pragma once
2
3#include "MRMeshFwd.h"
4#include <algorithm>
5#include <cassert>
6#include <vector>
7
8namespace MR
9{
10
12template<typename T>
14{
15public:
17 explicit FewSmallest( size_t maxElms = 0 ) { reset( maxElms ); }
18
20 void reset( size_t maxElms );
21
23 size_t maxElms() const { return maxElms_; }
24
26 bool empty() const { return heap_.empty(); }
27
29 size_t size() const { return heap_.size(); }
30
32 bool full() const { return size() == maxElms(); }
33
35 const std::vector<T> & get() const { return heap_; }
36
38 const T & top() const { assert( !heap_.empty() ); return heap_.front(); }
39
41 const T & topOr( const T & emptyRes ) const { return !heap_.empty() ? heap_.front() : emptyRes; }
42
44 void push( T t );
45
47 void clear() { heap_.clear(); }
48
49private:
50 std::vector<T> heap_;
51 size_t maxElms_ = 0;
52};
53
54template<typename T>
55void FewSmallest<T>::reset( size_t maxElms )
56{
57 heap_.clear();
58 heap_.reserve( maxElms );
59 maxElms_ = maxElms;
60}
61
62template<typename T>
64{
65 assert( heap_.size() <= maxElms() );
66 if ( heap_.size() == maxElms() )
67 {
68 if ( t < heap_.front() )
69 {
70 std::pop_heap( heap_.begin(), heap_.end() );
71 assert( t < heap_.back() );
72 heap_.back() = std::move( t );
73 std::push_heap( heap_.begin(), heap_.end() );
74 }
75 return;
76 }
77 heap_.push_back( std::move( t ) );
78 std::push_heap( heap_.begin(), heap_.end() );
79}
80
81} //namespace MR
the class stores some number of smallest elements from a larger number of candidates
Definition MRFewSmallest.h:14
void push(T t)
considers one more element, storing it if it is within the smallest
Definition MRFewSmallest.h:63
const T & top() const
returns the largest among stored smallest elements
Definition MRFewSmallest.h:38
void reset(size_t maxElms)
clears the content and reconfigure the object to store at most given number of elements
Definition MRFewSmallest.h:55
const T & topOr(const T &emptyRes) const
returns the largest among stored smallest elements or given element if this is empty
Definition MRFewSmallest.h:41
bool empty() const
returns whether the container is currently empty
Definition MRFewSmallest.h:26
const std::vector< T > & get() const
returns the smallest elements found so far
Definition MRFewSmallest.h:35
size_t maxElms() const
returns the maximum number of elements to be stored here
Definition MRFewSmallest.h:23
size_t size() const
returns current number of stored element
Definition MRFewSmallest.h:29
bool full() const
returns whether we have already maximum number of elements stored
Definition MRFewSmallest.h:32
void clear()
removes all stored elements
Definition MRFewSmallest.h:47
FewSmallest(size_t maxElms=0)
configure the object to store at most given number of elements
Definition MRFewSmallest.h:17
Definition MRCameraOrientationPlugin.h:8