Limi
helpers.h
1 /*
2  * Copyright 2016, IST Austria
3  *
4  * This file is part of Limi.
5  *
6  * Limi is free software: you can redistribute it and/or modify
7  * it under the terms of the GNU Lesser General Public License as published by
8  * the Free Software Foundation, either version 3 of the License, or
9  * (at your option) any later version.
10  *
11  * Limi is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14  * GNU Lesser General Public License for more details.
15  *
16  * You should have received a copy of the GNU Lesser General Public License
17  * along with Limi. If not, see <http://www.gnu.org/licenses/>.
18  */
19 
20 #ifndef LIMI_INTERNAL_HELPERS_H
21 #define LIMI_INTERNAL_HELPERS_H
22 
23 #include <unordered_set>
24 #include <vector>
25 #include <list>
26 #include <ostream>
27 #include "../generics.h"
28 
29 namespace Limi {
30 namespace internal {
31  template <class Key, class Hash = std::hash<Key>, class Pred = std::equal_to<Key>>
32  void print_set(const std::unordered_set<Key, Hash, Pred>& set1, std::ostream& out, const printer_base<Key>& printerK = printer<Key>()) {
33  out << "{ ";
34  for (auto it = set1.begin(); it!=set1.end();) {
35  out << printerK(*it);
36  ++it;
37  if (it!=set1.end()) {
38  out << ",";
39  } else {
40  break;
41  }
42  }
43  out << " }";
44  }
45 
46  template <class Key>
47  void print_vector(const std::vector<Key>& vector1, std::ostream& out, const printer_base<Key>& printerK = printer<Key>()) {
48  out << "[";
49  for (auto it = vector1.begin(); it!=vector1.end();) {
50  out << printerK(*it);
51  ++it;
52  if (it!=vector1.end()) {
53  out << ", ";
54  } else {
55  break;
56  }
57  }
58  out << "]";
59  }
60 
61  template <class Key>
62  void print_list(const std::list<Key>& vector1, std::ostream& out, const printer_base<Key>& printerK = printer<Key>()) {
63  out << "[";
64  for (auto it = vector1.begin(); it!=vector1.end();) {
65  printerK(*it, out);
66  ++it;
67  if (it!=vector1.end()) {
68  out << ", ";
69  } else {
70  break;
71  }
72  }
73  out << "]";
74  }
75 
76  template <class Items>
77  void print_array(const Items* items, unsigned length, std::ostream& out, const printer_base<Items>& printerK = printer<Items>()) {
78  out << "[";
79  for (unsigned i = 0; i<length; i++) {
80  printerK(items[i], out);
81  if (i < length-1) {
82  out << ", ";
83  }
84  }
85  out << "]";
86  }
87 
88  template <class Keys, class Hash, class Pred, class Alloc>
89  void set_remove(std::unordered_set<Keys,Hash,Pred,Alloc>& set1, const std::unordered_set<Keys,Hash,Pred,Alloc>& set2) {
90  for (const auto& item : set2) {
91  auto it = set1.find(item);
92  if (it!=set1.end())
93  set1.erase(it);
94  }
95  }
96 
97  template <class Keys, class Hash, class Pred, class Alloc>
98  void set_intersect(std::unordered_set<Keys,Hash,Pred,Alloc>& set1, const std::unordered_set<Keys,Hash,Pred,Alloc>& set2) {
99  for (auto it = set1.begin(); it!=set1.end(); ) {
100  if (set2.find(*it)==set2.end())
101  it = set1.erase(it);
102  else
103  ++it;
104  }
105  }
106 
107  template <class Keys, class Hash, class Pred, class Alloc>
108  bool set_intersection_empty(const std::unordered_set<Keys,Hash,Pred,Alloc>& set1, const std::unordered_set<Keys,Hash,Pred,Alloc>& set2) {
109  for (const auto& item : set1) {
110  if (set2.find(item)!=set2.end())
111  return false;
112  }
113  return true;
114  }
115 
116  template <class Keys, class Hash, class Pred, class Alloc>
117  void set_union(std::unordered_set<Keys,Hash,Pred,Alloc>& set1, const std::unordered_set<Keys,Hash,Pred,Alloc>& set2) {
118  for (const auto& item : set2) {
119  set1.insert(item);
120  }
121  }
122 }
123 }
124 
125 #endif // LIMI_INTERNAL_HELPERS_H
The main namespace of the library.
Definition: antichain_algo.h:40