185 cout <<
"(" << node->get_key() <<
"," << node->get_data() <<
")";
193 return x != 0
and not (x & (x - 1));
196typedef tuple<double, double, double, double, double>
Stat;
200template <
typename Key>
205 return get<1>(t1) < get<1>(t2);
209template <
template <
typename,
class>
class TreeType,
215 cout <<
"Sampling at 2^" <<
k <<
" = " << n <<
" ..." << endl;
219 cout <<
" Computing height ..." << endl;
221 cout <<
" done = " << height << endl
223 <<
" Computing IPL ..." << endl;
225 cout <<
" done = " << ipl << endl
229 using Chrono = std::chrono::high_resolution_clock;
232 double ins_avg = 0, rem_avg = 0;
239 while (tree.search(
value) !=
nullptr)
243 static constexpr int Num_Measures = 100;
245 double ins_time = rem_time = 0;
246 for (
int k = 0;
k < Num_Measures; ++
k)
248 auto sample_time = Chrono::now();
249 (void) tree.insert(p);
250 auto dd = Chrono::now() - sample_time;
251 ins_time += dd.count();
253 sample_time = Chrono::now();
254 (void) tree.remove(
KEY(p));
255 rem_time += (Chrono::now() - sample_time).
count();
258 ins_time /= Num_Measures;
259 rem_time /= Num_Measures;
267 Aleph::insertion_sort<Sample, Cmp_Sample<int>>
269 Aleph::insertion_sort<Sample, Cmp_Sample<int>>
272 double ins_desv = 0, rem_desv = 0,
273 ins_min = get<1>(ins_sample[0]),
276 rem_min = get<1>(rem_sample[0]),
282 double s = ins_avg - get<1>(ins_sample[i]);
284 s = rem_avg - get<1>(rem_sample[i]);
298 cout <<
" min Ins time = " << 1e6 * ins_min << endl
299 <<
" avg ins time = " << 1e6 * ins_avg << endl
300 <<
" med ins time = " << 1e6 * ins_med << endl
301 <<
" sig ins time = " << 1e6 *
sqrt(ins_desv) << endl
302 <<
" max ins time = " << 1e6 * ins_max << endl
303 <<
" min Rem time = " << 1e6 * rem_min << endl
304 <<
" avg rem time = " << 1e6 * rem_avg << endl
305 <<
" med rem time = " << 1e6 * rem_med << endl
306 <<
" sig rem time = " << 1e6 *
sqrt(rem_desv) << endl
307 <<
" max rem time = " << 1e6 * rem_max << endl
308 <<
" height = " << height << endl
309 <<
" ipl = " << ipl << endl
310 <<
"done!" << endl << endl;
312 return tuple<Stat, Stat, int, int>
313 (
Stat(ins_min, ins_avg, ins_med, ins_desv, ins_max),
314 Stat(rem_min, rem_avg, rem_med, rem_desv, rem_max),
318template <
template <
typename ,
class >
class TreeType>
324 static const size_t N = 64;
325 Stat ins_stats[
N], rem_stats[
N];
326 int height[
N], ipl[
N];
329 for (
unsigned int i = 0; i < n; i++)
340 tuple<Stat, Stat, int, int> stats =
342 ins_stats[num_pow] = get<0>(stats);
343 rem_stats[num_pow] = get<1>(stats);
344 height[num_pow] = get<2>(stats);
345 ipl[num_pow] = get<3>(stats);
352 printf(
"#2^k n h ipc [min ins med sigma max] [min ins med sigma max]\n");
353 for (
int i = 0; i < num_pow; ++i)
355 Stat & si = ins_stats[i];
356 Stat & sr = rem_stats[i];
357 printf(
"%02d %08d %02d %08d %02.2f %02.2f %02.2f %02.2f %02.2f"
358 " %02.2f %02.2f %02.2f %02.2f %02.2f\n",
359 i, (
int)
pow(2, i), height[i], ipl[i],
360 1e6 * get<0>(si), 1e6 * get<1>(si), 1e6 * get<2>(si),
361 1e6 * get<3>(si), 1e6 * get<4>(si),
362 1e6 * get<0>(sr), 1e6 * get<1>(sr), 1e6 * get<2>(sr),
363 1e6 * get<3>(sr), 1e6 * get<4>(sr));
383template <
template <
typename,
class>
class Tree>
419 return bench.
type == type;
442static char doc[] =
"timeAllTree -- Benchmark Aleph tree implementations";
443static char argDoc[] =
"-n num_nodes -m seed_for_random [tree options]\n";
457 {
"bin",
'b', 0, OPTION_ARG_OPTIONAL,
"Pure binary tree", 0},
458 {
"avl",
'a', 0, OPTION_ARG_OPTIONAL,
"AVL tree", 0},
459 {
"avlrk",
OPT_AVL_RK, 0, OPTION_ARG_OPTIONAL,
"AVL tree (rank)", 0},
460 {
"splay",
's', 0, OPTION_ARG_OPTIONAL,
"Splay tree", 0},
461 {
"splayrk",
OPT_SPLAY_RK, 0, OPTION_ARG_OPTIONAL,
"Splay tree (rank)", 0},
462 {
"redblack",
'r', 0, OPTION_ARG_OPTIONAL,
"Red-black tree", 0},
463 {
"redblackrk",
OPT_RB_RK, 0, OPTION_ARG_OPTIONAL,
"Red-black tree (rank)", 0},
464 {
"tdrb",
OPT_TD_RB, 0, OPTION_ARG_OPTIONAL,
"Top-down red-black tree", 0},
465 {
"tdrbrk",
OPT_TD_RB_RK, 0, OPTION_ARG_OPTIONAL,
"Top-down red-black tree (rank)", 0},
466 {
"rand",
'd', 0, OPTION_ARG_OPTIONAL,
"Randomized tree", 0},
467 {
"treap",
'p', 0, OPTION_ARG_OPTIONAL,
"Treap tree", 0},
468 {
"treaprk",
OPT_TREAP_RK, 0, OPTION_ARG_OPTIONAL,
"Treap tree (rank)", 0},
469 {
"all",
OPT_ALL, 0, OPTION_ARG_OPTIONAL,
"Benchmark all tree types", 0},
471 "nodes",
'n',
"num_nodes", OPTION_ARG_OPTIONAL,
472 "Specify the number of nodes to be generated", 0
475 "seed",
'm',
"seed_for_random", OPTION_ARG_OPTIONAL,
476 "Specify the seed for randon number generator", 0
481static error_t
parser_opt(
int key,
char *,
struct argp_state *state)
483 auto *parsPtr =
static_cast<Parameters *
>(state->input);
485 auto add_tree = [parsPtr](
TreeType type)
496 if (state->argv[state->next] ==
nullptr)
498 parsPtr->n = strtol(state->argv[state->next], &end, 10);
499 if (*end !=
'\0' && *end !=
'\n')
540 parsPtr->run_all =
true;
543 if (state->argv[state->next] ==
nullptr)
545 parsPtr->seed = strtol(state->argv[state->next], &end, 10);
546 if (*end !=
'\0' && *end !=
'\n')
551 default:
return ARGP_ERR_UNKNOWN;
558int main(
int argc,
char *argv[])
562 cout <<
"timeAllTree -- benchmark Aleph-w binary search tree implementations\n"
564 <<
"Grows a tree to n keys (sampling at each power of 2) and reports\n"
565 <<
"height, internal path length (IPL), and insertion/removal timing\n"
566 <<
"statistics (min / avg / median / sigma / max).\n"
569 <<
" " << argv[0] <<
" <tree-flags> [-n num_nodes] [-m seed]\n"
571 <<
"Tree type flags (at least one required, or use --all):\n"
572 <<
" -b, --bin Unbalanced BST (BinTree)\n"
573 <<
" -a, --avl AVL tree\n"
574 <<
" --avlrk AVL tree (ranked)\n"
575 <<
" -s, --splay Splay tree\n"
576 <<
" --splayrk Splay tree (ranked)\n"
577 <<
" -r, --redblack Red-black tree\n"
578 <<
" --redblackrk Red-black tree (ranked)\n"
579 <<
" --tdrb Top-down red-black tree\n"
580 <<
" --tdrbrk Top-down red-black tree (ranked)\n"
581 <<
" -p, --treap Treap\n"
582 <<
" --treaprk Treap (ranked)\n"
583 <<
" -d, --rand Randomized tree\n"
584 <<
" -l, --all Benchmark all tree types\n"
587 <<
" -n num_nodes Number of keys to insert (default: 1000)\n"
588 <<
" -m seed RNG seed (default: current time)\n"
591 <<
" " << argv[0] <<
" --all -n 10000 -m 42\n"
592 <<
" " << argv[0] <<
" -a -r -n 50000\n"
593 <<
" " << argv[0] <<
" --avlrk --treaprk -n 20000 -m 456\n";
599 if (error_t status = argp_parse(&
argDefs, argc, argv, 0, 0, &pars); status != 0)
602 unsigned long n = pars.
n;
606 std::vector<const TreeBenchmark *> benches;
609 benches.push_back(&bench);
613 benches.push_back(bench);
617 benches.erase(ranges::remove(benches,
nullptr).begin(),
621 AH_ERROR((
"No valid tree types selected"));
627 cout <<
"timeAllTree<" << bench->label <<
"> "
628 << n <<
" " << pars.
seed << endl;
631 cout <<
"timeAllTree<" << bench->label <<
"> "
632 << n <<
" " << pars.
seed << endl;
635 catch (exception & e)
637 cout <<
"**** Exception: " << e.what() << endl;
The random number generators of Aleph: the one that gives the numbers of the gsl_rng_mt19937 of GSL,...
#define AH_ERROR(...)
Print an error message (always enabled).
WeightedDigraph::Node Node
size_t size_t int32_t value
A random number generator with the sequences of GSL's gsl_rng_mt19937.
static constexpr result_type max() noexcept
The greatest number of operator()(): 2^32 - 1.
void seed(const unsigned long s) noexcept
Seeds again, as gsl_rng_set(r, s).
unsigned long get() noexcept
The next number, as operator()().
Number Theoretic Transform over Z / MOD Z.
Node for QuadTree spatial data structure.
QuadTree - Hierarchical spatial index for 2D points.
Point * search(const Point &p) noexcept
Search for a point in the tree.
Point * insert(Node *&r, const Point &p)
Recursive insert helper.
__gmp_expr< T, __gmp_unary_expr< __gmp_expr< T, U >, __gmp_log_function > > log(const __gmp_expr< T, U > &expr)
__gmp_expr< T, __gmp_unary_expr< __gmp_expr< T, U >, __gmp_sqrt_function > > sqrt(const __gmp_expr< T, U > &expr)
__gmp_expr< typename __gmp_resolve_expr< T, V >::value_type, __gmp_binary_expr< __gmp_expr< T, U >, __gmp_expr< V, W >, __gmp_pow_function > > pow(const __gmp_expr< T, U > &expr1, const __gmp_expr< V, W > &expr2)
size_t internal_path_length(Node *p) noexcept
Compute the internal path length.
void destroyRec(Node *&root) noexcept
Free all the memory occupied by the tree root
size_t computeHeightRec(Node *root) noexcept
Compute the height of root
and
Check uniqueness with explicit hash + equality functors.
@ Tree
Basic arc (in spanning tree).
Itor::difference_type count(const Itor &beg, const Itor &end, const T &value)
Count elements equal to a value.
bool operator()(const Sample &t1, const Sample &t2) const
std::vector< TreeType > selected
Parameters(int _n, int _seed)
void(* runner)(unsigned long, Gsl_Compatible_Rng &)
static void printNode(Node *node, int, int)
void run_tree(unsigned long n, Gsl_Compatible_Rng &r)
tuple< int, double > Sample
bool is_two_power(const unsigned int x)
static const TreeBenchmark * find_benchmark(TreeType type)
const char * argp_program_version
static error_t parser_opt(int key, char *, struct argp_state *state)
tuple< double, double, double, double, double > Stat
const char * argp_program_bug_address
static const std::array< TreeBenchmark, 12 > kBenchmarks
static struct argp_option options[]
static struct argp argDefs
tuple< Stat, Stat, int, int > sample_tree(TreeType< Key, Compare > &tree, Gsl_Compatible_Rng &r, int n, int k)
constexpr int Num_Samples
AVL tree with rank (order statistics).
AVL tree implementation (height-balanced BST).
Utility functions for binary tree operations.
Generic unbalanced binary search tree.
Dynamic key-value map based on balanced binary search trees.
Randomized binary search tree.
Red-Black tree with rank (order statistics).
Red-Black tree implementation (bottom-up balancing).
Comprehensive sorting algorithms and search utilities for Aleph-w.
Top-down splay tree with rank support.
Top-down splay tree implementation (without rank support).
Top-down Red-Black tree with rank support.
Top-down Red-Black tree implementation.
Treap with rank (order statistics).
Treap: randomized BST combining tree and heap properties.