arduino-audio-tools
poly.hpp
1 /* Author: Mike Lubinets (aka mersinvald)
2  * Date: 29.12.15
3  *
4  * See LICENSE */
5 
6 #ifndef POLY_H
7 #define POLY_H
8 #include <stdint.h>
9 #include <string.h>
10 
11 #if !defined DEBUG && !defined __CC_ARM
12 #include <assert.h>
13 #else
14 #define assert(dummy)
15 #endif
16 
18 namespace RS {
19 
20 struct Poly {
21  Poly()
22  : length(0), _memory(NULL) {}
23 
24  Poly(uint8_t id, uint16_t offset, uint8_t size) \
25  : length(0), _id(id), _size(size), _offset(offset), _memory(NULL) {}
26 
27  /* @brief Append number at the end of polynomial
28  * @param num - number to append
29  * @return false if polynomial can't be stretched */
30  inline bool Append(uint8_t num) {
31  assert(length+1 < _size);
32  ptr()[length++] = num;
33  return true;
34  }
35 
36  /* @brief Polynomial initialization */
37  inline void Init(uint8_t id, uint16_t offset, uint8_t size, uint8_t** memory_ptr) {
38  this->_id = id;
39  this->_offset = offset;
40  this->_size = size;
41  this->length = 0;
42  this->_memory = memory_ptr;
43  }
44 
45  /* @brief Polynomial memory zeroing */
46  inline void Reset() {
47  memset((void*)ptr(), 0, this->_size);
48  }
49 
50  /* @brief Copy polynomial to memory
51  * @param src - source byte-sequence
52  * @param size - size of polynomial
53  * @param offset - write offset */
54  inline void Set(const uint8_t* src, uint8_t len, uint8_t offset = 0) {
55  assert(src && len <= this->_size-offset);
56  memcpy(ptr()+offset, src, len * sizeof(uint8_t));
57  length = len + offset;
58  }
59 
60  #define poly_max(a, b) ((a > b) ? (a) : (b))
61 
62  inline void Copy(const Poly* src) {
63  length = poly_max(length, src->length);
64  Set(src->ptr(), length);
65  }
66 
67  inline uint8_t& at(uint8_t i) const {
68  assert(i < _size);
69  return ptr()[i];
70  }
71 
72  inline uint8_t id() const {
73  return _id;
74  }
75 
76  inline uint8_t size() const {
77  return _size;
78  }
79 
80  // Returns pointer to memory of this polynomial
81  inline uint8_t* ptr() const {
82  assert(_memory && *_memory);
83  return (*_memory) + _offset;
84  }
85 
86  uint8_t length;
87 
88 protected:
89 
90  uint8_t _id;
91  uint8_t _size; // Size of reserved memory for this polynomial
92  uint16_t _offset; // Offset in memory
93  uint8_t** _memory; // Pointer to pointer to memory
94 };
95 
96 
97 }
98 
99 #endif // POLY_H
AudioTools internal: Reed-Solomon.
Definition: gf.hpp:19
Definition: poly.hpp:20