Skip to content

Commit

Permalink
Add CGAL::unordered_flat_map implementation
Browse files Browse the repository at this point in the history
...and `refactor Polyline_constraint_hierarchy_2` to use it.

`CGAL::unordered_flat_map` will be Boost `unordered_flat_map` if availlable, or the standard `std::unordered_map` otherwise.
  • Loading branch information
lrineau committed Jan 8, 2025
1 parent aaac2c2 commit 5526857
Show file tree
Hide file tree
Showing 2 changed files with 62 additions and 13 deletions.
60 changes: 60 additions & 0 deletions STL_Extension/include/CGAL/unordered_flat_map.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
// Copyright (c) 2025 GeometryFactory Sarl (France).
//
// This file is part of CGAL (www.cgal.org).
//
// $URL$
// $Id$
// SPDX-License-Identifier: LGPL-3.0-or-later OR LicenseRef-Commercial
//
// Author(s) : Laurent Rineau

#ifndef CGAL_UNORDERED_FLAT_MAP_H
#define CGAL_UNORDERED_FLAT_MAP_H

#include <CGAL/config.h>

#include <boost/version.hpp>
#if BOOST_VERSION >= 108100 && !defined(CGAL_USE_BOOST_UNORDERED)
# define CGAL_USE_BOOST_UNORDERED 1
#endif

#if CGAL_USE_BARE_STD_MAP // to benchmark with the ordered std::map
# include <map>
#elif CGAL_USE_BOOST_UNORDERED
# include <boost/unordered/unordered_flat_map.hpp>
#else // Boost before 1.81.0, use the C++11 std::unordered_map
# include <unordered_map>
#endif

#if CGAL_USE_BARE_STD_MAP
#include <map>
#endif

#include <functional>

namespace CGAL {

template <
typename Key,
typename T,
typename Hash = std::hash<Key>,
typename KeyEqual = std::equal_to<Key>,
typename Allocator = std::allocator<std::pair<const Key, T>>
>
#if CGAL_USE_BARE_STD_MAP

using unordered_flat_map = std::map<Key, T, std::less<Key>, Allocator>;

#elif CGAL_USE_BOOST_UNORDERED

using unordered_flat_map = boost::unordered_flat_map<Key, T, Hash, KeyEqual, Allocator>;

#else // use the C++11 std::unordered_map

using unordered_flat_map = std::unordered_map<Key, T, Hash, KeyEqual, Allocator>;

#endif

} // end namespace CGAL

#endif // CGAL_UNORDERED_FLAT_MAP_H
Original file line number Diff line number Diff line change
Expand Up @@ -22,14 +22,7 @@
#include <set>
#include <list>

#include <boost/version.hpp>
#if BOOST_VERSION >= 108100
# include <boost/unordered/unordered_flat_map.hpp>
# define CGAL_USE_BOOST_UNORDERED 1
#else // BOOST before 1.81.0
# include <unordered_map>
#endif

#include <CGAL/unordered_flat_map.h>
#include <CGAL/Skiplist.h>
#include <CGAL/assertions.h>

Expand Down Expand Up @@ -162,11 +155,7 @@ class Polyline_constraint_hierarchy_2
typedef typename Context_list::iterator Context_iterator;

typedef std::set<Constraint_id> Constraint_set;
#if CGAL_USE_BOOST_UNORDERED
typedef boost::unordered_flat_map<Edge, Context_list*, boost::hash<Edge>> Sc_to_c_map;
#else
typedef std::unordered_map<Edge, Context_list*, boost::hash<Edge>> Sc_to_c_map;
#endif
typedef CGAL::unordered_flat_map<Edge, Context_list*, boost::hash<Edge>> Sc_to_c_map;
typedef typename Constraint_set::iterator C_iterator;
typedef typename Sc_to_c_map::const_iterator Sc_iterator;
typedef Sc_iterator Subconstraint_iterator;
Expand Down

0 comments on commit 5526857

Please sign in to comment.