00001
00002
00003
00004
00005
00006
00007 #ifndef __ugTightIndex
00008 #define __ugTightIndex
00009 #include <map>
00010 #include <iostream>
00011 #include <sstream>
00012 #include "tpt_typedefs.h"
00013 #include <cassert>
00014
00015 #ifndef uchar
00016 #endif
00017
00018 #define FLAGBITS 2
00019 #define FLAGMASK (uchar(3))
00020 #define HAS_VALUE_MASK (uchar(2))
00021 #define HAS_CHILD_MASK (uchar(1))
00022
00023
00024 extern bool debug;
00025
00026 namespace tpt
00027 {
00028
00029 void
00030 tightwrite(std::ostream& out, uint64_t data, bool flag);
00031
00032 filepos_type
00033 tightread(std::istream& in, std::ios::pos_type stop);
00034
00035 bool
00036 tightfind(std::istream& in,
00037 filepos_type start,
00038 filepos_type stop,
00039 id_type key,
00040 unsigned char& flags);
00041
00042 bool
00043 tightfind_noflags(std::istream& in,
00044 filepos_type start,
00045 filepos_type stop,
00046 id_type key);
00047
00048 char const*
00049 tightfind(char const* const start,
00050 char const* const stop,
00051 id_type key,
00052 unsigned char& flags);
00053
00054 char const*
00055 tightfind_noflags(char const* const start,
00056 char const* const stop,
00057 id_type key);
00058
00059
00060
00068 bool
00069 tightfind_midpoint(std::istream& in, filepos_type start, filepos_type stop);
00070
00071
00072
00073
00074
00075
00076
00077
00085 char const*
00086 tightread2(char const* start, char const* stop, uint16_t& dest);
00087
00088 char const*
00089 tightread4(char const* start, char const* stop, uint32_t& dest);
00090
00091 char const*
00092 tightread8(char const* start, char const* stop, uint64_t& dest);
00093
00094 template<typename numType>
00095 char const*
00096 tightread(char const* start, char const* stop, numType& dest)
00097 {
00098 if (sizeof(numType)==2)
00099 return tightread2(start,stop,reinterpret_cast<uint16_t&>(dest));
00100 if (sizeof(numType)==4)
00101 return tightread4(start,stop,reinterpret_cast<uint32_t&>(dest));
00102 else if (sizeof(numType)==8)
00103 return tightread8(start,stop,reinterpret_cast<uint64_t&>(dest));
00104 assert(0);
00105 return NULL;
00106 }
00107
00108
00109
00110
00111
00112
00113
00114 #if 0
00115 template<typename dtype>
00116 char const*
00117 tightread(char const* start,
00118 char const* stop,
00119 dtype& dest)
00120 {
00121 static char bitmask=127;
00122 dest = 0;
00123 if (*start < 0)
00124 {
00125 dest = (*start)&bitmask;
00126 if (++start==stop || *start >= 0) return start;
00127 dest += dtype((*start)&bitmask)<<7;
00128 if (++start==stop || *start >= 0) return start;
00129 dest += dtype((*start)&bitmask)<<14;
00130 if (++start==stop || *start >= 0) return start;
00131 dest += dtype((*start)&bitmask)<<21;
00132 if (++start==stop || *start >= 0) return start;
00133 dest += dtype((*start)&bitmask)<<28;
00134 if (++start==stop || *start >= 0) return start;
00135 assert(sizeof(dtype) > 4);
00136 dest += dtype((*start)&bitmask)<<35;
00137 if (++start==stop || *start >= 0) return start;
00138 dest += dtype((*start)&bitmask)<<42;
00139 if (++start==stop || *start >= 0) return start;
00140 dest += dtype((*start)&bitmask)<<49;
00141 if (++start==stop || *start >= 0) return start;
00142 dest += dtype((*start)&bitmask)<<56;
00143 if (++start==stop || *start >= 0) return start;
00144 dest += dtype((*start)&bitmask)<<63;
00145 }
00146 else
00147 {
00148 dest = *start;
00149 if (++start==stop || *start < 0) return start;
00150 dest += dtype(*start)<<7;
00151 if (++start==stop || *start < 0) return start;
00152 dest += dtype(*start)<<14;
00153 if (++start==stop || *start < 0) return start;
00154 dest += dtype(*start)<<21;
00155 if (++start==stop || *start < 0) return start;
00156 dest += dtype(*start)<<28;
00157 if (++start==stop || *start < 0) return start;
00158 assert(sizeof(dtype) > 4);
00159 dest += dtype(*start)<<35;
00160 if (++start==stop || *start < 0) return start;
00161 dest += dtype(*start)<<42;
00162 if (++start==stop || *start < 0) return start;
00163 dest += dtype(*start)<<49;
00164 if (++start==stop || *start < 0) return start;
00165 dest += dtype(*start)<<56;
00166 if (++start==stop || *start < 0) return start;
00167 dest += dtype(*start)<<63;
00168 }
00169 assert(start<stop);
00170 return ++start;
00171 }
00172 #endif
00173
00174
00175 }
00176 #endif