AERA
time_buffer.h
1 //_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/
2 //_/_/
3 //_/_/ AERA
4 //_/_/ Autocatalytic Endogenous Reflective Architecture
5 //_/_/
6 //_/_/ Copyright (c) 2018-2025 Jeff Thompson
7 //_/_/ Copyright (c) 2018-2025 Kristinn R. Thorisson
8 //_/_/ Copyright (c) 2018-2025 Icelandic Institute for Intelligent Machines
9 //_/_/ http://www.iiim.is
10 //_/_/
11 //_/_/ Copyright (c) 2010-2012 Eric Nivel
12 //_/_/ Center for Analysis and Design of Intelligent Agents
13 //_/_/ Reykjavik University, Menntavegur 1, 102 Reykjavik, Iceland
14 //_/_/ http://cadia.ru.is
15 //_/_/
16 //_/_/ Part of this software was developed by Eric Nivel
17 //_/_/ in the HUMANOBS EU research project, which included
18 //_/_/ the following parties:
19 //_/_/
20 //_/_/ Autonomous Systems Laboratory
21 //_/_/ Technical University of Madrid, Spain
22 //_/_/ http://www.aslab.org/
23 //_/_/
24 //_/_/ Communicative Machines
25 //_/_/ Edinburgh, United Kingdom
26 //_/_/ http://www.cmlabs.com/
27 //_/_/
28 //_/_/ Istituto Dalle Molle di Studi sull'Intelligenza Artificiale
29 //_/_/ University of Lugano and SUPSI, Switzerland
30 //_/_/ http://www.idsia.ch/
31 //_/_/
32 //_/_/ Institute of Cognitive Sciences and Technologies
33 //_/_/ Consiglio Nazionale delle Ricerche, Italy
34 //_/_/ http://www.istc.cnr.it/
35 //_/_/
36 //_/_/ Dipartimento di Ingegneria Informatica
37 //_/_/ University of Palermo, Italy
38 //_/_/ http://diid.unipa.it/roboticslab/
39 //_/_/
40 //_/_/
41 //_/_/ --- HUMANOBS Open-Source BSD License, with CADIA Clause v 1.0 ---
42 //_/_/
43 //_/_/ Redistribution and use in source and binary forms, with or without
44 //_/_/ modification, is permitted provided that the following conditions
45 //_/_/ are met:
46 //_/_/ - Redistributions of source code must retain the above copyright
47 //_/_/ and collaboration notice, this list of conditions and the
48 //_/_/ following disclaimer.
49 //_/_/ - Redistributions in binary form must reproduce the above copyright
50 //_/_/ notice, this list of conditions and the following disclaimer
51 //_/_/ in the documentation and/or other materials provided with
52 //_/_/ the distribution.
53 //_/_/
54 //_/_/ - Neither the name of its copyright holders nor the names of its
55 //_/_/ contributors may be used to endorse or promote products
56 //_/_/ derived from this software without specific prior
57 //_/_/ written permission.
58 //_/_/
59 //_/_/ - CADIA Clause: The license granted in and to the software
60 //_/_/ under this agreement is a limited-use license.
61 //_/_/ The software may not be used in furtherance of:
62 //_/_/ (i) intentionally causing bodily injury or severe emotional
63 //_/_/ distress to any person;
64 //_/_/ (ii) invading the personal privacy or violating the human
65 //_/_/ rights of any person; or
66 //_/_/ (iii) committing or preparing for any act of war.
67 //_/_/
68 //_/_/ THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
69 //_/_/ CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
70 //_/_/ INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
71 //_/_/ MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
72 //_/_/ DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
73 //_/_/ CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
74 //_/_/ SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
75 //_/_/ BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
76 //_/_/ SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
77 //_/_/ INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
78 //_/_/ WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
79 //_/_/ NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
80 //_/_/ OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY
81 //_/_/ OF SUCH DAMAGE.
82 //_/_/
83 //_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/
84 
85 #ifndef r_code_time_buffer_h
86 #define r_code_time_buffer_h
87 
88 #include "list.h"
89 #include "utils.h"
90 
91 
92 using namespace core;
93 
94 namespace r_code {
95 
96 // Time limited buffer.
97 // T is expected a function: bool is_invalidated(uint64 time_reference,uint32 thz) const where time_reference and thz are valuated with the buffer's own.
98 template<typename T, class IsInvalidated> class time_buffer :
99  public list<T> {
100 protected:
101  std::chrono::microseconds thz_; // time horizon.
102  Timestamp time_reference_;
103 public:
104  time_buffer() : thz_(Utils::MaxTHZ) {}
105 
106  void set_thz(std::chrono::microseconds thz) { thz_ = thz; }
107 
108  class iterator {
109  friend class time_buffer;
110  private:
111  time_buffer *buffer_;
112  int32 cell_;
113  iterator(time_buffer *b, int32 c) : buffer_(b), cell_(c) {}
114  public:
115  iterator() : buffer_(NULL), cell_(list<T>::null_) {}
116  T &operator *() const { return buffer_->cells_[cell_].data_; }
117  T *operator ->() const { return &(buffer_->cells_[cell_].data_); }
118  iterator &operator ++() { // moves to the next time-compliant cell and erase old cells met in the process.
119 
120  cell_ = buffer_->cells_[cell_].next_;
121  if (cell_ != list<T>::null_) {
122 
123  IsInvalidated i;
124  check: if (i(buffer_->cells_[cell_].data_, buffer_->time_reference_, buffer_->thz_)) {
125 
126  cell_ = buffer_->_erase(cell_);
127  if (cell_ != list<T>::null_)
128  goto check;
129  }
130  }
131  return *this;
132  }
133  bool operator==(iterator &i) const { return cell_ == i.cell_; }
134  bool operator!=(iterator &i) const { return cell_ != i.cell_; }
135  };
136 private:
137  static iterator end_iterator_;
138 public:
139  iterator begin(Timestamp time_reference) {
140 
141  time_reference_ = time_reference;
142  return iterator(this, list<T>::used_cells_head_);
143  }
144  iterator &end() { return end_iterator_; }
145  iterator find(Timestamp time_reference, const T &t) {
146 
147  iterator i;
148  for (i = begin(time_reference); i != end(); ++i) {
149 
150  if ((*i) == t)
151  return i;
152  }
153  return end_iterator_;
154  }
155  iterator find(const T &t) {
156 
157  for (int32 c = list<T>::used_cells_head_; c != list<T>::null_; c = list<T>::cells_[c].next_) {
158 
159  if (list<T>::cells_[c].data_ == t)
160  return iterator(this, c);
161  }
162  return end_iterator_;
163  }
164  iterator erase(iterator &i) { return iterator(this, list<T>::_erase(i.cell_)); }
165 };
166 
167 template<typename T, class IsInvalidated> typename time_buffer<T, IsInvalidated>::iterator time_buffer<T, IsInvalidated>::end_iterator_;
168 }
169 
170 
171 #endif
r_code::time_buffer::iterator
Definition: time_buffer.h:108
r_code::time_buffer
Definition: time_buffer.h:99
r_code::list
Definition: list.h:99