Gobelijn API documentation  - generated for commit a0cbea7
 All Classes Namespaces Files Functions Variables Typedefs Friends Macros Pages
algo.cpp
Go to the documentation of this file.
1 /*
2  * This file is part of the gobelijn software.
3  * Gobelijn is free software: you can redistribute it and/or modify it
4  * under the terms of the GNU General Public License as published by the
5  * Free Software Foundation, either version 3 of the License, or any later
6  * version. Gobelijn is distributed in the hope that it will be useful, but
7  * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
8  * or FITNESS FOR A PARTICULAR PURPOSE.
9  * See the GNU General Public License for details. You should have received
10  * a copy of the GNU General Public License along with the software. If not,
11  * see <http://www.gnu.org/licenses/>.
12  *
13  * Copyright 2012, Jan Broeckhove, CoMP/UA.
14  */
20 #include "RandInt.h"
21 
22 #include <algorithm>
23 #include <iomanip>
24 #include <iostream>
25 #include <list>
26 #include <map>
27 
28 using std::cout;
29 using std::endl;
30 using std::exception;
31 using std::string;
32 
33 namespace {
34 
36 template <typename C>
37 void SimpleRandInit(C& c)
38 {
39  RandInt r(1, 100);
40  for (auto it = c.begin(); it != c.end(); ++it) {
41  *it = r();
42  }
43 }
44 
46 template <typename C>
47 typename C::value_type Sum(const C& c)
48 {
49  typename C::value_type val = typename C::value_type();
50  for (auto it = c.cbegin(); it != c.cend(); ++it) {
51  val += *it;
52  }
53  return val;
54 }
55 
57 template <typename C>
58 std::map<typename C::value_type, unsigned int> Bin(C const& c)
59 {
60  std::map<typename C::value_type, unsigned int> m;
61  for (auto it = c.cbegin(); it != c.cend(); ++it) {
62  m[*it]++;
63  }
64  return m;
65 }
66 
68 template <typename It>
69 typename It::value_type Sum(It first, It last)
70 {
71  typename It::value_type val = typename It::value_type();
72  for (It it = first; it != last; ++it) {
73  val += *it;
74  }
75  return val;
76 }
77 
79 template <typename It>
80 std::map<typename It::value_type, unsigned int> Bin(It first, It last)
81 {
82  std::map<typename It::value_type, unsigned int> m;
83  for (It it = first; it != last; ++it) {
84  m[*it]++;
85  }
86  return m;
87 }
88 
90 template <typename T>
91 class Accumulator
92 {
93 public:
95  explicit Accumulator(T t = T()) : m_accum(t) {}
96 
98  T operator()(T t) { return (m_accum += t); }
99 
101  T operator()() const { return m_accum; }
102 
103 private:
104  T m_accum;
105 };
106 
108 class Incrementor
109 {
110 public:
112  explicit Incrementor(unsigned int n) : m_num(n) {}
113 
115  template <typename T>
116  T operator()(T t)
117  {
118  for (unsigned int i = 1; i <= m_num; i++) {
119  t++;
120  }
121  return t;
122  }
123 
124 private:
125  unsigned int m_num;
126 };
127 
129 template <typename T, typename U>
130 std::ostream& operator<<(std::ostream& out, std::pair<T, U> p)
131 {
132  operator<<(out.operator<<(p.first), " - ");
133  (out.operator<<(p.second)).operator<<(endl);
134  return out;
135 }
136 
138 template <typename C>
139 std::ostream& Printer(std::ostream& out, C const& c)
140 {
141  for (auto it = c.cbegin(); it != c.cend(); ++it) {
142  out << " " << *it;
143  }
144  return out;
145 }
146 
147 template <typename T>
148 std::ostream& operator<<(std::ostream& os, std::vector<T> const& v)
149 {
150  return Printer<std::vector<T>>(os, v);
151 }
152 
153 template <typename T>
154 std::ostream& operator<<(std::ostream& os, std::list<T> const& v)
155 {
156  return Printer<std::list<T>>(os, v);
157 }
158 
159 template <typename T, typename U>
160 std::ostream& operator<<(std::ostream& os, std::map<T, U> const& v)
161 {
162  return Printer<std::map<T, U>>(os, v);
163 }
164 
165 } // namespace
166 
167 int main()
168 {
169  string marker = "\n------------------------------------\n";
170  cout << std::boolalpha;
171  // block 1
172  {
173  cout << marker << "simpleRandInit for vector of 10 int:" << endl;
174  std::vector<double> v(10);
175  SimpleRandInit(v);
176  cout << v << endl << "sum: " << Sum(v) << endl;
177  }
178  // block 2
179  {
180  cout << marker << "bin processes list of 15 int:" << endl;
181  std::list<int> v(15);
182  SimpleRandInit(v);
183  cout << v << endl << "sum: " << Sum(v.cbegin(), v.cend()) << endl << Bin(v) << endl;
184  }
185  // block 3
186  {
187  cout << marker << "bin processes vector of 10 double:" << endl;
188  std::vector<double> v(10);
189  generate(v.begin(), v.end(), RandInt(7, static_cast<unsigned int>(v.size())));
190  cout << v << endl << Bin(v.begin(), v.end()) << endl;
191  }
192  // block 4
193  {
194  cout << marker << "find in list of 15 int:" << endl;
195  std::list<int> v(15);
196  SimpleRandInit(v);
197  cout << v << endl;
198  auto it1 = find(v.cbegin(), v.cend(), 6);
199  auto it2 = find(it1, v.cend(), 14);
200  auto it3 = find(it1, it2, 13);
201  cout << (it3 != it2) << endl;
202  }
203  // block 5
204  {
205  cout << marker << "functor to accumulate sequence:" << endl;
206  std::vector<int> v(10);
207  std::list<int> l(12);
208  generate(v.begin(), v.end(), RandInt(8, static_cast<unsigned int>(v.size())));
209  generate(l.begin(), l.end(), RandInt(6, static_cast<unsigned int>(v.size())));
210  Accumulator<int> a;
211  a = for_each(v.begin(), v.end(), a);
212  cout << v << endl << a() << endl;
213  Incrementor b(2);
214  for_each(l.begin(), l.end(), b);
215  cout << l << endl << a() << endl;
216  }
217  // block 6
218  {
219  cout << marker << "functor to transform sequence:" << endl;
220  std::vector<int> v(8);
221  std::list<int> l(8);
222  generate(v.begin(), v.end(), RandInt(3, static_cast<unsigned int>(v.size())));
223  cout << "transform out of place: " << endl;
224  transform(v.begin(), v.end(), l.begin(), Incrementor(2));
225  cout << v << endl << l << endl;
226  cout << "transform in place: " << endl;
227  transform(v.begin(), v.end(), v.begin(), Incrementor(5));
228  cout << v << endl;
229  }
230 
231  return 0;
232 }
std::ostream & operator<<(std::ostream &out, std::pair< T, U > const &p)
Definition: Range.h:46
Produce random integer (int) values.
Definition: RandInt.h:27
Functor produces random values.
int main()
Definition: algo.cpp:167