Skip to content

Commit

Permalink
fix typo in unset and use inserter and insert function, change small
Browse files Browse the repository at this point in the history
value to un_set
  • Loading branch information
NanmiaoWu committed Jun 18, 2021
1 parent f15a41e commit eaa6450
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 34 deletions.
11 changes: 4 additions & 7 deletions tutorial/03_Iterators_and_Algorithms/ex2_solution.cc
Original file line number Diff line number Diff line change
Expand Up @@ -14,19 +14,16 @@ int main(int argc, char *argv[]) {
// #HINT: shad::transform

using value_type = shad::Set<int>::value_type;
using shad_buffered_inserter_t =
shad::buffered_insert_iterator<shad::unordered_set<int>>;
using insert_iterator = shad::insert_iterator<shad::unordered_set<int>>;

// unordered_set
shad::unordered_set<int> set_;

// create set
shad_buffered_inserter_t ins(set_, set_.begin());
for (size_t i = 0; i < 4; ++i) {
ins = i + 2;
set_.insert(i + 2);
}
ins.wait();
ins.flush();


std::cout << "==> Create the unodered_set: \n";
for (auto v: set_) std::cout << v << std::endl;
Expand All @@ -36,7 +33,7 @@ int main(int argc, char *argv[]) {

shad::transform(
shad::distributed_parallel_tag{}, set_.begin(), set_.end(),
shad_buffered_inserter_t(out, out.begin()), [](const value_type &i) { return i * 2; });
insert_iterator(out, out.begin()), [](const value_type &i) { return i * 2; });

std::cout << "==> After using shad::transform, another unodered_set is: \n";
for (auto v: out) std::cout << v << std::endl;
Expand Down
43 changes: 16 additions & 27 deletions tutorial/03_Iterators_and_Algorithms/unordered_set.cc
Original file line number Diff line number Diff line change
Expand Up @@ -4,40 +4,30 @@
#include "shad/core/unordered_set.h"
#include "shad/util/measure.h"

constexpr static size_t kSize = 128;
using iterator = shad::Set<int>::iterator;
using value_type = shad::Set<int>::value_type;
using shad_inserter_t = shad::insert_iterator<shad::unordered_set<int>>;
using shad_buffered_inserter_t =
shad::buffered_insert_iterator<shad::unordered_set<int>>;
using insert_iterator = shad::insert_iterator<shad::unordered_set<int>>;


template <typename shad_inserter>
void shad_transform_algorithm(shad::unordered_set<int> &in) {
shad::unordered_set<int> out;
shad::transform(
shad::distributed_parallel_tag{}, in.begin(), in.end(),
shad_inserter(out, out.begin()), [](const value_type &i) { return i; });
}

namespace shad {

int main(int argc, char *argv[]) {
// unordered_set
shad::unordered_set<int> set_;

// create set
shad_buffered_inserter_t ins(set_, set_.begin());
for (size_t i = 0; i < kSize; ++i) {
ins = 2 * (i + 1);
for (size_t i = 0; i < 4; ++i) {
set_.insert(2 * (i + 1));
}
ins.wait();
ins.flush();

std::cout << "==> Create unodered_set: " << std::endl;
for (auto v: set_) std::cout << v << std::endl;


// shad minmax algorithm
std::pair<iterator, iterator> min_max = shad::minmax_element(
shad::distributed_parallel_tag{}, set_.begin(), set_.end());
std::cout << "==> After using shad::count, min = " << *min_max.first
std::cout << "==> After using shad::minmax_element, min = " << *min_max.first
<< ", max = " << *min_max.second << std::endl;


Expand Down Expand Up @@ -78,15 +68,14 @@ int main(int argc, char *argv[]) {


// shad transform algorithm
auto execute_time = shad::measure<std::chrono::seconds>::duration(
[&]() { shad_transform_algorithm<shad_inserter_t>(set_); });
std::cout << "==> Using shad::transform with insert_iterator, took " << execute_time.count()
<< " seconds" << std::endl;

auto execute_time_ = shad::measure<std::chrono::seconds>::duration(
[&]() { shad_transform_algorithm<shad_buffered_inserter_t>(set_); });
std::cout << "==> Using shad::transform with buffered_insert_iterator, took "
<< execute_time_.count() << " seconds" << std::endl;
shad::unordered_set<int> out;
shad::transform(
shad::distributed_parallel_tag{}, set_.begin(), set_.end(),
insert_iterator(out, out.begin()), [](const value_type &i) { return i; });

std::cout << "==> Using shad::transform, the output set is: " << std::endl;
for(auto v:out) std::cout << v << std::endl;


// Exercise 2
// Create an unorder_set containing int value 2, 3, 4, 5,
Expand Down

0 comments on commit eaa6450

Please sign in to comment.