|
Blender
V2.59
|
00001 /* 00002 * $Id: CTR_TaggedIndex.h 36860 2011-05-24 11:19:11Z blendix $ 00003 * ***** BEGIN GPL LICENSE BLOCK ***** 00004 * 00005 * This program is free software; you can redistribute it and/or 00006 * modify it under the terms of the GNU General Public License 00007 * as published by the Free Software Foundation; either version 2 00008 * of the License, or (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 00016 * along with this program; if not, write to the Free Software Foundation, 00017 * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. 00018 * 00019 * The Original Code is Copyright (C) 2001-2002 by NaN Holding BV. 00020 * All rights reserved. 00021 * 00022 * The Original Code is: all of this file. 00023 * 00024 * Contributor(s): none yet. 00025 * 00026 * ***** END GPL LICENSE BLOCK ***** 00027 */ 00028 00041 #ifndef NAN_INCLUDED_CTR_TaggedIndex_h 00042 #define NAN_INCLUDED_CTR_TaggedIndex_h 00043 00053 #include <functional> 00054 00055 #include "MEM_sys_types.h" 00056 00057 enum { 00058 00059 empty_tag = 0x0, 00060 empty_index = 0xffffffff 00061 }; 00062 00063 template < 00064 int tag_shift, 00065 int index_mask 00066 > class CTR_TaggedIndex { 00067 public: 00068 CTR_TaggedIndex( 00069 ) : 00070 m_val ((empty_tag << tag_shift) | (empty_index & index_mask)) 00071 { 00072 } 00073 00074 CTR_TaggedIndex( 00075 const int val 00076 ) : 00077 m_val ((val & index_mask) | ((empty_tag << tag_shift) & (~index_mask))) { 00078 } 00079 00080 CTR_TaggedIndex( 00081 const unsigned int val 00082 ) : 00083 m_val ((val & index_mask) | ((empty_tag << tag_shift) & (~index_mask))) { 00084 } 00085 00086 CTR_TaggedIndex( 00087 const long int val 00088 ) : 00089 m_val ( ((long int) val & index_mask) 00090 | ( (empty_tag << tag_shift) 00091 & (~index_mask)) ) { 00092 } 00093 00094 CTR_TaggedIndex( 00095 const long unsigned int val 00096 ) : 00097 m_val ( ((long unsigned int)val & index_mask) 00098 | ( (empty_tag << tag_shift) 00099 & (~index_mask) ) ) { 00100 } 00101 00102 00103 #if defined(_WIN64) 00104 CTR_TaggedIndex( 00105 const uint64_t val 00106 ) : 00107 m_val ( ((uint64_t)val & index_mask) 00108 | ( (empty_tag << tag_shift) 00109 & (~index_mask) ) ) { 00110 } 00111 #endif 00112 00113 CTR_TaggedIndex( 00114 const CTR_TaggedIndex &my_index 00115 ): 00116 m_val(my_index.m_val) 00117 { 00118 } 00119 00120 bool 00121 operator == ( 00122 const CTR_TaggedIndex& rhs 00123 ) const { 00124 00125 return ((this->m_val & index_mask) == (rhs.m_val & index_mask)); 00126 } 00127 00128 operator unsigned int () const { 00129 return m_val & index_mask; 00130 } 00131 00132 operator unsigned long int () const { 00133 return (unsigned long int)(m_val & index_mask); 00134 } 00135 00136 operator int () const { 00137 return int(m_val & index_mask); 00138 } 00139 00140 operator long int () const { 00141 return (long int)(m_val & index_mask); 00142 } 00143 00144 #if defined(_WIN64) 00145 operator uint64_t () const { 00146 return (uint64_t)(m_val & index_mask); 00147 } 00148 #endif 00149 00150 bool 00151 IsEmpty( 00152 ) const { 00153 return ((m_val & index_mask) == (empty_index & index_mask)); 00154 } 00155 00156 00157 static 00158 CTR_TaggedIndex 00159 Empty( 00160 ) { 00161 return CTR_TaggedIndex(); 00162 } 00163 00164 void 00165 Invalidate( 00166 ) { 00167 m_val = (empty_tag << tag_shift) | (empty_index & index_mask); 00168 } 00169 00170 00171 unsigned int 00172 Tag ( 00173 ) const { 00174 return m_val >> tag_shift; 00175 } 00176 00177 void 00178 SetTag( 00179 unsigned int tag 00180 ) { 00181 m_val = (m_val & index_mask) | ((tag << tag_shift) & (~index_mask)); 00182 } 00183 00184 void 00185 EmptyTag( 00186 ) { 00187 m_val = (m_val & index_mask) | ((empty_tag << tag_shift) & (~index_mask)); 00188 } 00189 00190 bool 00191 IsEmptyTag( 00192 ) const { 00193 return (Tag() == Empty().Tag()); 00194 } 00195 00196 // functionals 00197 00198 struct greater : std::binary_function<CTR_TaggedIndex, CTR_TaggedIndex, bool> 00199 { 00200 bool 00201 operator()( 00202 const CTR_TaggedIndex& a, 00203 const CTR_TaggedIndex& b 00204 ) const { 00205 return (int(a) > int(b)); 00206 } 00207 }; 00208 00209 00210 private : 00211 CTR_TaggedIndex( 00212 const CTR_TaggedIndex *index 00213 ) {}; 00214 00215 unsigned int m_val; 00216 00217 00218 }; 00219 00220 #endif 00221