Array.hpp 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  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 <string>
  29. #include <algorithm>
  30. namespace ZeroTier {
  31. /**
  32. * Static array -- a simple thing that's belonged in STL since the time of the dinosaurs
  33. */
  34. template<typename T,std::size_t S>
  35. class Array
  36. {
  37. public:
  38. Array() throw() {}
  39. Array(const Array &a)
  40. {
  41. for(std::size_t i=0;i<S;++i)
  42. data[i] = a.data[i];
  43. }
  44. Array(const T *ptr)
  45. {
  46. for(std::size_t i=0;i<S;++i)
  47. data[i] = ptr[i];
  48. }
  49. inline Array &operator=(const Array &a)
  50. {
  51. for(std::size_t i=0;i<S;++i)
  52. data[i] = a.data[i];
  53. return *this;
  54. }
  55. typedef T value_type;
  56. typedef T* pointer;
  57. typedef const T* const_pointer;
  58. typedef T& reference;
  59. typedef const T& const_reference;
  60. typedef T* iterator;
  61. typedef const T* const_iterator;
  62. typedef std::size_t size_type;
  63. typedef std::ptrdiff_t difference_type;
  64. typedef std::reverse_iterator<iterator> reverse_iterator;
  65. typedef std::reverse_iterator<const_iterator> const_reverse_iterator;
  66. inline iterator begin() throw() { return data; }
  67. inline iterator end() throw() { return &(data[S]); }
  68. inline const_iterator begin() const throw() { return data; }
  69. inline const_iterator end() const throw() { return &(data[S]); }
  70. inline reverse_iterator rbegin() throw() { return reverse_iterator(begin()); }
  71. inline reverse_iterator rend() throw() { return reverse_iterator(end()); }
  72. inline const_reverse_iterator rbegin() const throw() { return const_reverse_iterator(begin()); }
  73. inline const_reverse_iterator rend() const throw() { return const_reverse_iterator(end()); }
  74. inline std::size_t size() const throw() { return S; }
  75. inline std::size_t max_size() const throw() { return S; }
  76. inline reference operator[](const std::size_t n) throw() { return data[n]; }
  77. inline const_reference operator[](const std::size_t n) const throw() { return data[n]; }
  78. inline reference front() throw() { return data[0]; }
  79. inline const_reference front() const throw() { return data[0]; }
  80. inline reference back() throw() { return data[S-1]; }
  81. inline const_reference back() const throw() { return data[S-1]; }
  82. inline bool operator==(const Array &k) const throw()
  83. {
  84. for(unsigned long i=0;i<S;++i) {
  85. if (data[i] != k.data[i])
  86. return false;
  87. }
  88. return true;
  89. }
  90. inline bool operator<(const Array &k) const throw() { return std::lexicographical_compare(begin(),end(),k.begin(),k.end()); }
  91. inline bool operator!=(const Array &k) const throw() { return !(*this == k); }
  92. inline bool operator>(const Array &k) const throw() { return (k < *this); }
  93. inline bool operator<=(const Array &k) const throw() { return !(k < *this); }
  94. inline bool operator>=(const Array &k) const throw() { return !(*this < k); }
  95. T data[S];
  96. };
  97. } // namespace ZeroTier
  98. #endif