Keyple Util C++ Library 2.0.0
Reference Terminal Reader API for C++
Arrays.h
Go to the documentation of this file.
1/**************************************************************************************************
2 * Copyright (c) 2021 Calypso Networks Association https://calypsonet.org/ *
3 * *
4 * See the NOTICE file(s) distributed with this work for additional information regarding *
5 * copyright ownership. *
6 * *
7 * This program and the accompanying materials are made available under the terms of the Eclipse *
8 * Public License 2.0 which is available at http://www.eclipse.org/legal/epl-2.0 *
9 * *
10 * SPDX-License-Identifier: EPL-2.0 *
11 **************************************************************************************************/
12
13#pragma once
14
15#include <algorithm>
16#include <string>
17#include <vector>
18#include <iostream>
19
20/* Keyple Core Util */
22
23namespace keyple {
24namespace core {
25namespace util {
26namespace cpp {
27
29
30class Arrays {
31public:
32 static bool equals(const std::vector<char>& a1, const std::vector<char>& a2)
33 {
34 if (a1.size() != a2.size()) {
35 return false;
36 }
37
38 for (auto i1 = a1.begin(), i2 = a2.begin(); i1 != a1.end();
39 i1++, i2++) {
40 if (*i1 != *i2)
41 return false;
42 }
43
44 return true;
45 }
46
47 static bool equals(const std::vector<uint8_t>& a1, const std::vector<uint8_t>& a2)
48 {
49 if (a1.size() != a2.size()) {
50 return false;
51 }
52
53 for (auto i1 = a1.begin(), i2 = a2.begin(); i1 != a1.end();
54 i1++, i2++) {
55 if (*i1 != *i2)
56 return false;
57 }
58
59 return true;
60 }
61
62 static int hashCode(const std::vector<char> a)
63 {
64 int hash = 0;
65
66 for (auto i = a.begin(); i != a.end(); i++)
67 hash ^= *i;
68
69 return hash;
70 }
71
72 static int hashCode(const std::vector<uint8_t> a)
73 {
74 int hash = 0;
75
76 for (auto i = a.begin(); i != a.end(); i++)
77 hash ^= *i;
78
79 return hash;
80 }
81
82 static std::vector<char> copyOfRange(const std::vector<char>& original,
83 const size_t from,
84 const size_t to)
85 {
86 if ((to - from) > original.size()) {
87 throw IndexOutOfBoundsException("index out of bound");
88 }
89
90 std::vector<char> vec;
91 std::copy(original.begin() + from, original.begin() + to, std::back_inserter(vec));
92
93 return vec;
94 }
95
96 static std::vector<uint8_t> copyOfRange(const std::vector<uint8_t>& original,
97 const size_t from,
98 const size_t to)
99 {
100 if ((to - from) > original.size()) {
101 throw IndexOutOfBoundsException("index out of bound");
102 }
103
104 std::vector<uint8_t> vec;
105 std::copy(original.begin() + from, original.begin() + to, std::back_inserter(vec));
106
107 return vec;
108 }
109
110 template <typename T>
111 static bool contains(const std::vector<T>& a, const T b)
112 {
113 for (const auto& v : a) {
114 if (v == b) {
115 return true;
116 }
117 }
118
119 return false;
120 }
121
122 template <typename T>
123 static bool containsAll(std::vector<T> a, std::vector<T> b)
124 {
125 std::sort(a.begin(), a.end());
126 std::sort(b.begin(), b.end());
127
128 return std::includes(a.begin(), a.end(), b.begin(), b.end());
129 }
130
131 template <typename T>
132 static bool containsOnly(const std::vector<T>& a, const T b)
133 {
134 for (const auto& v : a) {
135 if (v != b) {
136 return false;
137 }
138 }
139
140 return true;
141 }
142
143 template <typename T>
144 static int indexOf(const std::vector<T>& a, const T b)
145 {
146 auto it = std::find(a.begin(), a.end(), b);
147 if (it != a.end()) {
148 return static_cast<int>(it - a.begin());
149 }
150
151 return -1;
152 }
153
154 template <typename T>
155 static bool addAll(std::vector<T>& a, const std::vector<T>& b)
156 {
157 if (b.size() == 0) {
158 return false;
159 }
160
161 for (const auto& t : b) {
162 a.push_back(t);
163 }
164
165 return true;
166 }
167
168 template <typename T>
169 static bool addAll(std::vector<std::shared_ptr<T>>& a, const std::vector<std::shared_ptr<T>>& b)
170 {
171 if (b.size() == 0) {
172 return false;
173 }
174
175 for (const auto& t : b) {
176 a.push_back(t);
177 }
178
179 return true;
180 }
181
182
183 template <typename T, typename U>
184 static bool addAll(std::vector<std::shared_ptr<T>>& a, const std::vector<std::shared_ptr<U>>& b)
185 {
186 if (b.size() == 0) {
187 return false;
188 }
189
190 for (const auto& u : b) {
191 a.push_back(std::dynamic_pointer_cast<T>(u));
192 }
193
194 return true;
195 }
196
197 template <typename T>
198 static void remove(std::vector<std::shared_ptr<T>>& a, const std::shared_ptr<T>& b)
199 {
200 const auto it = std::find(a.begin(), a.end(), b);
201 if (it != a.end()) {
202 /* Value is present */
203 a.erase(it);
204 }
205 }
206
207 template <typename T, typename U>
208 static bool removeAll(std::vector<std::shared_ptr<T>>& a, const std::vector<std::shared_ptr<U>>& b)
209 {
210 bool ret = false;
211
212 if (b.size() == 0) {
213 return false;
214 }
215
216 for (const auto& u : b) {
217 const auto t = std::dynamic_pointer_cast<T>(u);
218 const auto it = std::find(a.begin(), a.end(), t);
219 if (it != a.end()) {
220 /* Value is present, remove it and set ret to true */
221 a.erase(it);
222 ret = true;
223 }
224 }
225
226 return ret;
227 }
228
229 template <typename T>
230 static void fill(std::vector<T>& a, const size_t from_Index, const size_t to_Index, T val)
231 {
232 for (size_t i = from_Index; i < to_Index; i++) {
233 a[i] = val;
234 }
235 }
236};
237
238}
239}
240}
241}
static bool addAll(std::vector< T > &a, const std::vector< T > &b)
Definition: Arrays.h:155
static bool addAll(std::vector< std::shared_ptr< T > > &a, const std::vector< std::shared_ptr< U > > &b)
Definition: Arrays.h:184
static void remove(std::vector< std::shared_ptr< T > > &a, const std::shared_ptr< T > &b)
Definition: Arrays.h:198
static bool containsOnly(const std::vector< T > &a, const T b)
Definition: Arrays.h:132
static void fill(std::vector< T > &a, const size_t from_Index, const size_t to_Index, T val)
Definition: Arrays.h:230
static bool contains(const std::vector< T > &a, const T b)
Definition: Arrays.h:111
static int hashCode(const std::vector< char > a)
Definition: Arrays.h:62
static bool removeAll(std::vector< std::shared_ptr< T > > &a, const std::vector< std::shared_ptr< U > > &b)
Definition: Arrays.h:208
static bool addAll(std::vector< std::shared_ptr< T > > &a, const std::vector< std::shared_ptr< T > > &b)
Definition: Arrays.h:169
static int indexOf(const std::vector< T > &a, const T b)
Definition: Arrays.h:144
static bool equals(const std::vector< uint8_t > &a1, const std::vector< uint8_t > &a2)
Definition: Arrays.h:47
static bool equals(const std::vector< char > &a1, const std::vector< char > &a2)
Definition: Arrays.h:32
static bool containsAll(std::vector< T > a, std::vector< T > b)
Definition: Arrays.h:123
static int hashCode(const std::vector< uint8_t > a)
Definition: Arrays.h:72
static std::vector< uint8_t > copyOfRange(const std::vector< uint8_t > &original, const size_t from, const size_t to)
Definition: Arrays.h:96
static std::vector< char > copyOfRange(const std::vector< char > &original, const size_t from, const size_t to)
Definition: Arrays.h:82