HyperspaceExplorer 0.7.1
|
00001 /* 00002 Hyperspace Explorer - vizualizing higher-dimensional geometry 00003 Copyright (C) 2010 Lene Preuss <lene.preuss@gmail.com> 00004 00005 This program is free software; you can redistribute it and/or modify 00006 it under the terms of the GNU General Public License as published by 00007 the Free Software Foundation; either version 2 of the License, or 00008 (at your option) any later version. 00009 00010 This program is distributed in the hope that it will be useful, 00011 but WITHOUT ANY WARRANTY; without even the implied warranty of 00012 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 00013 GNU General Public License for more details. 00014 00015 You should have received a copy of the GNU General Public License along 00016 with this program; if not, write to the Free Software Foundation, Inc., 00017 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. 00018 00019 */ 00020 #ifndef ARRAYLIST_IMPL_H 00021 #define ARRAYLIST_IMPL_H 00022 00023 #include "ArrayList.h" 00024 00025 #include <sstream> 00026 #include <stdexcept> 00027 #include <cstdarg> 00028 #include <cassert> 00029 00030 template <unsigned size, typename T> 00031 ArrayList<size, T>::ArrayList(): 00032 _elements() { } 00035 template <unsigned size, typename T> 00036 ArrayList<size, T>::ArrayList(const T &x): 00037 _elements(std::make_pair(x, ArrayList<size-1, T>(x))) { } 00041 template <unsigned size, typename T> 00042 ArrayList<size, T>::ArrayList(const T &head, const ArrayList<size-1, T> &tail): 00043 _elements(std::make_pair(head, tail)) { } 00044 00045 00046 template <unsigned size, typename T> 00047 const T &ArrayList<size, T>::head() const { 00048 return _elements.first; 00049 } 00050 00051 template <unsigned size, typename T> 00052 T &ArrayList<size, T>::head() { 00053 return _elements.first; 00054 } 00055 00056 template <unsigned size, typename T> 00057 const ArrayList<size-1, T> &ArrayList<size, T>::tail() const { 00058 return _elements.second; 00059 } 00060 00061 template <unsigned size, typename T> 00062 ArrayList<size-1, T> &ArrayList<size, T>::tail() { 00063 return _elements.second; 00064 } 00065 00068 template <unsigned size, typename T> 00069 T &ArrayList<size, T>::operator[](unsigned i) { 00070 assert(i < size); 00071 if (i == 0) return head(); 00072 ArrayList<size-1, T> _tail = tail(); 00073 return _tail.operator[](i-1); 00074 } 00075 00078 template <unsigned size, typename T> 00079 const T &ArrayList<size, T>::operator[](unsigned i) const { 00080 assert(i < size); 00081 if (i == 0) return head(); 00082 return tail()[i-1]; 00083 } 00084 00085 template <unsigned size, typename T> 00086 ArrayList<size-1, T> ArrayList<size, T>::minusElement(unsigned i) const { 00087 assert(i < size); 00088 if (i == 0) return tail(); 00089 return ArrayList<size-1, T>(head(), tail().minusElement(i-1)); 00090 } 00091 00092 template <unsigned size, typename T> 00093 bool ArrayList<size, T>::contains (const T &x) const { 00094 if (head() == x) return true; 00095 return tail().contains(x); 00096 } 00097 00098 template <unsigned size, typename T> 00099 std::string ArrayList<size, T>::toString() const { 00100 std::ostringstream o; 00101 o << _elements.first << ", " << _elements.second.toString(); 00102 return o.str(); 00103 } 00104 00106 00107 template <typename T> 00108 ArrayList<1, T>::ArrayList() { } 00109 00110 template <typename T> 00111 ArrayList<1, T>::ArrayList(const T &x): element_(x) { } 00112 00113 template <typename T> 00114 ArrayList<1, T>::ArrayList(T x, ArrayList<0, T>): element_(x) { } 00115 00116 template <typename T> 00117 const T &ArrayList<1, T>::head() const { 00118 return element_; 00119 } 00120 00121 template <typename T> 00122 T &ArrayList<1, T>::head() { 00123 return element_; 00124 } 00125 00126 template <typename T> 00127 ArrayList<0, T> ArrayList<1, T>::tail() const { 00128 return ArrayList<0, T>(); 00129 } 00130 00133 template <typename T> 00134 T &ArrayList<1, T>::operator[](unsigned) { 00135 return head(); 00136 } 00137 00140 template <typename T> 00141 const T &ArrayList<1, T>::operator[](unsigned) const { 00142 return head(); 00143 } 00144 00145 template <typename T> 00146 ArrayList<0, T> ArrayList<1, T>::minusElement(unsigned) const { 00147 return tail(); 00148 } 00149 00150 template <typename T> 00151 bool ArrayList<1, T>::contains (const T &x) const { 00152 return head() == x; 00153 } 00154 00155 template <typename T> 00156 std::string ArrayList<1, T>::toString() const { 00157 std::ostringstream o; 00158 o << element_; 00159 return o.str(); 00160 } 00161 00163 00164 template <typename T> 00165 ArrayList<0, T>::ArrayList() { } 00166 00167 template <typename T> 00168 ArrayList<0, T>::ArrayList(const T &) { } 00169 00170 template <typename T> 00171 std::string ArrayList<0, T>::toString() const { 00172 return ""; 00173 } 00174 00176 00177 template<unsigned size, typename T> 00178 bool isPermutation(const ArrayList<size, T> &list1, const ArrayList<size, T> &list2) { 00179 for (unsigned i = 0; i < size; ++i) { 00180 if (list1.head() == list2[i] && 00181 isPermutation(list1.tail(), list2.minusElement(i))) return true; 00182 } 00183 return false; 00184 } 00185 00186 template<typename T> 00187 bool isPermutation(const ArrayList<1, T> &list1, const ArrayList<1, T> &list2) { 00188 return list1.head() == list2.head(); 00189 } 00190 00192 00193 template<typename T> 00194 ArrayList<1, T> makeArrayList(const T &x0) { 00195 return ArrayList<1, T>(x0); 00196 } 00197 template<typename T> 00198 ArrayList<2, T> makeArrayList(const T &x0, const T &x1) { 00199 return ArrayList<2, T>(x0, makeArrayList(x1)); 00200 } 00201 template<typename T> 00202 ArrayList<3, T> makeArrayList(const T &x0, const T &x1, const T &x2) { 00203 return ArrayList<3, T>(x0, makeArrayList(x1, x2)); 00204 } 00205 template<typename T> 00206 ArrayList<4, T> makeArrayList(const T &x0, const T &x1, const T &x2, const T &x3) { 00207 return ArrayList<4, T>(x0, makeArrayList(x1, x2, x3)); 00208 } 00209 template<typename T> 00210 ArrayList<5, T> makeArrayList(const T &x0, const T &x1, const T &x2, const T &x3, const T &x4) { 00211 return ArrayList<5, T>(x0, makeArrayList(x1, x2, x3, x4)); 00212 } 00213 template<typename T> 00214 ArrayList<6, T> makeArrayList(const T &x0, const T &x1, const T &x2, const T &x3, const T &x4, 00215 const T &x5) { 00216 return ArrayList<6, T>(x0, makeArrayList(x1, x2, x3, x4, x5)); 00217 } 00218 template<typename T> 00219 ArrayList<7, T> makeArrayList(const T &x0, const T &x1, const T &x2, const T &x3, const T &x4, 00220 const T &x5, const T &x6) { 00221 return ArrayList<7, T>(x0, makeArrayList(x1, x2, x3, x4, x5, x6)); 00222 } 00223 template<typename T> 00224 ArrayList<8, T> makeArrayList(const T &x0, const T &x1, const T &x2, const T &x3, const T &x4, 00225 const T &x5, const T &x6, const T &x7) { 00226 return ArrayList<8, T>(x0, makeArrayList(x1, x2, x3, x4, x5, x6, x7)); 00227 } 00228 template<typename T> 00229 ArrayList<9, T> makeArrayList(const T &x0, const T &x1, const T &x2, const T &x3, const T &x4, 00230 const T &x5, const T &x6, const T &x7, const T &x8) { 00231 return ArrayList<9, T>(x0, makeArrayList(x1, x2, x3, x4, x5, x6, x7, x8)); 00232 } 00233 template<typename T> 00234 ArrayList<10, T> makeArrayList(const T &x0, const T &x1, const T &x2, const T &x3, const T &x4, 00235 const T &x5, const T &x6, const T &x7, const T &x8, const T &x9) { 00236 return ArrayList<10, T>(x0, makeArrayList(x1, x2, x3, x4, x5, x6, x7, x8, x9)); 00237 } 00238 00239 #endif