00001 /* Copyright (C) 2010 webis.de 00002 * All rights reserved. 00003 */ 00004 #ifndef AITOOLS_INVERTEDINDEX_RECORD_HPP 00005 #define AITOOLS_INVERTEDINDEX_RECORD_HPP 00006 00007 #include "GenericRecord.hpp" 00008 00009 namespace aitools { 00010 namespace invertedindex { 00011 00021 template<typename Value> 00022 class Record { 00023 00024 public: // class data 00025 00026 static const std::string empty_key; 00027 static const char separator = '\t'; 00028 00029 public: // c'tor / d'tor 00030 00031 Record(); 00032 00033 Record(const std::string& key, const Value& value); 00034 00035 Record(const Record<Value>& record); 00036 00037 ~Record(); 00038 00039 public: // member 00040 00041 std::string& key(); 00042 00043 const std::string& key() const; 00044 00045 bool operator==(const Record<Value>& record) const; 00046 00047 bool operator!=(const Record<Value>& record) const; 00048 00049 Record<Value>& operator=(const Record<Value>& record); 00050 00051 void parse(std::istream& is); 00052 00053 void print(std::ostream& os) const; 00054 00055 void to_generic(GenericRecord& record) const; 00056 00057 Value& value(); 00058 00059 const Value& value() const; 00060 00061 private: // data 00062 00063 std::string key_; 00064 Value value_; 00065 00066 }; 00067 00068 // IMPLEMENTATION 00069 00070 template<typename Value> 00071 const std::string 00072 Record<Value>::empty_key("EMPTY_KEY"); 00073 00074 template<typename Value> 00075 Record<Value>::Record() 00076 {} 00077 00078 template<typename Value> 00079 Record<Value>::Record(const std::string& key, const Value& value) 00080 : key_(key), 00081 value_(value) 00082 {} 00083 00084 template<typename Value> 00085 Record<Value>::Record(const Record<Value>& record) 00086 : key_(record.key()), 00087 value_(record.value()) 00088 {} 00089 00090 template<typename Value> 00091 Record<Value>::~Record() 00092 {} 00093 00094 template<typename Value> 00095 std::string& 00096 Record<Value>::key() 00097 { 00098 return key_; 00099 } 00100 00101 template<typename Value> 00102 const std::string& 00103 Record<Value>::key() const 00104 { 00105 return key_; 00106 } 00107 00108 template<typename Value> 00109 bool 00110 Record<Value>::operator==(const Record<Value>& record) const 00111 { 00112 return key_ == record.key_ && value_ == record.value_; 00113 } 00114 00115 template<typename Value> 00116 bool 00117 Record<Value>::operator!=(const Record<Value>& record) const 00118 { 00119 return !(*this == record); 00120 } 00121 00122 template<typename Value> 00123 Record<Value>& 00124 Record<Value>::operator=(const Record<Value>& record) 00125 { 00126 key_ = record.key_; 00127 value_ = record.value_; 00128 return *this; 00129 } 00130 00131 template<typename Value> 00132 void 00133 Record<Value>::parse(std::istream& is) 00134 { 00135 is >> key_ >> value_; 00136 } 00137 00138 template<typename Value> 00139 void 00140 Record<Value>::print(std::ostream& os) const 00141 { 00142 os << (key_.empty() ? empty_key : key_) << separator << value_; 00143 } 00144 00145 template<typename Value> 00146 void 00147 Record<Value>::to_generic(GenericRecord& record) const 00148 { 00149 record.key().assign(key_); 00150 value_.to_bytes(record.value()); 00151 } 00152 00153 template<typename Value> 00154 Value& 00155 Record<Value>::value() 00156 { 00157 return value_; 00158 } 00159 00160 template<typename Value> 00161 const Value& 00162 Record<Value>::value() const 00163 { 00164 return value_; 00165 } 00166 00167 } // namespace invertedindex 00168 } // namespace aitools 00169 00170 namespace std { 00171 00172 template<typename Value> istream& 00173 operator>>(istream& is, aitools::invertedindex::Record<Value>& record) 00174 { 00175 if (is) record.parse(is); 00176 return is; 00177 } 00178 00179 template<typename Value> ostream& 00180 operator<<(ostream& os, const aitools::invertedindex::Record<Value>& record) 00181 { 00182 if (os) record.print(os); 00183 return os; 00184 } 00185 00186 } // namespace std 00187 00188 #endif // AITOOLS_INVERTEDINDEX_RECORD_HPP