SDL Window Engine  20200905
swe_tools.h
1 /***************************************************************************
2  * Copyright (C) 2017 by SWE team <sdl.window.engine@gmail.com> *
3  * *
4  * Part of the SWE: SDL Window Engine: *
5  * https://github.com/AndreyBarmaley/sdl-window-engine *
6  * *
7  * This program is free software; you can redistribute it and/or modify *
8  * it under the terms of the GNU General Public License as published by *
9  * the Free Software Foundation; either version 3 of the License, or *
10  * (at your option) any later version. *
11  * *
12  * This program is distributed in the hope that it will be useful, *
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of *
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
15  * GNU General Public License for more details. *
16  * *
17  * You should have received a copy of the GNU General Public License *
18  * along with this program; if not, write to the *
19  * Free Software Foundation, Inc., *
20  * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *
21  ***************************************************************************/
22 
23 #ifndef _SWE_TOOLS_
24 #define _SWE_TOOLS_
25 
26 #include <list>
27 #include <vector>
28 #include <memory>
29 #include <iterator>
30 #include <algorithm>
31 #include <bitset>
32 #include <numeric>
33 
34 #include "swe_types.h"
35 #include "swe_binarybuf.h"
36 #include "swe_rect.h"
37 #include "swe_systems.h"
38 #include "swe_serialize.h"
39 
41 namespace SWE
42 {
43  namespace Tools
44  {
45  void delay(unsigned int ms);
46  int countBits(unsigned long);
47 
48  int rand(int min, int max);
49  float randf(float min, float max);
50 
51  u32 crc32b(const std::string &);
52  u32 crc32b(const u8*, size_t);
53  u32 crc32b(const u8*, size_t, u32 magic);
54 
55  int crc16b(const std::string &);
56  int crc16b(const u8*, size_t);
57  int crc16b(const u8*, size_t, u16 magic);
58 
59  u32 ticks(void);
60  int signValue(int v);
61 
62  std::pair<int, float>
63  modf(float);
64 
65  std::string stringEncode(const std::string &, const char* charset);
66 
67  BinaryBuf zlibUncompress(const char*, size_t, size_t real = 0);
68  BinaryBuf zlibCompress(const char*, size_t);
69 
70  BinaryBuf zlibUncompress(const u8*, size_t, size_t real = 0);
71  BinaryBuf zlibCompress(const u8*, size_t);
72 
73  BinaryBuf base64Decode(const char*);
74  BinaryBuf base64Decode(const u8*, size_t);
75 
76  BinaryBuf base64Encode(const char*);
77  BinaryBuf base64Encode(const u8*, size_t);
78 
79  Points renderCircle(const Point &, int, bool fill = false);
80  Points renderLine(const Point &, const Point &, int step = 1);
81 
82  template<typename InputIterator>
83  InputIterator random_n(InputIterator first, InputIterator last)
84  {
85  auto dist = std::distance(first, last);
86  InputIterator res = first;
87 
88  if(1 < dist)
89  std::advance(res, rand(0, dist - 1));
90 
91  return res;
92  }
93 
94 
95  /* RandQueue */
96  template<typename T>
97  class RandQueue : protected std::vector< std::pair<T, size_t> >
98  {
99  protected:
100  size_t getMaxWeight(void) const
101  {
102  return std::accumulate(this->begin(), this->end(), 0,
103  [](size_t v, auto & pair){ return v += pair.second; });
104  }
105 
106  public:
107  RandQueue(size_t size = 0)
108  {
109  if(size) this->reserve(size);
110  }
111 
112  void push(const T & value, size_t weight)
113  {
114  if(weight) this->emplace_back(value, weight);
115  }
116 
117  bool isValid(void) const
118  {
119  return 0 < this->size();
120  }
121 
122  T get(void)
123  {
124  if(isValid())
125  {
126  std::vector<size_t> percents;
127  percents.reserve(this->size());
128  // get max
129  size_t max = getMaxWeight();
130 
131  // set weights
132  for(auto & pair : *this)
133  percents.push_back(100 * pair.second / max);
134 
135  // calc max
136  max = std::accumulate(percents.begin(), percents.end(), 0,
137  [](size_t v, auto & val){ return v += val; });
138 
139  size_t rnd = rand(0, max);
140  size_t amount = 0;
141 
142  // get rnd
143  auto it = std::find_if(percents.begin(), percents.end(),
144  [&](auto & val){ amount += val; return rnd <= amount; });
145 
146  if(it != percents.end())
147  return this->operator[](std::distance(percents.begin(), it)).first;
148  }
149 
150  ERROR("weight not found");
151  return T();
152  }
153  };
154 
155  /* RandomChance */
157  {
158  size_t chance;
159  size_t index;
160  std::bitset<100> seeds;
161 
162  void fill(void)
163  {
164  seeds.reset();
165  const int min = 0;
166  const int max = seeds.size() - 1;
167 
168  while(seeds.count() < chance)
169  {
170  int rnd = rand(min, max);
171  seeds.set(rnd);
172  }
173  }
174 
175  public:
176  RandomChance(size_t v/* 1 .. 99 chance */) : chance(v), index(0)
177  {
178  if(chance < 1 || chance > 99) chance = 50;
179 
180  fill();
181  }
182 
183  int getChance(void) const
184  {
185  return chance;
186  }
187 
188  bool check(void)
189  {
190  if(index < seeds.size())
191  return seeds.test(index++);
192  else
193  {
194  fill();
195  index = 0;
196  return check();
197  }
198  }
199 
200  bool last(void) const
201  {
202  return seeds.test(index);
203  }
204 
205  std::string toString(void) const
206  {
207  return seeds.to_string();
208  }
209  };
210 
211  template<typename T>
212  std::list<T> AdvancedSplit(const T & buf, const T & sep)
213  {
214  std::list<T> list;
215  auto itbeg = buf.begin();
216 
217  for(;;)
218  {
219  auto itend = std::search(itbeg, buf.end(), sep.begin(), sep.end());
220  list.push_back(T(itbeg, itend));
221 
222  if(itend >= buf.end()) break;
223 
224  itbeg = itend;
225  std::advance(itbeg, sep.size());
226  }
227 
228  return list;
229  }
230  }
231 
232  /* Timer */
233  class Timer
234  {
235  struct TimerDeleter
236  {
237  void operator() (SDL_TimerID* ptr)
238  {
239  SDL_RemoveTimer(*ptr);
240  delete ptr;
241  };
242  };
243 
244  protected:
245  std::unique_ptr<SDL_TimerID, TimerDeleter> ptr;
246  Timer(const SDL_TimerID &);
247 
248  public:
249  Timer() {}
250 
251  static Timer create(u32 interval /* ms */, u32(*)(u32, void*), void* param = nullptr);
252  void destroy(void);
253  bool isValid(void) const;
254  };
255 
256  /* TickTrigger */
257  struct TickTrigger
258  {
259  mutable u32 latest;
260 
261  TickTrigger();
262 
263  bool check(u32 ms, u32 period) const;
264  void reset(void);
265  void disabled(bool f);
266  };
267 
268  /* KeyValue */
269  template<typename T>
270  struct KeyValue : std::pair<std::string, T>
271  {
272  KeyValue() {}
273  KeyValue(const std::string & str, const T & val) : std::pair<std::string, T>(str, val) {}
274 
275  const std::string & key(void) const
276  {
277  return this->first;
278  }
279  const T & value(void) const
280  {
281  return this->second;
282  }
283  };
284 
285 } // SWE
286 #endif
пространство SWE.
Definition: swe_binarybuf.cpp:30
Definition: swe_tools.h:97
Definition: swe_tools.h:233
Definition: swe_tools.h:257
Definition: swe_tools.h:156
Definition: swe_tools.h:270