Branch data Line data Source code
1 : : // Copyright (C) 2012 Internet Systems Consortium, Inc. ("ISC")
2 : : //
3 : : // Permission to use, copy, modify, and/or distribute this software for any
4 : : // purpose with or without fee is hereby granted, provided that the above
5 : : // copyright notice and this permission notice appear in all copies.
6 : : //
7 : : // THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES WITH
8 : : // REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
9 : : // AND FITNESS. IN NO EVENT SHALL ISC BE LIABLE FOR ANY SPECIAL, DIRECT,
10 : : // INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
11 : : // LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE
12 : : // OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
13 : : // PERFORMANCE OF THIS SOFTWARE.
14 : :
15 : : #ifndef __RANGE_UTIL_H_
16 : : #define __RANGE_UTIL_H_ 1
17 : :
18 : : #include <stdlib.h>
19 : : #include <algorithm>
20 : :
21 : : // This header contains useful methods for conduction operations on
22 : : // a range of container elements. Currently the collection is limited,
23 : : // but it is expected to grow.
24 : :
25 : : namespace isc {
26 : : namespace util {
27 : :
28 : : /// @brief Checks if specified range in a container contains only zeros
29 : : ///
30 : : /// @param begin beginning of the range
31 : : /// @param end end of the range
32 : : ///
33 : : /// @return true if there are only zeroes, false otherwise
34 : : template <typename Iterator>
35 : : bool
36 : 4 : isRangeZero(Iterator begin, Iterator end) {
37 : : return (std::find_if(begin, end,
38 : : std::bind1st(std::not_equal_to<int>(), 0))
39 : 4 : == end);
40 : : }
41 : :
42 : : /// @brief Fill in specified range with a random data.
43 : : ///
44 : : /// Make sure that random number generator is initialized properly. Otherwise you
45 : : /// will get a the same pseudo-random sequence after every start of your process.
46 : : /// Calling srand() is enough. This method uses default rand(), which is usually
47 : : /// a LCG pseudo-random number generator, so it is not suitable for security
48 : : /// purposes. Please get a decent PRNG implementation, like mersene twister, if
49 : : /// you are doing anything related with security.
50 : : ///
51 : : /// PRNG initialization is left out of this function on purpose. It may be
52 : : /// initialized to specific value on purpose, e.g. to repeat exactly the same
53 : : /// sequence in a test.
54 : : ///
55 : : /// @param begin
56 : : /// @param end
57 : : template <typename Iterator>
58 : : void
59 : : fillRandom(Iterator begin, Iterator end) {
60 [ + + ][ + + ]: 1717 : for (Iterator x = begin; x != end; ++x) {
61 : 1616 : *x = random();
62 : : }
63 : : }
64 : :
65 : : } // end of isc::util namespace
66 : : } // end of isc namespace
67 : :
68 : : #endif // __PKTINFO_UTIL_H_
|