1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
//! A set of Sized pools that can be use kindof like a sizable Array or Vec.
//!
//! These pools come in 7 different capacity flavours: 16, 32, 64, 128, 256, 512 and 1024.
//! These pools cannot exceed their maximum capacity.
pub use SizedPool16;
pub use SizedPool32;
pub use SizedPool64;
pub use SizedPool128;
pub use SizedPool256;
pub use SizedPool512;
pub use SizedPool1024;
/// The StackPool trait is used by this module to communicate with the different pool
/// types on a polymorphic level.
///
/// It was not meant to be public, but hiding the ugly
/// beast would dissapoint the compiler when used externally.
/// Loop through all pushed items and edit them using a callback handler.
///
/// # Example
/// ```
/// use swarm_pool::tools::sized_pool::SizedPool16;
/// use swarm_pool::tools::sized_pool;
///
/// fn main() {
/// let mut pool: SizedPool16<u8> = SizedPool16::new();
/// sized_pool::push(&mut pool, 1);
/// sized_pool::push(&mut pool, 2);
/// sized_pool::push(&mut pool, 3);
///
/// sized_pool::for_each(&mut pool, |item| *item *= 2 );
///
/// assert_eq!(sized_pool::get_ref(&pool, 0), &Some(2));
/// assert_eq!(sized_pool::get_ref(&pool, 1), &Some(4));
/// assert_eq!(sized_pool::get_ref(&pool, 2), &Some(6));
/// }
/// ```
/// Find the position of an item within the pool. Returns Some(usize)
/// if the callback handler returns true or None if no callbacks returned a success
///
/// # Example
/// ```
/// use swarm_pool::tools::sized_pool::SizedPool16;
/// use swarm_pool::tools::sized_pool;
/// use swarm_pool::tools::byte_str::ByteStr;
///
/// fn main() {
/// let mut pool: SizedPool16<u8> = SizedPool16::new();
/// sized_pool::push(&mut pool, "A".as_bytes()[0]); // index position 0
/// sized_pool::push(&mut pool, "B".as_bytes()[0]); // index position 1
/// sized_pool::push(&mut pool, "C".as_bytes()[0]); // index position 2
///
/// let position = sized_pool::position(&mut pool, |item| *item == "B".as_bytes()[0] );
///
/// assert_eq!(&position, &Some(1)); // the position of "B" is 1
/// assert_eq!(sized_pool::get_ref(&pool, position.unwrap()).unwrap(), "B".as_bytes()[0]);
/// }
/// ```
/// Find the position of an item within the pool. Returns Some(usize)
/// if the item was found, else it returns None.
///
/// # Example
/// ```
/// use swarm_pool::tools::sized_pool::SizedPool16;
/// use swarm_pool::tools::sized_pool;
/// use swarm_pool::tools::byte_str::ByteStr;
///
/// fn main() {
/// let mut pool: SizedPool16<u8> = SizedPool16::new();
/// sized_pool::push(&mut pool, "A".as_bytes()[0]); // index position 0
/// sized_pool::push(&mut pool, "B".as_bytes()[0]); // index position 1
/// sized_pool::push(&mut pool, "C".as_bytes()[0]); // index position 2
///
/// let position = sized_pool::find(&mut pool, &"B".as_bytes()[0]);
///
/// assert_eq!(&position, &Some(1)); // the position of "B" is 1
/// assert_eq!(sized_pool::get_ref(&pool, position.unwrap()).unwrap(), "B".as_bytes()[0]);
/// }
/// ```
/// Loop through all items and keep looping until the callback handler returns false.
/// Returns the position where the loop was interrupted or None if all
/// callback handlers returned true.
///
/// # Example
/// ```
/// use swarm_pool::tools::sized_pool::SizedPool16;
/// use swarm_pool::tools::sized_pool;
///
/// fn main() {
/// let mut pool: SizedPool16<u8> = SizedPool16::new();
/// sized_pool::push(&mut pool, 10); // index position 0
/// sized_pool::push(&mut pool, 20); // index position 1
/// sized_pool::push(&mut pool, 30); // index position 2
///
/// let position = sized_pool::for_while(&mut pool, |item| *item < 25);
///
/// assert_eq!(&position, &Some(2)); // at index position '2' the loop was interrupted
/// }
/// ```
/// Add a new item to the pool.
///
/// # Example
/// ```
/// use swarm_pool::tools::sized_pool::SizedPool16;
/// use swarm_pool::tools::sized_pool;
///
/// fn main() {
/// let mut pool: SizedPool16<u8> = SizedPool16::new();
/// sized_pool::push(&mut pool, 10); // index position 0
/// sized_pool::push(&mut pool, 20); // index position 1
///
/// assert_eq!(sized_pool::get_ref(&pool, 0), &Some(10));
/// assert_eq!(sized_pool::get_ref(&pool, 1), &Some(20));
/// }
/// ```
/// Removes the last item from the pool and returns that item.
///
/// # Example
/// ```
/// use swarm_pool::tools::sized_pool::SizedPool16;
/// use swarm_pool::tools::sized_pool;
///
/// fn main() {
/// let mut pool: SizedPool16<u8> = SizedPool16::new();
/// sized_pool::push(&mut pool, 10); // index position 0
/// sized_pool::push(&mut pool, 20); // index position 1
/// assert_eq!(sized_pool::count(&pool), &2);
///
/// let popped_item = sized_pool::pop(&mut pool);
///
/// assert_eq!(sized_pool::count(&pool), &1);
/// assert_eq!(popped_item, Some(20));
/// }
/// ```
/// Removes the first item from the pool and returns that item.
///
/// # Example
/// ```
/// use swarm_pool::tools::sized_pool::SizedPool16;
/// use swarm_pool::tools::sized_pool;
///
/// fn main() {
/// let mut pool: SizedPool16<u8> = SizedPool16::new();
/// sized_pool::push(&mut pool, 10); // index position 0
/// sized_pool::push(&mut pool, 20); // index position 1
/// assert_eq!(sized_pool::count(&pool), &2);
///
/// let shifted_item = sized_pool::shift(&mut pool);
///
/// assert_eq!(sized_pool::count(&pool), &1);
/// assert_eq!(shifted_item, Some(10));
/// }
/// ```
/// Returns the number of items in the pool.
///
/// # Example
/// ```
/// use swarm_pool::tools::sized_pool::SizedPool16;
/// use swarm_pool::tools::sized_pool;
///
/// fn main() {
/// let mut pool: SizedPool16<u8> = SizedPool16::new();
/// assert_eq!(sized_pool::count(&pool), &0);
///
/// sized_pool::push(&mut pool, 10); // index position 0
/// assert_eq!(sized_pool::count(&pool), &1);
///
/// sized_pool::push(&mut pool, 20); // index position 1
/// assert_eq!(sized_pool::count(&pool), &2);
/// }
/// ```
/// Remove an item at position.
/// Returns the removed item or None if it wans't found.
///
/// # Example
/// ```
/// use swarm_pool::tools::sized_pool::SizedPool16;
/// use swarm_pool::tools::sized_pool;
///
/// fn main() {
/// let mut pool: SizedPool16<u8> = SizedPool16::new();
/// sized_pool::push(&mut pool, 10); // index position 0
/// sized_pool::push(&mut pool, 20); // index position 1
/// sized_pool::push(&mut pool, 30); // index position 2
///
/// let position = sized_pool::position(&pool, |item| *item == 20);
/// assert_eq!(position, Some(1), "expected position to be Some(1)");
///
/// let removed = sized_pool::remove_at(&mut pool, position.unwrap());
/// assert_eq!(removed, Some(20), "expected removed to be Some(20)");
///
/// assert_eq!(sized_pool::count(&pool), &2, "expected pool count to be 2");
/// assert_eq!(sized_pool::get_ref(&pool, 2), &None, "expected item 2 to be None");
/// assert_eq!(sized_pool::get_ref(&pool, 1), &Some(30), "expected item 1 to be Some(30)"); // second position should point to the last item
///
/// let find_20 = sized_pool::position(&pool, |item| *item == 20);
/// assert_eq!(find_20, None);
/// }
/// ```
/// Remove an item that equals another item.
/// Returns the removed item or None if it wans't found.
///
/// # Example
/// ```
/// use swarm_pool::tools::sized_pool::SizedPool16;
/// use swarm_pool::tools::sized_pool;
///
/// fn main() {
/// let mut pool: SizedPool16<u8> = SizedPool16::new();
/// sized_pool::push(&mut pool, 10); // index position 0
/// sized_pool::push(&mut pool, 20); // index position 1
/// sized_pool::push(&mut pool, 30); // index position 2
///
/// let position = sized_pool::position(&pool, |item| *item == 20);
/// let removed = sized_pool::remove_equal(&mut pool, 20);
///
/// assert_eq!(removed, Some(20));
/// assert_eq!(sized_pool::count(&pool), &2);
/// assert_eq!(sized_pool::get_ref(&pool, 1), &Some(30)); // second position should point to the last item
///
/// let find_20 = sized_pool::position(&pool, |item| *item == 20);
/// assert_eq!(find_20, None);
/// }
/// ```
/// Returns a mutable reference of an item from the pool at position.
///
/// # Example
/// ```
/// use swarm_pool::tools::sized_pool::SizedPool16;
/// use swarm_pool::tools::sized_pool;
///
/// fn main() {
/// let mut pool: SizedPool16<u8> = SizedPool16::new();
/// sized_pool::push(&mut pool, 10); // index position 0
/// sized_pool::push(&mut pool, 20); // index position 1
///
/// let get_item_0 = sized_pool::get_item(&mut pool, 0);
/// assert_eq!(get_item_0, &mut Some(10));
///
/// let get_item_1 = sized_pool::get_ref(&pool, 1);
/// assert_eq!(get_item_1, &Some(20));
/// }
/// ```
/// Returns a immutable reference of an item from the pool at position.
///
/// # Example
/// ```
/// use swarm_pool::tools::sized_pool::SizedPool16;
/// use swarm_pool::tools::sized_pool;
///
/// fn main() {
/// let mut pool: SizedPool16<u8> = SizedPool16::new();
/// sized_pool::push(&mut pool, 10); // index position 0
/// sized_pool::push(&mut pool, 20); // index position 1
///
/// let get_item_0 = sized_pool::get_item(&mut pool, 0);
/// assert_eq!(get_item_0, &mut Some(10));
///
/// let get_item_1 = sized_pool::get_ref(&pool, 1);
/// assert_eq!(get_item_1, &Some(20));
/// }
/// ```
/// Swap the positions of two items in the pool.
///
/// # Example
/// ```
/// use swarm_pool::tools::sized_pool::SizedPool16;
/// use swarm_pool::tools::sized_pool;
///
/// fn main() {
/// let mut pool: SizedPool16<u8> = SizedPool16::new();
/// sized_pool::push(&mut pool, 10); // index position 0
/// sized_pool::push(&mut pool, 20); // index position 1
/// sized_pool::push(&mut pool, 30); // index position 2
///
/// assert_eq!(sized_pool::get_ref(&pool, 0), &Some(10));
/// assert_eq!(sized_pool::get_ref(&pool, 1), &Some(20));
/// assert_eq!(sized_pool::get_ref(&pool, 2), &Some(30));
///
/// sized_pool::swap(&mut pool, &1, &2);
///
/// assert_eq!(sized_pool::get_ref(&pool, 0), &Some(10));
/// assert_eq!(sized_pool::get_ref(&pool, 1), &Some(30));
/// assert_eq!(sized_pool::get_ref(&pool, 2), &Some(20));
///
/// sized_pool::swap(&mut pool, &0, &2);
///
/// assert_eq!(sized_pool::get_ref(&pool, 0), &Some(20));
/// assert_eq!(sized_pool::get_ref(&pool, 1), &Some(30));
/// assert_eq!(sized_pool::get_ref(&pool, 2), &Some(10));
///
/// sized_pool::swap(&mut pool, &0, &1);
///
/// assert_eq!(sized_pool::get_ref(&pool, 0), &Some(30));
/// assert_eq!(sized_pool::get_ref(&pool, 1), &Some(20));
/// assert_eq!(sized_pool::get_ref(&pool, 2), &Some(10));
/// }
/// ```