00001 00038 #ifndef __PrefixNodeBase__ 00039 #define __PrefixNodeBase__ 00040 00041 00042 #include <stdlib.h> 00043 #include <stdint.h> 00044 00063 template<class Head> 00064 class PrefixNodeBase { 00065 public: 00079 protected: 00081 Head head; 00082 00083 public: 00084 00085 PrefixNodeBase() { 00086 head = 0; 00087 } 00088 00090 static const Head endBitMask = (Head(1))<<(8*sizeof(Head)-1); 00091 00095 static inline Head encode(unsigned char letter, Head* codes) { 00096 return codes[letter]; 00097 } 00098 00107 static inline Head autocode(unsigned char letter, Head* codes) { 00108 Head& code = codes[letter]; 00109 if( code == 0 ) { 00110 code = (Head(1))<<codes[256]; 00111 codes[256]++; 00112 if( codes[256] >= 8*sizeof(Head) ) throw "attempt to encode too much characters"; 00113 } 00114 return code; 00115 } 00116 00122 static void resetCodes(Head* codes) { 00123 for(int i=0; i<=256; i++) codes[i] = 0; 00124 codes[0] = endBitMask; 00125 codes[10] = endBitMask; 00126 codes[13] = endBitMask; 00127 } 00128 00135 inline void addLetter(Head encodedLetter) { 00136 head |= encodedLetter; 00137 } 00138 00144 inline int countLetters() { 00145 static Head lettersMask = ~endBitMask; 00146 return __builtin_popcountl( head & lettersMask ); 00147 } 00148 00152 inline int countLetters(Head encodedLetter) { 00153 return __builtin_popcountl( head & (encodedLetter-1) ); 00154 } 00155 00159 inline bool hasLetter(Head encodedLetter) { 00160 return head & encodedLetter; 00161 } 00162 }; 00163 00164 #endif