Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(116)

Side by Side Diff: base/strings/string_piece.h

Issue 1666843002: Refactor StringPiece hash. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Sort includes. Created 4 years, 10 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « no previous file | components/mus/ws/ids.h » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 // Copied from strings/stringpiece.h with modifications 4 // Copied from strings/stringpiece.h with modifications
5 // 5 //
6 // A string-like object that points to a sized piece of memory. 6 // A string-like object that points to a sized piece of memory.
7 // 7 //
8 // You can use StringPiece as a function or method parameter. A StringPiece 8 // You can use StringPiece as a function or method parameter. A StringPiece
9 // parameter can receive a double-quoted string literal argument, a "const 9 // parameter can receive a double-quoted string literal argument, a "const
10 // char*" argument, a string argument, or a StringPiece argument with no data 10 // char*" argument, a string argument, or a StringPiece argument with no data
(...skipping 10 matching lines...) Expand all
21 21
22 #ifndef BASE_STRINGS_STRING_PIECE_H_ 22 #ifndef BASE_STRINGS_STRING_PIECE_H_
23 #define BASE_STRINGS_STRING_PIECE_H_ 23 #define BASE_STRINGS_STRING_PIECE_H_
24 24
25 #include <stddef.h> 25 #include <stddef.h>
26 26
27 #include <iosfwd> 27 #include <iosfwd>
28 #include <string> 28 #include <string>
29 29
30 #include "base/base_export.h" 30 #include "base/base_export.h"
31 #include "base/containers/hash_tables.h"
32 #include "base/logging.h" 31 #include "base/logging.h"
33 #include "base/strings/string16.h" 32 #include "base/strings/string16.h"
34 33
35 namespace base { 34 namespace base {
36 35
37 template <typename STRING_TYPE> class BasicStringPiece; 36 template <typename STRING_TYPE> class BasicStringPiece;
38 typedef BasicStringPiece<std::string> StringPiece; 37 typedef BasicStringPiece<std::string> StringPiece;
39 typedef BasicStringPiece<string16> StringPiece16; 38 typedef BasicStringPiece<string16> StringPiece16;
40 39
41 // internal -------------------------------------------------------------------- 40 // internal --------------------------------------------------------------------
(...skipping 383 matching lines...) Expand 10 before | Expand all | Expand 10 after
425 return !(x > y); 424 return !(x > y);
426 } 425 }
427 426
428 inline bool operator>=(const StringPiece16& x, const StringPiece16& y) { 427 inline bool operator>=(const StringPiece16& x, const StringPiece16& y) {
429 return !(x < y); 428 return !(x < y);
430 } 429 }
431 430
432 BASE_EXPORT std::ostream& operator<<(std::ostream& o, 431 BASE_EXPORT std::ostream& operator<<(std::ostream& o,
433 const StringPiece& piece); 432 const StringPiece& piece);
434 433
435 } // namespace base
436
437 // Hashing --------------------------------------------------------------------- 434 // Hashing ---------------------------------------------------------------------
438 435
439 // We provide appropriate hash functions so StringPiece and StringPiece16 can 436 // We provide appropriate hash functions so StringPiece and StringPiece16 can
440 // be used as keys in hash sets and maps. 437 // be used as keys in hash sets and maps.
441 438
442 // This hash function is copied from base/strings/string16.h. We don't use the 439 // This hash function is copied from base/strings/string16.h. We don't use the
443 // ones already defined for string and string16 directly because it would 440 // ones already defined for string and string16 directly because it would
444 // require the string constructors to be called, which we don't want. 441 // require the string constructors to be called, which we don't want.
445 #define HASH_STRING_PIECE(StringPieceType, string_piece) \ 442 #define HASH_STRING_PIECE(StringPieceType, string_piece) \
446 std::size_t result = 0; \ 443 std::size_t result = 0; \
447 for (StringPieceType::const_iterator i = string_piece.begin(); \ 444 for (StringPieceType::const_iterator i = string_piece.begin(); \
448 i != string_piece.end(); ++i) \ 445 i != string_piece.end(); ++i) \
449 result = (result * 131) + *i; \ 446 result = (result * 131) + *i; \
450 return result; \ 447 return result;
451 448
452 namespace BASE_HASH_NAMESPACE { 449 struct StringPieceHash {
453 450 std::size_t operator()(const StringPiece& sp) const {
454 template<> 451 HASH_STRING_PIECE(StringPiece, sp);
455 struct hash<base::StringPiece> {
456 std::size_t operator()(const base::StringPiece& sp) const {
457 HASH_STRING_PIECE(base::StringPiece, sp);
458 } 452 }
459 }; 453 };
460 template<> 454 struct StringPiece16Hash {
461 struct hash<base::StringPiece16> { 455 std::size_t operator()(const StringPiece16& sp16) const {
462 std::size_t operator()(const base::StringPiece16& sp16) const { 456 HASH_STRING_PIECE(StringPiece16, sp16);
463 HASH_STRING_PIECE(base::StringPiece16, sp16);
464 } 457 }
465 }; 458 };
466 459
467 } // namespace BASE_HASH_NAMESPACE 460 } // namespace base
468 461
469 #endif // BASE_STRINGS_STRING_PIECE_H_ 462 #endif // BASE_STRINGS_STRING_PIECE_H_
OLDNEW
« no previous file with comments | « no previous file | components/mus/ws/ids.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698