Array.hpp 3.4 KB

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