CSet++
Set of defined functions to work with sets
Loading...
Searching...
No Matches
set.cpp
Go to the documentation of this file.
1
7#include <iostream>
8#include <string>
9#include <vector>
10#include <chrono>
11#include <thread>
12#include <functional>
13#include <mutex>
14#include "set.h"
18std::mutex out;
23void print(std::string msg) {
24 bool waiting = true;
25 while(waiting) {
26 if (out.try_lock()) {
27 std::cout << msg << std::endl;
28 out.unlock();
29 waiting = false;
30 } else {
31 std::this_thread::sleep_for(std::chrono::milliseconds(5));
32 }
33 }
34 return;
35}
36
43void benchmark(std::string name, std::function<void()> function, int n) {
44 std::chrono::time_point<std::chrono::high_resolution_clock> pstr, pend;
45
46 pstr = std::chrono::high_resolution_clock::now();
47 for (int i = 0; i < n; i++) {function();}
48 pend = std::chrono::high_resolution_clock::now();
49
50 auto mstr = std::chrono::time_point_cast<std::chrono::microseconds>(pstr).time_since_epoch().count();
51 auto mend = std::chrono::time_point_cast<std::chrono::microseconds>(pend).time_since_epoch().count();
52 auto nstr = std::chrono::time_point_cast<std::chrono::nanoseconds>(pstr).time_since_epoch().count();
53 auto nend = std::chrono::time_point_cast<std::chrono::nanoseconds>(pend).time_since_epoch().count();
54
55 print("Benchmark " + name + ": " + std::to_string(mend - mstr) + "ms - " + std::to_string(nend - nstr) + "ns");
56 return;
57}
58
63int main() {
64 std::vector<int> t;
65
66 std::thread contains([]() {benchmark("Contains", []() {set::contains({0, 0, 1, 0, 0}, 1);}, 1000);});
67 std::thread count([]() {benchmark("Count", []() {set::count({0, 0, 1, 0, 0}, 0);}, 1000);});
68 std::thread unique([]() {benchmark("Unique", []() {set::unique(std::vector<int>({0, 0, 1, 0, 0}));}, 1000);});
69 std::thread sort([]() {benchmark("Sort", []() {set::sort(std::vector<int>({0, 0, 1, 0, 0}));}, 1000);});
70 std::thread rsort([]() {benchmark("Reverse Sort", []() {set::rsort(std::vector<int>({0, 0, 1, 0, 0}));}, 1000);});
71 std::thread reverse([]() {benchmark("Reverse", []() {set::reverse(std::vector<int>({0, 0, 1, 0, 0}));}, 1000);});
72 std::thread sunion([]() {benchmark("Union", []() {set::sunion(std::vector<int>({1, 2, 3}), std::vector<int>({3, 4, 5}));}, 1000);});
73 std::thread sintersection([]() {benchmark("Intersection", []() {set::intersection(std::vector<int>({1, 2, 3}), std::vector<int>({3, 4, 5}));}, 1000);});
74 std::thread diff([]() {benchmark("Difference", []() {set::diff(std::vector<int>({1, 2, 3}), std::vector<int>({3, 4, 5}));}, 1000);});
75 std::thread sdiff([]() {benchmark("Symmetric difference", []() {set::sdiff(std::vector<int>({1, 2, 3}), std::vector<int>({3, 4, 5}));}, 1000);});
76
77 contains.join();
78 count.join();
79 unique.join();
80 sort.join();
81 rsort.join();
82 reverse.join();
83 sunion.join();
84 sintersection.join();
85 diff.join();
86 sdiff.join();
87
88 return 0;
89}
void benchmark(std::string name, std::function< void()> function, int n)
Manages the benchmarking of a passed function.
Definition: set.cpp:43
std::mutex out
The std::mutex object that locks the std::cout resource.
Definition: set.cpp:18
void print(std::string msg)
Manages the std::cout resource between simultaneously running threads.
Definition: set.cpp:23
int main()
Main entry point that runs a benchmark for every library method.
Definition: set.cpp:63
std::vector< T > & diff(std::vector< T > &c, const std::vector< T > &k, const std::vector< T > &kk)
Obtains the set theory difference of two sets.
Definition: set.h:453
bool contains(const std::vector< T > &k, const T &kk) noexcept
Checks if an element is present in the set.
Definition: set.h:300
std::vector< T > & sdiff(std::vector< T > &c, const std::vector< T > &k, const std::vector< T > &kk)
Obtains the set theory symmetric difference of two sets.
Definition: set.h:476
std::vector< T > & intersection(std::vector< T > &c, const std::vector< T > &k, const std::vector< T > &kk)
Obtains the set theory intersection of two sets.
Definition: set.h:429
std::vector< T > & rsort(std::vector< T > &c, const std::vector< T > &k)
Sorts a set in descending order.
Definition: set.h:361
int count(const std::vector< T > &k, const T &kk) noexcept
Counts the number of times an element is present in the set.
Definition: set.h:307
std::vector< T > & unique(std::vector< T > &c, const std::vector< T > &k)
Removes duplicates from a set.
Definition: set.h:315
std::vector< T > & sort(std::vector< T > &c, const std::vector< T > &k)
Sorts a set in ascending order.
Definition: set.h:339
std::vector< T > & reverse(std::vector< T > &c, const std::vector< T > &k)
Reverses the passed set.
Definition: set.h:383
std::vector< T > & sunion(std::vector< T > &c, const std::vector< T > &k, const std::vector< T > &kk)
Obtains the set theory union of two sets.
Definition: set.h:405
This is the main header file of the cset library. It contains all the functions and their implementat...