1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
/**
 * @file bounded_heap.hpp
 * @author Robin Passama
 * @brief include file for bounded heap class
 * @date 2022-06-10
 * 
 * @ingroup containers
 * 
 */
#pragma once

#include <cstdint>
#include <utility>
#include <string>
#include <iostream>
#include <array>
#include <functional>
#include <pid/memory_zone.hpp>

namespace pid{

template<typename T, unsigned int Limit>
class BoundedHeap: public MemoryZone<T,Limit>{

using iterator = typename MemoryZone<T,Limit>::iterator;
using const_iterator = typename MemoryZone<T,Limit>::const_iterator;

public:
	BoundedHeap(): MemoryZone<T,Limit>(){}
	BoundedHeap(const BoundedHeap & copied): MemoryZone<T,Limit>(copied){}
	BoundedHeap(BoundedHeap && moved): MemoryZone<T,Limit>(std::move(moved)){}
	BoundedHeap& operator=(const BoundedHeap & copied){
		this->MemoryZone<T,Limit>::operator=(copied);
		return (*this);
	}

	BoundedHeap& operator=(BoundedHeap && moved){
	  this->MemoryZone<T,Limit>::operator=(std::move(moved));
  	return (*this);
	}

	virtual ~BoundedHeap()=default;


  iterator put(){
    return(put(T()));
  }

  iterator put(const T& element){
    if(this->size()==this->capacity()){
      return (this->end());
    }
    auto it = this->add_element(this->begin());//always adding at beginning
    *it = element;//set the value
    return (it);
	}

	iterator put(T&& element){
    if(this->size()==this->capacity()){
      return (this->end());
    }
    auto it = this->add_element(this->begin());
    *it = std::move(element);//set the value
    return (it);
	}

  iterator rem(iterator& iter){<--- Parameter 'iter' can be declared with const
      return (this->remove_element(iter));
  }

	bool rem(const T& element){
    if(this->size()==0){
      return (false);
    }
    for(auto iter = this->begin(); iter != this->end(); ++iter){
      if(element == *iter){//the pointed element must be removed (require an operator== on T)
        rem(iter);
        return (true);
      }
    }
    return (false);
	}

};

}