LAL: Linear Arrangement Library 21.07.01
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 - 2021
7 *
8 * This file is part of Linear Arrangement Library. To see the full code
9 * visit the webpage:
10 * https://github.com/lluisalemanypuig/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 (lalemany@cs.upc.edu)
28 * LARCA (Laboratory for Relational Algorithmics, Complexity and Learning)
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 * LARCA (Laboratory for Relational Algorithmics, Complexity and Learning)
35 * CQL (Complexity and Quantitative Linguistics Lab)
36 * Office S124, 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/internal/data_array.hpp>
49#include <lal/internal/sorting/insertion_sort.hpp>
50#include <lal/internal/macros.hpp>
51
52namespace lal {
53namespace internal {
54
55namespace __lal {
56
57/*
58 * @brief Sorts the elements within range [begin, end)
59 *
60 * @param begin Iterator at the beginning of the container.
61 * @param end Iterator at the end of the container.
62 * @param seen The bit array used to sort.
63 * @pre All values of @e mem must be set to false.
64 * @pre All values within [begin, end) must be unique.
65 * @post All the values of @e seen are set to false.
66 * @post The elements in the range [begin,end) are sorted increasingly.
67 */
68template<
69 typename T, typename It,
70 std::enable_if_t<
71 std::is_integral_v<T> && is_pointer_iterator_v<T, It>,
72 bool
73 > = true
74>
75void __bit_sort(It begin, It end, const T& m, char * const seen) {
76 // fill bit array
77 for (auto it = begin; it != end; ++it) {
78 seen[*it - m] = 1;
79 }
80
81 // pointer to see
82 size_t seenit = 0;
83 // element to assign to container
84 T i = m;
85
86 // pointer to container
87 It it = begin;
88 while (it != end) {
89 // assign value to container
90 *it = i;
91 it += seen[seenit];
92
93 // move pointer in bit array
94 seen[seenit] = 0;
95 ++i;
96 ++seenit;
97 }
98}
99
100} // -- namespace __lal
101
102/*
103 * @brief Sort integer values increasingly.
104 *
105 * @param begin Iterator at the beginning of the container.
106 * @param end Iterator at the end of the container.
107 * @param size The value of std::distance(begin, end), i.e., the number of
108 * elements from begin to sort, i.e., the size of the container.
109 * @param seen The bit array used to sort.
110 * @pre All values of @e mem must be set to 0.
111 * @pre All values within [begin, end) must be unique.
112 * @post All the values of @e seen are set to 0.
113 * @post The elements in the range [begin,end) are sorted increasingly.
114 */
115template<
116 typename T, typename It,
117 std::enable_if_t<
118 std::is_integral_v<T> && is_pointer_iterator_v<T, It>,
119 bool
120 > = true
121>
122void bit_sort_mem(It begin, It end, const size_t size, char * const seen)
123{
124 if (size <= 1) { return; }
125 if (size <= 14) {
126 insertion_sort(begin, end);
127 return;
128 }
129 if (size <= 30) {
130 std::sort(begin, end);
131 return;
132 }
133
134 // sort
135 __lal::__bit_sort(begin,end, static_cast<T>(0), seen);
136}
137
138/*
139 * @brief Sort integer values increasingly.
140 *
141 * @param begin Iterator at the beginning of the container.
142 * @param end Iterator at the end of the container.
143 * @param size The value of std::distance(begin, end), i.e., the number of
144 * elements from begin to sort, i.e., the size of the container.
145 * @pre All values within [begin, end) must be unique.
146 * @post The elements in the range [begin,end) are sorted increasingly.
147 */
148template<
149 typename T, typename It,
150 std::enable_if_t<
151 std::is_integral_v<T> && is_pointer_iterator_v<T, It>,
152 bool
153 > = true
154>
155void bit_sort(It begin, It end, const size_t size)
156{
157 if (size <= 1) { return; }
158 if (size <= 14) {
159 insertion_sort(begin, end);
160 return;
161 }
162 if (size <= 30) {
163 std::sort(begin, end);
164 return;
165 }
166
167 // maximum & minimum elements within array
168 const auto [m_it,M_it] = std::minmax_element(begin, end);
169 const auto m = *m_it;
170 const auto M = *M_it;
171
172 // bit array
173 data_array<char> seen(M - m + 1, 0);
174
175 // sort
176 __lal::__bit_sort(begin,end, m, seen.data);
177}
178
179} // -- namspace utils
180} // -- namespace lal
Main namespace of the library.
Definition definitions.hpp:48