00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024 #ifndef DeSR_TokenAttributes_H
00025 #define DeSR_TokenAttributes_H
00026
00027
00028
00029
00030 #include "text/WordIndex.h"
00031 #include "include/error.h"
00032
00033
00034 #include <vector>
00035 #include <sstream>
00036
00037 namespace Tanl {
00038
00039 typedef unsigned AttributeId;
00040
00046 struct AttributeIndex : public Text::WordIndex
00047 {
00048 std::vector<char const*> names;
00049
00054 AttributeId insert(const char* name);
00055
00056 AttributeIndex& operator =(AttributeIndex const& ai);
00057
00058 ~AttributeIndex();
00059 };
00060
00061 std::ostream& operator <<(std::ostream& os, AttributeIndex const& ai);
00062
00071 struct Attributes
00072 {
00073 typedef std::string Attribute;
00074
00075 AttributeIndex* attributeIndex;
00076 std::vector<Attribute> values;
00077
00078 static AttributeIndex* emptyAttrIndex;
00079
00080 Attributes(AttributeIndex* attributeIndex);
00081
00082 Attributes(AttributeIndex* attributeIndex, std::vector<Attribute>& values);
00083
00084 Attribute& operator [](int i) { return values[i]; }
00085
00086 bool operator ==(const Attributes& other) {
00087 return attributeIndex == other.attributeIndex &&
00088 values == other.values;
00089 }
00090
00091 bool operator !=(const Attributes& other) { return !(*this == other); }
00092
00093 struct const_iterator {
00094 const_iterator(Attributes const& attributes, int fit = 0) :
00095 attributes(attributes),
00096 fit(fit)
00097 { }
00098
00099 bool operator ==(const const_iterator& other) {
00100 return &attributes == &other.attributes && fit == other.fit;
00101 }
00102 bool operator !=(const const_iterator& other) { return !(*this == other); }
00103 const_iterator& operator ++() { fit++; return *this; }
00104 const_iterator operator ++(int) {
00105 const_iterator tmp = *this;
00106 ++*this;
00107 return tmp;
00108 }
00109
00110
00111 std::pair<char const*, std::string const*> operator*() {
00112 std::string const& val = attributes.values[fit];
00113 return std::make_pair(attributes.attributeIndex->names[fit], &val);
00114 }
00115
00116 Attributes const& attributes;
00117 int fit;
00118 };
00119
00120 const_iterator begin() const { return const_iterator(*this); }
00121 const_iterator end() const { return const_iterator(*this, values.size()); }
00122
00126 std::string const* get(std::string const& name) const {
00127 AttributeId id = attributeIndex->index(name.c_str());
00128 return (id == Text::WordIndex::None) ? 0 : &values[id];
00129 }
00130
00134 std::string const* get(char const* name) const {
00135 AttributeId id = attributeIndex->index(name);
00136 return (id == Text::WordIndex::None) ? 0 : &values[id];
00137 }
00138
00142 AttributeId index(char const* key) const {
00143 return attributeIndex->index(key);
00144 }
00145
00149 void insert(char const* key, char const* value) {
00150 AttributeId id = attributeIndex->insert(key);
00151 if (id >= values.size())
00152 values.resize(id + 1);
00153 values[id] = value;
00154 }
00155
00159 void insert(char const* key, std::string const& value) {
00160 AttributeId id = attributeIndex->insert(key);
00161 if (id >= values.size())
00162 values.resize(id + 1);
00163 values[id] = value;
00164 }
00165
00169 void insert(char const* key, int value) {
00170 AttributeId id = attributeIndex->insert(key);
00171 if (id >= values.size())
00172 values.resize(id + 1);
00173 std::ostringstream oss;
00174 oss << value;
00175 values[id] = oss.str();
00176 }
00177 };
00178
00179 }
00180
00181 #endif // DeSR_TokenAttributes_H