Aleph-w 7.10.0
A C++ Library for Data Structures and Algorithms
Loading...
Searching...
No Matches
sort_arrays.cc
Go to the documentation of this file.
1
2/*
3 Aleph_w
4
5 Data structures & Algorithms
6 version 2.0.0b
7 https://github.com/lrleon/Aleph-w
8
9 This file is part of Aleph-w library
10
11 Copyright (c) 2002-2026 Leandro Rabindranath Leon
12
13 Permission is hereby granted, free of charge, to any person obtaining a copy
14 of this software and associated documentation files (the "Software"), to deal
15 in the Software without restriction, including without limitation the rights
16 to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
17 copies of the Software, and to permit persons to whom the Software is
18 furnished to do so, subject to the following conditions:
19
20 The above copyright notice and this permission notice shall be included in all
21 copies or substantial portions of the Software.
22
23 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
24 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
25 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
26 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
27 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
28 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
29 SOFTWARE.
30*/
31
32
38//
39// Created by lrleon on 1/6/25.
40//
41
42# include <chrono>
43# include <vector>
44# include <sstream>
45# include <array>
46# include <algorithm>
47# include <functional>
48# include <iostream>
49# include <ranges>
50# include <numeric>
51# include <stdexcept>
52# include <gtest/gtest.h>
53
54# include <ah-random.H>
55# include <ah-string-utils.H>
56# include <ah-stl-utils.H>
57# include <ahSort.H>
58
59using namespace Aleph;
60using namespace std;
61using namespace std::chrono;
62using namespace testing;
63
64struct SimpleArray : public Test
65{
66 vector <int> a = {3, 2, 1, 0, 4, 5, 6, 7, 8, 9};
67 Array <int> b = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9};
68 Array <string> c = {"a", "b", "c", "d", "e", "f", "g", "h", "i", "j"};
69};
70
71struct TenVectors : public Test
72{
73 static vector <int> rand_vec(int n, Gsl_Compatible_Rng & rng)
74 {
75 vector <int> v(n);
76 for (int i = 0; i < n; i++)
77 v[i] = rng.uniform_int(n);
78 return v;
79 }
80
81 Gsl_Compatible_Rng rng; // the seed 0, as that of gsl_rng_alloc()
82
83 vector <int> a = rand_vec(1000000, rng);
84 vector <int> b = rand_vec(1000000, rng);
85 vector <int> c = rand_vec(1000000, rng);
86 vector <int> d = rand_vec(1000000, rng);
87 vector <int> e = rand_vec(1000000, rng);
88 vector <int> f = rand_vec(1000000, rng);
89 vector <int> g = rand_vec(1000000, rng);
90 vector <int> h = rand_vec(1000000, rng);
91 vector <int> i = rand_vec(1000000, rng);
92 vector <int> j = rand_vec(1000000, rng);
93};
94
96{
97 in_place_multisort_arrays(std::less <int>(), a, b, c);
98
99 ASSERT_EQ(a, vector<int>({ 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 }));
100
101 ASSERT_EQ(to_vector(b), vector<int>({ 3, 2, 1, 0, 4, 5, 6, 7, 8, 9 }));
102
103 ASSERT_EQ(to_vector(c),
104 vector<string>({"d", "c", "b", "a", "e", "f", "g", "h", "i", "j"}));
105}
106
108{
109 auto start = high_resolution_clock::now();
110 in_place_multisort_arrays(std::less <int>(), a, b, c, d, e, f, g, h, i, j);
111 auto stop = high_resolution_clock::now();
112 auto duration = duration_cast <milliseconds>(stop - start);
113 cout << "Time taken by function: "
114 << duration.count() << " milliseconds" << endl;
115}
116
117TEST(MultiSortArrays, stable_with_duplicates)
118{
119 vector<int> keys = {2, 1, 2, 1, 2};
120 vector<char> aux = {'a', 'b', 'c', 'd', 'e'};
121 vector<int> expect_keys = {1, 1, 2, 2, 2};
122 vector<char> expect_aux = {'b', 'd', 'a', 'c', 'e'}; // stable order within equal keys
123
124 in_place_multisort_arrays(std::less<int>(), keys, aux);
125
126 EXPECT_EQ(keys, expect_keys);
127 EXPECT_EQ(aux, expect_aux);
128}
129
130TEST(MultiSortArrays, size_mismatch_throws)
131{
132 vector<int> keys = {1, 0};
133 vector<int> aux = {10};
134
135 EXPECT_THROW(in_place_multisort_arrays(std::less<int>(), keys, aux), std::invalid_argument);
136}
The random number generators of Aleph: the one that gives the numbers of the gsl_rng_mt19937 of GSL,...
Conversion utilities between Aleph-w containers and STL containers.
String manipulation utilities.
High-level sorting functions for Aleph containers.
long double h
Definition btreepic.C:154
A random number generator with the sequences of GSL's gsl_rng_mt19937.
Definition ah-random.H:147
unsigned long uniform_int(const unsigned long n)
An integer in [0, n), each with the same probability, as gsl_rng_uniform_int(r, n).
Definition ah-random.H:302
#define TEST(name)
Main namespace for Aleph-w library functions.
DynArray< T > sort(const DynArray< T > &a, Cmp &&cmp=Cmp())
Returns a sorted copy of a DynArray.
Definition ahSort.H:234
void in_place_multisort_arrays(Cmp cmp, const bool stable, C &first, Args &... args)
Sorts multiple arrays in place, using the first array as the key.
Definition ahSort.H:842
std::vector< typename C::Item_Type > to_vector(const C &c)
Convert a container to a std::vector.
STL namespace.
TEST_F(SimpleArray, sort)
Array< int > b
Array< string > c
Gsl_Compatible_Rng rng
vector< int > b
vector< int > h
vector< int > i
static vector< int > rand_vec(int n, Gsl_Compatible_Rng &rng)
vector< int > d
vector< int > g
vector< int > c
vector< int > f
vector< int > e
vector< int > j
vector< int > a
int keys[]
Dnode< int > Test
Definition testDnode.C:42