Array.hpp 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. /*
  2. * ZeroTier One - Network Virtualization Everywhere
  3. * Copyright (C) 2011-2017 ZeroTier, Inc. https://www.zerotier.com/
  4. *
  5. * This program is free software: you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License as published by
  7. * the Free Software Foundation, either version 3 of the License, or
  8. * (at your option) any later version.
  9. *
  10. * This program is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU General Public License
  16. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  17. *
  18. * --
  19. *
  20. * You can be released from the requirements of the license by purchasing
  21. * a commercial license. Buying such a license is mandatory as soon as you
  22. * develop commercial closed-source software that incorporates or links
  23. * directly against ZeroTier software without disclosing the source code
  24. * of your own application.
  25. */
  26. #ifndef ZT_ARRAY_HPP
  27. #define ZT_ARRAY_HPP
  28. #include <algorithm>
  29. namespace ZeroTier {
  30. /**
  31. * Static array -- a simple thing that's belonged in STL since the time of the dinosaurs
  32. */
  33. template<typename T,std::size_t S>
  34. class Array
  35. {
  36. public:
  37. Array() {}
  38. Array(const Array &a)
  39. {
  40. for(unsigned long i=0;i<S;++i)
  41. data[i] = a.data[i];
  42. }
  43. Array(const T *ptr)
  44. {
  45. for(unsigned long i=0;i<S;++i)
  46. data[i] = ptr[i];
  47. }
  48. inline Array &operator=(const Array &a)
  49. {
  50. for(unsigned long i=0;i<S;++i)
  51. data[i] = a.data[i];
  52. return *this;
  53. }
  54. typedef T value_type;
  55. typedef T* pointer;
  56. typedef const T* const_pointer;
  57. typedef T& reference;
  58. typedef const T& const_reference;
  59. typedef unsigned long size_type;
  60. typedef long difference_type;
  61. /*
  62. typedef T* iterator;
  63. typedef const T* const_iterator;
  64. typedef std::reverse_iterator<iterator> reverse_iterator;
  65. typedef std::reverse_iterator<const_iterator> const_reverse_iterator;
  66. inline iterator begin() { return data; }
  67. inline iterator end() { return &(data[S]); }
  68. inline const_iterator begin() const { return data; }
  69. inline const_iterator end() const { return &(data[S]); }
  70. inline reverse_iterator rbegin() { return reverse_iterator(begin()); }
  71. inline reverse_iterator rend() { return reverse_iterator(end()); }
  72. inline const_reverse_iterator rbegin() const { return const_reverse_iterator(begin()); }
  73. inline const_reverse_iterator rend() const { return const_reverse_iterator(end()); }
  74. */
  75. inline unsigned long size() const { return S; }
  76. inline unsigned long max_size() const { return S; }
  77. inline reference operator[](const std::size_t n) { return data[n]; }
  78. inline const_reference operator[](const std::size_t n) const { return data[n]; }
  79. inline reference front() { return data[0]; }
  80. inline const_reference front() const { return data[0]; }
  81. inline reference back() { return data[S-1]; }
  82. inline const_reference back() const { return data[S-1]; }
  83. inline bool operator==(const Array &k) const
  84. {
  85. for(unsigned long i=0;i<S;++i) {
  86. if (data[i] != k.data[i])
  87. return false;
  88. }
  89. return true;
  90. }
  91. inline bool operator!=(const Array &k) const { return !(*this == k); }
  92. inline bool operator<(const Array &k) const { return std::lexicographical_compare(data,data + S,k.data,k.data + S); }
  93. inline bool operator>(const Array &k) const { return (k < *this); }
  94. inline bool operator<=(const Array &k) const { return !(k < *this); }
  95. inline bool operator>=(const Array &k) const { return !(*this < k); }
  96. T data[S];
  97. };
  98. } // namespace ZeroTier
  99. #endif