array.h

Go to the documentation of this file.
00001 00004 /* 00005 * Copyright © 2001 Sofus Mortensen 00006 * 00007 * This material is provided "as is", with absolutely no warranty 00008 * expressed or implied. Any use is at your own risk. Permission to 00009 * use or copy this software for any purpose is hereby granted without 00010 * fee, provided the above notices are retained on all copies. 00011 * Permission to modify the code and to distribute modified code is 00012 * granted, provided the above notices are retained, and a notice that 00013 * the code was modified is included with the above copyright notice. 00014 * 00015 * This header is part of comet. 00016 * http://www.lambdasoft.dk/comet 00017 * 00018 */ 00019 00020 /* 00021 * comet::array_t is adapted from class array by Nicolai M. Josuttis 00022 * 00023 * (C) Copyright Nicolai M. Josuttis 2001. 00024 * Permission to copy, use, modify, sell and distribute this software 00025 * is granted provided this copyright notice appears in all copies. 00026 * This software is provided "as is" without express or implied 00027 * warranty, and with no claim as to its suitability for any purpose. 00028 * 00029 */ 00030 00031 #ifndef COMET_ARRAY_H 00032 #define COMET_ARRAY_H 00033 00034 #include <cstddef> 00035 #include <stdexcept> 00036 #include <iterator> 00037 #include <algorithm> 00038 00039 namespace comet { 00040 00041 #pragma pack(push) 00042 #pragma pack(1) 00043 00047 00048 template<typename T, size_t SZ> class array_t 00049 { 00050 T a_[SZ]; 00051 public: 00052 typedef T value_type; 00053 typedef typename std::vector<T>::iterator iterator; 00054 typedef typename std::vector<T>::const_iterator const_iterator; 00055 00056 typedef typename std::vector<T>::reverse_iterator reverse_iterator; 00057 typedef typename std::vector<T>::const_reverse_iterator const_reverse_iterator; 00058 00059 typedef typename std::vector<T>::size_type size_type; 00060 typedef typename std::vector<T>::difference_type difference_type; 00061 00062 typedef T& reference; 00063 typedef const T& const_reference; 00064 00065 // reference operator[](size_type i) { return a_[i]; } 00066 // const_reference operator[](size_type i) const { return a_[i]; } 00067 00068 iterator begin() { return iterator(a_); } 00069 iterator end() { return iterator(a_ + SZ); } 00070 const_iterator begin() const { return const_iterator(a_); } 00071 const_iterator end() const { return const_iterator(a_ + SZ); } 00072 00073 reverse_iterator rbegin() { return reverse_iterator(a_); } 00074 reverse_iterator rend() { return reverse_iterator(a_ + SZ); } 00075 const_reverse_iterator rbegin() const { return const_reverse_iterator(a_); } 00076 const_reverse_iterator rend() const { return const_reverse_iterator(a_ + SZ); } 00077 00078 operator const T*() const { return a_; } 00079 operator T*() { return a_; } 00080 00081 static size_type size() { return SZ; } 00082 static bool empty() { return false; } 00083 static size_type max_size() { return SZ; } 00084 enum { static_size = SZ }; 00085 00086 reference front() { return a_[0]; } 00087 const_reference front() const { return a_[0]; } 00088 reference back() { return a_[SZ-1]; }; 00089 const_reference back() const { return a_[SZ-1]; } 00090 00091 // swap (note: linear complexity) 00092 void swap (array_t<T,SZ>& y) { 00093 std::swap_ranges(begin(),end(),y.begin()); 00094 } 00095 00096 // assignment with type conversion 00097 template <typename T2> 00098 array_t<T,SZ>& operator= (const array_t<T2,SZ>& rhs) { 00099 std::copy(rhs.begin(),rhs.end(), begin()); 00100 return *this; 00101 } 00102 00103 // assign one value to all elements 00104 void assign (const T& value) 00105 { 00106 std::fill_n(begin(),size(),value); 00107 } 00108 00109 reference at(size_type i) { rangecheck(i); return a_[i]; } 00110 const_reference at(size_type i) const { rangecheck(i); return a_[i]; } 00111 00112 private: 00113 // check range (may be private because it is static) 00114 static void rangecheck (size_type i) { 00115 if (i >= size()) { throw std::range_error("array"); } 00116 } 00117 00118 }; 00120 #pragma pack(pop) 00121 00122 // comparisons 00123 template<class T, size_t SZ> 00124 bool operator== (const array_t<T,SZ>& x, const array_t<T,SZ>& y) { 00125 return std::equal(x.begin(), x.end(), y.begin()); 00126 } 00127 template<class T, size_t SZ> 00128 bool operator< (const array_t<T,SZ>& x, const array_t<T,SZ>& y) { 00129 return std::lexicographical_compare(x.begin(),x.end(),y.begin(),y.end()); 00130 } 00131 template<class T, size_t SZ> 00132 bool operator!= (const array_t<T,SZ>& x, const array_t<T,SZ>& y) { 00133 return !(x==y); 00134 } 00135 template<class T, size_t SZ> 00136 bool operator> (const array_t<T,SZ>& x, const array_t<T,SZ>& y) { 00137 return y<x; 00138 } 00139 template<class T, size_t SZ> 00140 bool operator<= (const array_t<T,SZ>& x, const array_t<T,SZ>& y) { 00141 return !(y<x); 00142 } 00143 template<class T, size_t SZ> 00144 bool operator>= (const array_t<T,SZ>& x, const array_t<T,SZ>& y) { 00145 return !(x<y); 00146 } 00147 } 00148 00149 #endif