blitz Version 0.10
|
00001 // -*- C++ -*- 00002 /*************************************************************************** 00003 * blitz/traversal.h Declaration of the TraversalOrder classes 00004 * 00005 * $Id: traversal.h,v 1.6 2011/03/25 22:41:16 julianc Exp $ 00006 * 00007 * Copyright (C) 1997-2011 Todd Veldhuizen <tveldhui@acm.org> 00008 * 00009 * This file is a part of Blitz. 00010 * 00011 * Blitz is free software: you can redistribute it and/or modify 00012 * it under the terms of the GNU Lesser General Public License 00013 * as published by the Free Software Foundation, either version 3 00014 * of the License, or (at your option) any later version. 00015 * 00016 * Blitz is distributed in the hope that it will be useful, 00017 * but WITHOUT ANY WARRANTY; without even the implied warranty of 00018 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 00019 * GNU Lesser General Public License for more details. 00020 * 00021 * You should have received a copy of the GNU Lesser General Public 00022 * License along with Blitz. If not, see <http://www.gnu.org/licenses/>. 00023 * 00024 * Suggestions: blitz-devel@lists.sourceforge.net 00025 * Bugs: blitz-support@lists.sourceforge.net 00026 * 00027 * For more information, please see the Blitz++ Home Page: 00028 * https://sourceforge.net/projects/blitz/ 00029 * 00030 ***************************************************************************/ 00031 00032 // Fast traversal orders require the ISO/ANSI C++ standard library 00033 // (particularly set). 00034 #ifdef BZ_HAVE_STD 00035 00036 #ifndef BZ_TRAVERSAL_H 00037 #define BZ_TRAVERSAL_H 00038 00039 #ifndef BZ_TINYVEC_H 00040 #include <blitz/tinyvec.h> 00041 #endif 00042 00043 #ifndef BZ_VECTOR_H 00044 #include <blitz/vector.h> 00045 #endif 00046 00047 #include <set> 00048 00049 BZ_NAMESPACE(blitz) 00050 00051 template<int N_dimensions> 00052 class TraversalOrder { 00053 00054 public: 00055 typedef TinyVector<int, N_dimensions> T_coord; 00056 typedef Vector<T_coord> T_traversal; 00057 00058 TraversalOrder() 00059 { 00060 size_ = 0; 00061 } 00062 00063 TraversalOrder(const T_coord& size, T_traversal& order) 00064 : size_(size), order_(order) 00065 { } 00066 00067 TraversalOrder(const T_coord& size) 00068 : size_(size) 00069 { } 00070 00071 T_coord operator[](int i) const 00072 { return order_[i]; } 00073 00074 T_coord& operator[](int i) 00075 { return order_[i]; } 00076 00077 int length() const 00078 { return order_.length(); } 00079 00080 bool operator<(const TraversalOrder<N_dimensions>& x) const 00081 { 00082 for (int i=0; i < N_dimensions; ++i) 00083 { 00084 if (size_[i] < x.size_[i]) 00085 return true; 00086 else if (size_[i] > x.size_[i]) 00087 return false; 00088 } 00089 return false; 00090 } 00091 00092 bool operator==(const TraversalOrder<N_dimensions>& x) const 00093 { 00094 for (int i=0; i < N_dimensions; ++i) 00095 { 00096 if (size_[i] != x.size_[i]) 00097 return false; 00098 } 00099 00100 return true; 00101 } 00102 00103 protected: 00104 T_traversal order_; 00105 T_coord size_; 00106 }; 00107 00108 /* 00109 * This specialization is provided to avoid problems with zero-length 00110 * vectors. 00111 */ 00112 template<> 00113 class TraversalOrder<0> { 00114 public: 00115 TraversalOrder () {} // AJS 00116 }; 00117 00118 template<int N_dimensions> 00119 class TraversalOrderCollection { 00120 public: 00121 typedef TraversalOrder<N_dimensions> T_traversal; 00122 typedef _bz_typename T_traversal::T_coord T_coord; 00123 typedef set<T_traversal> T_set; 00124 typedef _bz_typename set<T_traversal>::const_iterator T_iterator; 00125 00126 const T_traversal* find(const T_coord& size) 00127 { 00128 T_iterator iter = traversals_.find(T_traversal(size)); 00129 if (iter != traversals_.end()) 00130 return &(*iter); 00131 return 0; 00132 } 00133 00134 void insert(T_traversal x) 00135 { 00136 traversals_.insert(x); 00137 } 00138 00139 protected: 00140 static T_set traversals_; 00141 }; 00142 00143 template<int N_dimensions> 00144 _bz_typename TraversalOrderCollection<N_dimensions>::T_set 00145 TraversalOrderCollection<N_dimensions>::traversals_; 00146 00147 /* 00148 * This specialization is provided to avoid problems with zero-length 00149 * vectors. 00150 */ 00151 00152 template<> 00153 class TraversalOrderCollection<0> { 00154 public: 00155 typedef int T_traversal; 00156 typedef int T_coord; 00157 typedef int T_set; 00158 typedef int T_iterator; 00159 00160 const T_traversal* find(const T_coord& size) 00161 { return 0; } 00162 }; 00163 00164 BZ_NAMESPACE_END 00165 00166 #include <blitz/traversal.cc> 00167 00168 #endif // BZ_TRAVERSAL_H 00169 00170 #endif // BZ_HAVE_STD 00171