LAL: Linear Arrangement Library 24.10.00
A library focused on algorithms on linear arrangements of graphs.
Loading...
Searching...
No Matches
bit_sort.hpp
1/*********************************************************************
2 *
3 * Linear Arrangement Library - A library that implements a collection
4 * algorithms for linear arrangments of graphs.
5 *
6 * Copyright (C) 2019 - 2024
7 *
8 * This file is part of Linear Arrangement Library. The full code is available
9 * at:
10 * https://github.com/LAL-project/linear-arrangement-library.git
11 *
12 * Linear Arrangement Library is free software: you can redistribute it
13 * and/or modify it under the terms of the GNU Affero General Public License
14 * as published by the Free Software Foundation, either version 3 of the
15 * License, or (at your option) any later version.
16 *
17 * Linear Arrangement Library is distributed in the hope that it will be
18 * useful, but WITHOUT ANY WARRANTY; without even the implied warranty of
19 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 * GNU Affero General Public License for more details.
21 *
22 * You should have received a copy of the GNU Affero General Public License
23 * along with Linear Arrangement Library. If not, see <http://www.gnu.org/licenses/>.
24 *
25 * Contact:
26 *
27 * LluĂ­s Alemany Puig (lluis.alemany.puig@upc.edu)
28 * LQMC (Quantitative, Mathematical, and Computational Linguisitcs)
29 * CQL (Complexity and Quantitative Linguistics Lab)
30 * Jordi Girona St 1-3, Campus Nord UPC, 08034 Barcelona. CATALONIA, SPAIN
31 * Webpage: https://cqllab.upc.edu/people/lalemany/
32 *
33 * Ramon Ferrer i Cancho (rferrericancho@cs.upc.edu)
34 * LQMC (Quantitative, Mathematical, and Computational Linguisitcs)
35 * CQL (Complexity and Quantitative Linguistics Lab)
36 * Office 220, Omega building
37 * Jordi Girona St 1-3, Campus Nord UPC, 08034 Barcelona. CATALONIA, SPAIN
38 * Webpage: https://cqllab.upc.edu/people/rferrericancho/
39 *
40 ********************************************************************/
41
42#pragma once
43
44// C++ includes
45#include <algorithm>
46
47// lal includes
48#include <lal/detail/array.hpp>
49#include <lal/detail/sorting/insertion_sort.hpp>
50#include <lal/detail/type_traits/is_pointer_iterator.hpp>
51
52namespace lal {
53namespace detail {
54namespace sorting {
55
69template <
70 typename T, typename iterator_t,
71 std::enable_if_t<
72 std::is_integral_v<T> and is_pointer_iterator_v<T, iterator_t>,
73 bool
74 > = true
75>
77(
78 const iterator_t begin,
79 const iterator_t end,
80 const T& m,
81 char * const seen
82)
83noexcept
84{
85 // fill bit array
86 for (auto it = begin; it != end; ++it) {
87 seen[*it - m] = 1;
88 }
89
90 // pointer to see
91 std::size_t seenit = 0;
92 // element to assign to container
93 T i = m;
94
95 // pointer to container
96 iterator_t it = begin;
97 while (it != end) {
98 // assign value to container
99 *it = i;
100 it += seen[seenit];
101
102 // move pointer in bit array
103 seen[seenit] = 0;
104 ++i;
105 ++seenit;
106 }
107}
108
123template <
124 typename T, typename It,
125 std::enable_if_t<
126 std::is_integral_v<T> and is_pointer_iterator_v<T, It>,
127 bool
128 > = true
129>
131(
132 const It begin,
133 const It end,
134 const std::size_t size,
135 char * const seen
136)
137noexcept
138{
139 if (size <= 1) { return; }
140 if (size <= 14) {
141 insertion_sort(begin, end);
142 return;
143 }
144 if (size <= 30) {
145 std::sort(begin, end);
146 return;
147 }
148
149 bit_sort(begin,end, T{0}, seen);
150}
151
163template <
164 typename T, typename It,
165 std::enable_if_t<
166 std::is_integral_v<T> and is_pointer_iterator_v<T, It>,
167 bool
168 > = true
169>
171(
172 const It begin,
173 const It end,
174 const std::size_t size
175)
176noexcept
177{
178 if (size <= 1) { return; }
179 if (size <= 14) {
180 insertion_sort(begin, end);
181 return;
182 }
183 if (size <= 30) {
184 std::sort(begin, end);
185 return;
186 }
187
188 // maximum & minimum elements within array
189 const auto [m_it,M_it] = std::minmax_element(begin, end);
190 const auto m = *m_it;
191 const auto M = *M_it;
192
193 // bit array
194 array<char> seen(M - m + 1, 0);
195
196 bit_sort(begin,end, m, seen.begin());
197}
198
199} // -- namespace sorting
200} // -- namspace utils
201} // -- namespace lal
void insertion_sort(const It begin, const It end) noexcept
Insertion sort.
Definition insertion_sort.hpp:58
void bit_sort(const iterator_t begin, const iterator_t end, const T &m, char *const seen) noexcept
Sorts the elements within range [begin, end)
Definition bit_sort.hpp:77
void bit_sort_mem(const It begin, const It end, const std::size_t size, char *const seen) noexcept
Sort integer values increasingly.
Definition bit_sort.hpp:131
constexpr bool is_pointer_iterator_v
Shorthand for is_pointer_iterator.
Definition is_pointer_iterator.hpp:68
Main namespace of the library.
Definition basic_types.hpp:48
Wrapper of a C array for automatic deallocation of memory.
Definition array.hpp:59
T * begin() noexcept
Non-constant raw pointer to first element.
Definition array.hpp:300