17 #ifndef PAGESPEED_KERNEL_BASE_STRING_MULTI_MAP_H_
18 #define PAGESPEED_KERNEL_BASE_STRING_MULTI_MAP_H_
25 #include "base/logging.h"
30 namespace net_instaweb {
47 return vector_.empty();
51 for (
int i = 0, n = vector_.size(); i < n; ++i) {
52 delete vector_[i].second;
69 bool Lookup(
const StringPiece& name, ConstStringStarVector* values)
const {
70 SetEntry lookup_entry(name);
71 typename Set::const_iterator p = set_.find(lookup_entry);
73 if (p != set_.end()) {
75 const SetEntry& stored_entry = *p;
76 *values = stored_entry.values();
84 ConstStringStarVector v;
85 if (
Lookup(name, &v) && v.size() == 1) {
91 bool Has(
const StringPiece& name)
const {
92 SetEntry lookup_entry(name);
93 return set_.find(lookup_entry) != set_.end();
106 for (
int i = 1; i < names_size; ++i) {
107 StringCompare compare;
113 DCHECK(!compare(names[i], names[i - 1]))
114 <<
"\"" << names[i - 1] <<
"\" vs \"" << names[i];
120 SetEntry lookup_entry;
126 const int kNotFound = -1;
127 int index_of_first_match = kNotFound;
128 typename Set::iterator set_entry_of_first_match;
129 for (
int i = 0; i < names_size; ++i) {
130 lookup_entry.set_key(names[i]);
131 set_entry_of_first_match = set_.find(lookup_entry);
132 if (set_entry_of_first_match != set_.end()) {
133 index_of_first_match = i;
138 if (index_of_first_match == kNotFound) {
141 StringPairVector temp_vector;
142 temp_vector.reserve(vector_.size() - 1);
143 StringCompare compare;
145 if (std::binary_search(names, names + names_size, name(i), compare)) {
146 delete vector_[i].second;
148 temp_vector.push_back(vector_[i]);
152 vector_.swap(temp_vector);
154 set_.erase(set_entry_of_first_match);
155 for (
int i = index_of_first_match + 1; i < names_size; ++i) {
156 lookup_entry.set_key(names[i]);
157 set_.erase(lookup_entry);
163 StringPiece name(
int index)
const {
return vector_[index].first; }
169 void Add(
const StringPiece& key,
const StringPiece&
value) {
170 SetEntry lookup_entry(key);
171 std::pair<typename Set::iterator, bool> iter_inserted =
172 set_.insert(lookup_entry);
173 typename Set::iterator iter = iter_inserted.first;
184 SetEntry& entry =
const_cast<SetEntry&
>(*iter);
185 if (iter_inserted.second) {
190 if (value.data() != NULL) {
193 entry.AddValue(value_copy);
194 vector_.push_back(StringPair(iter->key(), value_copy));
204 const StringPiece& separators,
205 char value_separator,
206 bool omit_if_no_value) {
207 StringPieceVector pairs;
209 for (
int i = 0, n = pairs.size(); i < n; ++i) {
210 StringPiece& pair = pairs[i];
211 StringPiece::size_type pos = pair.find(value_separator);
212 if (pos != StringPiece::npos) {
213 Add(pair.substr(0, pos), pair.substr(pos + 1));
214 }
else if (!omit_if_no_value) {
215 Add(pair, StringPiece(NULL, 0));
222 for (
int i = 0; i < string_multi_map.
num_values(); ++i) {
225 Add(string_multi_map.name(i), *
value);
227 Add(string_multi_map.name(i), StringPiece());
238 SetEntry(StringPiece key) : key_(key) { }
239 SetEntry(
const SetEntry& src)
243 DCHECK(src.values_.empty());
246 SetEntry& operator=(
const SetEntry& src) {
248 DCHECK(src.values_.empty());
254 void set_key(StringPiece key) {
259 values_.push_back(value);
266 key_.CopyToString(&key_storage_);
270 StringPiece key()
const {
return key_; }
271 const ConstStringStarVector& values()
const {
return values_; }
276 ConstStringStarVector values_;
279 struct EntryCompare {
280 bool operator()(
const SetEntry& a,
const SetEntry& b)
const {
281 return compare(a.key(), b.key());
284 StringCompare compare;
296 typedef std::pair<StringPiece, GoogleString*> StringPair;
297 typedef std::set<SetEntry, EntryCompare> Set;
298 typedef std::vector<StringPair> StringPairVector;
301 StringPairVector vector_;
void Add(const StringPiece &key, const StringPiece &value)
Add a new variable. The value can be null.
Definition: string_multi_map.h:169
bool Lookup(const StringPiece &name, ConstStringStarVector *values) const
Definition: string_multi_map.h:69
bool RemoveAll(const StringPiece &key)
Remove all variables by name. Returns true if anything was removed.
Definition: string_multi_map.h:97
int num_values() const
Definition: string_multi_map.h:63
Definition: string_multi_map.h:39
int num_names() const
Returns the number of distinct names.
Definition: string_multi_map.h:59
std::string GoogleString
PAGESPEED_KERNEL_BASE_STRING_H_.
Definition: string.h:24
void AddFromNameValuePairs(const StringPiece &name_value_list, const StringPiece &separators, char value_separator, bool omit_if_no_value)
Definition: string_multi_map.h:203
Definition: string_multi_map.h:314
const GoogleString * value(int index) const
Note that the value can be NULL.
Definition: string_multi_map.h:166
Definition: string_multi_map.h:306
const GoogleString * Lookup1(const StringPiece &name) const
Definition: string_multi_map.h:83
bool RemoveAllFromSortedArray(const StringPiece *names, int names_size)
Definition: string_multi_map.h:104
void SplitStringPieceToVector(StringPiece sp, StringPiece separators, StringPieceVector *components, bool omit_empty_strings)