Keyple Util C++ Library 2.3.0.5-SNAPSHOT
Reference Terminal Reader API for C++
Arrays.h
Go to the documentation of this file.
1/**************************************************************************************************
2 * Copyright (c) 2023 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 */
23
24namespace keyple {
25namespace core {
26namespace util {
27namespace cpp {
28
30
31class Arrays {
32public:
33 static bool equals(const std::vector<char>& a1, const std::vector<char>& a2)
34 {
35 if (a1.size() != a2.size()) {
36 return false;
37 }
38
39 for (auto i1 = a1.begin(), i2 = a2.begin(); i1 != a1.end();
40 i1++, i2++) {
41 if (*i1 != *i2)
42 return false;
43 }
44
45 return true;
46 }
47
48 static bool equals(const std::vector<uint8_t>& a1, const std::vector<uint8_t>& a2)
49 {
50 if (a1.size() != a2.size()) {
51 return false;
52 }
53
54 for (auto i1 = a1.begin(), i2 = a2.begin(); i1 != a1.end();
55 i1++, i2++) {
56 if (*i1 != *i2)
57 return false;
58 }
59
60 return true;
61 }
62
63 static int hashCode(const std::vector<char> a)
64 {
65 int hash = 0;
66
67 for (auto i = a.begin(); i != a.end(); i++)
68 hash ^= *i;
69
70 return hash;
71 }
72
73 static int hashCode(const std::vector<uint8_t> a)
74 {
75 int hash = 0;
76
77 for (auto i = a.begin(); i != a.end(); i++)
78 hash ^= *i;
79
80 return hash;
81 }
82
87 static std::vector<uint8_t> copyOf(const std::vector<uint8_t>& original, const size_t size)
88 {
89 std::vector<uint8_t> vec;
90 std::copy(original.begin(), original.begin() + size, std::back_inserter(vec));
91
92 return vec;
93 }
94
95 static std::vector<char> copyOfRange(const std::vector<char>& original,
96 const size_t from,
97 const size_t to)
98 {
99 if ((to - from) > original.size()) {
100 throw IndexOutOfBoundsException("index out of bound");
101 }
102
103 std::vector<char> vec;
104 std::copy(original.begin() + from, original.begin() + to, std::back_inserter(vec));
105
106 return vec;
107 }
108
109 static std::vector<uint8_t> copyOfRange(const std::vector<uint8_t>& original,
110 const size_t from,
111 const size_t to)
112 {
113 if (from > original.size()) {
114
115 throw IndexOutOfBoundsException("from > original.size()");
116 }
117
118 if (from > to) {
119
120 throw IllegalArgumentException("from > to");
121 }
122
123 std::vector<uint8_t> vec;
124
125 if (to <= original.size()) {
126
127 std::copy(original.begin() + from, original.begin() + to, std::back_inserter(vec));
128
129 } else {
130
131 std::copy(original.begin() + from,
132 original.begin() + original.size(),
133 std::back_inserter(vec));
134 vec.resize(to - from, 0);
135 }
136
137 return vec;
138 }
139
140 template <typename T>
141 static bool contains(const std::vector<T>& a, const T b)
142 {
143 for (const auto& v : a) {
144 if (v == b) {
145 return true;
146 }
147 }
148
149 return false;
150 }
151
152 template <typename T>
153 static bool containsAll(std::vector<T> a, std::vector<T> b)
154 {
155 std::sort(a.begin(), a.end());
156 std::sort(b.begin(), b.end());
157
158 return std::includes(a.begin(), a.end(), b.begin(), b.end());
159 }
160
161 template <typename T>
162 static bool containsOnly(const std::vector<T>& a, const T b)
163 {
164 for (const auto& v : a) {
165 if (v != b) {
166 return false;
167 }
168 }
169
170 return true;
171 }
172
173 template <typename T>
174 static bool startsWith(const std::vector<T>& a, const std::vector<T>& b)
175 {
176 if (b.size() > a.size()) {
177
178 return false;
179 }
180
181 for (size_t i = 0; i < b.size(); i++) {
182
183 if (a[i] != b[i]) {
184
185 return false;
186 }
187 }
188
189 return true;
190 }
191
192 template <typename T>
193 static bool endsWith(const std::vector<T>& a, const std::vector<T>& b)
194 {
195 if (b.size() > a.size()) {
196
197 return false;
198 }
199
200 const size_t offset = a.size() - b.size();
201
202 for (size_t i = 0; i < b.size(); i++) {
203
204 if (a[offset + i] != b[i]) {
205
206 return false;
207 }
208 }
209
210 return true;
211 }
212
213 template <typename T>
214 static int indexOf(const std::vector<T>& a, const T b)
215 {
216 auto it = std::find(a.begin(), a.end(), b);
217 if (it != a.end()) {
218 return static_cast<int>(it - a.begin());
219 }
220
221 return -1;
222 }
223
224 template <typename T>
225 static bool addAll(std::vector<T>& a, const std::vector<T>& b)
226 {
227 if (b.size() == 0) {
228 return false;
229 }
230
231 for (const auto& t : b) {
232 a.push_back(t);
233 }
234
235 return true;
236 }
237
238 template <typename T>
239 static bool addAll(std::vector<std::shared_ptr<T>>& a, const std::vector<std::shared_ptr<T>>& b)
240 {
241 if (b.size() == 0) {
242 return false;
243 }
244
245 for (const auto& t : b) {
246 a.push_back(t);
247 }
248
249 return true;
250 }
251
252
253 template <typename T, typename U>
254 static bool addAll(std::vector<std::shared_ptr<T>>& a, const std::vector<std::shared_ptr<U>>& b)
255 {
256 if (b.size() == 0) {
257 return false;
258 }
259
260 for (const auto& u : b) {
261 a.push_back(std::dynamic_pointer_cast<T>(u));
262 }
263
264 return true;
265 }
266
267 template <typename T>
268 static void remove(std::vector<std::shared_ptr<T>>& a, const std::shared_ptr<T>& b)
269 {
270 const auto it = std::find(a.begin(), a.end(), b);
271 if (it != a.end()) {
272 /* Value is present */
273 a.erase(it);
274 }
275 }
276
277 template <typename T, typename U>
278 static bool removeAll(std::vector<std::shared_ptr<T>>& a, const std::vector<std::shared_ptr<U>>& b)
279 {
280 bool ret = false;
281
282 if (b.size() == 0) {
283 return false;
284 }
285
286 for (const auto& u : b) {
287 const auto t = std::dynamic_pointer_cast<T>(u);
288 const auto it = std::find(a.begin(), a.end(), t);
289 if (it != a.end()) {
290 /* Value is present, remove it and set ret to true */
291 a.erase(it);
292 ret = true;
293 }
294 }
295
296 return ret;
297 }
298
299 template <typename T>
300 static void fill(std::vector<T>& a, const size_t from_Index, const size_t to_Index, T val)
301 {
302 for (size_t i = from_Index; i < to_Index; i++) {
303 a[i] = val;
304 }
305 }
306};
307
308}
309}
310}
311}
static bool startsWith(const std::vector< T > &a, const std::vector< T > &b)
Definition: Arrays.h:174
static bool addAll(std::vector< T > &a, const std::vector< T > &b)
Definition: Arrays.h:225
static bool addAll(std::vector< std::shared_ptr< T > > &a, const std::vector< std::shared_ptr< U > > &b)
Definition: Arrays.h:254
static void remove(std::vector< std::shared_ptr< T > > &a, const std::shared_ptr< T > &b)
Definition: Arrays.h:268
static std::vector< uint8_t > copyOf(const std::vector< uint8_t > &original, const size_t size)
Definition: Arrays.h:87
static bool containsOnly(const std::vector< T > &a, const T b)
Definition: Arrays.h:162
static void fill(std::vector< T > &a, const size_t from_Index, const size_t to_Index, T val)
Definition: Arrays.h:300
static bool contains(const std::vector< T > &a, const T b)
Definition: Arrays.h:141
static int hashCode(const std::vector< char > a)
Definition: Arrays.h:63
static bool removeAll(std::vector< std::shared_ptr< T > > &a, const std::vector< std::shared_ptr< U > > &b)
Definition: Arrays.h:278
static bool addAll(std::vector< std::shared_ptr< T > > &a, const std::vector< std::shared_ptr< T > > &b)
Definition: Arrays.h:239
static int indexOf(const std::vector< T > &a, const T b)
Definition: Arrays.h:214
static bool equals(const std::vector< uint8_t > &a1, const std::vector< uint8_t > &a2)
Definition: Arrays.h:48
static bool equals(const std::vector< char > &a1, const std::vector< char > &a2)
Definition: Arrays.h:33
static bool containsAll(std::vector< T > a, std::vector< T > b)
Definition: Arrays.h:153
static int hashCode(const std::vector< uint8_t > a)
Definition: Arrays.h:73
static bool endsWith(const std::vector< T > &a, const std::vector< T > &b)
Definition: Arrays.h:193
static std::vector< uint8_t > copyOfRange(const std::vector< uint8_t > &original, const size_t from, const size_t to)
Definition: Arrays.h:109
static std::vector< char > copyOfRange(const std::vector< char > &original, const size_t from, const size_t to)
Definition: Arrays.h:95