Fishnet 2.0.1
Framework for Graph-Based Analysis of GIS Vector Data
Loading...
Searching...
No Matches
FixedSizeBuffer.hpp
Go to the documentation of this file.
1#pragma once
2#include <vector>
4
5namespace fishnet::util {
6template<typename T, typename C> requires std::totally_ordered<C>
8private:
9 std::vector<T> collection;
10 std::vector<C> cache;
11 size_t capacity;
12 std::function<C(const T &)> mapper;
13public:
14 FixedSizeBuffer(size_t capacity,UnaryFunction<T,C> auto mapper):capacity(capacity),mapper(mapper){
15 collection.reserve(capacity);
16 cache.reserve(capacity);
17 }
18
19 bool empty() const noexcept {
20 return collection.empty();
21 }
22
23 template<typename U>
24 void push(U && value){
25 static_assert(std::is_same_v<std::decay_t<U>, T>, "Type of value must be T");
26 auto mappedValue = mapper(value);
27 if(collection.size() < capacity){
28 cache.push_back(mappedValue);
29 collection.push_back(std::forward<U>(value));
30 }else{
31 int swapIndex=0;
32 auto max = cache[0];
33 for(int i=1; i < int(capacity);i++){
34 auto val = cache[i];
35 if(val> max){
36 max = val;
37 swapIndex = i;
38 }
39 }
40 if(mappedValue < max){
41 cache[swapIndex] = mappedValue;
42 collection[swapIndex] = std::forward<U>(value);
43 }
44 }
45 }
46
47 auto begin(){
48 return collection.begin();
49 }
50
51 auto end(){
52 return collection.end();
53 }
54};
55}
Definition FixedSizeBuffer.hpp:7
void push(U &&value)
Definition FixedSizeBuffer.hpp:24
FixedSizeBuffer(size_t capacity, UnaryFunction< T, C > auto mapper)
Definition FixedSizeBuffer.hpp:14
bool empty() const noexcept
Definition FixedSizeBuffer.hpp:19
auto begin()
Definition FixedSizeBuffer.hpp:47
auto end()
Definition FixedSizeBuffer.hpp:51
Definition FunctionalConcepts.hpp:9
Definition AlternativeKeyMap.hpp:4