circular_buffer.h
1 /*
2  * This file is part of SPORE.
3  *
4  * Copyright (C) 2016, the SPORE team (see AUTHORS).
5  *
6  * SPORE is free software: you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation, either version 2 of the License, or
9  * (at your option) any later version.
10  *
11  * SPORE is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with SPORE. If not, see <http://www.gnu.org/licenses/>.
18  *
19  * For more information see: https://github.com/IGITUGraz/spore-nest-module
20  *
21  * File: circular_buffer.h
22  * Author: Kappel
23  *
24  * Created on November 3, 2016, 6:23 PM
25  */
26 
27 #ifndef CIRCULAR_BUFFER_H
28 #define CIRCULAR_BUFFER_H
29 
30 
31 namespace spore
32 {
33 
41 template<typename T>
43 {
44 public:
45 
50  {
51  public:
52 
57  : ptr_(src.ptr_),
58  begin_(src.begin_),
59  end_(src.end_)
60  {
61  }
62 
66  inline
68  {
69  if (++ptr_ == end_)
70  ptr_ = begin_;
71  return *this;
72  }
73 
77  inline
79  {
80  if (ptr_ == begin_)
81  ptr_ = end_ - 1;
82  else
83  ptr_--;
84  return *this;
85  }
86 
91  inline
92  T const& operator*() const
93  {
94  return *ptr_;
95  }
96 
97  private:
98  friend class CircularBuffer;
99 
103  const_iterator(size_t offs, T const* mem, size_t size)
104  : ptr_(&mem[offs]),
105  begin_(mem),
106  end_(&mem[size])
107  {
108  }
109 
110  T const* ptr_;
111  T const* begin_;
112  T const* end_;
113  };
114 
119  : mem_(0),
120  size_(0)
121  {
122  }
123 
128  : mem_(0),
129  size_(src.size_)
130  {
131  if (src.size_ > 0)
132  {
133  mem_ = new T[size_];
134  for (size_t i = 0; i < size_; i++)
135  mem_[i] = src.mem_[i];
136  }
137  }
138 
143  {
144  delete mem_;
145  }
146 
151  void resize(size_t new_size, T v)
152  {
153  delete mem_;
154  mem_ = 0;
155  if (new_size > 0)
156  {
157  mem_ = new T[new_size];
158  for (size_t i = 0; i < new_size; i++)
159  mem_[i] = v;
160  }
161  size_ = new_size;
162  }
163 
168  T &operator[](size_t at)
169  {
170  assert(mem_);
171  return mem_[get_index(at)];
172  }
173 
178  const_iterator get(size_t at) const
179  {
180  assert(mem_);
181  return const_iterator(get_index(at), mem_, size_);
182  }
183 
187  size_t size() const
188  {
189  return size_;
190  }
191 
192 private:
193 
197  inline
198  size_t get_index(size_t at) const
199  {
200  return at % size_;
201  }
202 
203  T* mem_;
204  size_t size_;
205 };
206 
207 }
208 
209 #endif /* CIRCULAR_BUFFER_H */
void resize(size_t new_size, T v)
Definition: circular_buffer.h:151
CircularBuffer(const CircularBuffer &src)
Definition: circular_buffer.h:127
T const & operator*() const
Definition: circular_buffer.h:92
An iterable circular buffer.
Definition: circular_buffer.h:42
const_iterator(const const_iterator &src)
Definition: circular_buffer.h:56
size_t size() const
Definition: circular_buffer.h:187
const_iterator & operator++()
Definition: circular_buffer.h:67
T & operator[](size_t at)
Definition: circular_buffer.h:168
Constant iterator class.
Definition: circular_buffer.h:49
CircularBuffer()
Definition: circular_buffer.h:118
Global namespace holding all classes of the SPORE NEST module.
Definition: circular_buffer.h:31
~CircularBuffer()
Definition: circular_buffer.h:142
const_iterator & operator--()
Definition: circular_buffer.h:78