PriorityList.h
Go to the documentation of this file.
1 /****
2  * Sming Framework Project - Open Source framework for high efficiency native ESP8266 development.
3  * Created 2015 by Skurydin Alexey
4  * http://github.com/SmingHub/Sming
5  * All files of the Sming Core are provided under the LGPL v3 license.
6  *
7  *
8  * @author: 2021 - Slavey Karadzhov <slaff@attachix.com>
9  *
10  ****/
11 #pragma once
12 
13 #include "LinkedObjectList.h"
14 
15 template <typename ObjectType> class PriorityNode : public LinkedObjectTemplate<PriorityNode<ObjectType>>
16 {
17 public:
19  {
20  }
21 
23  int priority;
24 };
25 
26 template <typename ObjectType> class PriorityList : public OwnedLinkedObjectListTemplate<PriorityNode<ObjectType>>
27 {
28 public:
30 
38  bool add(ObjectType object, int priority)
39  {
40  auto node = new PriorityNode<ObjectType>{object, priority};
41  if(node == nullptr) {
42  return false;
43  }
44 
45  if(List::isEmpty()) {
46  List::add(node);
47  return true;
48  }
49 
50  auto current = this->head();
51  if(current->priority < node->priority) {
52  List::insert(node);
53  return true;
54  }
55 
56  while(current->next() != nullptr && current->getNext()->priority > node->priority) {
57  current = current->getNext();
58  }
59 
60  node->insertAfter(current);
61 
62  return true;
63  }
64 };
ObjectType * head()
Definition: LinkedObjectList.h:104
bool insert(ObjectType *object)
Definition: LinkedObjectList.h:144
bool add(ObjectType *object)
Definition: LinkedObjectList.h:134
bool isEmpty() const
Definition: LinkedObjectList.h:80
Base class template for linked items with type casting.
Definition: LinkedObject.h:62
Class template for singly-linked list of objects.
Definition: LinkedObjectList.h:175
Definition: PriorityList.h:27
bool add(ObjectType object, int priority)
Adds and element and orders it according to its priority. Order is: High to low.
Definition: PriorityList.h:38
Definition: PriorityList.h:16
ObjectType data
Definition: PriorityList.h:22
PriorityNode(ObjectType data, int priority)
Definition: PriorityList.h:18
int priority
Definition: PriorityList.h:23
ObjectType
Definition: Libraries/jerryscript/src/include/Jerryscript/Types.h:34