Aleph-w 3.0
A C++ Library for Data Structures and Algorithms
Loading...
Searching...
No Matches
al-vector.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
37# include <gtest/gtest.h>
38
39# include <ahSort.H>
40# include <ah-string-utils.H>
41# include <al-vector.H>
42
43using namespace std;
44using namespace testing;
45using namespace Aleph;
46
47struct SmallDomain : public testing::Test
48{
49 AlDomain<char> chars = { 'a', 'b', 'c', 'd', 'e'};
51};
52
54{
55 Vector<char, int> v1(chars);
56 Vector<char, int> v2(chars, { 0, 1, 2, 3, 4 });
57 auto v3 = v2;
58
60 all([&v1] (char c) { return v1.search_entry(c) == nullptr; }));
61
63 build_dynlist<char>('a', 'b', 'c', 'd', 'e')));
64 EXPECT_EQ(v2, v3);
65
66 auto v4 = v1;
67 v4 += v2;
68
69 EXPECT_EQ(v2, v4);
70
71 EXPECT_EQ(v2 + v3, 2*v2);
72
74
75 EXPECT_EQ(v2 - v3, zero);
76
77 Vector<char, int> v5(chars, { 1, 2, 3, 4, 5 });
78 EXPECT_TRUE(v5.get_domain().keys().
79 all([&v5] (char c) { return v5.search_entry(c); }));
80}
81
83{
84 EXPECT_THROW(VType(chars, { 1, 2, 3, 4 }), length_error);
85 VType v = { chars, { 1, 2, 3, 4, 5 } };
86
87 EXPECT_NO_THROW(VType v1 = { chars });
88
89 AlDomain<char> auxd = { '1', '2', '3', '4' };
90 VType vaux = { auxd };
91
92 EXPECT_THROW(vaux = v, domain_error);
93 EXPECT_THROW(vaux = VType(chars), domain_error);
94 EXPECT_NO_THROW(vaux = VType(auxd));
95
96 VType vaux1 = { auxd };
98}
99
101{
102 constexpr size_t N = 10000;
103 const AlDomain<int> r = range<int>(N);
104 const DynList<int> & D = r.keys();
105 const Vector<int> zero(r);
106
107 Vector<int> odd = { r, D.maps<double>([] (auto i)
108 { return (i % 2) == 0 ? 0 : i; }) };
109 Vector<int> even = { r, D.maps<double>([] (auto i)
110 { return (i % 2) == 0 ? i : 0; }) };
111 Vector<int> full = { r, range<double>(N) };
112 Vector<int> I = { r, rep<double>(N, 1) };
113
114 EXPECT_EQ(odd + even, full);
115 EXPECT_EQ(odd*even, 0);
116
117 EXPECT_EQ(I*I, N);
118 EXPECT_EQ(full*I, N*(N - 1)/2);
119 EXPECT_EQ(even*I, N*(N-2)/4);
120 EXPECT_EQ(odd*I, N*N/4);
121}
String manipulation utilities.
High-level sorting functions for Aleph containers.
Sparse vector with named elements.
TEST_F(SmallDomain, basic)
Definition al-vector.cc:53
Generic domain class based on hash set.
Definition al-domain.H:85
const DynList< T > & keys() const
Get sorted list of all elements in the domain.
Definition al-domain.H:101
DynList< T > to_list() const
Convert domain to sorted list.
Definition al-domain.H:115
Dynamic singly linked list with functional programming support.
Definition htlist.H:1423
const Domain & get_domain() const noexcept
Get the domain over which this vector is defined.
Definition al-vector.H:137
NumType * search_entry(const T &i) const noexcept
Search for an entry and return pointer to its value.
Definition al-vector.H:378
Aleph::DynList< __T > maps(Operation &op) const
Map the elements of the container.
Definition ah-dry.H:904
#define TEST(name)
#define N
Definition fib.C:294
Main namespace for Aleph-w library functions.
Definition ah-arena.H:89
bool all(Container &container, Operation &operation)
Return true if all elements satisfy a predicate.
bool eq(const C1 &c1, const C2 &c2, Eq e=Eq())
Check equality of two containers using a predicate.
DynArray< T > sort(const DynArray< T > &a, Cmp &&cmp=Cmp())
Returns a sorted copy of a DynArray.
Definition ahSort.H:227
DynList< T > maps(const C &c, Op op)
Classic map operation.
STL namespace.
Aleph::DynList< T > keys() const
Definition ah-dry.H:1516
AlDomain< char > chars
Definition al-vector.cc:49