18#if !defined(_GENERIC_H)
33 return (x < 0) ? -x : x;
40inline T absDiff(T x, T y)
42 return (x < y) ? (y - x) : (x - y);
48inline T in(T x, T y, T z)
50 return (x >= y && x < z);
64template <
typename A,
typename B>
65std::ostream &
operator << (std::ostream &stream, std::pair<A, B> p)
67 stream <<
"(" << p.first <<
", " << p.second <<
")";
80std::ostream &
operator << (std::ostream &stream, std::vector<T>
const &v)
83 typename std::vector<T>::const_iterator i;
84 for (i = v.begin(); i != v.end(); i++)
86 stream << (i - v.begin()) <<
": " << *i << std::endl;
95std::ostream &
operator << (std::ostream &stream, std::list<T>
const &l)
98 typename std::list<T>::const_iterator i;
100 for (i = l.begin(); i != l.end(); i++, j++)
102 stream << j <<
": " << *i << std::endl;
107template <
typename TITLE,
typename ITEM,
typename EXPECT,
typename GOT>
108void check(
int &returnCode, TITLE title, ITEM item, EXPECT expect, GOT got)
112 std::cout <<
"Test " << title <<
": expect " << item <<
" = '" << expect <<
"', but got '" << got <<
"'." << std::endl;
121inline std::istream &
operator >> (std::istream &stream, std::vector<std::string> &vec)
126 if (!stream.good())
break;
127 getline(stream, val);
140std::istream &
operator >> (std::istream &stream, std::vector<T> &vec)
145 if (!stream.good())
break;
159 std::ostream &stream,
161 std::pair< std::vector<typename T>::iterator , std::vector< typename T>::iterator >
166 typename IteratorType i;
167 typename std::vector<T>::iterator i;
168 for (i = v.begin(); i != v.end(); i++)
170 stream << *i << std::endl;
184inline uint32_t PackedAccess_1Bit(T byteSequence, uint32_t bitIndex)
186 return (((byteSequence)[bitIndex>>3] >> (bitIndex&0x7)) & 0x1);
190inline void PackedAssign_1Bit(T byteSequence, uint32_t bitIndex, uint32_t value)
192 (byteSequence)[bitIndex>>3] =
193 ((byteSequence)[bitIndex>>3]
194 & ~(1<<(bitIndex&0x07)))
195 | ((value&0x01)<<(bitIndex&0x7));
198inline size_t Packed1BitElementCount2Bytes(uint32_t i)
200 return (
size_t)(i+7)/8;
204inline uint32_t PackedAccess_2Bit(T byteSequence, uint32_t index)
206 return (((byteSequence)[index>>2] >> ((index&0x3)<<1)) & 0x3);
210inline void PackedAssign_2Bit(T byteSequence, uint32_t index, uint32_t value)
212 (byteSequence)[index>>2] =
213 ((byteSequence)[index>>2]
214 & ~(3<<((index&0x03)<<1)))
215 | ((value&0x03)<<((index&0x3)<<1));
218inline size_t Packed2BitElementCount2Bytes(uint32_t i)
220 return (
size_t)(i+3)/4;
224inline uint32_t PackedAccess_4Bit(T byteSequence, uint32_t index)
226 return (((byteSequence)[index>>1] >> ((index&0x1)<<2)) & 0xf);
230inline void PackedAssign_4Bit(T byteSequence, uint32_t index, uint32_t value)
232 (byteSequence)[index>>1] =
233 ((byteSequence)[index>>1]
234 & ~(7<<((index&0x01)<<2)))
235 | ((value&0x0f)<<((index&0x1)<<2));
238inline size_t Packed4BitElementCount2Bytes(uint32_t i)
240 return (
size_t)(i+1)/2;