robot-rust 0.2.1

The robot-rust project, bindings for wpilib, ctre, and rev in rust.
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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
// Copyright (c) FIRST and other WPILib contributors.
// Open Source Software; you can modify and/or share it under the terms of
// the WPILib BSD license file in the root directory of this project.

#pragma once

#include <cstddef>
#include <iterator>
#include <vector>

namespace wpi {

/**
 * This is a simple circular buffer so we don't need to "bucket brigade" copy
 * old values.
 *
 * @tparam T Buffer element type.
 */
template <class T>
class circular_buffer {
 public:
  /**
   * Constructs a circular buffer.
   *
   * @param size Maximum number of buffer elements.
   */
  constexpr explicit circular_buffer(size_t size) : m_data(size, T{}) {}

  constexpr circular_buffer(const circular_buffer&) = default;
  constexpr circular_buffer& operator=(const circular_buffer&) = default;
  constexpr circular_buffer(circular_buffer&&) = default;
  constexpr circular_buffer& operator=(circular_buffer&&) = default;

  class iterator {
   public:
    using iterator_category = std::bidirectional_iterator_tag;
    using value_type = T;
    using difference_type = std::ptrdiff_t;
    using pointer = T*;
    using reference = T&;

    constexpr iterator(circular_buffer* buffer, size_t index)
        : m_buffer{buffer}, m_index{index} {}

    constexpr iterator& operator++() {
      ++m_index;
      return *this;
    }
    constexpr iterator operator++(int) {
      iterator retval = *this;
      ++(*this);
      return retval;
    }
    constexpr iterator& operator--() {
      --m_index;
      return *this;
    }
    constexpr iterator operator--(int) {
      iterator retval = *this;
      --(*this);
      return retval;
    }
    constexpr bool operator==(const iterator&) const = default;
    constexpr reference operator*() { return (*m_buffer)[m_index]; }

   private:
    circular_buffer* m_buffer;
    size_t m_index;
  };

  class const_iterator {
   public:
    using iterator_category = std::bidirectional_iterator_tag;
    using value_type = T;
    using difference_type = std::ptrdiff_t;
    using pointer = T*;
    using const_reference = const T&;

    constexpr const_iterator(const circular_buffer* buffer, size_t index)
        : m_buffer{buffer}, m_index{index} {}

    constexpr const_iterator& operator++() {
      ++m_index;
      return *this;
    }
    constexpr const_iterator operator++(int) {
      const_iterator retval = *this;
      ++(*this);
      return retval;
    }
    constexpr const_iterator& operator--() {
      --m_index;
      return *this;
    }
    constexpr const_iterator operator--(int) {
      const_iterator retval = *this;
      --(*this);
      return retval;
    }
    constexpr bool operator==(const const_iterator&) const = default;
    constexpr const_reference operator*() const { return (*m_buffer)[m_index]; }

   private:
    const circular_buffer* m_buffer;
    size_t m_index;
  };

  using reverse_iterator = std::reverse_iterator<iterator>;
  using const_reverse_iterator = std::reverse_iterator<const_iterator>;

  /**
   * Returns begin iterator.
   */
  constexpr iterator begin() { return iterator(this, 0); }

  /**
   * Returns end iterator.
   */
  constexpr iterator end() {
    return iterator(this, ::wpi::circular_buffer<T>::size());
  }

  /**
   * Returns const begin iterator.
   */
  constexpr const_iterator begin() const { return const_iterator(this, 0); }

  /**
   * Returns const end iterator.
   */
  constexpr const_iterator end() const {
    return const_iterator(this, ::wpi::circular_buffer<T>::size());
  }

  /**
   * Returns const begin iterator.
   */
  constexpr const_iterator cbegin() const { return const_iterator(this, 0); }

  /**
   * Returns const end iterator.
   */
  constexpr const_iterator cend() const {
    return const_iterator(this, ::wpi::circular_buffer<T>::size());
  }

  /**
   * Returns reverse begin iterator.
   */
  constexpr reverse_iterator rbegin() { return reverse_iterator(end()); }

  /**
   * Returns reverse end iterator.
   */
  constexpr reverse_iterator rend() { return reverse_iterator(begin()); }

  /**
   * Returns const reverse begin iterator.
   */
  constexpr const_reverse_iterator rbegin() const {
    return const_reverse_iterator(end());
  }

  /**
   * Returns const reverse end iterator.
   */
  constexpr const_reverse_iterator rend() const {
    return const_reverse_iterator(begin());
  }

  /**
   * Returns const reverse begin iterator.
   */
  constexpr const_reverse_iterator crbegin() const {
    return const_reverse_iterator(cend());
  }

  /**
   * Returns const reverse end iterator.
   */
  constexpr const_reverse_iterator crend() const {
    return const_reverse_iterator(cbegin());
  }

  /**
   * Returns number of elements in buffer
   */
  constexpr size_t size() const { return m_length; }

  /**
   * Returns value at front of buffer
   */
  constexpr T& front() { return (*this)[0]; }

  /**
   * Returns value at front of buffer
   */
  constexpr const T& front() const { return (*this)[0]; }

  /**
   * Returns value at back of buffer
   *
   * If there are no elements in the buffer, calling this function results in
   * undefined behavior.
   */
  constexpr T& back() {
    return m_data[(m_front + m_length - 1) % m_data.size()];
  }

  /**
   * Returns value at back of buffer
   *
   * If there are no elements in the buffer, calling this function results in
   * undefined behavior.
   */
  constexpr const T& back() const {
    return m_data[(m_front + m_length - 1) % m_data.size()];
  }

  /**
   * Push a new value onto the front of the buffer.
   *
   * The value at the back is overwritten if the buffer is full.
   */
  constexpr void push_front(T value) {
    if (m_data.size() == 0) {
      return;
    }

    m_front = ModuloDec(m_front);

    m_data[m_front] = value;

    if (m_length < m_data.size()) {
      m_length++;
    }
  }

  /**
   * Push a new value onto the back of the buffer.
   *
   * The value at the front is overwritten if the buffer is full.
   */
  constexpr void push_back(T value) {
    if (m_data.size() == 0) {
      return;
    }

    m_data[(m_front + m_length) % m_data.size()] = value;

    if (m_length < m_data.size()) {
      m_length++;
    } else {
      // Increment front if buffer is full to maintain size
      m_front = ModuloInc(m_front);
    }
  }

  /**
   * Push a new value onto the front of the buffer that is constructed with the
   * provided constructor arguments.
   *
   * The value at the back is overwritten if the buffer is full.
   */
  template <class... Args>
  constexpr void emplace_front(Args&&... args) {
    if (m_data.size() == 0) {
      return;
    }

    m_front = ModuloDec(m_front);

    m_data[m_front] = T{args...};

    if (m_length < m_data.size()) {
      m_length++;
    }
  }

  /**
   * Push a new value onto the back of the buffer that is constructed with the
   * provided constructor arguments.
   *
   * The value at the front is overwritten if the buffer is full.
   */
  template <class... Args>
  constexpr void emplace_back(Args&&... args) {
    if (m_data.size() == 0) {
      return;
    }

    m_data[(m_front + m_length) % m_data.size()] = T{args...};

    if (m_length < m_data.size()) {
      m_length++;
    } else {
      // Increment front if buffer is full to maintain size
      m_front = ModuloInc(m_front);
    }
  }

  /**
   * Pop value at front of buffer.
   *
   * If there are no elements in the buffer, calling this function results in
   * undefined behavior.
   */
  constexpr T pop_front() {
    T& temp = m_data[m_front];
    m_front = ModuloInc(m_front);
    m_length--;
    return temp;
  }

  /**
   * Pop value at back of buffer.
   *
   * If there are no elements in the buffer, calling this function results in
   * undefined behavior.
   */
  constexpr T pop_back() {
    m_length--;
    return m_data[(m_front + m_length) % m_data.size()];
  }

  /**
   * Resizes internal buffer to given size.
   */
  constexpr void resize(size_t size) {
    if (size > m_data.size()) {
      // Find end of buffer
      size_t insertLocation = (m_front + m_length) % m_data.size();

      // If insertion location precedes front of buffer, push front index back
      if (insertLocation <= m_front) {
        m_front += size - m_data.size();
      }

      // Add elements to end of buffer
      m_data.insert(m_data.begin() + insertLocation, size - m_data.size(), 0);
    } else if (size < m_data.size()) {
      /* 1) Shift element block start at "front" left as many blocks as were
       *    removed up to but not exceeding buffer[0]
       * 2) Shrink buffer, which will remove even more elements automatically if
       *    necessary
       */
      size_t elemsToRemove = m_data.size() - size;
      auto frontIter = m_data.begin() + m_front;
      if (m_front < elemsToRemove) {
        /* Remove elements from end of buffer before shifting start of element
         * block. Doing so saves a few copies.
         */
        m_data.erase(frontIter + size, m_data.end());

        // Shift start of element block to left
        m_data.erase(m_data.begin(), frontIter);

        // Update metadata
        m_front = 0;
      } else {
        // Shift start of element block to left
        m_data.erase(frontIter - elemsToRemove, frontIter);

        // Update metadata
        m_front -= elemsToRemove;
      }

      /* Length only changes during a shrink if all unused spaces have been
       * removed. Length decreases as used spaces are removed to meet the
       * required size.
       */
      if (m_length > size) {
        m_length = size;
      }
    }
  }

  /**
   * Empties internal buffer.
   */
  constexpr void reset() {
    m_front = 0;
    m_length = 0;
  }

  /**
   * @return Element at index starting from front of buffer.
   */
  constexpr T& operator[](size_t index) {
    return m_data[(m_front + index) % m_data.size()];
  }

  /**
   * @return Element at index starting from front of buffer.
   */
  constexpr const T& operator[](size_t index) const {
    return m_data[(m_front + index) % m_data.size()];
  }

 private:
  std::vector<T> m_data;

  // Index of element at front of buffer
  size_t m_front = 0;

  // Number of elements used in buffer
  size_t m_length = 0;

  /**
   * Increment an index modulo the size of the buffer.
   *
   * @param index Index into the buffer.
   * @return The incremented index.
   */
  constexpr size_t ModuloInc(size_t index) {
    return (index + 1) % m_data.size();
  }

  /**
   * Decrement an index modulo the size of the buffer.
   *
   * @param index Index into the buffer.
   * @return The decremented index.
   */
  constexpr size_t ModuloDec(size_t index) {
    if (index == 0) {
      return m_data.size() - 1;
    } else {
      return index - 1;
    }
  }
};

}  // namespace wpi