00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028 #ifndef PackedList_I
00029 #define PackedList_I
00030
00031 #include "IOstreams.H"
00032
00033
00034
00035 namespace Foam
00036 {
00037
00038
00039 template<int nBits>
00040 inline label PackedList<nBits>::intSize(const label sz)
00041 {
00042 const label nElemsPerLabel = sizeof(unsigned int)*8/nBits;
00043
00044 return (sz+nElemsPerLabel-1)/nElemsPerLabel;
00045 }
00046
00047
00048
00049 template<int nBits>
00050 inline label PackedList<nBits>::intIndex(const label i)
00051 {
00052 const label nElemsPerLabel = sizeof(unsigned int)*8/nBits;
00053
00054
00055 label elemI = i/nElemsPerLabel;
00056
00057 return elemI;
00058 }
00059
00060
00061
00062 template<int nBits>
00063 inline void PackedList<nBits>::checkIndex(const label i) const
00064 {
00065 if (!size_)
00066 {
00067 FatalErrorIn("PackedList<nBits>::checkIndex(const label)")
00068 << "attempt to access element from zero sized list"
00069 << abort(FatalError);
00070 }
00071 else if (i<0 || i>=size_)
00072 {
00073 FatalErrorIn("PackedList<nBits>::checkIndex(const label)")
00074 << "index " << i << " out of range 0 ... " << size_-1
00075 << abort(FatalError);
00076 }
00077 }
00078
00079
00080
00081 template<int nBits>
00082 inline void PackedList<nBits>::checkValue(const unsigned int val) const
00083 {
00084 if (val>=(1u << nBits))
00085 {
00086 FatalErrorIn("PackedList<T>::set(const unsigned int)")
00087 << "value " << label(val) << " out of range 0 ... "
00088 << label((1u << nBits)-1)
00089 << " representable by " << nBits << " bits"
00090 << abort(FatalError);
00091 }
00092 }
00093
00094
00095
00096
00097 template<int nBits>
00098 inline PackedList<nBits>::PackedList()
00099 :
00100 List<unsigned int>(0),
00101 size_(0)
00102 {}
00103
00104
00105
00106 template<int nBits>
00107 inline PackedList<nBits>::PackedList(const label size)
00108 :
00109 List<unsigned int>(intSize(size), 0u),
00110 size_(size)
00111 {}
00112
00113
00114
00115
00116 template<int nBits>
00117 inline label PackedList<nBits>::size() const
00118 {
00119 return size_;
00120 }
00121
00122
00123
00124 template<int nBits>
00125 inline unsigned int PackedList<nBits>::get(const label i) const
00126 {
00127 # ifdef DEBUGList
00128 checkIndex(i);
00129 # endif
00130
00131
00132 const label nElemsPerLabel = sizeof(unsigned int)*8/nBits;
00133
00134 unsigned int mask = ((1u << nBits) - 1);
00135
00136 label indexInLabel = i % nElemsPerLabel;
00137
00138
00139 label startBit = nBits*indexInLabel;
00140
00141 return (List<unsigned int>::operator[](intIndex(i)) >> startBit) & mask;
00142 }
00143
00144
00145 template<int nBits>
00146 inline unsigned int PackedList<nBits>::operator[](const label i) const
00147 {
00148 return get(i);
00149 }
00150
00151
00152
00153 template<int nBits>
00154 inline void PackedList<nBits>::set(const label i, const unsigned int val)
00155 {
00156 # ifdef DEBUGList
00157 checkIndex(i);
00158 checkValue(val);
00159 # endif
00160
00161
00162 const label nElemsPerLabel = sizeof(unsigned int)*8/nBits;
00163
00164 unsigned int mask = ((1u << nBits) - 1);
00165
00166 label indexInLabel = i % nElemsPerLabel;
00167
00168
00169 label startBit = nBits*indexInLabel;
00170
00171
00172 unsigned int shiftedMask = mask << startBit;
00173
00174 unsigned int shiftedVal = val << startBit;
00175
00176 unsigned int& elem = List<unsigned int>::operator[](intIndex(i));
00177
00178 elem = (elem & ~shiftedMask) | shiftedVal;
00179 }
00180
00181
00182 template<int nBits>
00183 inline ::Foam::reference PackedList<nBits>::operator[](const label i)
00184 {
00185 # ifdef DEBUGList
00186 checkIndex(i);
00187 # endif
00188
00189
00190 const label nElemsPerLabel = sizeof(unsigned int)*8/nBits;
00191
00192 unsigned int mask = ((1u << nBits) - 1);
00193
00194 label indexInLabel = i % nElemsPerLabel;
00195
00196
00197 label startBit = nBits*indexInLabel;
00198
00199 unsigned int& elem = List<unsigned int>::operator[](intIndex(i));
00200
00201 return ::Foam::reference(elem, mask, startBit);
00202 }
00203
00204
00205
00206 template<int nBits>
00207 inline void PackedList<nBits>::operator=(const unsigned int val)
00208 {
00209 # ifdef DEBUGList
00210 checkValue(val);
00211 # endif
00212 List<unsigned int>::operator=(val);
00213 }
00214
00215
00216
00217 }
00218
00219
00220
00221 #endif
00222
00223
Copyright © 2000-2007 OpenCFD Ltd