Aleph-w 3.0
A C++ Library for Data Structures and Algorithms
Loading...
Searching...
No Matches
ah-unique.H
Go to the documentation of this file.
1/*
2 Aleph_w
3
4 Data structures & Algorithms
5 version 2.0.0b
6 https://github.com/lrleon/Aleph-w
7
8 This file is part of Aleph-w library
9
10 Copyright (c) 2002-2026 Leandro Rabindranath Leon
11
12 Permission is hereby granted, free of charge, to any person obtaining a copy
13 of this software and associated documentation files (the "Software"), to deal
14 in the Software without restriction, including without limitation the rights
15 to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
16 copies of the Software, and to permit persons to whom the Software is
17 furnished to do so, subject to the following conditions:
18
19 The above copyright notice and this permission notice shall be included in all
20 copies or substantial portions of the Software.
21
22 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
23 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
24 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
25 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
26 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
27 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
28 SOFTWARE.
29*/
30
40#ifndef AH_UNIQUE_H
41#define AH_UNIQUE_H
42
43#include <concepts>
44#include <set>
45#include <ah-concepts.H>
46
47namespace Aleph {
48
50 template <class Container>
51 concept AlephSequential = requires(Container c,
52 typename Container::Key_Type v,
53 bool (&op)(typename Container::Key_Type &))
54 {
55 typename Container::Key_Type;
56 { c.traverse(op) } -> std::convertible_to<bool>;
57 c.append(v);
58 c.swap(c);
59 { c.size() } -> std::convertible_to<size_t>;
60 };
61
71 template <AlephSequential Container,
72 class Compare = std::less<typename Container::Key_Type>>
74 void in_place_unique(Container & c, Compare cmp = {})
75 {
76 using Key = typename Container::Key_Type;
77
78 std::set<Key, Compare> seen(cmp);
80
81 // Reserve only for vector-like containers (e.g. Array) where reserve()
82 // does not change logical size. DynArray is intentionally excluded.
83 if constexpr (requires(Container & x)
84 {
85 { x.capacity() } -> std::convertible_to<size_t>;
86 x.reserve(size_t{});
87 })
88 if (clean.capacity() < c.size())
89 clean.reserve(c.size());
90
91 c.traverse([&](Key & item)
92 {
93 if (seen.contains(item))
94 return true; // skip duplicates
95
96 seen.insert(item);
97 clean.append(item);
98 return true;
99 });
100
101 c.swap(clean);
102 }
103
104} // namespace Aleph
105
106#endif // AH_UNIQUE_H
C++20 concepts for constraining comparison functors.
Concept for Aleph sequential containers with traversal and append semantics.
Definition ah-unique.H:51
Strict weak ordering constraint for BST comparators.
Definition ah-concepts.H:84
int cmp(const __gmp_expr< T, U > &expr1, const __gmp_expr< V, W > &expr2)
Definition gmpfrxx.h:4118
Main namespace for Aleph-w library functions.
Definition ah-arena.H:89
void in_place_unique(Container &c, Compare cmp={})
Remove duplicates in-place preserving first occurrence order.
Definition ah-unique.H:74
Divide_Conquer_DP_Result< Cost > divide_and_conquer_partition_dp(const size_t groups, const size_t n, Transition_Cost_Fn transition_cost, const Cost inf=dp_optimization_detail::default_inf< Cost >())
Optimize partition DP using divide-and-conquer optimization.