Array.hpp 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. /*
  2. * ZeroTier One - Global Peer to Peer Ethernet
  3. * Copyright (C) 2012-2013 ZeroTier Networks LLC
  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. * ZeroTier may be used and distributed under the terms of the GPLv3, which
  21. * are available at: http://www.gnu.org/licenses/gpl-3.0.html
  22. *
  23. * If you would like to embed ZeroTier into a commercial application or
  24. * redistribute it in a modified binary form, please contact ZeroTier Networks
  25. * LLC. Start here: http://www.zerotier.com/
  26. */
  27. #ifndef ZT_ARRAY_HPP
  28. #define ZT_ARRAY_HPP
  29. #include <string>
  30. #include <algorithm>
  31. namespace ZeroTier {
  32. /**
  33. * Static array -- a simple thing that's belonged in STL since the time of the dinosaurs
  34. */
  35. template<typename T,std::size_t S>
  36. class Array
  37. {
  38. public:
  39. Array() throw() {}
  40. Array(const Array &a)
  41. {
  42. for(std::size_t i=0;i<S;++i)
  43. data[i] = a.data[i];
  44. }
  45. Array(const T *ptr)
  46. {
  47. for(std::size_t i=0;i<S;++i)
  48. data[i] = ptr[i];
  49. }
  50. inline Array &operator=(const Array &a)
  51. {
  52. for(std::size_t i=0;i<S;++i)
  53. data[i] = a.data[i];
  54. return *this;
  55. }
  56. typedef T value_type;
  57. typedef T* pointer;
  58. typedef const T* const_pointer;
  59. typedef T& reference;
  60. typedef const T& const_reference;
  61. typedef T* iterator;
  62. typedef const T* const_iterator;
  63. typedef std::size_t size_type;
  64. typedef std::ptrdiff_t difference_type;
  65. typedef std::reverse_iterator<iterator> reverse_iterator;
  66. typedef std::reverse_iterator<const_iterator> const_reverse_iterator;
  67. inline iterator begin() throw() { return data; }
  68. inline iterator end() throw() { return &(data[S]); }
  69. inline const_iterator begin() const throw() { return data; }
  70. inline const_iterator end() const throw() { return &(data[S]); }
  71. inline reverse_iterator rbegin() throw() { return reverse_iterator(begin()); }
  72. inline reverse_iterator rend() throw() { return reverse_iterator(end()); }
  73. inline const_reverse_iterator rbegin() const throw() { return const_reverse_iterator(begin()); }
  74. inline const_reverse_iterator rend() const throw() { return const_reverse_iterator(end()); }
  75. inline std::size_t size() const throw() { return S; }
  76. inline std::size_t max_size() const throw() { return S; }
  77. inline reference operator[](const std::size_t n) throw() { return data[n]; }
  78. inline const_reference operator[](const std::size_t n) const throw() { return data[n]; }
  79. inline reference front() throw() { return data[0]; }
  80. inline const_reference front() const throw() { return data[0]; }
  81. inline reference back() throw() { return data[S-1]; }
  82. inline const_reference back() const throw() { return data[S-1]; }
  83. inline bool operator==(const Array &k) const throw()
  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 throw() { return std::lexicographical_compare(begin(),end(),k.begin(),k.end()); }
  92. inline bool operator!=(const Array &k) const throw() { return !(*this == k); }
  93. inline bool operator>(const Array &k) const throw() { return (k < *this); }
  94. inline bool operator<=(const Array &k) const throw() { return !(k < *this); }
  95. inline bool operator>=(const Array &k) const throw() { return !(*this < k); }
  96. T data[S];
  97. };
  98. } // namespace ZeroTier
  99. #endif