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
#ifndef MB_MESHSET_HPP
#define MB_MESHSET_HPP

#ifndef IS_BUILDING_MB
#error "MB_MeshSet.hpp isn't supposed to be included into an application"
#endif

#include "moab/Interface.hpp"
#include "Internals.hpp"
#include "moab/Range.hpp"
#include "moab/CN.hpp"

#include <cassert>
#include <vector>
#include <algorithm>
#include <iterator>

namespace moab
{

class AEntityFactory;

/** \brief Class to implement entity set functionality
 * \author Jason Kraftcheck <[email protected]>
 */
class MeshSet
{
  public:
    //! create an empty meshset
    inline MeshSet();
    inline MeshSet( unsigned flags );

    //! destructor
    inline ~MeshSet();

    inline ErrorCode set_flags( unsigned flags, EntityHandle my_handle, AEntityFactory* adjacencies );

    //! get all children pointed to by this meshset
    inline const EntityHandle* get_children( int& count_out ) const;

    //! get all parents pointed to by this meshset
    inline const EntityHandle* get_parents( int& count_out ) const;

    //! return the number of children pointed to by this meshset
    inline int num_children() const;

    //! return the number of parents pointed to by this meshset
    inline int num_parents() const;

    //! add a parent to this meshset; returns true if parent was added, 0 if it was
    //! already a parent of this meshset
    int add_parent( EntityHandle parent );

    //! add a child to this meshset; returns true if child was added, 0 if it was
    //! already a child of this meshset
    int add_child( EntityHandle child );

    //! remove a parent from this meshset; returns true if parent was removed, 0 if it was
    //! not a parent of this meshset
    int remove_parent( EntityHandle parent );

    //! remove a child from this meshset; returns true if child was removed, 0 if it was
    //! not a child of this meshset
    int remove_child( EntityHandle child );

    unsigned flags() const
    {
        return mFlags;
    }
    //! returns whether entities of meshsets know this meshset
    int tracking() const
    {
        return mFlags & MESHSET_TRACK_OWNER;
    }
    int set() const
    {
        return mFlags & MESHSET_SET;
    }
    int ordered() const
    {
        return mFlags & MESHSET_ORDERED;
    }
    int vector_based() const
    {
        return ordered();
    }

    //! replace one entity with another in the set (contents and parent/child
    //! lists); returns whether it was replaced or not
    ErrorCode replace_entities( EntityHandle my_handle,
                                const EntityHandle* old_entities,
                                const EntityHandle* new_entities,
                                size_t num_entities,
                                AEntityFactory* mAdjFact );

    /** Clear *contents* of set (not parents or children) */
    inline ErrorCode clear( EntityHandle myhandle, AEntityFactory* adjacencies );

    /** Clear all set lists (contents, parents, and children) */
    inline ErrorCode clear_all( EntityHandle myhandle, AEntityFactory* adjacencies );

    /** Get contents data array.  NOTE: this may not contain what you expect if not vector_based */
    inline const EntityHandle* get_contents( size_t& count_out ) const;
    /** Get contents data array.  NOTE: this may not contain what you expect if not vector_based */
    inline EntityHandle* get_contents( size_t& count_out );

    /** Get entities contained in set */
    inline ErrorCode get_entities( std::vector< EntityHandle >& entities ) const;

    /** Get entities contained in set */
    inline ErrorCode get_entities( Range& entities ) const;

    //! get all entities in this MeshSet with the specified type
    inline ErrorCode get_entities_by_type( EntityType entity_type, std::vector< EntityHandle >& entity_list ) const;

    inline ErrorCode get_entities_by_type( EntityType type, Range& entity_list ) const;

    //! return the number of entities with the given type contained in this meshset
    inline unsigned int num_entities_by_type( EntityType type ) const;

    inline ErrorCode get_entities_by_dimension( int dimension, std::vector< EntityHandle >& entity_list ) const;

    inline ErrorCode get_entities_by_dimension( int dimension, Range& entity_list ) const;

    //! return the number of entities with the given type contained in this meshset
    inline unsigned int num_entities_by_dimension( int dimension ) const;

    inline ErrorCode get_non_set_entities( Range& range ) const;

    /** Test of meshset contains some or all of passed entities
     *
     *\param entities Array of entities
     *\param num_entities Length of array of entities.
     *\param op - Interface::UNION     : Test if set contains any of the input entities
     *          - Interface::INTERSECT : Test if set contains all of the input entities
     */
    inline bool contains_entities( const EntityHandle* entities, int num_entities, const int op ) const;

    //! subtract/intersect/unite meshset_2 from/with/into meshset_1; modifies meshset_1
    inline ErrorCode subtract( const MeshSet* meshset_2, EntityHandle my_handle, AEntityFactory* adjacencies );

    ErrorCode intersect( const MeshSet* meshset_2, EntityHandle my_handle, AEntityFactory* adjacencies );

    inline ErrorCode unite( const MeshSet* meshset_2, EntityHandle my_handle, AEntityFactory* adjacencies );

    //! add these entities to this meshset
    inline ErrorCode add_entities( const EntityHandle* entity_handles,
                                   const int num_entities,
                                   EntityHandle my_handle,
                                   AEntityFactory* adjacencies );

    //! add these entities to this meshset
    inline ErrorCode add_entities( const Range& entities, EntityHandle my_handle, AEntityFactory* adjacencies );

    //! add these entities to this meshset
    inline ErrorCode remove_entities( const Range& entities, EntityHandle my_handle, AEntityFactory* adjacencies );

    //! remove these entities from this meshset
    inline ErrorCode remove_entities( const EntityHandle* entities,
                                      const int num_entities,
                                      EntityHandle my_handle,
                                      AEntityFactory* adjacencies );

    //! return the number of entities contained in this meshset
    inline unsigned int num_entities() const;

    inline bool empty() const
    {
        return mContentCount == ZERO;
    }

    unsigned long get_memory_use() const;

  protected:
    /** Convert for changing flag values */
    ErrorCode convert( unsigned flags, EntityHandle my_handle, AEntityFactory* adj );

    /** Add explicit adjacencies from all contained entities to this (i.e. convert to tracking) */
    ErrorCode create_adjacencies( EntityHandle myhandle, AEntityFactory* adjacencies );

    /** Remvoe explicit adjacencies from all contained entities to this (i.e. convert from tracking)
     */
    ErrorCode remove_adjacencies( EntityHandle myhandle, AEntityFactory* adjacencies );

    /** Insert vector of handles into MeshSet */
    ErrorCode insert_entity_vector( const EntityHandle* vect, size_t len, EntityHandle my_h, AEntityFactory* adj );

    /** Insert vector of handle range pairs into MeshSet */
    ErrorCode insert_entity_ranges( const EntityHandle* range_vect,
                                    size_t len,
                                    EntityHandle my_h,
                                    AEntityFactory* adj );

    /** Insert Range of handles into MeshSet */
    ErrorCode insert_entity_ranges( const Range& range, EntityHandle my_h, AEntityFactory* adj );

    /** Remove vector of handles from MeshSet */
    ErrorCode remove_entity_vector( const EntityHandle* vect, size_t len, EntityHandle my_h, AEntityFactory* adj );

    /** Remove vector of handle range pairs from MeshSet */
    ErrorCode remove_entity_ranges( const EntityHandle* range_vect,
                                    size_t len,
                                    EntityHandle my_h,
                                    AEntityFactory* adj );

    /** Remove Range of handles from MeshSet */
    ErrorCode remove_entity_ranges( const Range& range, EntityHandle my_h, AEntityFactory* adj );

  public:
    //! Possible values of mParentCount and mChildCount
    enum Count
    {
        ZERO = 0,
        ONE  = 1,
        TWO  = 2,
        MANY = 3
    };
    //! If the number of entities is less than 3, store
    //! the handles directly in the hnd member.  Otherwise
    //! use the ptr member to hold the beginning and end
    //! of a dynamically allocated array.
    union CompactList
    {
        EntityHandle hnd[2];   //!< Two handles
        EntityHandle* ptr[2];  //!< begin and end pointers for array
    };

  private:
    //! Meshset propery flags
    unsigned char mFlags;
    //! If less than MANY, the number of parents stored inline in
    //! parentMeshSets.hnd.  If MANY, then parentMeshSets.ptr contains
    //! array begin and end pointers for a dynamically allocated array
    //! of parent handles.
    unsigned mParentCount : 2;
    //! If less than MANY, the number of children stored inline in
    //! childMeshSets.hnd.  If MANY, then childMeshSets.ptr contains
    //! array begin and end pointers for a dynamically allocated array
    //! of child handles.
    unsigned mChildCount : 2;
    //! If less than MANY, the number of children stored inline in
    //! contentList.hnd.  If MANY, then contentList.ptr contains
    //! array begin and end pointers for a dynamically allocated array..
    unsigned mContentCount : 2;
    //! Storage for data lists
    CompactList parentMeshSets, childMeshSets, contentList;

  public:
    /** get dimension of enity */
    static inline int DIM_FROM_HANDLE( EntityHandle h )
    {
        return CN::Dimension( TYPE_FROM_HANDLE( h ) );
    }

    /** Get smallest possible handle with specified dimension (first handle for first type of
     * dimension) */
    static inline EntityHandle FIRST_OF_DIM( int dim )
    {
        return FIRST_HANDLE( CN::TypeDimensionMap[dim].first );
    }

    /** Get largest possible handle with specified dimension (largest handle for last type of
     * dimension) */
    static inline EntityHandle LAST_OF_DIM( int dim )
    {
        return LAST_HANDLE( CN::TypeDimensionMap[dim].second );
    }

    /** functor: test if handle is not of type */
    struct not_type_test
    {
        inline not_type_test( EntityType type ) : mType( type ) {}
        inline bool operator()( EntityHandle handle )
        {
            return TYPE_FROM_HANDLE( handle ) != mType;
        }
        EntityType mType;
    };

    /** functor: test if handle is of type */
    struct type_test
    {
        inline type_test( EntityType type ) : mType( type ) {}
        inline bool operator()( EntityHandle handle )
        {
            return TYPE_FROM_HANDLE( handle ) == mType;
        }
        EntityType mType;
    };

    /** functor: test if handle is not of dimension */
    struct not_dim_test
    {
        inline not_dim_test( int dimension ) : mDim( dimension ) {}
        inline bool operator()( EntityHandle handle ) const
        {
            return DIM_FROM_HANDLE( handle ) != mDim;
        }
        int mDim;
    };

    /** functor: test if handle is of dimension */
    struct dim_test
    {
        inline dim_test( int dimension ) : mDim( dimension ) {}
        inline bool operator()( EntityHandle handle ) const
        {
            return DIM_FROM_HANDLE( handle ) == mDim;
        }
        int mDim;
    };

    /** Iterate over range of handles.  That is, given [first_handle,last_handle],
     *  step through all contained values.
     */
    struct hdl_iter
    {
        EntityHandle h;
        hdl_iter( EntityHandle val ) : h( val ) {}
        hdl_iter& operator++()
        {
            ++h;
            return *this;
        }
        hdl_iter& operator--()
        {
            --h;
            return *this;
        }
        hdl_iter operator++( int )
        {
            return hdl_iter( h++ );
        }
        hdl_iter operator--( int )
        {
            return hdl_iter( h-- );
        }
        hdl_iter& operator+=( size_t s )
        {
            h += s;
            return *this;
        }
        hdl_iter& operator-=( size_t s )
        {
            h -= s;
            return *this;
        }
        EntityHandle operator*() const
        {
            return h;
        }
        bool operator==( hdl_iter other ) const
        {
            return h == other.h;
        }
        bool operator!=( hdl_iter other ) const
        {
            return h != other.h;
        }
        bool operator<( hdl_iter other ) const
        {
            return h < other.h;
        }
        bool operator>( hdl_iter other ) const
        {
            return h > other.h;
        }
        bool operator<=( hdl_iter other ) const
        {
            return h <= other.h;
        }
        bool operator>=( hdl_iter other ) const
        {
            return h >= other.h;
        }

        struct iterator_category : public std::random_access_iterator_tag
        {
        };
        typedef EntityID difference_type;
        typedef EntityHandle value_type;
        typedef EntityHandle* pointer;
        typedef EntityHandle& reference;
    };
};

inline MeshSet::hdl_iter::difference_type operator-( const MeshSet::hdl_iter& a, const MeshSet::hdl_iter& b )
{
    return (MeshSet::hdl_iter::difference_type)a.h - (MeshSet::hdl_iter::difference_type)b.h;
}

//! create an empty meshset
MeshSet::MeshSet() : mFlags( 0 ), mParentCount( ZERO ), mChildCount( ZERO ), mContentCount( ZERO ) {}

//! create an empty meshset
MeshSet::MeshSet( unsigned flg )
    : mFlags( (unsigned char)flg ), mParentCount( ZERO ), mChildCount( ZERO ), mContentCount( ZERO )
{
}

//! destructor
MeshSet::~MeshSet()
{
    if( mChildCount == MANY ) free( childMeshSets.ptr[0] );
    if( mParentCount == MANY ) free( parentMeshSets.ptr[0] );
    if( mContentCount == MANY ) free( contentList.ptr[0] );
    mChildCount = mParentCount = mContentCount = ZERO;
}

ErrorCode MeshSet::set_flags( unsigned flg, EntityHandle my_handle, AEntityFactory* adjacencies )
{
    if( ZERO != mContentCount )
    {
        ErrorCode result = convert( flg, my_handle, adjacencies );
        if( MB_SUCCESS != result ) return result;
    }
    mFlags = (unsigned char)flg;
    return MB_SUCCESS;
}

//! get all children pointed to by this meshset
const EntityHandle* MeshSet::get_children( int& count_out ) const
{
    count_out = mChildCount;
    if( count_out < MANY ) return childMeshSets.hnd;

    count_out = childMeshSets.ptr[1] - childMeshSets.ptr[0];
    return childMeshSets.ptr[0];
}

//! get all parents pointed to by this meshset
const EntityHandle* MeshSet::get_parents( int& count_out ) const
{
    count_out = mParentCount;
    if( count_out < MANY ) return parentMeshSets.hnd;

    count_out = parentMeshSets.ptr[1] - parentMeshSets.ptr[0];
    return parentMeshSets.ptr[0];
}

//! return the number of children pointed to by this meshset
int MeshSet::num_children() const
{
    if( mChildCount < MANY )
        return mChildCount;
    else
        return childMeshSets.ptr[1] - childMeshSets.ptr[0];
}

//! return the number of parents pointed to by this meshset
int MeshSet::num_parents() const
{
    if( mParentCount < MANY )
        return mParentCount;
    else
        return parentMeshSets.ptr[1] - parentMeshSets.ptr[0];
}

inline ErrorCode MeshSet::clear( EntityHandle myhandle, AEntityFactory* adjacencies )
{
    if( tracking() ) remove_adjacencies( myhandle, adjacencies );
    if( mContentCount == MANY ) free( contentList.ptr[0] );
    mContentCount = ZERO;
    return MB_SUCCESS;
}

inline ErrorCode MeshSet::clear_all( EntityHandle myhandle, AEntityFactory* adjacencies )
{
    ErrorCode rval = clear( myhandle, adjacencies );
    if( mChildCount == MANY ) free( childMeshSets.ptr[0] );
    mChildCount = ZERO;
    if( mParentCount == MANY ) free( parentMeshSets.ptr[0] );
    mParentCount = ZERO;
    return rval;
}

inline const EntityHandle* MeshSet::get_contents( size_t& count_out ) const
{
    if( mContentCount == MANY )
    {
        count_out = contentList.ptr[1] - contentList.ptr[0];
        return contentList.ptr[0];
    }
    else
    {
        count_out = mContentCount;
        return contentList.hnd;
    }
}

inline EntityHandle* MeshSet::get_contents( size_t& count_out )
{
    if( mContentCount == MANY )
    {
        count_out = contentList.ptr[1] - contentList.ptr[0];
        return contentList.ptr[0];
    }
    else
    {
        count_out = mContentCount;
        return contentList.hnd;
    }
}

inline ErrorCode MeshSet::get_entities( std::vector< EntityHandle >& entities ) const
{
    size_t count;
    const EntityHandle* ptr = get_contents( count );
    if( vector_based() )
    {
        size_t old_size = entities.size();
        entities.resize( count + old_size );
        std::copy( ptr, ptr + count, entities.begin() + old_size );
    }
    else
    {
        assert( count % 2 == 0 );
        for( size_t i = 0; i < count; i += 2 )
            std::copy( hdl_iter( ptr[i] ), hdl_iter( ptr[i + 1] + 1 ), std::back_inserter( entities ) );
    }
    return MB_SUCCESS;
}

inline ErrorCode MeshSet::get_entities( Range& entities ) const
{
    size_t count;
    const EntityHandle* ptr = get_contents( count );
    if( vector_based() )
    {
        std::copy( ptr, ptr + count, range_inserter( entities ) );
    }
    else
    {
        assert( count % 2 == 0 );
        Range::iterator in = entities.begin();
        for( size_t i = 0; i < count; i += 2 )
            in = entities.insert( in, ptr[i], ptr[i + 1] );
    }
    return MB_SUCCESS;
}

//! get all entities in this MeshSet with the specified type
inline ErrorCode MeshSet::get_entities_by_type( EntityType type, std::vector< EntityHandle >& entity_list ) const
{
    size_t count;
    const EntityHandle* ptr = get_contents( count );
    if( MBMAXTYPE == type )
    {
        return get_entities( entity_list );
    }
    else if( vector_based() )
    {
        std::remove_copy_if( ptr, ptr + count, std::back_inserter( entity_list ), not_type_test( type ) );
    }
    else
    {
        size_t idx = std::lower_bound( ptr, ptr + count, FIRST_HANDLE( type ) ) - ptr;
        if( idx < count && TYPE_FROM_HANDLE( ptr[idx] ) == type )
        {
            if( idx % 2 )
            {  // only part of first block is of type
                std::copy( hdl_iter( FIRST_HANDLE( type ) ), hdl_iter( ptr[idx] + 1 ),
                           std::back_inserter( entity_list ) );
                ++idx;
            }
            for( ; idx < count; idx += 2 )
            {
                if( TYPE_FROM_HANDLE( ptr[idx + 1] ) == type )  // whole block is of type
                    std::copy( hdl_iter( ptr[idx] ), hdl_iter( ptr[idx + 1] + 1 ), std::back_inserter( entity_list ) );
                else
                {
                    if( TYPE_FROM_HANDLE( ptr[idx] ) == type )  // part of last block is of type
                        std::copy( hdl_iter( ptr[idx] ), hdl_iter( LAST_HANDLE( type ) ),
                                   std::back_inserter( entity_list ) );
                    break;
                }
            }
        }
    }

    return MB_SUCCESS;
}

inline ErrorCode MeshSet::get_entities_by_type( EntityType type, Range& entity_list ) const
{
    size_t count;
    const EntityHandle* ptr = get_contents( count );
    if( MBMAXTYPE == type )
    {
        return get_entities( entity_list );
    }
    else if( vector_based() )
    {
        std::remove_copy_if( ptr, ptr + count, range_inserter( entity_list ), not_type_test( type ) );
    }
    else
    {
        size_t idx         = std::lower_bound( ptr, ptr + count, FIRST_HANDLE( type ) ) - ptr;
        Range::iterator in = entity_list.begin();
        if( idx < count && TYPE_FROM_HANDLE( ptr[idx] ) == type )
        {
            if( idx % 2 )
            {  // only part of first block is of type
                in = entity_list.insert( in, FIRST_HANDLE( type ), ptr[idx] );
                ++idx;
            }
            for( ; idx < count; idx += 2 )
            {
                if( TYPE_FROM_HANDLE( ptr[idx + 1] ) == type )  // whole block is of type
                    in = entity_list.insert( in, ptr[idx], ptr[idx + 1] );
                else
                {
                    if( TYPE_FROM_HANDLE( ptr[idx] ) == type )  // part of last block is of type
                        entity_list.insert( in, ptr[idx], LAST_HANDLE( type ) );
                    break;
                }
            }
        }
    }

    return MB_SUCCESS;
}

//! return the number of entities with the given type contained in this meshset
inline unsigned int MeshSet::num_entities_by_type( EntityType type ) const
{
    unsigned int result;
    size_t count;
    const EntityHandle* ptr = get_contents( count );
    if( MBMAXTYPE == type )
    {
        return num_entities();
    }
    else if( vector_based() )
    {
#ifndef __SUNPRO_CC
        result = std::count_if( ptr, ptr + count, type_test( type ) );
#else
        std::count_if( ptr, ptr + count, type_test( type ), result );
#endif
    }
    else
    {
        result     = 0;
        size_t idx = std::lower_bound( ptr, ptr + count, FIRST_HANDLE( type ) ) - ptr;
        if( idx < count && TYPE_FROM_HANDLE( ptr[idx] ) == type )
        {
            if( idx % 2 )
            {  // only part of first block is of type
                result += ptr[idx] - FIRST_HANDLE( type ) + 1;
                ++idx;
            }
            for( ; idx < count; idx += 2 )
            {
                if( TYPE_FROM_HANDLE( ptr[idx + 1] ) == type )  // whole block is of type
                    result += ptr[idx + 1] - ptr[idx] + 1;
                else
                {
                    if( TYPE_FROM_HANDLE( ptr[idx] ) == type )  // part of last block is of type
                        result += LAST_HANDLE( type ) - ptr[idx] + 1;
                    break;
                }
            }
        }
    }

    return result;
}

inline ErrorCode MeshSet::get_entities_by_dimension( int dimension, std::vector< EntityHandle >& entity_list ) const
{
    size_t count;
    const EntityHandle* ptr = get_contents( count );
    if( vector_based() )
    {
        std::remove_copy_if( ptr, ptr + count, std::back_inserter( entity_list ), not_dim_test( dimension ) );
    }
    else
    {
        size_t idx = std::lower_bound( ptr, ptr + count, FIRST_OF_DIM( dimension ) ) - ptr;
        if( idx < count && DIM_FROM_HANDLE( ptr[idx] ) == dimension )
        {
            if( idx % 2 )
            {  // only part of first block is of type
                std::copy( hdl_iter( FIRST_OF_DIM( dimension ) ), hdl_iter( ptr[idx] + 1 ),
                           std::back_inserter( entity_list ) );
                ++idx;
            }
            for( ; idx < count; idx += 2 )
            {
                if( DIM_FROM_HANDLE( ptr[idx + 1] ) == dimension )  // whole block is of type
                    std::copy( hdl_iter( ptr[idx] ), hdl_iter( ptr[idx + 1] + 1 ), std::back_inserter( entity_list ) );
                else
                {
                    if( DIM_FROM_HANDLE( ptr[idx] ) == dimension )  // part of last block is of type
                        std::copy( hdl_iter( ptr[idx] ), hdl_iter( LAST_OF_DIM( dimension ) ),
                                   std::back_inserter( entity_list ) );
                    break;
                }
            }
        }
    }

    return MB_SUCCESS;
}

inline ErrorCode MeshSet::get_entities_by_dimension( int dimension, Range& entity_list ) const
{
    size_t count;
    const EntityHandle* ptr = get_contents( count );
    if( vector_based() )
    {
        std::remove_copy_if( ptr, ptr + count, range_inserter( entity_list ), not_dim_test( dimension ) );
    }
    else
    {
        size_t idx         = std::lower_bound( ptr, ptr + count, FIRST_OF_DIM( dimension ) ) - ptr;
        Range::iterator in = entity_list.begin();
        if( idx < count && DIM_FROM_HANDLE( ptr[idx] ) == dimension )
        {
            if( idx % 2 )
            {  // only part of first block is of type
                in = entity_list.insert( in, FIRST_OF_DIM( dimension ), ptr[idx] );
                ++idx;
            }
            for( ; idx < count; idx += 2 )
            {
                if( DIM_FROM_HANDLE( ptr[idx + 1] ) == dimension )  // whole block is of type
                    in = entity_list.insert( in, ptr[idx], ptr[idx + 1] );
                else
                {
                    if( DIM_FROM_HANDLE( ptr[idx] ) == dimension )  // part of last block is of type
                        entity_list.insert( in, ptr[idx], LAST_OF_DIM( dimension ) );
                    break;
                }
            }
        }
    }

    return MB_SUCCESS;
}

//! return the number of entities with the given type contained in this meshset
inline unsigned int MeshSet::num_entities_by_dimension( int dimension ) const
{
    unsigned int result;
    size_t count;
    const EntityHandle* ptr = get_contents( count );
    if( vector_based() )
    {
#ifndef __SUNPRO_CC
        result = std::count_if( ptr, ptr + count, dim_test( dimension ) );
#else
        std::count_if( ptr, ptr + count, dim_test( dimension ), result );
#endif
    }
    else
    {
        result     = 0;
        size_t idx = std::lower_bound( ptr, ptr + count, FIRST_OF_DIM( dimension ) ) - ptr;
        if( idx < count && DIM_FROM_HANDLE( ptr[idx] ) == dimension )
        {
            if( idx % 2 )
            {  // only part of first block is of type
                result += ptr[idx] - FIRST_OF_DIM( dimension ) + 1;
                ++idx;
            }
            for( ; idx < count; idx += 2 )
            {
                if( DIM_FROM_HANDLE( ptr[idx + 1] ) == dimension )  // whole block is of type
                    result += ptr[idx + 1] - ptr[idx] + 1;
                else
                {
                    if( DIM_FROM_HANDLE( ptr[idx] ) == dimension )  // part of last block is of type
                        result += LAST_OF_DIM( dimension ) - ptr[idx] + 1;
                    break;
                }
            }
        }
    }

    return result;
}

inline ErrorCode MeshSet::get_non_set_entities( Range& range ) const
{
    size_t count;
    const EntityHandle* ptr = get_contents( count );
    if( vector_based() )
    {
        std::remove_copy_if( ptr, ptr + count, range_inserter( range ), type_test( MBENTITYSET ) );
    }
    else
    {
        Range::iterator in = range.begin();
        for( size_t idx = 0; idx < count; idx += 2 )
        {
            if( TYPE_FROM_HANDLE( ptr[idx + 1] ) != MBENTITYSET )
                in = range.insert( in, ptr[idx], ptr[idx + 1] );
            else
            {
                if( TYPE_FROM_HANDLE( ptr[idx] ) != MBENTITYSET )
                    in = range.insert( in, ptr[idx], LAST_HANDLE( MBENTITYSET - 1 ) );
                break;
            }
        }
    }

    return MB_SUCCESS;
}

inline bool MeshSet::contains_entities( const EntityHandle* entities, int num_ents, const int op ) const
{
    size_t count;
    const EntityHandle* const ptr = get_contents( count );
    const EntityHandle* const end = ptr + count;
    size_t found_count            = 0;
    if( vector_based() )
    {
        for( int i = 0; i < num_ents; ++i )
            if( std::find( ptr, end, entities[i] ) < end ) ++found_count;
    }
    else
    {
        assert( 0 == count % 2 );
        for( int i = 0; i < num_ents; ++i )
        {
            const unsigned long idx = std::lower_bound( ptr, end, entities[i] ) - ptr;
            if( idx < count && ( idx % 2 != 0 || ptr[idx] == entities[i] ) ) ++found_count;
        }
    }

    return found_count >= ( ( Interface::INTERSECT == op ) ? (unsigned)num_ents : 1u );
}

//! subtract/intersect/unite meshset_2 from/with/into meshset_1; modifies meshset_1
inline ErrorCode MeshSet::subtract( const MeshSet* meshset_2, EntityHandle my_handle, AEntityFactory* adjacencies )
{
    size_t count;
    const EntityHandle* const ptr = meshset_2->get_contents( count );
    if( meshset_2->vector_based() )
        return remove_entity_vector( ptr, count, my_handle, adjacencies );
    else
        return remove_entity_ranges( ptr, count, my_handle, adjacencies );
}

inline ErrorCode MeshSet::unite( const MeshSet* meshset_2, EntityHandle my_handle, AEntityFactory* adjacencies )
{
    size_t count;
    const EntityHandle* const ptr = meshset_2->get_contents( count );
    if( meshset_2->vector_based() )
        return insert_entity_vector( ptr, count, my_handle, adjacencies );
    else
        return insert_entity_ranges( ptr, count, my_handle, adjacencies );
}

//! add these entities to this meshset
inline ErrorCode MeshSet::add_entities( const EntityHandle* entity_handles,
                                        const int num_ents,
                                        EntityHandle my_handle,
                                        AEntityFactory* adjacencies )
{
    return insert_entity_vector( entity_handles, num_ents, my_handle, adjacencies );
}

//! add these entities to this meshset
inline ErrorCode MeshSet::add_entities( const Range& entities, EntityHandle my_handle, AEntityFactory* adjacencies )
{
    return insert_entity_ranges( entities, my_handle, adjacencies );
}

//! add these entities to this meshset
inline ErrorCode MeshSet::remove_entities( const Range& entities, EntityHandle my_handle, AEntityFactory* adjacencies )
{
    return remove_entity_ranges( entities, my_handle, adjacencies );
}

//! remove these entities from this meshset
inline ErrorCode MeshSet::remove_entities( const EntityHandle* entities,
                                           const int num_ents,
                                           EntityHandle my_handle,
                                           AEntityFactory* adjacencies )
{
    return remove_entity_vector( entities, num_ents, my_handle, adjacencies );
}

//! return the number of entities contained in this meshset
unsigned int MeshSet::num_entities() const
{
    size_t count;
    const EntityHandle* list = get_contents( count );
    if( vector_based() ) return count;

    int result                    = 0;
    const EntityHandle* const end = list + count;
    for( ; list < end; list += 2 )
        result += list[1] - list[0] + 1;
    return result;
}

}  // namespace moab

#endif