replicode
time_buffer.h
Go to the documentation of this file.
1 // time_buffer.h
2 //
3 // Author: Eric Nivel
4 //
5 // BSD license:
6 // Copyright (c) 2010, Eric Nivel
7 // All rights reserved.
8 // Redistribution and use in source and binary forms, with or without
9 // modification, are permitted provided that the following conditions are met:
10 //
11 // - Redistributions of source code must retain the above copyright
12 // notice, this list of conditions and the following disclaimer.
13 // - Redistributions in binary form must reproduce the above copyright
14 // notice, this list of conditions and the following disclaimer in the
15 // documentation and/or other materials provided with the distribution.
16 // - Neither the name of Eric Nivel nor the
17 // names of their contributors may be used to endorse or promote products
18 // derived from this software without specific prior written permission.
19 //
20 // THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND ANY
21 // EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
22 // WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
23 // DISCLAIMED. IN NO EVENT SHALL THE REGENTS AND CONTRIBUTORS BE LIABLE FOR ANY
24 // DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
25 // (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
26 // LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
27 // ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
28 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
29 // SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30 
31 #ifndef r_code_time_buffer_h
32 #define r_code_time_buffer_h
33 
34 #include "list.h"
35 #include "utils.h"
36 
37 
38 using namespace core;
39 
40 namespace r_code {
41 
42 // Time limited buffer.
43 // T is expected a function: bool is_invalidated(uint64_t time_reference,uint64_t thz) const where time_reference and thz are valuated with the buffer's own.
44 template<typename T, class IsInvalidated> class time_buffer:
45  public list<T> {
46 protected:
48 //using list<T>::_cells;
49  uint64_t thz; // time horizon.
51 public:
52  time_buffer() : list<T>(), thz(Utils::MaxTHZ) {}
53 
54  void set_thz(uint64_t thz) {
55  this->thz = thz;
56  }
57 
58  class iterator {
59  friend class time_buffer;
60  private:
62  uintptr_t _cell;
63  iterator(time_buffer *b, int64_t c): buffer(b), _cell(c) {}
64  public:
65  iterator(): buffer(nullptr), _cell(0) {}
66  T &operator *() const {
67  return buffer->cells[_cell].data;
68  }
69  T *operator ->() const {
70  return &(buffer->cells[_cell].data);
71  }
72  iterator &operator ++() { // moves to the next time-compliant cell and erase old cells met in the process.
73 
74  _cell = buffer->cells[_cell].next;
75  if (_cell != 0) {
76 
77  IsInvalidated i;
78  check: if (i(buffer->cells[_cell].data, buffer->time_reference, buffer->thz)) {
79 
80  _cell = buffer->_erase(_cell);
81  if (_cell != 0)
82  goto check;
83  }
84  }
85  return *this;
86  }
87  bool operator==(iterator &i) const {
88  return _cell == i._cell;
89  }
90  bool operator!=(iterator &i) const {
91  return _cell != i._cell;
92  }
93  };
94 private:
95  static iterator end_iterator;
96 public:
97  iterator begin(uint64_t time_reference) {
98 
99  this->time_reference = time_reference;
100  return iterator(this, used_cells_head);
101  }
102  iterator &end() {
103  return end_iterator;
104  }
105  iterator find(uint64_t time_reference, const T &t) {
106 
107  iterator i;
108  for (i = begin(time_reference); i != end(); ++i) {
109 
110  if ((*i) == t)
111  return i;
112  }
113  return end_iterator;
114  }
115  iterator find(const T &t) {
116 
117  for (uintptr_t c = used_cells_head; c != 0; c = this->_cells[c].next) {
118 
119  if (this->_cells[c].data == t)
120  return iterator(this, c);
121  }
122  return end_iterator;
123  }
124  iterator erase(iterator &i) {
125  return iterator(this, this->_erase(i._cell));
126  }
127 };
128 
129 template<typename T, class IsInvalidated> typename time_buffer<T, IsInvalidated>::iterator time_buffer<T, IsInvalidated>::end_iterator;
130 }
131 
132 
133 #endif
std::vector< cell > cells
Definition: list.h:54
iterator find(const T &t)
Definition: time_buffer.h:115
Definition: time_buffer.h:58
void set_thz(uint64_t thz)
Definition: time_buffer.h:54
Definition: base.cpp:35
time_buffer()
Definition: time_buffer.h:52
time_buffer * buffer
Definition: time_buffer.h:61
Definition: atom.cpp:36
iterator find(uint64_t time_reference, const T &t)
Definition: time_buffer.h:105
int64_t _erase(int64_t c)
Definition: list.h:137
iterator erase(iterator &i)
Definition: time_buffer.h:124
iterator & end()
Definition: time_buffer.h:102
Definition: time_buffer.h:44
uintptr_t _cell
Definition: time_buffer.h:62
uint64_t time_reference
Definition: time_buffer.h:50
iterator begin(uint64_t time_reference)
Definition: time_buffer.h:97
bool operator==(iterator &i) const
Definition: time_buffer.h:87
iterator(time_buffer *b, int64_t c)
Definition: time_buffer.h:63
bool operator!=(iterator &i) const
Definition: time_buffer.h:90
Definition: utils.h:62
static iterator end_iterator
Definition: time_buffer.h:95
uint64_t thz
Definition: time_buffer.h:49
iterator()
Definition: time_buffer.h:65
Definition: list.h:42