@@ -353,6 +353,146 @@ class Gap :
353
353
/* ===================================================================== */
354
354
/* private implementation follows */
355
355
356
+ private:
357
+ /* * List in random order */
358
+ template <typename EventType, typename IndexType, IndexType MAX_EVENTS>
359
+ class EventList {
360
+ public:
361
+ EventList ()
362
+ {
363
+ };
364
+
365
+ ~EventList ()
366
+ {
367
+ for (IndexType i = 0 ; i < _current_size; ++i) {
368
+ delete _pointers[i];
369
+ }
370
+ };
371
+
372
+ /* * Add event to the list. List takes ownership of memory.
373
+ *
374
+ * @param event List will point to this event.
375
+ * @return False if list full.
376
+ */
377
+ bool push (EventType *event)
378
+ {
379
+ if (_current_size < MAX_EVENTS) {
380
+ _pointers[_current_size] = event;
381
+ _current_size++;
382
+ return true ;
383
+ }
384
+ return false ;
385
+ };
386
+
387
+ /* * Take one entry of the list. Transfers ownership to caller.
388
+ *
389
+ * @return The event return. Memory belongs to caller.
390
+ */
391
+ EventType* pop ()
392
+ {
393
+ MBED_ASSERT (_current_size);
394
+
395
+ if (!_current_size) {
396
+ return nullptr ;
397
+ }
398
+
399
+ EventType* event_returned = _pointers[_current_index];
400
+
401
+ _current_size--;
402
+ if (_current_size != _current_index) {
403
+ _pointers[_current_index] = _pointers[_current_size];
404
+ } else {
405
+ _current_index = 0 ;
406
+ }
407
+
408
+ return event_returned;
409
+ };
410
+
411
+ /* * Return pointer to the first element that fulfills the passed in condition and remove the entry
412
+ * that was pointing to the item. Transfers ownership to caller.
413
+ *
414
+ * @param compare_func The condition that is checked for all the items.
415
+ * @return First element that fulfills the passed in condition or nullptr if no such item found.
416
+ */
417
+ EventType* pop (mbed::Callback<bool (EventType&)> compare_func)
418
+ {
419
+ for (IndexType i = 0 ; i < _current_size ; ++i) {
420
+ if (compare_func (*_pointers[_current_index])) {
421
+ return pop ();
422
+ }
423
+ increment_current_index ();
424
+ }
425
+
426
+ return nullptr ;
427
+ }
428
+
429
+ /* * Return pointer to the first element that fulfills the passed in condition and remove the entry
430
+ * that was pointing to the item. Takes and returns number of failed matches allowing to speed up search.
431
+ * Transfers ownership to caller.
432
+ *
433
+ * @note Calls must be consecutive - any call to pop or find will invalidate the search.
434
+ *
435
+ * @param compare_func The condition that is checked for all the items.
436
+ * @param events_not_matching Pointer to the number of items already searched but not matching.
437
+ * This is updated in the method.
438
+ * @return First element that fulfills the passed in condition or nullptr if no such item found.
439
+ */
440
+ EventType* continue_pop (mbed::Callback<bool (EventType&)> compare_func, IndexType *events_not_matching)
441
+ {
442
+ _current_index = *events_not_matching;
443
+ for (IndexType i = *events_not_matching; i < _current_size ; ++i) {
444
+ if (compare_func (*_pointers[_current_index])) {
445
+ return pop ();
446
+ }
447
+ (*events_not_matching)++;
448
+ increment_current_index ();
449
+ }
450
+
451
+ return nullptr ;
452
+ }
453
+
454
+ /* * Return pointer to the first element that fulfills the passed in condition. Does not remove item from list.
455
+ *
456
+ * @param compare_func The condition that is checked for all the items.
457
+ * @return First element that fulfills the passed in condition or nullptr if no such item found.
458
+ */
459
+ EventType* find (mbed::Callback<bool (EventType&)> compare_func)
460
+ {
461
+ for (IndexType i = 0 ; i < _current_size ; ++i) {
462
+ if (compare_func (*_pointers[_current_index])) {
463
+ return _pointers[_current_index];
464
+ }
465
+ increment_current_index ();
466
+ }
467
+
468
+ return nullptr ;
469
+ }
470
+
471
+ /* * Return number of events stored.
472
+ *
473
+ * @return Number of events stored.
474
+ */
475
+ IndexType get_size ()
476
+ {
477
+ return _current_size;
478
+ }
479
+
480
+ private:
481
+ void increment_current_index ()
482
+ {
483
+ _current_index++;
484
+ if (_current_index == _current_size) {
485
+ _current_index = 0 ;
486
+ }
487
+ }
488
+
489
+ private:
490
+ EventType* _pointers[MAX_EVENTS];
491
+ IndexType _current_size = 0 ;
492
+ /* this helps us find the event faster */
493
+ IndexType _current_index = 0 ;
494
+ };
495
+
356
496
private:
357
497
/* Disallow copy and assignment. */
358
498
Gap (const Gap &);
0 commit comments