File: SparseSet.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 (989 lines) | stat: -rw-r--r-- 21,906 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
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
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
/*========================== begin_copyright_notice ============================

Copyright (C) 2017-2021 Intel Corporation

SPDX-License-Identifier: MIT

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

#pragma once

#include "Object.h"
#include "MemCopy.h"

namespace iSTD
{

/*****************************************************************************\
Template Parameters
\*****************************************************************************/
#define SparseSetTemplateList   class CAllocatorType
#define CSparseSetType          CSparseSet<CAllocatorType>

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

Class:
    CSparseSet

Description:

\*****************************************************************************/
template<SparseSetTemplateList>
class CSparseSet : public CObject<CAllocatorType>
{
public:

    CSparseSet( void );
    CSparseSet( DWORD size );
    CSparseSet( const CSparseSetType& other );

    virtual ~CSparseSet( void );

    void    Resize( DWORD size );

    void    Clear( void );
    void    SetAll( void );
    void    Invert( void );

    bool    IsEmpty() const;
    bool    IsSet( DWORD index ) const;
    bool    Intersects( const CSparseSetType& other ) const;

    void    Set( DWORD index );
    void    Set( const CSparseSetType& other );
    void    UnSet( DWORD index );
    void    UnSet( const CSparseSetType& other );

    bool    UnsafeIsSet( DWORD index ) const;
    void    UnsafeSet( DWORD index );

    DWORD   GetSize( void ) const;

    bool    operator==( const CSparseSetType& other ) const;
    bool    operator!=( const CSparseSetType& other ) const;

    CSparseSetType& operator=  ( const CSparseSetType& other );
    CSparseSetType& operator|= ( const CSparseSetType& other );
    CSparseSetType& operator&= ( const CSparseSetType& other );

    DWORD   GetNumMembers( void ) const;
    DWORD   GetMember( DWORD memberNum ) const;

protected:

    DWORD*  m_Array;
    DWORD*  m_Members;
    DWORD   m_Size;
    DWORD   m_NumMembers;

    void    Create( DWORD size );
    void    Copy( const CSparseSetType& other );
    void    Delete( void );
};

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

Function:
    CSparseSet Constructor

Description:
    Initializes the set.  The set is initially empty.

Input:

Output:
    none

\*****************************************************************************/
template<SparseSetTemplateList>
CSparseSetType::CSparseSet( void )
    : CObject<CAllocatorType>()
{
    m_Array = 0;
    m_Members = 0;
    m_Size = 0;
    m_NumMembers = 0;
}

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

Function:
    CSparseSet Constructor

Description:
    Initializes the set.  The set is initially empty.

Input:
    DWORD size - initial size of the set

Output:
    none

\*****************************************************************************/
template<SparseSetTemplateList>
CSparseSetType::CSparseSet( DWORD size )
    : CObject<CAllocatorType>()
{
    m_Array = 0;
    m_Members = 0;
    m_Size = 0;
    m_NumMembers = 0;

    Create( size );
}

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

Function:
    CSparseSet Copy Constructor

Description:
    Initializes the set.

Input:
    const CSparseSetType& other - other set to copy

Output:
    none

\*****************************************************************************/
template<SparseSetTemplateList>
CSparseSetType::CSparseSet( const CSparseSetType& other )
    : CObject<CAllocatorType>()
{
    m_Array = 0;
    m_Members = 0;
    m_Size = 0;
    m_NumMembers = 0;

    Copy( other );
}

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

Function:
    CSparseSet Destructor

Description:
    Frees all internal dynamic memory

Input:
    none

Output:
    none

\*****************************************************************************/
template<SparseSetTemplateList>
CSparseSetType::~CSparseSet( void )
{
    Delete();
}

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

Function:
    CSparseSet::Resize

Description:
    Resizes the set.  Note that this is a destructive operation, i.e. the set
    contents are not preserved on a resize.  This behavior is different than
    the behavior for BitSets.

Input:
    DWORD size - new size of the set

Output:
    none

\*****************************************************************************/
template<SparseSetTemplateList>
void CSparseSetType::Resize( DWORD size )
{
    Create( size );
}

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

Function:
    CSparseSet::Clear

Description:
    Unsets all bits in the sparse set

Input:
    none

Output:
    none

\*****************************************************************************/
template<SparseSetTemplateList>
void CSparseSetType::Clear( void )
{
    m_NumMembers = 0;
}

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

Function:
    CSparseSet::SetAll

Description:
    Sets all the bits in this sparse set.  This is not a particularly fast
    operation.

Input:
    void

Output:
    void

\*****************************************************************************/
template<SparseSetTemplateList>
void CSparseSetType::SetAll( void )
{
    DWORD   index = 0;
    for( index = 0; index < GetSize(); index++ )
    {
        Set( index );
    }
}

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

Function:
    CSparseSet::Invert

Description:
    Computes the inverse of this sparse set.  This is not a particularly fast
    operation either.

Input:
    void

Output:
    void

\*****************************************************************************/
template<SparseSetTemplateList>
void CSparseSetType::Invert( void )
{
    DWORD   index = 0;
    for( index = 0; index < GetSize(); index++ )
    {
        if( IsSet( index ) )
        {
            UnSet( index );
        }
        else
        {
            Set( index );
        }
    }
}

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

Function:
    CSparseSet::IsEmpty

Description:
    Determines if any bits are on in the bit set.

Input:
    none

Output:
    bool

\*****************************************************************************/
template<SparseSetTemplateList>
bool CSparseSetType::IsEmpty( void ) const
{
    if( GetNumMembers() == 0 )
    {
        return true;
    }

    return false;
}

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

Function:
    CSparseSet::IsSet

Description:
    Returns true if the specified index exists in the set, false otherwise.

Input:
    DWORD index - index to check

Output:
    bool

\*****************************************************************************/
template<SparseSetTemplateList>
bool CSparseSetType::IsSet( DWORD index ) const
{
    bool    isSet = false;

    if( index < GetSize() )
    {
        DWORD   memberNum = m_Array[ index ];
        if( memberNum < GetNumMembers() )
        {
            DWORD   member = GetMember( memberNum );
            isSet = ( member == index );
        }
    }

    return isSet;
}

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

Function:
    CSparseSet::UnsafeIsSet

Description:
    Returns true if the specified index exists in the set, false otherwise.

    Note that function does not check if index is in range! 

Input:
    DWORD index - index to check

Output:
    bool

\*****************************************************************************/
template<SparseSetTemplateList>
bool CSparseSetType::UnsafeIsSet( DWORD index ) const
{
    bool    isSet = false;

    DWORD   memberNum = m_Array[ index ];
    if( memberNum < GetNumMembers() )
    {
        DWORD   member = GetMember( memberNum );
        isSet = ( member == index );
    }

    return isSet;
}

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

Function:
    CSparseSet::Intersects

Description:
    Returns true if any members exist in both this set and the passed-in
    set.  This is a shortcut for ( Set1 & Set2 ).IsEmpty().

Input:
    const CSparseSetType& other - Other set

Output:
    bool

\*****************************************************************************/
template<SparseSetTemplateList>
bool CSparseSetType::Intersects( const CSparseSetType& other ) const
{
    const CSparseSetType*   smallSet = &other;
    const CSparseSetType*   bigSet = this;

    if( smallSet->GetNumMembers() > bigSet->GetNumMembers() )
    {
        const CSparseSetType *tmp = smallSet;
        smallSet = bigSet;
        bigSet = tmp;
    }

    DWORD   memberNum = smallSet->GetNumMembers();
    DWORD   index = 0;
    while( memberNum-- )
    {
        index = smallSet->GetMember( memberNum );
        if( bigSet->IsSet( index ) )
        {
            return true;
        }
    }

    return false;
}

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

Function:
    CSparseSet::Set

Description:
    Sets the bit at the given index.

Input:
    DWORD index - index of the bit to set

Output:
    void

\*****************************************************************************/
template<SparseSetTemplateList>
void CSparseSetType::Set( DWORD index )
{
    // If the index is larger than the size of this set then grow the set.
    if( index >= GetSize() )
    {
        Create( index + 1 );
    }

    if( index < GetSize() )
    {
        if( IsSet( index ) == false )
        {
            m_Array[ index ] = m_NumMembers;
            m_Members[ m_NumMembers ] = index;
            m_NumMembers++;
        }
    }
    else
    {
        ASSERT(0);
    }

    ASSERT( IsSet( index ) );
}

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

Function:
    CSparseSet::UnsafeSet

Description:
    Sets the bit at the given index.

    Note that function does not check if index is in range! 

Input:
    DWORD index - index of the bit to set

Output:
    void

\*****************************************************************************/
template<SparseSetTemplateList>
void CSparseSetType::UnsafeSet( DWORD index )
{
    ASSERT( index < GetSize() );

    if( UnsafeIsSet( index ) == false )
    {
        m_Array[ index ] = m_NumMembers;
        m_Members[ m_NumMembers ] = index;
        m_NumMembers++;
    }

    ASSERT( IsSet( index) );
}

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

Function:
    CSparseSet::Set

Description:
    Sets all of the given bits.

Input:
    CSparseSetType other - bits to set.

Output:
    void

\*****************************************************************************/
template<SparseSetTemplateList>
void CSparseSetType::Set( const CSparseSetType& other )
{
    DWORD   memberNum = other.GetNumMembers();
    DWORD   index = 0;
    while( memberNum-- )
    {
        index = other.GetMember( memberNum );
        Set( index );
    }
}

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

Function:
    CSparseSet::UnSet

Description:
    Un-Sets the bit at the given index.

Input:
    DWORD index - index of the bit to un-set

Output:
    void

\*****************************************************************************/
template<SparseSetTemplateList>
void CSparseSetType::UnSet( DWORD index )
{
    // If the index is larger than the size of this set then grow the set.
    if( index >= GetSize() )
    {
        Create( index + 1 );
    }

    if( index < GetSize() )
    {
        if( IsSet( index ) )
        {
            m_NumMembers--;

            DWORD   movedIndex = m_Members[ m_NumMembers ];
            DWORD   movedMember = m_Array[ index ];

            m_Array[ movedIndex ] = movedMember;
            m_Members[ movedMember ] = movedIndex;
        }
    }
    else
    {
        ASSERT(0);
    }

    ASSERT( IsSet( index) == false );
}

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

Function:
    CSparseSet::UnSet

Description:
    Un-Sets all of the given bits.

Input:
    CSparseSetType other - bits to un-set.

Output:
    void

\*****************************************************************************/
template<SparseSetTemplateList>
void CSparseSetType::UnSet( const CSparseSetType& other )
{
    DWORD   memberNum = other.GetNumMembers();
    DWORD   index = 0;
    while( memberNum-- )
    {
        index = other.GetMember( memberNum );
        UnSet( index );
    }
}

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

Function:
    CSparseSet::GetSize

Description:
    Returns the maximum number of elements in the sparse set.

Input:
    void

Output:
    DWORD length

\*****************************************************************************/
template<SparseSetTemplateList>
DWORD CSparseSetType::GetSize( void ) const
{
    return m_Size;
}

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

Function:
    CSparseSet::operator ==

Description:
    Tests this sparse set and another sparse set for equality.

Input:
    CSparseSetType& other - other sparse set

Output:
    bool

\*****************************************************************************/
template<SparseSetTemplateList>
bool CSparseSetType::operator==( const CSparseSetType& other ) const
{
    if( ( GetSize() == other.GetSize() ) &&
        ( GetNumMembers() == other.GetNumMembers() ) )
    {
        DWORD   memberNum = other.GetNumMembers();
        DWORD   index = 0;
        while( memberNum-- )
        {
            index = other.GetMember( memberNum );
            if( IsSet( index ) == false )
            {
                return false;
            }
        }

        return true;
    }

    return false;
}

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

Function:
    CSparseSet::operator !=

Description:
    Tests this sparse set and another sparse set for inequality.

Input:
    CSparseSetType& other - other sparse set

Output:
    bool

\*****************************************************************************/
template<SparseSetTemplateList>
bool CSparseSetType::operator!=( const CSparseSetType& other ) const
{
    if( ( GetSize() == other.GetSize() ) &&
        ( GetNumMembers() == other.GetNumMembers() ) )
    {
        DWORD   memberNum = other.GetNumMembers();
        DWORD   index = 0;
        while( memberNum-- )
        {
            index = other.GetMember( memberNum );
            if( IsSet( index ) == false )
            {
                return true;
            }
        }

        return false;
    }

    return true;
}

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

Function:
    CSparseSet::operator =

Description:
    Equal operator to copy a sparse set.

Input:
    CSparseSetType& other - sparse set to copy

Output:
    *this

\*****************************************************************************/
template<SparseSetTemplateList>
CSparseSetType& CSparseSetType::operator= ( const CSparseSetType &other )
{
    Copy( other );

    return *this;
}

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

Function:
    CSparseSet::operator |=

Description:
    Computes the union of this sparse set with another sparse set.

Input:
    CSparseSetType& other - other sparse set

Output:
    *this

\*****************************************************************************/
template<SparseSetTemplateList>
CSparseSetType& CSparseSetType::operator|= ( const CSparseSetType &other )
{
    DWORD   memberNum = other.GetNumMembers();
    DWORD   index = 0;
    while( memberNum-- )
    {
        index = other.GetMember( memberNum );
        Set( index );
    }

    return *this;
}

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

Function:
    CSparseSet::operator &=

Description:
    Computes the intersection of this sparse set with another sparse set.

Input:
    CSparseSetType& other - other sparse set

Output:
    *this

\*****************************************************************************/
template<SparseSetTemplateList>
CSparseSetType& CSparseSetType::operator&= ( const CSparseSetType &other )
{
    DWORD   memberNum = GetNumMembers();
    DWORD   index = 0;

    while( memberNum-- )
    {
        index = GetMember( memberNum );
        if( other.IsSet( index ) == false )
        {
            UnSet( index );
        }
    }

    return *this;
}

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

Function:
    CSparseSet::GetNumMembers

Description:
    Returns the number of members in this sparse set.

Input:
    void

Output:
    DWORD number of members in this sparse set

\*****************************************************************************/
template<SparseSetTemplateList>
DWORD CSparseSetType::GetNumMembers( void ) const
{
    return m_NumMembers;
}

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

Function:
    CSparseSet::GetMember

Description:
    Returns a member of this sparse set, given a member number.

Input:
    void

Output:
    DWORD member of this sparse set

\*****************************************************************************/
template<SparseSetTemplateList>
DWORD CSparseSetType::GetMember( DWORD memberNum ) const
{
    ASSERT( memberNum < GetNumMembers() );    
    return m_Members[ memberNum ];
}

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

Function:
    CSparseSet::Create

Description:
    Creates the internal sparse set structure of the specified size

Input:
    DWORD size - number of elements

Output:
    void

\*****************************************************************************/
template<SparseSetTemplateList>
void CSparseSetType::Create( DWORD size )
{
    const DWORD newArraySize = size;
    const DWORD oldArraySize = GetSize();

    if( newArraySize == oldArraySize )
    {
        // Nothing to do.
    }
    else if( newArraySize )
    {
        DWORD*  newArray = (DWORD*)CAllocatorType::Allocate(
            sizeof(DWORD) * newArraySize );
        DWORD*  newMembers = (DWORD*)CAllocatorType::Allocate(
            sizeof(DWORD) * newArraySize );

        if( newArray && newMembers )
        {
            if( m_Array && ( newArraySize > oldArraySize ) )
            {
                MemCopy( newArray,
                    m_Array,
                    oldArraySize * sizeof(DWORD) );
                SafeMemSet( newArray + oldArraySize,
                    0,
                    ( newArraySize - oldArraySize) * sizeof(DWORD) );
            }
            else
            {
                SafeMemSet( newArray,
                    0,
                    newArraySize * sizeof(DWORD) );
            }

            if( m_Members && ( newArraySize > oldArraySize ) )
            {
                MemCopy( newMembers,
                    m_Members,
                    oldArraySize * sizeof(DWORD) );
                SafeMemSet( newMembers + oldArraySize,
                    0,
                    ( newArraySize - oldArraySize) * sizeof(DWORD) );
            }
            else
            {
                SafeMemSet( newMembers,
                    0,
                    newArraySize * sizeof(DWORD) );
            }

            Delete();

            m_Array = newArray;
            m_Members = newMembers;
            m_Size = size;
        }
        else
        {
            CAllocatorType::Deallocate( newArray );
            newArray = 0;

            CAllocatorType::Deallocate( newMembers );
            newMembers = 0;
        }
    }
    else
    {
        Delete();
    }
}

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

Function:
    CSparseSet::Copy

Description:
    Copies information from one sparse set to this sparse set.

Input:
    void

Output:
    void

\*****************************************************************************/
template<SparseSetTemplateList>
void CSparseSetType::Copy( const CSparseSetType& other )
{
    if( GetSize() == other.GetSize() )
    {
        if( this != &other )
        {
            MemCopy( m_Array,
                       other.m_Array,
                       GetSize() * sizeof(DWORD) );
            MemCopy( m_Members,
                       other.m_Members,
                       GetSize() * sizeof(DWORD) );

            m_NumMembers = other.GetNumMembers();
        }
        else
        {
            // Nothing to do.
        }
    }
    else
    {
        Create( other.GetSize() );
        if( GetSize() == other.GetSize() )
        {
            MemCopy( m_Array,
                       other.m_Array,
                       GetSize() * sizeof(DWORD) );
            MemCopy( m_Members,
                       other.m_Members,
                       GetSize() * sizeof(DWORD) );

            m_NumMembers = other.GetNumMembers();
        }
        else
        {
            ASSERT( 0 );
        }
    }
}

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

Function:
    CSparseSet::Delete

Description:
    Deletes the internal sparse set structure

Input:
    void

Output:
    void

\*****************************************************************************/
template<SparseSetTemplateList>
void CSparseSetType::Delete( void )
{
    CAllocatorType::Deallocate( m_Array );
    m_Array = 0;

    CAllocatorType::Deallocate( m_Members );
    m_Members = 0;

    m_Size = 0;
    m_NumMembers = 0;
}

} // iSTD