1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280
|
// Copyright (c) 2019,2025
// GeometryFactory (France)
//
// This file is part of CGAL (www.cgal.org)
//
// $URL: https://github.com/CGAL/cgal/blob/v6.1.1/Kernel_23/include/CGAL/Kernel/hash_functions.h $
// $Id: include/CGAL/Kernel/hash_functions.h 08b27d3db14 $
// SPDX-License-Identifier: LGPL-3.0-or-later OR LicenseRef-Commercial
//
//
// Author(s) : Simon Giraudot
//
// Test file: test/Kernel_23/test_hash_functions.cpp
#ifndef CGAL_KERNEL_HASH_FUNCTIONS_H
#define CGAL_KERNEL_HASH_FUNCTIONS_H
#include <boost/functional/hash.hpp>
#include <type_traits>
#include <CGAL/representation_tags.h>
#include <CGAL/Aff_transformation_2.h>
#include <CGAL/Aff_transformation_3.h>
#include <CGAL/Bbox_2.h>
#include <CGAL/Bbox_3.h>
#include <CGAL/Circle_2.h>
#include <CGAL/Iso_rectangle_2.h>
#include <CGAL/Iso_cuboid_3.h>
#include <CGAL/Point_2.h>
#include <CGAL/Point_3.h>
#include <CGAL/Segment_2.h>
#include <CGAL/Segment_3.h>
#include <CGAL/Sphere_3.h>
#include <CGAL/Vector_2.h>
#include <CGAL/Vector_3.h>
#include <CGAL/Weighted_point_2.h>
#include <CGAL/Weighted_point_3.h>
namespace CGAL
{
using boost::hash_value;
template <typename K, typename = void>
inline constexpr bool has_rep_tag_v = false;
template <typename K>
inline constexpr bool has_rep_tag_v<K, std::void_t<typename K::Rep_tag>> = true;
template <typename K, typename = void>
struct Rep_tag {
using type = void;
};
template <typename K>
struct Rep_tag<K, std::enable_if_t<has_rep_tag_v<K>>> {
using type = typename K::Rep_tag;
};
template <typename K>
using Rep_tag_t = typename Rep_tag<K>::type;
template <typename K>
inline constexpr bool is_Cartesian_v = std::is_same<Rep_tag_t<K>, Cartesian_tag>::value;
template <typename K, typename = void>
struct Is_kernel_hashable : public std::false_type {};
template <typename K>
struct Is_kernel_hashable<K, std::void_t<decltype(hash_value(std::declval<typename K::FT>()))>> : public std::true_type {};
template <typename K>
inline constexpr bool is_kernel_hashable_v = Is_kernel_hashable<K>::value;
template <typename K, typename T>
using enable_if_Cartesian_and_hashable_t =
std::enable_if_t<is_Cartesian_v<K> && is_kernel_hashable_v<K>, T>;
template <typename K>
inline enable_if_Cartesian_and_hashable_t<K, std::size_t>
hash_value (const Aff_transformation_2<K>& transform)
{
std::size_t result = hash_value(transform.cartesian(0,0));
for(int i=0; i < 3; ++i)
for(int j=0; j < 3; ++j)
// Skip (0,0) as it was already used to initialize the hash
if (!(i == 0 && j == 0))
boost::hash_combine(result, hash_value(transform.cartesian(i,j)));
return result;
}
inline std::size_t
hash_value (const Bbox_2& bbox)
{
std::size_t result = hash_value(bbox.xmin());
boost::hash_combine(result, hash_value(bbox.xmax()));
boost::hash_combine(result, hash_value(bbox.ymin()));
boost::hash_combine(result, hash_value(bbox.ymax()));
return result;
}
template <typename K>
inline enable_if_Cartesian_and_hashable_t<K, std::size_t>
hash_value (const Circle_2<K>& circle)
{
std::size_t result = hash_value(circle.center());
boost::hash_combine(result, hash_value(circle.squared_radius()));
boost::hash_combine(result, hash_value(circle.orientation()));
return result;
}
template <typename K>
inline enable_if_Cartesian_and_hashable_t<K, std::size_t>
hash_value (const Iso_rectangle_2<K>& iso_rectangle)
{
std::size_t result = hash_value((iso_rectangle.min)());
boost::hash_combine(result, hash_value((iso_rectangle.max)()));
return result;
}
template <typename K>
inline enable_if_Cartesian_and_hashable_t<K, std::size_t>
hash_value (const Point_2<K>& point)
{
std::size_t result = hash_value(point.x());
boost::hash_combine(result, hash_value(point.y()));
return result;
}
template <typename K>
inline enable_if_Cartesian_and_hashable_t<K, std::size_t>
hash_value (const Segment_2<K>& segment)
{
std::size_t result = hash_value(segment.source());
boost::hash_combine(result, hash_value(segment.target()));
return result;
}
template <typename K>
inline enable_if_Cartesian_and_hashable_t<K, std::size_t>
hash_value (const Vector_2<K>& vector)
{
std::size_t result = hash_value(vector.x());
boost::hash_combine(result, hash_value(vector.y()));
return result;
}
template <typename K>
inline enable_if_Cartesian_and_hashable_t<K, std::size_t>
hash_value (const Weighted_point_2<K>& weighed_point)
{
std::size_t result = hash_value(weighed_point.point());
boost::hash_combine(result, hash_value(weighed_point.weight()));
return result;
}
template <typename K>
inline enable_if_Cartesian_and_hashable_t<K, std::size_t>
hash_value (const Aff_transformation_3<K>& transform)
{
std::size_t result = hash_value(transform.cartesian(0,0));
for(int i = 0; i < 3; ++i)
for(int j = (i == 0 ? 1 : 0); j < 4; ++j)
boost::hash_combine(result, hash_value(transform.cartesian(i,j)));
return result;
}
inline std::size_t
hash_value (const Bbox_3& bbox)
{
std::size_t result = hash_value(bbox.xmin());
boost::hash_combine(result, hash_value(bbox.xmax()));
boost::hash_combine(result, hash_value(bbox.ymin()));
boost::hash_combine(result, hash_value(bbox.ymax()));
boost::hash_combine(result, hash_value(bbox.zmin()));
boost::hash_combine(result, hash_value(bbox.zmax()));
return result;
}
template <typename K>
inline enable_if_Cartesian_and_hashable_t<K, std::size_t>
hash_value (const Iso_cuboid_3<K>& iso_cuboid)
{
std::size_t result = hash_value((iso_cuboid.min)());
boost::hash_combine(result, hash_value((iso_cuboid.max)()));
return result;
}
template <typename K>
inline enable_if_Cartesian_and_hashable_t<K, std::size_t>
hash_value (const Point_3<K>& point)
{
std::size_t result = hash_value(point.x());
boost::hash_combine(result, hash_value(point.y()));
boost::hash_combine(result, hash_value(point.z()));
return result;
}
template <typename K>
inline enable_if_Cartesian_and_hashable_t<K, std::size_t>
hash_value (const Segment_3<K>& segment)
{
std::size_t result = hash_value(segment.source());
boost::hash_combine(result, hash_value(segment.target()));
return result;
}
template <typename K>
inline enable_if_Cartesian_and_hashable_t<K, std::size_t>
hash_value (const Sphere_3<K>& sphere)
{
std::size_t result = hash_value(sphere.center());
boost::hash_combine(result, hash_value(sphere.squared_radius()));
boost::hash_combine(result, hash_value(sphere.orientation()));
return result;
}
template <typename K>
inline enable_if_Cartesian_and_hashable_t<K, std::size_t>
hash_value (const Vector_3<K>& vector)
{
std::size_t result = hash_value(vector.x());
boost::hash_combine(result, hash_value(vector.y()));
boost::hash_combine(result, hash_value(vector.z()));
return result;
}
template <typename K>
inline enable_if_Cartesian_and_hashable_t<K, std::size_t>
hash_value (const Weighted_point_3<K>& weighed_point)
{
std::size_t result = hash_value(weighed_point.point());
boost::hash_combine(result, hash_value(weighed_point.weight()));
return result;
}
struct Forward_to_hash_value {
template <typename T>
std::size_t operator()(T&& t) const {
using boost::hash_value;
return hash_value(std::forward<T>(t));
}
};
template <typename K, typename = void>
struct Maybe_forward_to_hash_value {
Maybe_forward_to_hash_value() = delete;
Maybe_forward_to_hash_value(const Maybe_forward_to_hash_value&) = delete;
};
template <typename K>
struct Maybe_forward_to_hash_value<K, std::enable_if_t<is_kernel_hashable_v<K>>>
: public Forward_to_hash_value {};
} //namespace CGAL
// overloads of std::hash used for using std::unordered_[set/map] on CGAL Kernel objects
namespace std {
template <typename K> struct hash<CGAL::Aff_transformation_2<K>> : CGAL::Maybe_forward_to_hash_value<K> {};
template <typename K> struct hash<CGAL::Circle_2<K>> : CGAL::Maybe_forward_to_hash_value<K> {};
template <typename K> struct hash<CGAL::Iso_rectangle_2<K>> : CGAL::Maybe_forward_to_hash_value<K> {};
template <typename K> struct hash<CGAL::Point_2<K>> : CGAL::Maybe_forward_to_hash_value<K> {};
template <typename K> struct hash<CGAL::Segment_2<K>> : CGAL::Maybe_forward_to_hash_value<K> {};
template <typename K> struct hash<CGAL::Vector_2<K>> : CGAL::Maybe_forward_to_hash_value<K> {};
template <typename K> struct hash<CGAL::Weighted_point_2<K>> : CGAL::Maybe_forward_to_hash_value<K> {};
template <> struct hash<CGAL::Bbox_2> : CGAL::Forward_to_hash_value {};
template <typename K> struct hash<CGAL::Aff_transformation_3<K>> : CGAL::Maybe_forward_to_hash_value<K> {};
template <typename K> struct hash<CGAL::Iso_cuboid_3<K>> : CGAL::Maybe_forward_to_hash_value<K> {};
template <typename K> struct hash<CGAL::Point_3<K>> : CGAL::Maybe_forward_to_hash_value<K> {};
template <typename K> struct hash<CGAL::Segment_3<K>> : CGAL::Maybe_forward_to_hash_value<K> {};
template <typename K> struct hash<CGAL::Sphere_3<K>> : CGAL::Maybe_forward_to_hash_value<K> {};
template <typename K> struct hash<CGAL::Vector_3<K>> : CGAL::Maybe_forward_to_hash_value<K> {};
template <typename K> struct hash<CGAL::Weighted_point_3<K>> : CGAL::Maybe_forward_to_hash_value<K> {};
template <> struct hash<CGAL::Bbox_3> : CGAL::Forward_to_hash_value {};
} // namespace std
#endif // CGAL_KERNEL_HASH_FUNCTIONS_H
|