| OLD | NEW |
| (Empty) |
| 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 | |
| 3 // found in the LICENSE file. | |
| 4 | |
| 5 #ifndef CHROME_COMMON_STRING_ORDINAL_H_ | |
| 6 #define CHROME_COMMON_STRING_ORDINAL_H_ | |
| 7 | |
| 8 #include <string> | |
| 9 | |
| 10 // A StringOrdinal represents a specially-formatted string that can be used | |
| 11 // for ordering. The StringOrdinal class has an unbounded dense strict total | |
| 12 // order, which mean for any StringOrdinals a, b and c: | |
| 13 // | |
| 14 // - a < b and b < c implies a < c (transitivity); | |
| 15 // - exactly one of a < b, b < a and a = b holds (trichotomy); | |
| 16 // - if a < b, there is a StringOrdinal x such that a < x < b (density); | |
| 17 // - there are StringOrdinals x and y such that x < a < y (unboundedness). | |
| 18 // | |
| 19 // This means that when StringOrdinal is used for sorting a list, if | |
| 20 // any item changes its position in the list, only its StringOrdinal value | |
| 21 // has to change to represent the new order, and all the other older values | |
| 22 // can stay the same. | |
| 23 class StringOrdinal { | |
| 24 public: | |
| 25 // Creates a StringOrdinal from the given string. It may be valid or invalid. | |
| 26 explicit StringOrdinal(const std::string& string_ordinal); | |
| 27 | |
| 28 // Creates an invalid StringOrdinal. | |
| 29 StringOrdinal(); | |
| 30 | |
| 31 // Creates a valid initial StringOrdinal, this is called to create the first | |
| 32 // element of StringOrdinal list (i.e. before we have any other values we can | |
| 33 // generate from). | |
| 34 static StringOrdinal CreateInitialOrdinal(); | |
| 35 | |
| 36 bool IsValid() const; | |
| 37 | |
| 38 // All remaining functions can only be called if IsValid() holds. | |
| 39 // It is an error to call them if IsValid() is false. | |
| 40 | |
| 41 // Order-related Functions | |
| 42 | |
| 43 // Returns true iff |*this| < |other|. | |
| 44 bool LessThan(const StringOrdinal& other) const; | |
| 45 | |
| 46 // Returns true iff |*this| > |other|. | |
| 47 bool GreaterThan(const StringOrdinal& other) const; | |
| 48 | |
| 49 // Returns true iff |*this| == |other| (i.e. |*this| < |other| and | |
| 50 // |other| < |*this| are both false). | |
| 51 bool Equal(const StringOrdinal& other) const; | |
| 52 | |
| 53 // Returns true iff |*this| == |other| or |*this| and |other| | |
| 54 // are both invalid. | |
| 55 bool EqualOrBothInvalid(const StringOrdinal& other) const; | |
| 56 | |
| 57 // Given |*this| != |other|, returns a StringOrdinal x such that | |
| 58 // min(|*this|, |other|) < x < max(|*this|, |other|). It is an error | |
| 59 // to call this function when |*this| == |other|. | |
| 60 StringOrdinal CreateBetween(const StringOrdinal& other) const; | |
| 61 | |
| 62 // Returns a StringOrdinal |x| such that |x| < |*this|. | |
| 63 StringOrdinal CreateBefore() const; | |
| 64 | |
| 65 // Returns a StringOrdinal |x| such that |*this| < |x|. | |
| 66 StringOrdinal CreateAfter() const; | |
| 67 | |
| 68 // It is guaranteed that a StringOrdinal constructed from the returned | |
| 69 // string will be valid. | |
| 70 std::string ToString() const; | |
| 71 | |
| 72 // Do this so we can use std::find on a std::vector of StringOrdinals. | |
| 73 bool operator==(const StringOrdinal& rhs) const; | |
| 74 | |
| 75 // Use of copy constructor and default assignment for this class is allowed. | |
| 76 | |
| 77 private: | |
| 78 // The string representation of the StringOrdinal. | |
| 79 std::string string_ordinal_; | |
| 80 | |
| 81 // The validity of the StringOrdinal (i.e., is it of the format [a-z]*[b-z]), | |
| 82 // created to cache validity to prevent frequent recalculations. | |
| 83 bool is_valid_; | |
| 84 }; | |
| 85 | |
| 86 // A helper class that can be used by STL containers that require sorting. | |
| 87 class StringOrdinalLessThan { | |
| 88 public: | |
| 89 bool operator() (const StringOrdinal& lhs, const StringOrdinal& rhs) const; | |
| 90 }; | |
| 91 | |
| 92 #endif // CHROME_COMMON_STRING_ORDINAL_H_ | |
| OLD | NEW |