1 /*
2  * CEventQueue.hh
3  *
4  * Copyright 2014-2018 D. Mitch Bailey  cvc at shuharisystem dot com
5  *
6  * This file is part of cvc.
7  *
8  * cvc is free software: you can redistribute it and/or modify
9  * it under the terms of the GNU General Public License as published by
10  * the Free Software Foundation, either version 3 of the License, or
11  * (at your option) any later version.
12  *
13  * cvc is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16  * GNU General Public License for more details.
17  *
18  * You should have received a copy of the GNU General Public License
19  * along with cvc.  If not, see <http://www.gnu.org/licenses/>.
20  *
21  * You can download cvc from https://github.com/d-m-bailey/cvc.git
22  */
23 
24 #ifndef CEVENTQUEUE_HH_
25 #define CEVENTQUEUE_HH_
26 
27 #include "Cvc.hh"
28 
29 #include "CVirtualNet.hh"
30 #include "CPower.hh"
31 #include "CConnection.hh"
32 
33 enum queuePosition_t {QUEUE_HIZ = -3, SKIP_QUEUE, MOS_DIODE, MAIN_BACK, DELAY_FRONT, DELAY_BACK};
34 
35 #define DefaultQueuePosition_(flag, queue) (((flag) || queue.queueType == SIM_QUEUE) ? MAIN_BACK : DELAY_FRONT)
36 
37 class CEventList : public pair<deviceId_t, deviceId_t> {
38 public:
39 	vector<deviceId_t>& queueArray;
40 	size_t eventListSize = 0;
41 
CEventList(vector<deviceId_t> & theQueueArray)42 	CEventList(vector<deviceId_t>& theQueueArray) : queueArray(theQueueArray) { first = second = UNKNOWN_DEVICE; }
43 	void push_back(deviceId_t theDevice);
44 	void push_front(deviceId_t theDevice);
45 	deviceId_t pop_front();
46 	deviceId_t front();
size()47 	inline size_t size() {return eventListSize;};
48 	bool empty();
49 };
50 
51 class CLeakList : public forward_list<deviceId_t> {
52 public:
53 	int listSize = 0;
push_front(deviceId_t theDeviceId)54 	void push_front(deviceId_t theDeviceId) {
55 		listSize++;
56 		forward_list<deviceId_t>::push_front(theDeviceId);
57 	}
58 	void Print(string theIndentation = "");
59 };
60 
61 class CLeakMap : public map<string, CLeakList> {
62 public:
63 	CPowerPtrList * powerPtrList_p = NULL;
64 
65 	string PrintLeakKey(string theKey);
66 	void Print(string theIndentation = "");
67 };
68 
69 class CEventSubQueue : public map<eventKey_t, CEventList> {
70 public:
71 	vector<deviceId_t>& queueArray;
72 
CEventSubQueue(vector<deviceId_t> & theQueueArray)73 	CEventSubQueue(vector<deviceId_t>& theQueueArray) : queueArray(theQueueArray) {}
74 	CEventList& operator[] (eventKey_t theEventKey);
75 	eventKey_t QueueTime(eventQueue_t theQueueType);
76 };
77 
78 class CEventQueue {
79 public:
80 	vector<deviceId_t> queueArray;
81 
82 	const eventQueue_t	queueType;
83 	const deviceStatus_t		inactiveBit;
84 	const deviceStatus_t		pendingBit;
85 	CVirtualNetVector& virtualNet_v;
86 	CPowerPtrVector& netVoltage_v;
87 	CLeakMap  leakMap;
88 
89 	CEventSubQueue	mainQueue;
90 	CEventSubQueue	delayQueue;
91 
92 	bool	queueStart = false;
93 
94 	long	enqueueCount = 0;
95 	long	dequeueCount = 0;
96 	long	requeueCount = 0;
97 	int		printCounter = 1000000;
98 
CEventQueue(eventQueue_t theQueueType,deviceStatus_t theInactiveBit,deviceStatus_t thePendingBit,CVirtualNetVector & theVirtualNet_v,CPowerPtrVector & theNetVoltage_v)99 	CEventQueue(eventQueue_t theQueueType, deviceStatus_t theInactiveBit, deviceStatus_t thePendingBit, CVirtualNetVector& theVirtualNet_v, CPowerPtrVector& theNetVoltage_v) :
100 		queueType(theQueueType), inactiveBit(theInactiveBit), pendingBit(thePendingBit), virtualNet_v(theVirtualNet_v), netVoltage_v(theNetVoltage_v), mainQueue(queueArray), delayQueue(queueArray) {}
101 	void ResetQueue(deviceId_t theDeviceCount);
102 	void AddEvent(eventKey_t theEventKey, deviceId_t theDeviceId, queuePosition_t theQueuePosition);
103 	void BackupQueue();
104 	deviceId_t GetMainEvent();
105 	deviceId_t GetDelayEvent();
106 	bool IsNextMainQueue();
107 
108 	deviceId_t GetEvent();
109 	eventKey_t QueueTime();
QueueSize()110 	inline deviceId_t QueueSize() { return (enqueueCount - dequeueCount); };
111 	bool Later(eventKey_t theEventKey);
112 	bool Later(eventKey_t theFirstKey, eventKey_t theSecondKey);
113 
114 	void AddLeak(deviceId_t theDevice, CConnection& theConnections);
115 	void Print(string theIndentation = "");
116 	void PrintStatus(int theNextPrintCount = 1000000);
117 };
118 
119 
120 #endif /* CEVENTQUEUE_HH_ */
121