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