Skip to content

Commit

Permalink
Multisets is now a random access container!
Browse files Browse the repository at this point in the history
  • Loading branch information
mraggi committed Jan 6, 2018
1 parent b9e104b commit d480839
Show file tree
Hide file tree
Showing 14 changed files with 232 additions and 91 deletions.
14 changes: 14 additions & 0 deletions Benchmarks/benchmarker.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -57,3 +57,17 @@ double ForEachBenchmark(const Container& A)
});
});
}

template <class Container>
double ConstructionBenchmark(const Container& A, int numtimes)
{
return Benchmark([&A,numtimes]()
{
for (int i = 0; i < numtimes; ++i)
{
auto t = dscr::random::random_int<long>(0,A.size());
DoNotOptimize(A[t]);
}
});
}

10 changes: 10 additions & 0 deletions Benchmarks/benchtable.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -144,3 +144,13 @@ BenchRow ProduceRowForEach(std::string name, const Container& A)

return BenchRow(name, t, A.size());
}

template <class Container>
BenchRow ProduceRowConstruct(std::string name, const Container& A, int numtimes)
{
double t = ConstructionBenchmark(A,numtimes);

name += " Construct";

return BenchRow(name, t, numtimes);
}
18 changes: 0 additions & 18 deletions Benchmarks/combinations_benchmark.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,17 +4,6 @@
#include "do_not_optimize.hpp"
#include "Probability.hpp"

inline void BM_CombinationsConstruct(int n, int k, int numtimes)
{
dscr::combinations_fast X(n, k);

for (int i = 0; i < numtimes; ++i)
{
auto t = dscr::random::random_int<long>(0,X.size());
DoNotOptimize(X[t]);
}
}

inline void BM_CombinationsNAP(int n, int k)
{
dscr::combinations::combination comb(k);
Expand All @@ -41,13 +30,6 @@ inline void BM_combinationsIterator(int n, int k, long size)
}
}

inline void BM_combinations(int n, int k, long size)
{
for (auto& t : dscr::combinations(n,k))
{
DoNotOptimize(t);
}
}

class algoT_iterator : public boost::iterator_facade<
algoT_iterator,
Expand Down
11 changes: 0 additions & 11 deletions Benchmarks/combinations_tree_benchmark.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -37,17 +37,6 @@ inline long BM_CombinationsTreeFindAll(int n, int k)
return size;
}

inline void BM_CombinationsTreeConstruct(int n, int k, int numtimes)
{
dscr::combinations_tree_fast X(n, k);

for (int i = 0; i < numtimes; ++i)
{
auto t = dscr::random::random_int<long>(0,X.size());
DoNotOptimize(X[t]);
}
}

#include "external/euler314_combination_iterator.hpp"

inline void BM_CombinationsTreeEuler314(int n,int k)
Expand Down
98 changes: 59 additions & 39 deletions Benchmarks/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,15 +15,15 @@ int main()

using dscr::binomial;
std::ios_base::sync_with_stdio(false);
dscr::Chronometer C;
dscr::Chronometer chrono;

cout << "|============================== Starting Speed Tests =============================|" << endl;

//slow, real tests
const int n = 40;
const int k = 10;
const auto combs_size = binomial<long long>(n,k);
const int construct = 100000;
const int construct = 1000000;

const int nperm = 12;

Expand All @@ -33,65 +33,85 @@ int main()
const int nmotzkin = 20;
const int nmultiset = 19;

//fast tests
// const int n = 25;
// const int k = 12;
// const int combconstruct = 1000;
//
// const int nperm = 9;
//
// const int npart = 20;
// const int nsetpart = 7;
// const int ndyck = 10;
// const int nmotzkin = 10;

dscr::combinations C(n,k);
dscr::combinations_fast CF(n,k);
dscr::combinations_tree CT(n,k);
dscr::combinations_tree_fast CTF(n,k);

dscr::permutations P(nperm);
dscr::permutations_fast PF(nperm);

dscr::dyck_paths DP(ndyck);
dscr::dyck_paths_fast DPF(ndyck);
dscr::motzkin_paths MP(ndyck);
dscr::motzkin_paths_fast MPF(ndyck);

dscr::partitions PT(npart);
dscr::partitions_fast PTF(npart);
dscr::set_partitions SPT(nsetpart);

dscr::multisets MS({4,2,3,1,0,1,5,0,5,4,0,1,1,5,2,0,2});
dscr::multisets_fast MSF({4,2,3,1,0,1,5,0,5,4,0,1,1,5,2,0,2});


BenchRow::print_header(cout);
BenchRow::print_line(cout);
cout << ProduceRowForEach("Combinations", dscr::combinations(n,k));
cout << ProduceRowForEach("Combinations Stack", dscr::combinations_fast(n,k));
cout << BenchRow("Combinations (No iterator)", Benchmark([](){BM_CombinationsNAP(n,k);}), combs_size);
cout << ProduceRowForward("Combinations", dscr::combinations(n,k));
cout << ProduceRowForward("Combinations Stack", dscr::combinations_fast(n,k));
cout << ProduceRowReverse("Combinations", dscr::combinations(n,k));
cout << BenchRow("Combinations Construct", Benchmark([](){BM_CombinationsConstruct(n,k,construct);}), construct);
cout << ProduceRowForEach("Combinations", C);
cout << ProduceRowForEach("Combinations Stack", CF);
cout << ProduceRowForward("Combinations", C);
cout << ProduceRowForward("Combinations Stack", CF);
cout << ProduceRowReverse("Combinations", C);
cout << ProduceRowReverse("Combinations Stack", CF);
cout << ProduceRowConstruct("Combinations", C, construct);
cout << ProduceRowConstruct("Combinations Stack", CF, construct);

BenchRow::print_line(cout);
cout << ProduceRowForEach("Combinations Tree", dscr::combinations_tree(n,k));
cout << ProduceRowForEach("Combinations Tree Stack", dscr::combinations_tree_fast(n,k));
cout << BenchRow("Combinations Tree (No iterator)", Benchmark([](){BM_CombinationsTreeNAP(n,k);}), combs_size);
cout << ProduceRowForward("Combinations Tree", dscr::combinations_tree(n,k));
cout << ProduceRowForward("Combinations Tree Stack", dscr::combinations_tree_fast(n,k));
cout << ProduceRowReverse("Combinations Tree", dscr::combinations_tree(n,k));
cout << ProduceRowForEach("Combinations Tree", CT);
cout << ProduceRowForEach("Combinations Tree Stack", CTF);
cout << ProduceRowForward("Combinations Tree", CT);
cout << ProduceRowForward("Combinations Tree Stack", CTF);
cout << ProduceRowReverse("Combinations Tree", CT);
cout << ProduceRowReverse("Combinations Tree Stack", CTF);
#ifdef TEST_GSL_COMBINATIONS
cout << BenchRow("Combinations Tree GSL", Benchmark([](){BM_CombinationsTreeGSL(n,k);}), combs_size);
#endif
cout << BenchRow("Combinations Tree Construct", Benchmark([](){BM_CombinationsTreeConstruct(n,k,construct);}), construct);
cout << ProduceRowConstruct("Combinations Tree", CT, construct);
cout << ProduceRowConstruct("Combinations Tree Stack", CTF, construct);

BenchRow::print_line(cout);
cout << ProduceRowForward("Permutations", dscr::permutations_fast(nperm));
cout << ProduceRowReverse("Permutations", dscr::permutations_fast(nperm));
cout << BenchRow("Permutations Construct", Benchmark([](){BM_PermutationsConstruct(nperm,construct);}), construct);
cout << ProduceRowForward("Permutations", P);
cout << ProduceRowForward("Permutations Stack", PF);
cout << ProduceRowReverse("Permutations", P);
cout << ProduceRowReverse("Permutations Stack", PF);
cout << ProduceRowConstruct("Permutations", P, construct);
cout << ProduceRowConstruct("Permutations Stack", PF, construct);

BenchRow::print_line(cout);
cout << ProduceRowForward("Multisets", dscr::multisets_fast({4,2,3,1,0,1,5,0,5,4,0,1,1,5,2,0,2}));
cout << ProduceRowReverse("Multisets", dscr::multisets_fast({4,2,3,1,0,1,5,0,5,4,0,1,1,5,2,0,2}));

cout << ProduceRowForward("Multisets", MS);
cout << ProduceRowForward("Multisets Stack", MSF);
cout << ProduceRowReverse("Multisets", MS);
cout << ProduceRowReverse("Multisets Stack", MSF);
cout << ProduceRowConstruct("Multisets", MS, construct);
cout << ProduceRowConstruct("Multisets", MSF, construct);

BenchRow::print_line(cout);
cout << ProduceRowForward("Dyck Paths", dscr::dyck_paths_fast(ndyck));
cout << ProduceRowForward("Dyck Paths", DP);
cout << ProduceRowForward("Dyck Paths Stack", DPF);

BenchRow::print_line(cout);
cout << ProduceRowForward("Motzkin Paths", dscr::motzkin_paths_fast(nmotzkin));
cout << ProduceRowForward("Motzkin Paths", MP);
cout << ProduceRowForward("Motzkin Paths Stack", MPF);

BenchRow::print_line(cout);
cout << ProduceRowForward("Partitions", dscr::partitions_fast(npart));
cout << ProduceRowForward("Partitions", PT);
cout << ProduceRowForward("Partitions Stack", PTF);

BenchRow::print_line(cout);
cout << ProduceRowForward("Set Partitions", dscr::set_partitions(nsetpart));
cout << ProduceRowForward("Set Partitions", SPT);

BenchRow::print_line(cout);

cout << "\nTotal Time taken = " << C.Reset() << "s" << endl;
cout << "\nTotal Time taken = " << chrono.Reset() << "s" << endl;
return 0;

}
Expand Down
11 changes: 0 additions & 11 deletions Benchmarks/permutations_benchmark.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,17 +4,6 @@
#include "do_not_optimize.hpp"
#include "Probability.hpp"

inline void BM_PermutationsConstruct(int n, int numtimes)
{
dscr::permutations X(n);

for (int i = 0; i < numtimes; ++i)
{
auto t = dscr::random::random_int<long>(0,X.size());
DoNotOptimize(X[t]);
}
}

inline void BM_PermutationsRandom(int n, int numtimes)
{
dscr::permutations X(n);
Expand Down
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -388,3 +388,6 @@ The important columns are Speed and Speed (w/o _fast). Higher is better. They me

# Contributing
Please help us testing, debugging, benchmarking, packaging for the various distros, etc. Also, if you use discreture for your own purposes, let us know!

Here is what we hope to accomplish:

9 changes: 7 additions & 2 deletions examples/multisets.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,17 @@ int main()
cout << "These are all the submultisets of " << total << endl;

auto X = multisets(total);

int i = 0;
for (auto& x : X)
{
cout << x << endl;
// cout << x << " <---" << i << " == " << X.get_index(x) << endl;
// multisets::multiset u(total.size());
// multisets::construct_multiset(u,total,i);
// cout << "Constructed: " << u << endl;
// ++i;
}
// return 0;
cout << "And here they are in reverse order: " << endl;
for (auto it = X.rbegin(); it != X.rend(); ++it)
{
Expand Down
4 changes: 2 additions & 2 deletions include/Discreture/Combinations.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
#include "CombinationsTree.hpp"
#include "CombinationsTreePrunned.hpp"
#include "combinations_bf.hpp" //Horrible. Do NOT read. Please. But I can't find another way. Sorry about that. If you think you can do better, please, tell me about it.
#include "NaturalNumber.hpp"

namespace dscr
{
Expand Down Expand Up @@ -183,14 +184,13 @@ class basic_combinations
{
IntType r = k - i;
IntType first = r;

while (binomial<size_type>(first, r) <= m)
{
++first;
}

--first;

data[r - 1] = first;
m -= binomial<size_type>(first, r);
}
Expand Down
4 changes: 4 additions & 0 deletions include/Discreture/Misc.hpp
Original file line number Diff line number Diff line change
@@ -1,11 +1,14 @@
#pragma once

#include <cmath>
#include <iostream>
#include <cstdlib>

#include <boost/iterator/iterator_facade.hpp>
#include <boost/container/static_vector.hpp>
#include <boost/container/vector.hpp>


namespace dscr
{

Expand Down Expand Up @@ -42,4 +45,5 @@ inline T pow(T a, unsigned long n)
return r;
}


}
Loading

0 comments on commit d480839

Please sign in to comment.