tzh
2024-08-22 c7d0944258c7d0943aa7b2211498fd612971ce27
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
/*!****************************************************************************
 
 @file         PVRTSkipGraph.h
 @copyright    Copyright (c) Imagination Technologies Limited.
 @brief        A "tree-like" structure for storing data which, unlike a tree, can
               reference any other node.
 
******************************************************************************/
#ifndef __PVRTSKIPGRAPH_H__
#define __PVRTSKIPGRAPH_H__
 
 
#include "PVRTArray.h"
#include "PVRTHash.h"
 
/*!***************************************************************************
 @class                CPVRTSkipGraphNode
 @brief              Stores a pointer to the node's data and also uses a dynamic
                   array to store pointer to nodes this node depends on and
                   another to store pointers to nodes that are dependant on this node
*****************************************************************************/
template<class T>
class CPVRTSkipGraphNode
{
private:
   T                                m_pData;
   CPVRTArray<CPVRTSkipGraphNode*> m_apDependencies;    // What I depend on
   CPVRTArray<CPVRTSkipGraphNode*> m_apDependents;        // What depends on me
   
public:
   /*!***************************************************************************
   @brief          Constructor
   *****************************************************************************/
   CPVRTSkipGraphNode()
   {}
 
   /*!***************************************************************************
   @brief          Overloaded constructor
   @param[in]        data    Pointer to a node
   *****************************************************************************/
   CPVRTSkipGraphNode(const T& data) : m_pData(data)
   {}
 
   /*!***************************************************************************
   @brief          Destructor
   *****************************************************************************/
   ~CPVRTSkipGraphNode()
   {} 
 
   /*!***************************************************************************
   @fn               GetNumDependencies
   @return            unsigned int    
   @brief          Returns the number of dependencies referenced by this node.
   *****************************************************************************/
   unsigned int GetNumDependencies() const                    
   { 
       return (unsigned int)m_apDependencies.GetSize(); 
   }
 
   /*!***************************************************************************
   @fn               GetDependency
   @param[in]            ui32Id
   @return            CPVRTSkipGraphNode &    
   @brief          Returns given dependency.
   *****************************************************************************/
   CPVRTSkipGraphNode& GetDependency(const unsigned int ui32Id) const
   { 
       _ASSERT(ui32Id >= 0 && ui32Id < (unsigned int)m_apDependencies.GetSize());
       return *m_apDependencies[ui32Id];
   }
 
 
   /*!***************************************************************************
   @fn               AddDependency
   @param[out]            pDependentNode
   @return            bool    
   @brief          Adds a dependency to this node.
   *****************************************************************************/
   bool AddDependency(CPVRTSkipGraphNode* pDependentNode)
   {
       unsigned int ui(0);
 
       if(pDependentNode == this)
           return false;
 
       if(!pDependentNode)
           return false;
 
       /*
           Check the dependency doesn't already exist
       */
       for(ui = 0; ui < (unsigned int)m_apDependencies.GetSize(); ++ui)
       {
           if(m_apDependencies[ui] == pDependentNode)
           {
               return true;
           }
       }
 
       /*
           Add the dependency and also set this node as a dependent of
           the referenced node
       */
       m_apDependencies.Append(pDependentNode);
       pDependentNode->AddDependent(this);
       
       return true;
   }
 
   /*!***************************************************************************
   @fn               GetData
   @return            T &    
   @brief          Returns the data associated with this node.
   *****************************************************************************/
   T& GetData()                
   {
       return m_pData;
   }
 
private:
   /*!***************************************************************************
   @fn               AddDependent
   @param[out]            pDependancyNode
   @return            bool    
   @brief          Adds a dependent to this node.
   *****************************************************************************/
   bool AddDependent(CPVRTSkipGraphNode* pDependencyNode)
   {
       unsigned int ui(0);
 
       if(!pDependencyNode)
           return false;
 
       /*
           Check the dependency doesn't already exist
       */
       for(ui = 0; ui < (unsigned int)m_apDependents.GetSize(); ++ui)
       {
           if(m_apDependencies[ui] == pDependencyNode)
           {
               return true;
           }
       }
 
       /*
           Add the dependancy
       */
       m_apDependents.Append(pDependencyNode);
       return true;
   }
};
 
/*!***************************************************************************
 @class                CPVRTSkipGraphRoot
 @brief              This class is the entry point for creating and accessing
                   the elements of a skip graph. It uses a hash table to store
                   the nodes of the structure and a hash value that allows
                   fast searching of the skip graph
*****************************************************************************/
template<class T>
class CPVRTSkipGraphRoot
{
//-------------------------------------------------------------------------//
private:
 
   /*!***************************************************************************
    @struct        SPVRTHashElement
    @brief          A struct to store data and a hash value generated from the
                   data. The hash value allows faster searching of the skip graph.
   *****************************************************************************/
   struct SPVRTHashElement
   {        
   public:
       /*!***************************************************************************
       @fn               SPVRTHashElement
       @param[in]            hash
       @param[in]            data
       @brief          Overloaded constructor.
       *****************************************************************************/
       SPVRTHashElement(const CPVRTHash& hash, const T& data)
       :
       m_hash(hash),
       m_skipGraphNode(data)
       {}
 
       /*!***************************************************************************
       @fn               SPVRTHashElement
       @brief          Constructor
       *****************************************************************************/
       SPVRTHashElement()
       {}
 
       /*!***************************************************************************
       @fn               ~SPVRTHashElement
       @brief          Destructor
       *****************************************************************************/
       ~SPVRTHashElement()
       {}
       
       /*!***************************************************************************
       @fn               GetHash
       @return            unsigned int    
       @brief          Returns the element's hash value.
       *****************************************************************************/
       const CPVRTHash& GetHash() const                
       { 
           return m_hash;
       }
 
       /*!***************************************************************************
       @fn               GetNode
       @return            CPVRTSkipGraphNode<T>&    
       @brief          Return the node associated with this element.
       *****************************************************************************/
       CPVRTSkipGraphNode<T>& GetNode()            
       {
           return m_skipGraphNode; 
       }
 
       /*!***************************************************************************
       @fn               GetNode
       @return            CPVRTSkipGraphNode<T>&    
       @brief          Return the node associated with this element.
       *****************************************************************************/
       const CPVRTSkipGraphNode<T>& GetNode() const            
       {
           return m_skipGraphNode; 
       }
 
   private:
       CPVRTHash                m_hash;
       CPVRTSkipGraphNode<T>    m_skipGraphNode;
   };
 
   
   CPVRTArray<SPVRTHashElement>        m_aHashTable;
 
//-------------------------------------------------------------------------//
public:
 
   /*!***************************************************************************
    @fn                   AddNode
    @param[in]            data                            The data of the node to be added
    @return            CPVRTSkipGraphNode<T>* const    A handle to the added node
    @brief              Searches through the hash table to see if the added node already
                       exists. If it doesn't, it creates a node.
                       The function returns true if the node was found or was created
                       successfully.
   *****************************************************************************/
   bool AddNode(const T& data)
   {
       CPVRTHash NewNodeHash((void*)&data, sizeof(T), 1);
       int iArrayElement(-1);
       /*
           First, search the hash table to see
           if the node already exists
       */
       CPVRTSkipGraphNode<T>* skipGraphNode(FindNode(NewNodeHash));
 
       if(skipGraphNode == NULL)
       {
           /*
               The node wasn't found, so a new node needs to be
               created
           */
           iArrayElement = m_aHashTable.Append(SPVRTHashElement(NewNodeHash, data));
 
           /*
               Now point to the new instance
           */
           skipGraphNode = &m_aHashTable[iArrayElement].GetNode();
       }
 
       return skipGraphNode ? true : false;
   }
 
 
   /*!***************************************************************************
   @brief          Adds a node dependency.
   @param[in]        nodeData
   @param[in]        dependantData
   @return            bool    
   *****************************************************************************/
   bool AddNodeDependency(const T& nodeData, const T& dependantData)
   {
       CPVRTSkipGraphNode<T>* pNode(NULL);
       CPVRTSkipGraphNode<T>* pDependantNode(NULL);
 
       pNode = FindNode(nodeData);
       if(!pNode)
       {
           return false;
       }
 
       pDependantNode = FindNode(dependantData);
       if(!pDependantNode)
       {
           return false;
       }
 
       /*
           Nodes are not allowed to self reference
       */
       if(pNode == pDependantNode)
       {
           return false;
       }
       pNode->AddDependency(pDependantNode);
 
       return true;
   }
 
   /*!***************************************************************************
    @fn                   GetNumNodes
    @return            unsigned int    The total number of nodes
    @brief              Returns the total number of nodes in the skip graph.
   *****************************************************************************/
   unsigned int GetNumNodes() const
   {
       return (unsigned int)m_aHashTable.GetSize();
   }
 
   /*!***************************************************************************
    @brief              Returns a sorted list of dependencies for the specified
                       node. The list is ordered with the leaf nodes at the front,
                       followed by nodes that depend on them and so forth until
                       the root node is reached and added at the end of the list
    @param[in]            aOutputArray    The dynamic array to store
                                       the sorted results in
    @param[in]            ui32NodeID        The ID of the root node for
                                       the dependency search
   *****************************************************************************/
   void RetreiveSortedDependencyList(CPVRTArray<T> &aOutputArray, 
                                       const unsigned int ui32NodeID)
   {
       _ASSERT(ui32NodeID >= 0 && ui32NodeID < (unsigned int)m_aHashTable.GetSize());
       RecursiveSortedListAdd(aOutputArray, m_aHashTable[ui32NodeID].GetNode());
   }
 
   /*!***************************************************************************
    @brief              Overloads operator[] to returns a handle to the node data
                       for the specified ID
    @return            T&        Handle to the node data
   *****************************************************************************/
   T& operator[](const unsigned int ui32NodeID)
   {
       return *(GetNodeData(ui32NodeID));
   }
   
   /*!***************************************************************************
    @brief              Overloads operator[] to returns a const handle to the node 
                       data for the specified ID
    @return            T&        Handle to the node data
   *****************************************************************************/
   const T& operator[](const unsigned int ui32NodeID) const
   {
       return *(GetNodeData(ui32NodeID));
   }
 
//-------------------------------------------------------------------------//
private:
   /*!***************************************************************************
    @brief              Recursively adds node dependancies to aOutputArray.
                       By doing so, aOutputArray will be ordered from leaf nodes to
                       the root node that started the recursive chain
    @param[in]            aOutputArray    The dynamic array to store
                                       the sorted results in
    @param[in]            currentNode        The current node to process
   *****************************************************************************/
   void RecursiveSortedListAdd(CPVRTArray<T> &aOutputArray,
                               CPVRTSkipGraphNode<T> &currentNode)
   {
       unsigned int ui(0);
 
       /*
           Recursively add dependancies first
       */
       for(ui = 0; ui < currentNode.GetNumDependencies(); ++ui)
       {
           RecursiveSortedListAdd(aOutputArray, currentNode.GetDependency(ui));
       }
 
       /*
           Then add this node to the array
       */
       aOutputArray.Append(currentNode.GetData());
   }
 
   /*!***************************************************************************
    @fn                   GetNodeData
    @param[in,out]     ui32NodeID        The node's ID
    @return            T*                A handle to node's data
    @brief              Retrieve a handle to the specified node's data
   *****************************************************************************/
   T* GetNodeData(unsigned int ui32NodeID)
   {
       _ASSERT(ui32NodeID >= 0 && ui32NodeID < (unsigned int)m_aHashTable.GetSize());
       return &m_aHashTable[ui32NodeID].GetNode().GetData();
   }
   
   /*!***************************************************************************
    @brief              Use the input hash to search the hash table and see if the
                       node already exists. If it does, the function returns a pointer
                       to the node. If it doesn't, it returns NULL
    @param[in,out]     ui32Hash                        The hash value to search with
    @return            CPVRTSkipGraphNode<T>* const    A handle to the found node
   *****************************************************************************/
   CPVRTSkipGraphNode<T>* FindNode(const CPVRTHash& Hash)
   {
       int i(0);
       int i32HashTableSize(m_aHashTable.GetSize());
 
       /*
           A NULL hash means the node has not initialised
           correctly
       */
       if(Hash == 0)
           return NULL;
 
       /*
           NOTE:
           In the future, the hash table could be sorted from
           lowest hash value to highest so that binary search could
           be used to find a given node. It should be possible to
           use a bool (or some other mechanism) to toggle this form of search
           (if the skip graph is small, it might be faster to just use a brute
           force for loop to search through)
       */
       for(i = 0; i < i32HashTableSize; i++)
       {
           if(m_aHashTable[i].GetHash() == Hash)
           {
               return &m_aHashTable[i].GetNode();
           }
       }
 
       /*
           The element wasn't found, so return null
       */
       return NULL;
   }
 
   /*!***************************************************************************
    @brief              Use the input data to generate a hash and then
                       search the hash table and see if the node already exists.
                       If it does, the function returns a pointer
                       to the node. If it doesn't, it returns NULL
    @param[in,out]     data                            Data to use as a source for the hash
    @return            CPVRTSkipGraphNode<T>* const    A handle to the found node
   *****************************************************************************/
   CPVRTSkipGraphNode<T>* FindNode(const T& data)
   {
       CPVRTHash inputHash((void*)&data, sizeof(T), 1);    // Generate hash for searching
       return FindNode(inputHash);
   }
};
 
#endif //__PVRTSKIPGRAPH_H__
 
/*****************************************************************************
 End of file (PVRTSkipGraph.h)
*****************************************************************************/