SortCmp.h 1.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  1. /* Copyright The kNet Project.
  2. Licensed under the Apache License, Version 2.0 (the "License");
  3. you may not use this file except in compliance with the License.
  4. You may obtain a copy of the License at
  5. http://www.apache.org/licenses/LICENSE-2.0
  6. Unless required by applicable law or agreed to in writing, software
  7. distributed under the License is distributed on an "AS IS" BASIS,
  8. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  9. See the License for the specific language governing permissions and
  10. limitations under the License. */
  11. #pragma once
  12. /** @file SortCmp.h
  13. @brief The TriCmp and TriCmpObj constructs are the default sorting predicate objects. */
  14. #include <cassert>
  15. namespace kNet
  16. {
  17. namespace sort
  18. {
  19. template<typename T>
  20. int TriCmp(const T &a, const T &b)
  21. {
  22. // assume(!(a < b && b < a));
  23. if (a < b) return -1;
  24. if (b < a) return 1;
  25. // assert(a == b); // Require trichotomy.
  26. return 0;
  27. }
  28. template<typename T>
  29. class TriCmpObj
  30. {
  31. public:
  32. int operator ()(const T &a, const T &b)
  33. {
  34. if (a < b) return -1;
  35. if (b < a) return 1;
  36. assert(a == b); // Require trichotomy.
  37. return 0;
  38. }
  39. };
  40. } // ~sort
  41. } // ~kNet