/src/kea/src/lib/util/range_utilities.h
Line | Count | Source |
1 | | // Copyright (C) 2012-2019,2021 Internet Systems Consortium, Inc. ("ISC") |
2 | | // |
3 | | // This Source Code Form is subject to the terms of the Mozilla Public |
4 | | // License, v. 2.0. If a copy of the MPL was not distributed with this |
5 | | // file, You can obtain one at http://mozilla.org/MPL/2.0/. |
6 | | |
7 | | #ifndef RANGE_UTIL_H |
8 | | #define RANGE_UTIL_H 1 |
9 | | |
10 | | #include <stdlib.h> |
11 | | #include <algorithm> |
12 | | #include <functional> |
13 | | |
14 | | // This header contains useful methods for conduction operations on |
15 | | // a range of container elements. Currently the collection is limited, |
16 | | // but it is expected to grow. |
17 | | |
18 | | namespace isc { |
19 | | namespace util { |
20 | | |
21 | | /// @brief Checks if specified range in a container contains only zeros |
22 | | /// |
23 | | /// @param begin beginning of the range |
24 | | /// @param end end of the range |
25 | | /// |
26 | | /// @return true if there are only zeroes, false otherwise |
27 | | template <typename Iterator> |
28 | | bool |
29 | 26.5k | isRangeZero(Iterator begin, Iterator end) { |
30 | 26.5k | return (std::find_if(begin, end, [] (int x) { return (0 != x); }) |
31 | 26.5k | == end); |
32 | 26.5k | } |
33 | | |
34 | | /// @brief Fill in specified range with a random data. |
35 | | /// |
36 | | /// Make sure that random number generator is initialized |
37 | | /// properly. Otherwise you will get the same pseudo-random sequence |
38 | | /// after every start of your process. Calling srand() is enough. This |
39 | | /// method uses default rand(), which is usually a LCG pseudo-random |
40 | | /// number generator, so it is not suitable for security |
41 | | /// purposes. Please use cryptolink RNG if you are doing anything |
42 | | /// related with security. |
43 | | /// |
44 | | /// PRNG initialization is left out of this function on purpose. It may |
45 | | /// be initialized to specific value on purpose, e.g. to repeat exactly |
46 | | /// the same sequence in a test. |
47 | | /// |
48 | | /// @param begin |
49 | | /// @param end |
50 | | template <typename Iterator> |
51 | | void |
52 | 189 | fillRandom(Iterator begin, Iterator end) { |
53 | 1.32k | for (Iterator x = begin; x != end; ++x) { |
54 | 1.13k | *x = random(); |
55 | 1.13k | } |
56 | 189 | } |
57 | | |
58 | | } // end of isc::util namespace |
59 | | } // end of isc namespace |
60 | | |
61 | | #endif // RANGE_UTIL_H |