abseil-cpp/absl/container/btree_test.h
Go to the documentation of this file.
1 // Copyright 2018 The Abseil Authors.
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 // https://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14 
15 #ifndef ABSL_CONTAINER_BTREE_TEST_H_
16 #define ABSL_CONTAINER_BTREE_TEST_H_
17 
18 #include <algorithm>
19 #include <cassert>
20 #include <random>
21 #include <string>
22 #include <utility>
23 #include <vector>
24 
25 #include "absl/container/btree_map.h"
26 #include "absl/container/btree_set.h"
27 #include "absl/container/flat_hash_set.h"
28 #include "absl/strings/cord.h"
29 #include "absl/time/time.h"
30 
31 namespace absl {
33 namespace container_internal {
34 
35 // Like remove_const but propagates the removal through std::pair.
36 template <typename T>
38  using type = typename std::remove_const<T>::type;
39 };
40 template <typename T, typename U>
41 struct remove_pair_const<std::pair<T, U> > {
44 };
45 
46 // Utility class to provide an accessor for a key given a value. The default
47 // behavior is to treat the value as a pair and return the first element.
48 template <typename K, typename V>
49 struct KeyOfValue {
50  struct type {
51  const K& operator()(const V& p) const { return p.first; }
52  };
53 };
54 
55 // Partial specialization of KeyOfValue class for when the key and value are
56 // the same type such as in set<> and btree_set<>.
57 template <typename K>
58 struct KeyOfValue<K, K> {
59  struct type {
60  const K& operator()(const K& k) const { return k; }
61  };
62 };
63 
64 inline char* GenerateDigits(char buf[16], unsigned val, unsigned maxval) {
65  assert(val <= maxval);
66  constexpr unsigned kBase = 64; // avoid integer division.
67  unsigned p = 15;
68  buf[p--] = 0;
69  while (maxval > 0) {
70  buf[p--] = ' ' + (val % kBase);
71  val /= kBase;
72  maxval /= kBase;
73  }
74  return buf + p + 1;
75 }
76 
77 template <typename K>
78 struct Generator {
79  int maxval;
80  explicit Generator(int m) : maxval(m) {}
81  K operator()(int i) const {
82  assert(i <= maxval);
83  return K(i);
84  }
85 };
86 
87 template <>
88 struct Generator<absl::Time> {
89  int maxval;
90  explicit Generator(int m) : maxval(m) {}
91  absl::Time operator()(int i) const { return absl::FromUnixMillis(i); }
92 };
93 
94 template <>
95 struct Generator<std::string> {
96  int maxval;
97  explicit Generator(int m) : maxval(m) {}
98  std::string operator()(int i) const {
99  char buf[16];
100  return GenerateDigits(buf, i, maxval);
101  }
102 };
103 
104 template <>
105 struct Generator<Cord> {
106  int maxval;
107  explicit Generator(int m) : maxval(m) {}
108  Cord operator()(int i) const {
109  char buf[16];
110  return Cord(GenerateDigits(buf, i, maxval));
111  }
112 };
113 
114 template <typename T, typename U>
115 struct Generator<std::pair<T, U> > {
118 
119  explicit Generator(int m) : tgen(m), ugen(m) {}
120  std::pair<T, U> operator()(int i) const {
121  return std::make_pair(tgen(i), ugen(i));
122  }
123 };
124 
125 // Generate n values for our tests and benchmarks. Value range is [0, maxval].
126 inline std::vector<int> GenerateNumbersWithSeed(int n, int maxval, int seed) {
127  // NOTE: Some tests rely on generated numbers not changing between test runs.
128  // We use std::minstd_rand0 because it is well-defined, but don't use
129  // std::uniform_int_distribution because platforms use different algorithms.
130  std::minstd_rand0 rng(seed);
131 
132  std::vector<int> values;
133  absl::flat_hash_set<int> unique_values;
134  if (values.size() < n) {
135  for (int i = values.size(); i < n; i++) {
136  int value;
137  do {
138  value = static_cast<int>(rng()) % (maxval + 1);
139  } while (!unique_values.insert(value).second);
140 
141  values.push_back(value);
142  }
143  }
144  return values;
145 }
146 
147 // Generates n values in the range [0, maxval].
148 template <typename V>
149 std::vector<V> GenerateValuesWithSeed(int n, int maxval, int seed) {
150  const std::vector<int> nums = GenerateNumbersWithSeed(n, maxval, seed);
151  Generator<V> gen(maxval);
152  std::vector<V> vec;
153 
154  vec.reserve(n);
155  for (int i = 0; i < n; i++) {
156  vec.push_back(gen(nums[i]));
157  }
158 
159  return vec;
160 }
161 
162 } // namespace container_internal
164 } // namespace absl
165 
166 #endif // ABSL_CONTAINER_BTREE_TEST_H_
absl::Cord
Definition: abseil-cpp/absl/strings/cord.h:150
absl::container_internal::Generator< std::pair< T, U > >::operator()
std::pair< T, U > operator()(int i) const
Definition: abseil-cpp/absl/container/btree_test.h:120
absl::container_internal::Generator< std::string >::operator()
std::string operator()(int i) const
Definition: abseil-cpp/absl/container/btree_test.h:98
absl::Time
Definition: third_party/abseil-cpp/absl/time/time.h:642
absl::container_internal::Generator::maxval
int maxval
Definition: abseil-cpp/absl/container/btree_test.h:79
absl::container_internal::remove_pair_const
Definition: abseil-cpp/absl/container/btree_test.h:37
absl::container_internal::Generator< std::pair< T, U > >::ugen
Generator< typename remove_pair_const< U >::type > ugen
Definition: abseil-cpp/absl/container/btree_test.h:117
seed
static const uint8_t seed[20]
Definition: dsa_test.cc:79
buf
voidpf void * buf
Definition: bloaty/third_party/zlib/contrib/minizip/ioapi.h:136
absl::FromUnixMillis
constexpr Time FromUnixMillis(int64_t ms)
Definition: third_party/abseil-cpp/absl/time/time.h:1605
testing::internal::string
::std::string string
Definition: bloaty/third_party/protobuf/third_party/googletest/googletest/include/gtest/internal/gtest-port.h:881
absl::container_internal::raw_hash_set< absl::container_internal::FlatHashSetPolicy< T >, absl::container_internal::hash_default_hash< T >, absl::container_internal::hash_default_eq< T >, std::allocator< T > >::insert
std::pair< iterator, bool > insert(T &&value)
Definition: abseil-cpp/absl/container/internal/raw_hash_set.h:1382
absl::container_internal::Generator
Definition: abseil-cpp/absl/container/btree_test.h:78
absl::container_internal::KeyOfValue::type
Definition: abseil-cpp/absl/container/btree_test.h:50
absl::container_internal::Generator< Cord >::maxval
int maxval
Definition: abseil-cpp/absl/container/btree_test.h:106
absl::container_internal::Generator< Cord >::operator()
Cord operator()(int i) const
Definition: abseil-cpp/absl/container/btree_test.h:108
ABSL_NAMESPACE_END
#define ABSL_NAMESPACE_END
Definition: third_party/abseil-cpp/absl/base/config.h:171
setup.k
k
Definition: third_party/bloaty/third_party/capstone/bindings/python/setup.py:42
absl::container_internal::remove_pair_const< std::pair< T, U > >::type
std::pair< typename remove_pair_const< T >::type, typename remove_pair_const< U >::type > type
Definition: abseil-cpp/absl/container/btree_test.h:43
absl::container_internal::Generator< std::pair< T, U > >::Generator
Generator(int m)
Definition: abseil-cpp/absl/container/btree_test.h:119
absl::container_internal::Generator< absl::Time >::Generator
Generator(int m)
Definition: abseil-cpp/absl/container/btree_test.h:90
ABSL_NAMESPACE_BEGIN
#define ABSL_NAMESPACE_BEGIN
Definition: third_party/abseil-cpp/absl/base/config.h:170
absl::container_internal::Generator< absl::Time >::operator()
absl::Time operator()(int i) const
Definition: abseil-cpp/absl/container/btree_test.h:91
absl::flat_hash_set
Definition: abseil-cpp/absl/container/flat_hash_set.h:105
absl::container_internal::Generator::operator()
K operator()(int i) const
Definition: abseil-cpp/absl/container/btree_test.h:81
absl::container_internal::GenerateDigits
char * GenerateDigits(char buf[16], unsigned val, unsigned maxval)
Definition: abseil-cpp/absl/container/btree_test.h:64
absl::container_internal::Generator< std::string >::maxval
int maxval
Definition: abseil-cpp/absl/container/btree_test.h:96
gen
OPENSSL_EXPORT GENERAL_NAME * gen
Definition: x509v3.h:495
absl::container_internal::Generator< std::pair< T, U > >::tgen
Generator< typename remove_pair_const< T >::type > tgen
Definition: abseil-cpp/absl/container/btree_test.h:116
absl::container_internal::KeyOfValue< K, K >::type::operator()
const K & operator()(const K &k) const
Definition: abseil-cpp/absl/container/btree_test.h:60
n
int n
Definition: abseil-cpp/absl/container/btree_test.cc:1080
absl::container_internal::GenerateNumbersWithSeed
std::vector< int > GenerateNumbersWithSeed(int n, int maxval, int seed)
Definition: abseil-cpp/absl/container/btree_test.h:126
absl::container_internal::Generator< std::string >::Generator
Generator(int m)
Definition: abseil-cpp/absl/container/btree_test.h:97
absl::container_internal::Generator::Generator
Generator(int m)
Definition: abseil-cpp/absl/container/btree_test.h:80
value
const char * value
Definition: hpack_parser_table.cc:165
absl::container_internal::GenerateValuesWithSeed
std::vector< V > GenerateValuesWithSeed(int n, int maxval, int seed)
Definition: abseil-cpp/absl/container/btree_test.h:149
absl::container_internal::remove_pair_const::type
typename std::remove_const< T >::type type
Definition: abseil-cpp/absl/container/btree_test.h:38
absl::container_internal::KeyOfValue::type::operator()
const K & operator()(const V &p) const
Definition: abseil-cpp/absl/container/btree_test.h:51
std
Definition: grpcpp/impl/codegen/async_unary_call.h:407
values
std::array< int64_t, Size > values
Definition: abseil-cpp/absl/container/btree_benchmark.cc:608
absl
Definition: abseil-cpp/absl/algorithm/algorithm.h:31
absl::container_internal::KeyOfValue
Definition: abseil-cpp/absl/container/btree_test.h:49
asyncio_get_stats.type
type
Definition: asyncio_get_stats.py:37
absl::container_internal::Generator< Cord >::Generator
Generator(int m)
Definition: abseil-cpp/absl/container/btree_test.h:107
regress.m
m
Definition: regress/regress.py:25
pair
std::pair< std::string, std::string > pair
Definition: abseil-cpp/absl/container/internal/raw_hash_set_benchmark.cc:78
absl::container_internal::Generator< absl::Time >::maxval
int maxval
Definition: abseil-cpp/absl/container/btree_test.h:89
i
uint64_t i
Definition: abseil-cpp/absl/container/btree_benchmark.cc:230


grpc
Author(s):
autogenerated on Fri May 16 2025 02:57:50