/* * mstring.h 1.5 2004/11/16 * * A fast string implementation with a maximum of 8 characters * ONLY for LITTLE-ENDIAN machines! * * By Wu Yongwei * */ #ifndef _MSTRING_H #define _MSTRING_H #include #include #include #ifdef _WIN32 typedef unsigned __int64 ULONGLONG; #else typedef unsigned long long ULONGLONG; #endif // _WIN32 /** Reverse string copy. */ inline char* strrncpy(char* dest, const char* src, size_t n) { while (n && *src) { dest[--n] = *src; src++; } while (n) dest[--n] = '\0'; return dest; } /** * "Medium-sized" string. * A class intended for fast operations as key of map/hash_map. * Limitations: "string" length limited to 8 characters (more will be * truncated); and only implemented for little-endian machines. * * @author Wu Yongwei * @version 1.0, 2002/03/05 */ union mstring { char chars[8]; ULONGLONG index; mstring(const char* sz) { strrncpy(chars, sz, 8); // Little-endian! } friend bool operator< (const mstring& x, const mstring& y) { return x.index < y.index; } friend bool operator> (const mstring& x, const mstring& y) { return x.index > y.index; } friend bool operator<=(const mstring& x, const mstring& y) { return x.index <= y.index; } friend bool operator>=(const mstring& x, const mstring& y) { return x.index >= y.index; } friend bool operator==(const mstring& x, const mstring& y) { return x.index == y.index; } friend bool operator!=(const mstring& x, const mstring& y) { return x.index != y.index; } friend std::ostream& operator<<(std::ostream& os, const mstring& ms) { for (int i = 7; i >= 0 && ms.chars[i];) os << ms.chars[i--]; return os; } operator std::string() const { std::string _s; for (int i = 7; i >= 0 && chars[i];) _s += chars[i--]; return _s; } }; #ifndef SGI_HASH_NAMESPACE #if defined(__SGI_STL_HASH_MAP) || defined(_STLP_HASH_MAP) #define SGI_HASH_NAMESPACE std #elif defined(__SGI_STL_INTERNAL_HASH_MAP_H) #define SGI_HASH_NAMESPACE __gnu_cxx #endif #endif // SGI_HASH_NAMESPACE #ifdef SGI_HASH_NAMESPACE namespace SGI_HASH_NAMESPACE { /** Hash function class. */ template <> struct hash { size_t operator()(const mstring& x) const { return static_cast(x.index) ^ static_cast(x.index >> 32); } }; } #endif // SGI_HASH_NAMESPACE #endif // _MSTRING_H