vsg  1.1.0
VulkanSceneGraph library
compare.h
1 #pragma once
2 
3 /* <editor-fold desc="MIT License">
4 
5 Copyright(c) 2022 Robert Osfield
6 
7 Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
8 
9 The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
10 
11 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
12 
13 </editor-fold> */
14 
15 #include <vsg/core/ref_ptr.h>
16 
17 #include <cstring>
18 
19 namespace vsg
20 {
21 
22  template<class T, class R>
23  int compare_pointer(const ref_ptr<T>& lhs, const ref_ptr<R>& rhs)
24  {
25  if (lhs == rhs) return 0;
26  return lhs ? (rhs ? lhs->compare(*rhs) : 1) : (rhs ? -1 : 0);
27  }
28 
29  template<class T>
30  int compare_value(const T& lhs, const T& rhs)
31  {
32  return (lhs < rhs) ? -1 : ((rhs < lhs) ? 1 : 0);
33  }
34 
35  template<class T>
36  int compare_values(const T* lhs, const T* rhs, size_t size)
37  {
38  int result = 0;
39  for (size_t i = 0; i < size; ++i)
40  {
41  if ((result = compare_value(lhs[i], rhs[i]))) break;
42  }
43  return result;
44  }
45 
46  template<typename T>
47  int compare_memory(const T& lhs, const T& rhs)
48  {
49  return std::memcmp(&lhs, &rhs, sizeof(T));
50  }
51 
52  template<typename S, typename E>
53  int compare_region(const S& start, const E& end, const S& rhs)
54  {
55  const char* lhs_ptr = reinterpret_cast<const char*>(&start);
56  const char* rhs_ptr = reinterpret_cast<const char*>(&rhs);
57  size_t size = size_t(reinterpret_cast<const char*>(&end) - lhs_ptr) + sizeof(E);
58 
59  // use memcpy to compare the contents of the data
60  return std::memcmp(lhs_ptr, rhs_ptr, size);
61  }
62 
63  template<typename T>
64  int compare_pointer_container(const T& lhs, const T& rhs)
65  {
66  if (lhs.size() < rhs.size()) return -1;
67  if (lhs.size() > rhs.size()) return 1;
68  if (lhs.empty()) return 0;
69 
70  auto rhs_itr = rhs.begin();
71  for (auto& object : lhs)
72  {
73  int result = compare_pointer(object, *rhs_itr++);
74  if (result != 0) return result;
75  }
76  return 0;
77  }
78 
79  template<typename T>
80  int compare_value_container(const T& lhs, const T& rhs)
81  {
82  if (lhs.size() < rhs.size()) return -1;
83  if (lhs.size() > rhs.size()) return 1;
84  if (lhs.empty()) return 0;
85 
86  return compare_region(lhs.front(), lhs.back(), rhs.front());
87  }
88 
89  template<typename T>
90  int compare_container(const T& lhs, const T& rhs)
91  {
92  if (lhs.size() < rhs.size()) return -1;
93  if (lhs.size() > rhs.size()) return 1;
94  if (lhs.empty()) return 0;
95 
96  auto rhs_itr = rhs.begin();
97  for (auto& object : lhs)
98  {
99  int result = object.compare(*rhs_itr++);
100  if (result != 0) return result;
101  }
102  return 0;
103  }
104 
107  {
108  template<class P>
109  bool operator()(const P& lhs, const P& rhs) const
110  {
111  return lhs->compare(*rhs) < 0;
112  }
113  };
114 
115 } // namespace vsg
less functor for comparing ref_ptr<Object> typically used with std::set<> etc.
Definition: compare.h:107