range_utilities.h 2.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  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. #ifndef __RANGE_UTIL_H_
  15. #define __RANGE_UTIL_H_ 1
  16. #include <stdlib.h>
  17. #include <algorithm>
  18. // This header contains useful methods for conduction operations on
  19. // a range of container elements. Currently the collection is limited,
  20. // but it is expected to grow.
  21. namespace isc {
  22. namespace util {
  23. /// @brief Checks if specified range in a container contains only zeros
  24. ///
  25. /// @param begin beginning of the range
  26. /// @param end end of the range
  27. ///
  28. /// @return true if there are only zeroes, false otherwise
  29. template <typename Iterator>
  30. bool
  31. isRangeZero(Iterator begin, Iterator end) {
  32. return (std::find_if(begin, end,
  33. std::bind1st(std::not_equal_to<int>(), 0))
  34. == end);
  35. }
  36. /// @brief Fill in specified range with a random data.
  37. ///
  38. /// Make sure that random number generator is initialized properly. Otherwise you
  39. /// will get a the same pseudo-random sequence after every start of your process.
  40. /// Calling srand() is enough. This method uses default rand(), which is usually
  41. /// a LCG pseudo-random number generator, so it is not suitable for security
  42. /// purposes. Please get a decent PRNG implementation, like mersene twister, if
  43. /// you are doing anything related with security.
  44. ///
  45. /// PRNG initialization is left out of this function on purpose. It may be
  46. /// initialized to specific value on purpose, e.g. to repeat exactly the same
  47. /// sequence in a test.
  48. ///
  49. /// @param begin
  50. /// @param end
  51. template <typename Iterator>
  52. void
  53. fillRandom(Iterator begin, Iterator end) {
  54. for (Iterator x = begin; x != end; ++x) {
  55. *x = random();
  56. }
  57. }
  58. } // end of isc::util namespace
  59. } // end of isc namespace
  60. #endif // __PKTINFO_UTIL_H_