诸暨麻将添加redis
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 

96 line
3.6 KiB

  1. // Protocol Buffers - Google's data interchange format
  2. // Copyright 2008 Google Inc. All rights reserved.
  3. // https://developers.google.com/protocol-buffers/
  4. //
  5. // Redistribution and use in source and binary forms, with or without
  6. // modification, are permitted provided that the following conditions are
  7. // met:
  8. //
  9. // * Redistributions of source code must retain the above copyright
  10. // notice, this list of conditions and the following disclaimer.
  11. // * Redistributions in binary form must reproduce the above
  12. // copyright notice, this list of conditions and the following disclaimer
  13. // in the documentation and/or other materials provided with the
  14. // distribution.
  15. // * Neither the name of Google Inc. nor the names of its
  16. // contributors may be used to endorse or promote products derived from
  17. // this software without specific prior written permission.
  18. //
  19. // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  20. // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  21. // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  22. // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  23. // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  24. // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  25. // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  26. // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  27. // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  28. // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  29. // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  30. #include <google/protobuf/generated_enum_util.h>
  31. #include <algorithm>
  32. #include <google/protobuf/generated_message_util.h>
  33. namespace google {
  34. namespace protobuf {
  35. namespace internal {
  36. namespace {
  37. bool EnumCompareByName(const EnumEntry& a, const EnumEntry& b) {
  38. return StringPiece(a.name) < StringPiece(b.name);
  39. }
  40. // Gets the numeric value of the EnumEntry at the given index, but returns a
  41. // special value for the index -1. This gives a way to use std::lower_bound on a
  42. // sorted array of indices while searching for value that we associate with -1.
  43. int GetValue(const EnumEntry* enums, int i, int target) {
  44. if (i == -1) {
  45. return target;
  46. } else {
  47. return enums[i].value;
  48. }
  49. }
  50. } // namespace
  51. bool LookUpEnumValue(const EnumEntry* enums, size_t size,
  52. StringPiece name, int* value) {
  53. EnumEntry target{name, 0};
  54. auto it = std::lower_bound(enums, enums + size, target, EnumCompareByName);
  55. if (it != enums + size && it->name == name) {
  56. *value = it->value;
  57. return true;
  58. }
  59. return false;
  60. }
  61. int LookUpEnumName(const EnumEntry* enums, const int* sorted_indices,
  62. size_t size, int value) {
  63. auto comparator = [enums, value](int a, int b) {
  64. return GetValue(enums, a, value) < GetValue(enums, b, value);
  65. };
  66. auto it =
  67. std::lower_bound(sorted_indices, sorted_indices + size, -1, comparator);
  68. if (it != sorted_indices + size && enums[*it].value == value) {
  69. return it - sorted_indices;
  70. }
  71. return -1;
  72. }
  73. bool InitializeEnumStrings(
  74. const EnumEntry* enums, const int* sorted_indices, size_t size,
  75. internal::ExplicitlyConstructed<std::string>* enum_strings) {
  76. for (int i = 0; i < size; ++i) {
  77. enum_strings[i].Construct(enums[sorted_indices[i]].name);
  78. internal::OnShutdownDestroyString(enum_strings[i].get_mutable());
  79. }
  80. return true;
  81. }
  82. } // namespace internal
  83. } // namespace protobuf
  84. } // namespace google