00001 /* Copyright (C) 2010 webis.de 00002 * All rights reserved. 00003 */ 00004 #ifndef AITOOLS_INVERTEDINDEX_SINGLE_HPP 00005 #define AITOOLS_INVERTEDINDEX_SINGLE_HPP 00006 00007 #include "Value.hpp" 00008 #include <iostream> 00009 #include <cassert> 00010 #include <cstring> 00011 00012 namespace aitools { 00013 namespace invertedindex { 00014 00024 template <typename T> 00025 class Single : public Value 00026 { 00027 00028 public: // nested types 00029 00030 typedef T element_type; 00031 00032 public: // class data 00033 00034 static const std::string classname; 00035 00036 // constant buffer size 00037 static const size_t sizeof_T = sizeof(T); 00038 00039 public: // c'tor / d'tor 00040 00044 Single(); 00045 00049 Single(T element); 00050 00054 Single(const Single<T>& single); 00055 00059 virtual ~Single(); 00060 00061 public: // member 00062 00063 void clear(); 00064 00065 double score() const; 00066 00067 bool operator==(const Single<T>& single) const; 00068 00069 bool operator!=(const Single<T>& single) const; 00070 00071 bool operator>(const Single<T>& single) const; 00072 00073 bool operator<(const Single<T>& single) const; 00074 00075 void parse(std::istream& is); 00076 00077 T& element(); 00078 00079 const T& element() const; 00080 00081 void print(std::ostream& os) const; 00082 00083 void to_bytes(ByteBuffer& buffer) const; 00084 00085 void wrap(const char* data, size_t size); 00086 00087 void wrap(const ByteBuffer& buffer); 00088 00089 private: // data 00090 00091 T element_; 00092 00093 }; 00094 00095 // IMPLEMENTATION 00096 00097 template <typename T> 00098 const std::string 00099 Single<T>::classname 00100 ("Single<" + Converter::nameof<T>() + ">"); 00101 00102 template <typename T> 00103 Single<T>::Single() 00104 : element_(0) 00105 {} 00106 00107 template <typename T> 00108 Single<T>::Single(T element) 00109 : element_(element) 00110 {} 00111 00112 template <typename T> 00113 Single<T>::Single(Single<T> const& single) 00114 : element_(single.element_) 00115 {} 00116 00117 template <typename T> 00118 Single<T>::~Single() 00119 {} 00120 00121 template <typename T> 00122 void 00123 Single<T>::clear() 00124 { 00125 element_ = 0; 00126 } 00127 00128 template <typename T> 00129 double 00130 Single<T>::score() const 00131 { 00132 return element_; 00133 } 00134 00135 template <typename T> 00136 bool 00137 Single<T>::operator==(const Single<T>& single) const 00138 { 00139 return element_ == single.element_; 00140 } 00141 00142 template <typename T> 00143 bool 00144 Single<T>::operator!=(const Single<T>& single) const 00145 { 00146 return !(*this == single); 00147 } 00148 00149 template <typename T> 00150 bool 00151 Single<T>::operator>(const Single<T>& single) const 00152 { 00153 return score() > single.score(); 00154 } 00155 00156 template <typename T> 00157 bool 00158 Single<T>::operator<(const Single<T>& single) const 00159 { 00160 return score() < single.score(); 00161 } 00162 00163 template <typename T> 00164 void 00165 Single<T>::parse(std::istream& is) 00166 { 00167 // force parsing as integer instead of a single character 00168 if (sizeof_T == sizeof_int8_t) is >> tmp_int, element_ = tmp_int; 00169 else is >> element_; 00170 } 00171 00172 template <typename T> 00173 T& 00174 Single<T>::element() 00175 { 00176 return element_; 00177 } 00178 00179 template <typename T> 00180 const T& 00181 Single<T>::element() const 00182 { 00183 return element_; 00184 } 00185 00186 template <typename T> 00187 void 00188 Single<T>::print(std::ostream& os) const 00189 { 00190 // force printing as integer instead of a not printable character 00191 if (sizeof_T == sizeof_int8_t) os << (int) element_; 00192 else os << element_; 00193 } 00194 00195 template <typename T> 00196 void 00197 Single<T>::to_bytes(ByteBuffer& buffer) const 00198 { 00199 buffer.resize(sizeof_T); 00200 std::memcpy(buffer.data(), &element_, sizeof_T); 00201 } 00202 00203 template <typename T1> 00204 void 00205 Single<T1>::wrap(const char* data, size_t size) 00206 { 00207 assert(data != NULL); 00208 assert(size >= sizeof_T); 00209 std::memcpy(&element_, data, sizeof_T); 00210 } 00211 00212 template <typename T1> 00213 void 00214 Single<T1>::wrap(const ByteBuffer& buffer) 00215 { 00216 Value::wrap(buffer); 00217 } 00218 00219 } // namespace invertedindex 00220 } // namespace aitools 00221 00222 #endif // AITOOLS_INVERTEDINDEX_SINGLE_HPP