Aleph-w 3.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 <gsl/gsl_rng.h>
43
44# include <chrono>
45# include <vector>
46# include <sstream>
47# include <array>
48# include <algorithm>
49# include <functional>
50# include <iostream>
51# include <ranges>
52# include <numeric>
53# include <stdexcept>
54# include <gtest/gtest.h>
55
56# include <ah-string-utils.H>
57# include <ah-stl-utils.H>
58# include <ahSort.H>
59
60using namespace Aleph;
61using namespace std;
62using namespace std::chrono;
63using namespace testing;
64
65struct SimpleArray : public Test
66{
67 vector <int> a = {3, 2, 1, 0, 4, 5, 6, 7, 8, 9};
68 Array <int> b = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9};
69 Array <string> c = {"a", "b", "c", "d", "e", "f", "g", "h", "i", "j"};
70};
71
72struct TenVectors : public Test
73{
74 static vector <int> rand_vec(int n, const gsl_rng *rng)
75 {
76 vector <int> v(n);
77 for (int i = 0; i < n; i++)
78 v[i] = gsl_rng_uniform_int(rng, n);
79 return v;
80 }
81
83
84 vector <int> a = rand_vec(1000000, rng);
85 vector <int> b = rand_vec(1000000, rng);
86 vector <int> c = rand_vec(1000000, rng);
87 vector <int> d = rand_vec(1000000, rng);
88 vector <int> e = rand_vec(1000000, rng);
89 vector <int> f = rand_vec(1000000, rng);
90 vector <int> g = rand_vec(1000000, rng);
91 vector <int> h = rand_vec(1000000, rng);
92 vector <int> i = rand_vec(1000000, rng);
93 vector <int> j = rand_vec(1000000, rng);
94
96 {
98 }
99};
100
102{
103 in_place_multisort_arrays(std::less <int>(), a, b, c);
104
105 ASSERT_EQ(a, vector<int>({ 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 }));
106
107 ASSERT_EQ(to_vector(b), vector<int>({ 3, 2, 1, 0, 4, 5, 6, 7, 8, 9 }));
108
110 vector<string>({"d", "c", "b", "a", "e", "f", "g", "h", "i", "j"}));
111}
112
114{
115 auto start = high_resolution_clock::now();
116 in_place_multisort_arrays(std::less <int>(), a, b, c, d, e, f, g, h, i, j);
117 auto stop = high_resolution_clock::now();
118 auto duration = duration_cast <milliseconds>(stop - start);
119 cout << "Time taken by function: "
120 << duration.count() << " milliseconds" << endl;
121}
122
124{
125 vector<int> keys = {2, 1, 2, 1, 2};
126 vector<char> aux = {'a', 'b', 'c', 'd', 'e'};
127 vector<int> expect_keys = {1, 1, 2, 2, 2};
128 vector<char> expect_aux = {'b', 'd', 'a', 'c', 'e'}; // stable order within equal keys
129
130 in_place_multisort_arrays(std::less<int>(), keys, aux);
131
132 EXPECT_EQ(keys, expect_keys);
133 EXPECT_EQ(aux, expect_aux);
134}
135
137{
138 vector<int> keys = {1, 0};
139 vector<int> aux = {10};
140
141 EXPECT_THROW(in_place_multisort_arrays(std::less<int>(), keys, aux), std::invalid_argument);
142}
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
#define TEST(name)
Main namespace for Aleph-w library functions.
Definition ah-arena.H:89
void in_place_multisort_arrays(Cmp cmp, C &first, Args &... args)
Sorts multiple arrays in place, using the first array as the key.
Definition ahSort.H:668
DynArray< T > sort(const DynArray< T > &a, Cmp &&cmp=Cmp())
Returns a sorted copy of a DynArray.
Definition ahSort.H:227
std::vector< typename C::Item_Type > to_vector(const C &c)
Convert a container to a std::vector.
Definition ah-convert.H:228
DynList< T > maps(const C &c, Op op)
Classic map operation.
STL namespace.
TEST_F(SimpleArray, sort)
Array< int > b
Array< string > c
vector< int > b
vector< int > h
vector< int > i
vector< int > d
static vector< int > rand_vec(int n, const gsl_rng *rng)
vector< int > g
vector< int > c
vector< int > f
vector< int > e
vector< int > j
gsl_rng * rng
vector< int > a