File: ToDoList.h

package info (click to toggle)
fact%2B%2B 1.6.5~dfsg-3
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, bullseye, forky, sid, trixie
  • size: 4,496 kB
  • sloc: cpp: 28,000; java: 22,674; xml: 3,268; makefile: 102; ansic: 61; sh: 3
file content (341 lines) | stat: -rw-r--r-- 9,627 bytes parent folder | download | duplicates (3)
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
/* This file is part of the FaCT++ DL reasoner
Copyright (C) 2003-2015 Dmitry Tsarkov and The University of Manchester
Copyright (C) 2015-2016 Dmitry Tsarkov

This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 2.1 of the License, or (at your option) any later version.

This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
Lesser General Public License for more details.

You should have received a copy of the GNU Lesser General Public
License along with this library; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
*/

#ifndef TODOLIST_H
#define TODOLIST_H

#include "globaldef.h"
#include "fpp_assert.h"
#include "PriorityMatrix.h"
#include "tRareSaveStack.h"

/// the entry of TODO table
struct ToDoEntry
{
		/// node to include concept
	DlCompletionTree* Node;
		/// offset of included concept in Node's label
		// (it's not possible to use pointers because
		// std::vector invalidates them)
	int offset;

		/// empty C'tor
	ToDoEntry ( void ) : Node(NULL), offset(0) {}	// for initialisation
		/// C'tor (init values)
	ToDoEntry ( DlCompletionTree* n, int off ) : Node(n), offset(off) {}
}; // ToDoEntry

/// All-in-one version of arrayToDoTable
class ToDoList
{
protected:	// classes
		/// class for saving/restoring ToDoQueue
	class QueueSaveState
	{
	public:		// members
			/// save start point of queue of entries
		size_t sp;
			/// save end point of queue of entries
		size_t ep;

	public:		// methods
			/// empty c'tor
		QueueSaveState ( void ) {}
			/// empty d'tor
		~QueueSaveState ( void ) {}
	}; // QueueSaveState
	//--------------------------------------------------------------------------

		/// class to represent single queue
	class arrayQueue
	{
	protected:	// members
			/// waiting ops queue
		growingArray<ToDoEntry> Wait;
			/// start pointer; points to the 1st element in the queue
		size_t sPointer;

	public:		// interface
			/// c'tor: init queue with proper size and reset it
		arrayQueue ( void ) : sPointer(0)
		{
			Wait.reserve(50);	// initial size
			Wait.clear();
		}
			/// empty d'tor
		~arrayQueue ( void ) {}

			/// add entry to a queue
		void add ( DlCompletionTree* node, int offset ) { Wait.add(ToDoEntry(node,offset)); }
			/// clear queue
		void clear ( void ) { sPointer = 0; Wait.clear(); }
			/// check if queue empty
		bool empty ( void ) const { return sPointer == Wait.size(); }
			/// get next entry from the queue; works for non-empty queues
		const ToDoEntry* get ( void ) { return &(Wait[sPointer++]); }

			/// save queue content to the given entry
		void save ( QueueSaveState& tss ) const
		{
			tss.sp = sPointer;
			tss.ep = Wait.size();
		}
			/// restore queue content from the given entry
		void restore ( const QueueSaveState& tss )
		{
			sPointer = tss.sp;
			Wait.resize(tss.ep);
		}
	}; // arrayQueue
	//--------------------------------------------------------------------------

		/// class to represent single priority queue
	class queueQueue
	{
	protected:	// types
			/// type for restore the whole queue
		class QueueRestorer: public TRestorer
		{
		protected:	// members
				/// copy of a queue
			growingArray<ToDoEntry> Wait;
				/// pointer to a queue to restore
			queueQueue* queue;
				/// start pointer
			size_t sp;

		public:		// interface
				/// init c'tor
			QueueRestorer ( queueQueue* q ) : Wait(q->Wait), queue(q), sp(q->sPointer) {}
				/// empty d'tor
			virtual ~QueueRestorer ( void ) {}
				/// restore: copy the queue back, adjust pointers
			virtual void restore ( void ) { queue->Wait = Wait; queue->sPointer = sp; }
		};

	protected:	// members
			/// waiting ops queue
		growingArray<ToDoEntry> Wait;
			/// stack to save states for the overwritten queue
		TRareSaveStack* stack;
			/// start pointer; points to the 1st element in the queue
		size_t sPointer;

	public:		// interface
			/// c'tor: make an empty queue
		queueQueue ( TRareSaveStack* s ) : stack(s), sPointer(0) {}
			/// empty d'tor
		~queueQueue ( void ) {}

			/// add entry to a queue
		void add ( DlCompletionTree* Node, int offset )
		{
			ToDoEntry e(Node,offset);
			if ( empty() ||	// no problems with empty queue and if no priority clashes
				 Wait[Wait.size()-1].Node->getNominalLevel() <= Node->getNominalLevel() )
			{
				Wait.add(e);
				return;
			}

			// here we need to put e on the proper place
			stack->push(new QueueRestorer(this));
			size_t n = Wait.size();
			Wait.add(e);	// will be rewritten
			while ( n > sPointer && Wait[n-1].Node->getNominalLevel() > Node->getNominalLevel() )
			{
				Wait[n] = Wait[n-1];
				--n;
			}
			Wait[n] = e;
		}
			/// clear queue
		void clear ( void ) { sPointer = 0; Wait.clear(); }
			/// check if queue empty
		bool empty ( void ) const { return sPointer == Wait.size(); }
			/// get next entry from the queue; works for non-empty queues
		const ToDoEntry* get ( void ) { return &(Wait[sPointer++]); }

			/// save queue content to the given entry
		void save ( QueueSaveState& tss )
		{
			tss.sp = sPointer;
			tss.ep = Wait.size();
		}
			/// restore queue content from the given entry
		void restore ( const QueueSaveState& tss )
		{
			sPointer = tss.sp;
			Wait.resize(tss.ep);
		}
	}; // queueQueue
	//--------------------------------------------------------------------------

protected:	// internal typedefs
		/// typedef for NN-queue (which should support complete S/R)
	typedef queueQueue NNQueue;

protected:	// classes
		/// class for saving/restoring array TODO table
	class SaveState
	{
	public:		// members
			/// save state for queueID
		QueueSaveState backupID;
			/// save state for queueNN
		QueueSaveState backupNN;
			/// save state of all regular queues
		QueueSaveState backup[nRegularOps];
			/// save number-of-entries to do
		unsigned int noe;

	public:		// methods
			/// empty c'tor
		SaveState ( void ) {}
			/// empty d'tor
		~SaveState ( void ) {}
	}; // SaveState
	//--------------------------------------------------------------------------

private:	// safety
		/// no copy c'tor
	ToDoList ( ToDoList& );
		/// no assignment
	ToDoList& operator = ( ToDoList& );

protected:	// members
		/// waiting ops queue for IDs
	arrayQueue queueID;
		/// waiting ops queue for <= ops in nominal nodes
	NNQueue queueNN;
		/// waiting ops queues
	arrayQueue Wait[nRegularOps];
		/// stack of saved states
	TSaveStack<SaveState> SaveStack;
		/// priority matrix
	const ToDoPriorMatrix& Matrix;
		/// number of un-processed entries
	unsigned int noe;

protected:	// methods
		/// save current TODO table content to given saveState entry
	void saveState ( SaveState* tss )
	{
		queueID.save(tss->backupID);
		queueNN.save(tss->backupNN);
		for ( ToDoListIndex i = 0; i < nRegularOps; ++i )
			Wait[i].save(tss->backup[i]);

		tss->noe = noe;
	}
		/// restore TODO table content from given saveState entry
	void restoreState ( const SaveState* tss )
	{
		queueID.restore(tss->backupID);
		queueNN.restore(tss->backupNN);
		for ( ToDoListIndex i = 0; i < nRegularOps; ++i )
			Wait[i].restore(tss->backup[i]);

		noe = tss->noe;
	}

public:
		/// init c'tor
	ToDoList ( const ToDoPriorMatrix& matrix, TRareSaveStack* stack ) : queueNN(stack), Matrix(matrix), noe(0) {}
		/// d'tor: delete all entries
	~ToDoList ( void ) { clear(); }

	// global methods

		/// clear TODO table
	void clear ( void )
	{
		queueID.clear();
		queueNN.clear();
		for ( ToDoListIndex i = 0; i < nRegularOps; ++i )
			Wait[i].clear();

		SaveStack.clear();
		noe = 0;
	}
		/// check if TODO table is empty
	bool empty ( void ) const { return !noe; }

	// work with entries

		/// add entry with given NODE and CONCEPT with given OFFSET to the TODO table
	void addEntry ( DlCompletionTree* node, DagTag type, const ConceptWDep& C, int offset )
	{
		ToDoListIndex index = Matrix.getIndex ( type, isPositive(C.bp()), node->isNominalNode() );
		switch ( index )
		{
		case nRegularOps:	// unused entry
			return;
		case iId:			// ID
			queueID.add(node,offset); break;
		case iNN:			// NN
			queueNN.add(node,offset); break;
		default:			// regular queue
			Wait[index].add(node,offset); break;
		}
		++noe;
	}
		/// add entry with given NODE and CONCEPT of a TYPE to the ToDo table
	void addEntry ( DlCompletionTree* node, DagTag type, const ConceptWDep& C )
		{ addEntry ( node, type, C, node->label().getLast(type) ); }
		/// get the next TODO entry. @return NULL if the table is empty
	const ToDoEntry* getNextEntry ( void );

	// save/restore methods

		/// save current state using internal stack
	void save ( void ) { saveState(SaveStack.push()); }
		/// restore state using internal stack
	void restore ( void ) { fpp_assert ( !SaveStack.empty() ); restoreState(SaveStack.pop()); }
		/// restore state to the given level using internal stack
	void restore ( unsigned int level ) { restoreState(SaveStack.pop(level)); }
}; // ToDoList

inline const ToDoEntry* ToDoList :: getNextEntry ( void )
{
#ifdef ENABLE_CHECKING
	fpp_assert ( !empty () );	// safety check
#endif

	// decrease amount of elements-to-process
	--noe;

	// check ID queue
	if ( !queueID.empty() )
		return queueID.get();

	// check NN queue
	if ( !queueNN.empty() )
		return queueNN.get();

	// check regular queues
	for ( ToDoListIndex i = 0; i < nRegularOps; ++i )
		if ( !Wait[i].empty() )
			return Wait[i].get();

	// that's impossible, but still...
	return NULL;
}

#endif