00001 /* @HEADER@ */ 00002 // ************************************************************************ 00003 // 00004 // Sundance 00005 // Copyright (2005) Sandia Corporation 00006 // 00007 // Copyright (year first published) Sandia Corporation. Under the terms 00008 // of Contract DE-AC04-94AL85000 with Sandia Corporation, the U.S. Government 00009 // retains certain rights in this software. 00010 // 00011 // This library is free software; you can redistribute it and/or modify 00012 // it under the terms of the GNU Lesser General Public License as 00013 // published by the Free Software Foundation; either version 2.1 of the 00014 // License, or (at your option) any later version. 00015 // 00016 // This library is distributed in the hope that it will be useful, but 00017 // WITHOUT ANY WARRANTY; without even the implied warranty of 00018 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 00019 // 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 this library; if not, write to the Free Software 00023 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 00024 // USA 00025 // Questions? Contact Kevin Long (krlong@sandia.gov), 00026 // Sandia National Laboratories, Livermore, California, USA 00027 // 00028 // ************************************************************************ 00029 /* @HEADER@ */ 00030 00031 #include "SundanceMultiIndex.hpp" 00032 #include "PlayaExceptions.hpp" 00033 00034 using namespace Sundance; 00035 using namespace Sundance; 00036 00037 using namespace Sundance; 00038 using namespace Teuchos; 00039 00040 MultiIndex::MultiIndex() 00041 : m_(maxDim(), 0) 00042 {;} 00043 00044 MultiIndex::MultiIndex(int x, int y, int z) 00045 : m_(maxDim(), 0) 00046 { 00047 m_[0] = x; 00048 m_[1] = y; 00049 m_[2] = z; 00050 } 00051 00052 MultiIndex MultiIndex::operator+(const MultiIndex& other) const 00053 { 00054 MultiIndex rtn; 00055 00056 for (int i=0; i<maxDim(); i++) 00057 { 00058 rtn.m_[i] = m_[i] + other[i]; 00059 } 00060 return rtn; 00061 } 00062 00063 MultiIndex MultiIndex::operator-(const MultiIndex& other) const 00064 { 00065 MultiIndex rtn; 00066 00067 for (int i=0; i<maxDim(); i++) 00068 { 00069 rtn.m_[i] = m_[i] - other[i]; 00070 } 00071 return rtn; 00072 } 00073 00074 MultiIndex MultiIndex::operator-() const 00075 { 00076 MultiIndex rtn; 00077 00078 for (int i=0; i<maxDim(); i++) 00079 { 00080 rtn.m_[i] = -m_[i]; 00081 } 00082 return rtn; 00083 } 00084 00085 string MultiIndex::toString() const 00086 { 00087 return "(" + Teuchos::toString(m_[0]) + "," 00088 + Teuchos::toString(m_[1]) + "," 00089 + Teuchos::toString(m_[2]) + ")"; 00090 } 00091 00092 XMLObject MultiIndex::toXML() const 00093 { 00094 XMLObject rtn("MultiIndex"); 00095 rtn.addAttribute("indices", toString()); 00096 return rtn; 00097 } 00098 00099 bool MultiIndex::operator==(const MultiIndex& m) const 00100 { 00101 for (int i=0; i<maxDim(); i++) 00102 { 00103 if (m_[i] != m[i]) return false; 00104 } 00105 return true; 00106 } 00107 00108 bool MultiIndex::operator<(const MultiIndex& m) const 00109 { 00110 for (int i=0; i<maxDim(); i++) 00111 { 00112 if (m_[i] > m.m_[i]) return false; 00113 if (m_[i] < m.m_[i]) return true; 00114 } 00115 return false; 00116 } 00117 00118 00119 00120 int MultiIndex::order() const 00121 { 00122 int h = 0; 00123 for (int i=0; i<maxDim(); i++) 00124 { 00125 h += m_[i]; 00126 } 00127 return h; 00128 } 00129 00130 bool MultiIndex::isValid() const 00131 { 00132 for (int i=0; i<maxDim(); i++) 00133 { 00134 if (m_[i] < 0) return false; 00135 } 00136 return true; 00137 } 00138 00139 int MultiIndex::firstOrderDirection() const 00140 { 00141 TEUCHOS_TEST_FOR_EXCEPTION(order() != 1, std::logic_error, 00142 "bad order in MultiIndex::firstOrderDirection() const"); 00143 for (int i=0; i<maxDim(); i++) 00144 { 00145 if (m_[i] == 1) return i; 00146 } 00147 return -1; 00148 } 00149 00150 00151 00152 string MultiIndex::coordForm() const 00153 { 00154 std::string rtn; 00155 00156 for (int i=0; i<m_[0]; i++) 00157 { 00158 rtn += "x"; 00159 } 00160 for (int i=0; i<m_[1]; i++) 00161 { 00162 rtn += "y"; 00163 } 00164 for (int i=0; i<m_[2]; i++) 00165 { 00166 rtn += "z"; 00167 } 00168 return rtn; 00169 } 00170 00171