File: DisjointSet.h

package info (click to toggle)
intel-graphics-compiler 1.0.12504.6-1%2Bdeb12u1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 83,912 kB
  • sloc: cpp: 910,147; lisp: 202,655; ansic: 15,197; python: 4,025; yacc: 2,241; lex: 1,570; pascal: 244; sh: 104; makefile: 25
file content (270 lines) | stat: -rw-r--r-- 6,637 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
/*========================== begin_copyright_notice ============================

Copyright (C) 2019-2021 Intel Corporation

SPDX-License-Identifier: MIT

============================= end_copyright_notice ===========================*/

#pragma once

#include "Object.h"
#include "Queue.h"

namespace iSTD
{

/*****************************************************************************\
Struct: IsDisjointSetTypeSupported
\*****************************************************************************/
template<typename T>
struct IsDisjointSetTypeSupported                   { enum { value = false }; };

template<>
struct IsDisjointSetTypeSupported<bool>             { enum { value = true }; };

template<>
struct IsDisjointSetTypeSupported<char>             { enum { value = true }; };

template<>
struct IsDisjointSetTypeSupported<unsigned char>    { enum { value = true }; };

template<>
struct IsDisjointSetTypeSupported<int>              { enum { value = true }; };

template<>
struct IsDisjointSetTypeSupported<unsigned int>     { enum { value = true }; };

#ifndef __LP64__ // u/long on linux64 platform is 64-bit type and collides with U/INT64
template<>
struct IsDisjointSetTypeSupported<long>             { enum { value = true }; };

template<>
struct IsDisjointSetTypeSupported<unsigned long>    { enum { value = true }; };
#endif

template<>
struct IsDisjointSetTypeSupported<INT64>            { enum { value = true }; };

template<>
struct IsDisjointSetTypeSupported<UINT64>           { enum { value = true }; };

template<typename T>
struct IsDisjointSetTypeSupported<T*>               { enum { value = true }; };

/*****************************************************************************\
Template Parameters
\*****************************************************************************/
#define DisjointSetTemplateList class Type, class CAllocatorType
#define CDisjointSetType        CDisjointSet<Type,CAllocatorType>

/*****************************************************************************\

Class:
    CDisjointSet

Description:
    Implements a union find disjoint set

\*****************************************************************************/
template<DisjointSetTemplateList>
class CDisjointSet : public CObject<CAllocatorType>
{
public:

    CDisjointSet( void );
    virtual ~CDisjointSet( void );

    void    Union( CDisjointSetType& ds );

    const Type& Find( void );
    void    Set( const Type& item );

    C_ASSERT( IsDisjointSetTypeSupported<Type>::value == true );

protected:

    CDisjointSetType*   RootRecursive( CDisjointSetType* node );
    CDisjointSetType*   RootNonRecursive( CDisjointSetType* node );

    CDisjointSetType*   m_pParent;

    Type    m_Item;
};

/*****************************************************************************\

Function:
    CDisjointSet Constructor

Description:
    Initializes the disjoint set

Input:

Output:
    none

\*****************************************************************************/
template<DisjointSetTemplateList>
CDisjointSetType::CDisjointSet( void )
    : CObject<CAllocatorType>()
{
    m_pParent = this;
}

/*****************************************************************************\

Function:
    CDisjointSet Destructor

Description:
    Frees all internal dynamic memory

Input:
    none

Output:
    none

\*****************************************************************************/
template<DisjointSetTemplateList>
CDisjointSetType::~CDisjointSet( void )
{
    // Nothing!
}

/*****************************************************************************\

Function:
    CDisjointSet::Union

Description:
    Unions this disjoint set with the passed in disjoint set

Input:
    CDisjointSet& ds - other disjoint set

Output:
    void

\*****************************************************************************/
template<DisjointSetTemplateList>
void CDisjointSetType::Union( CDisjointSetType& ds )
{
    CDisjointSetType*   oldRoot = RootNonRecursive( this );
    CDisjointSetType*   newRoot = RootNonRecursive( &ds );

    if( oldRoot != newRoot )
    {
        oldRoot->m_pParent = newRoot;
    }
}

/*****************************************************************************\

Function:
    CDisjointSet::Find

Description:
    Finds the item in this disjoint set

Input:
    void

Output:
    const Type& - root of this disjoint set

\*****************************************************************************/
template<DisjointSetTemplateList>
const Type& CDisjointSetType::Find( void )
{
    CDisjointSetType*   root = RootNonRecursive( this );

    return root->m_Item;
}

/*****************************************************************************\

Function:
    CDisjointSet::Set

Description:
    Sets the item stored in this disjoint set

Input:
    const Type& - item stored in this disjoint set

Output:
    void

\*****************************************************************************/
template<DisjointSetTemplateList>
void CDisjointSetType::Set( const Type& item )
{
    CDisjointSetType*   root = RootNonRecursive( this );

    root->m_Item = item;
}

/*****************************************************************************\

Function:
    CDisjointSet::RootRecursive

Description:
    Returns the root node of this disjoint set.  Also performs path
    compression using recursion.

Input:
    void

Output:
    CDisjointSetType* root node

\*****************************************************************************/
template<DisjointSetTemplateList>
CDisjointSetType* CDisjointSetType::RootRecursive(
    CDisjointSetType* node )
{
    if( node->m_pParent != node )
    {
        CDisjointSetType*   root = RootRecursive( node->m_pParent );
        node->m_pParent = root;
    }

    return node;
}

/*****************************************************************************\

Function:
    CDisjointSet::RootNonRecursive

Description:
    Returns the root node of this disjoint set.  Also performs path
    compression without using recursion.  The actual path compression isn't
    quite as good as the recursive version but it's pretty good and the
    speed gains from not using recursion might be more important.

Input:
    void

Output:
    CDisjointSetType* root node

\*****************************************************************************/
template<DisjointSetTemplateList>
CDisjointSetType* CDisjointSetType::RootNonRecursive(
    CDisjointSetType* node )
{
    while( node->m_pParent != node )
    {
        node->m_pParent = node->m_pParent->m_pParent;
        node = node->m_pParent;
    }

    return node;
}

} // iSTD