Main Page | Modules | Namespace List | Class Hierarchy | Alphabetical List | Class List | File List | Namespace Members | Class Members | File Members | Related Pages

ShSwizzle.cpp

00001 // Sh: A GPU metaprogramming language. 00002 // 00003 // Copyright (c) 2003 University of Waterloo Computer Graphics Laboratory 00004 // Project administrator: Michael D. McCool 00005 // Authors: Zheng Qin, Stefanus Du Toit, Kevin Moule, Tiberiu S. Popa, 00006 // Michael D. McCool 00007 // 00008 // This software is provided 'as-is', without any express or implied 00009 // warranty. In no event will the authors be held liable for any damages 00010 // arising from the use of this software. 00011 // 00012 // Permission is granted to anyone to use this software for any purpose, 00013 // including commercial applications, and to alter it and redistribute it 00014 // freely, subject to the following restrictions: 00015 // 00016 // 1. The origin of this software must not be misrepresented; you must 00017 // not claim that you wrote the original software. If you use this 00018 // software in a product, an acknowledgment in the product documentation 00019 // would be appreciated but is not required. 00020 // 00021 // 2. Altered source versions must be plainly marked as such, and must 00022 // not be misrepresented as being the original software. 00023 // 00024 // 3. This notice may not be removed or altered from any source 00025 // distribution. 00027 #include <iostream> 00028 #include <sstream> 00029 #include "ShSwizzle.hpp" 00030 #include "ShError.hpp" 00031 00032 namespace SH { 00033 00034 ShSwizzle::ShSwizzle() 00035 : m_srcSize(0), 00036 m_indices(0), 00037 m_size(0) 00038 { 00039 } 00040 00041 ShSwizzle::ShSwizzle(int srcSize) 00042 : m_srcSize(srcSize), 00043 m_indices(new int[srcSize]), 00044 m_size(srcSize) 00045 { 00046 for (int i = 0; i < srcSize; i++) m_indices[i] = i; 00047 } 00048 00049 ShSwizzle::ShSwizzle(int srcSize, int i0) 00050 : m_srcSize(srcSize), 00051 m_indices(new int[1]), 00052 m_size(1) 00053 { 00054 if (i0 < 0 || i0 >= srcSize) { 00055 delete [] m_indices; 00056 shError( ShSwizzleException(*this, i0, srcSize) ); 00057 } 00058 m_indices[0] = i0; 00059 } 00060 00061 ShSwizzle::ShSwizzle(int srcSize, int i0, int i1) 00062 : m_srcSize(srcSize), 00063 m_indices(new int[2]), 00064 m_size(2) 00065 { 00066 if (i0 < 0 || i0 >= srcSize) { 00067 delete [] m_indices; 00068 shError( ShSwizzleException(*this, i0, srcSize) ); 00069 } 00070 if (i1 < 0 || i1 >= srcSize) { 00071 delete [] m_indices; 00072 shError( ShSwizzleException(*this, i1, srcSize) ); 00073 } 00074 m_indices[0] = i0; 00075 m_indices[1] = i1; 00076 } 00077 00078 ShSwizzle::ShSwizzle(int srcSize, int i0, int i1, int i2) 00079 : m_srcSize(srcSize), 00080 m_indices(new int[3]), 00081 m_size(3) 00082 { 00083 if (i0 < 0 || i0 >= srcSize) { 00084 delete [] m_indices; 00085 shError( ShSwizzleException(*this, i0, srcSize) ); 00086 } 00087 if (i1 < 0 || i1 >= srcSize) { 00088 delete [] m_indices; 00089 shError( ShSwizzleException(*this, i1, srcSize) ); 00090 } 00091 if (i2 < 0 || i2 >= srcSize) { 00092 delete [] m_indices; 00093 shError( ShSwizzleException(*this, i2, srcSize) ); 00094 } 00095 m_indices[0] = i0; 00096 m_indices[1] = i1; 00097 m_indices[2] = i2; 00098 } 00099 00100 ShSwizzle::ShSwizzle(int srcSize, int i0, int i1, int i2, int i3) 00101 : m_srcSize(srcSize), 00102 m_indices(new int[4]), 00103 m_size(4) 00104 { 00105 if (i0 < 0 || i0 >= srcSize) { 00106 delete [] m_indices; 00107 shError( ShSwizzleException(*this, i0, srcSize) ); 00108 } 00109 if (i1 < 0 || i1 >= srcSize) { 00110 delete [] m_indices; 00111 shError( ShSwizzleException(*this, i1, srcSize) ); 00112 } 00113 if (i2 < 0 || i2 >= srcSize) { 00114 delete [] m_indices; 00115 shError( ShSwizzleException(*this, i2, srcSize) ); 00116 } 00117 if (i3 < 0 || i3 >= srcSize) { 00118 delete [] m_indices; 00119 shError( ShSwizzleException(*this, i3, srcSize) ); 00120 } 00121 m_indices[0] = i0; 00122 m_indices[1] = i1; 00123 m_indices[2] = i2; 00124 m_indices[3] = i3; 00125 } 00126 00127 ShSwizzle::ShSwizzle(int srcSize, int size, int* indices) 00128 : m_srcSize(srcSize), 00129 m_indices(new int[size]), 00130 m_size(size) 00131 { 00132 for (int i = 0; i < size; i++) { 00133 if (indices[i] < 0 || indices[i] >= srcSize) { 00134 delete [] m_indices; 00135 shError( ShSwizzleException(*this, indices[i], srcSize) ); 00136 } 00137 m_indices[i] = indices[i]; 00138 } 00139 } 00140 00141 ShSwizzle::ShSwizzle(const ShSwizzle& other) 00142 : m_srcSize(other.m_srcSize), 00143 m_indices(new int[other.m_size]), 00144 m_size(other.m_size) 00145 { 00146 memcpy(m_indices, other.m_indices, sizeof(int)*m_size); 00147 } 00148 00149 ShSwizzle::~ShSwizzle() 00150 { 00151 delete [] m_indices; 00152 } 00153 00154 ShSwizzle& ShSwizzle::operator=(const ShSwizzle& other) 00155 { 00156 if (m_size != other.m_size) { 00157 delete [] m_indices; 00158 m_indices = new int[other.m_size]; 00159 m_size = other.m_size; 00160 } 00161 memcpy(m_indices, other.m_indices, sizeof(int)*m_size); 00162 m_srcSize = other.m_srcSize; 00163 return *this; 00164 } 00165 00166 ShSwizzle& ShSwizzle::operator*=(const ShSwizzle& other) 00167 { 00168 int* indices = new int[other.m_size]; 00169 00170 for (int i = 0; i < other.m_size; i++) { 00171 if (other[i] < 0 || other[i] >= m_size) { 00172 delete indices; 00173 shError( ShSwizzleException(*this, other[i], size()) ); 00174 } 00175 indices[i] = m_indices[other[i]]; 00176 } 00177 m_srcSize = other.m_srcSize; 00178 m_size = other.m_size; 00179 delete [] m_indices; 00180 m_indices = indices; 00181 return *this; 00182 } 00183 00184 ShSwizzle ShSwizzle::operator*(const ShSwizzle& other) const 00185 { 00186 ShSwizzle swizzle(*this); 00187 swizzle *= other; 00188 return swizzle; 00189 } 00190 00191 int ShSwizzle::operator[](int index) const 00192 { 00193 if (index >= m_size || index < 0) shError( ShSwizzleException(*this, index, m_size) ); 00194 return m_indices[index]; 00195 } 00196 00197 ShSwizzleException::ShSwizzleException(const ShSwizzle& s, int index, int size) 00198 : ShException("") 00199 { 00200 std::ostringstream out; 00201 out << "Swizzle error: " << index << " out of range [0, " << size << ") in " << s; 00202 00203 m_message = out.str(); 00204 } 00205 00206 bool ShSwizzle::identity() const 00207 { 00208 if (m_size != m_srcSize) return false; 00209 for (int i = 0; i < m_size; i++) { 00210 if (m_indices[i] != i) return false; 00211 } 00212 return true; 00213 } 00214 00215 bool ShSwizzle::operator==(const ShSwizzle& other) const 00216 { 00217 if (m_srcSize != other.m_srcSize) return false; 00218 if (m_size != other.m_size) return false; 00219 return memcmp(m_indices, other.m_indices, sizeof(int)*m_size) == 0; 00220 } 00221 00222 std::ostream& operator<<(std::ostream& out, const ShSwizzle& swizzle) 00223 { 00224 if (swizzle.identity()) return out; 00225 out << "("; 00226 for (int i = 0; i < swizzle.size(); i++) { 00227 if (i > 0) out << ", "; 00228 out << swizzle[i]; 00229 } 00230 out << ")"; 00231 return out; 00232 } 00233 00234 }

Generated on Mon Oct 18 14:17:40 2004 for Sh by doxygen 1.3.7