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
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288 | #ifdef WIN32
#ifdef _DEBUG
// turn off warnings that say they debugging identifier has been truncated
// this warning comes up when using some STL containers
#pragma warning( disable : 4786 )
#endif
#endif
#include "MeshSet.hpp"
#include "AEntityFactory.hpp"
namespace moab
{
/*****************************************************************************************
* Helper Function Declarations *
*****************************************************************************************/
/**\brief Insert into parent/child list */
static inline MeshSet::Count insert_in_vector( const MeshSet::Count count,
MeshSet::CompactList& list,
const EntityHandle h,
int& result );
/**\brief Remvoe from parent/child list */
static inline MeshSet::Count remove_from_vector( const MeshSet::Count count,
MeshSet::CompactList& list,
const EntityHandle h,
int& result );
/**\brief Resize MeshSet::CompactList. Returns pointer to storage */
static EntityHandle* resize_compact_list( MeshSet::Count& count, MeshSet::CompactList& clist, size_t new_list_size );
/**\brief Methods to insert/remove range-based data from contents list.
* Templatized to operate on both Range and set-based MeshSets.
*/
template < typename pair_iter_t >
class range_tool
{
public:
/** Insert range-based data into range-based MeshSet */
inline static ErrorCode ranged_insert_entities( MeshSet::Count& count,
MeshSet::CompactList& clist,
pair_iter_t begin,
pair_iter_t end,
EntityHandle my_handle,
AEntityFactory* adj );
/** Remove range-based data from range-based MeshSet */
inline static ErrorCode ranged_remove_entities( MeshSet::Count& count,
MeshSet::CompactList& clist,
pair_iter_t begin,
pair_iter_t end,
EntityHandle my_handle,
AEntityFactory* adj );
/** Insert range-based data into list-based MeshSet */
inline static ErrorCode vector_insert_entities( MeshSet::Count& count,
MeshSet::CompactList& clist,
pair_iter_t begin,
pair_iter_t end,
EntityHandle my_handle,
AEntityFactory* adj );
};
/** Remove Range of handles fromr vector-based MeshSet */
static ErrorCode vector_remove_range( MeshSet::Count& count,
MeshSet::CompactList& clist,
const Range& range,
EntityHandle my_handle,
AEntityFactory* adj );
/** Remove range-based MeshSet contents from vector-based MeshSet */
static ErrorCode vector_remove_ranges( MeshSet::Count& count,
MeshSet::CompactList& clist,
const EntityHandle* pair_list,
size_t num_pairs,
EntityHandle my_handle,
AEntityFactory* adj );
/** Remove unsorted array of handles from vector-based MeshSet */
static ErrorCode vector_remove_vector( MeshSet::Count& count,
MeshSet::CompactList& clist,
const EntityHandle* vect,
size_t vect_size,
EntityHandle my_handle,
AEntityFactory* adj );
/** Insert unsorted array of handles into vector-based MeshSet */
static ErrorCode vector_insert_vector( MeshSet::Count& count,
MeshSet::CompactList& clist,
const EntityHandle* vect,
size_t vect_size,
EntityHandle my_handle,
AEntityFactory* adj );
/** Convert unsorted array of handles into array of ranged [begin,end] pairs */
static void convert_to_ranges( const EntityHandle* vect_in, size_t vect_in_len, std::vector< EntityHandle >& vect_out );
/*****************************************************************************************
* Parent/Child Operations *
*****************************************************************************************/
static inline MeshSet::Count insert_in_vector( const MeshSet::Count count,
MeshSet::CompactList& list,
const EntityHandle h,
int& result )
{
switch( count )
{
case MeshSet::ZERO:
list.hnd[0] = h;
result = true;
return MeshSet::ONE;
case MeshSet::ONE:
if( list.hnd[0] == h )
{
result = false;
return MeshSet::ONE;
}
else
{
result = true;
list.hnd[1] = h;
return MeshSet::TWO;
}
case MeshSet::TWO:
if( list.hnd[0] == h || list.hnd[1] == h )
{
result = false;
return MeshSet::TWO;
}
else
{
EntityHandle* ptr = (EntityHandle*)malloc( 3 * sizeof( EntityHandle ) );
ptr[0] = list.hnd[0];
ptr[1] = list.hnd[1];
ptr[2] = h;
list.ptr[0] = ptr;
list.ptr[1] = ptr + 3;
result = true;
return MeshSet::MANY;
}
case MeshSet::MANY:
if( std::find( list.ptr[0], list.ptr[1], h ) != list.ptr[1] )
{
result = false;
}
else
{
int size = list.ptr[1] - list.ptr[0];
list.ptr[0] = (EntityHandle*)realloc( list.ptr[0], ( size + 1 ) * sizeof( EntityHandle ) );
list.ptr[0][size] = h;
list.ptr[1] = list.ptr[0] + size + 1;
result = true;
}
return MeshSet::MANY;
}
return MeshSet::ZERO;
}
static inline MeshSet::Count remove_from_vector( const MeshSet::Count count,
MeshSet::CompactList& list,
const EntityHandle h,
int& result )
{
switch( count )
{
case MeshSet::ZERO:
result = false;
return MeshSet::ZERO;
case MeshSet::ONE:
if( h == list.hnd[0] )
{
result = true;
return MeshSet::ZERO;
}
else
{
result = false;
return MeshSet::ONE;
}
case MeshSet::TWO:
if( h == list.hnd[0] )
{
list.hnd[0] = list.hnd[1];
result = true;
return MeshSet::ONE;
}
else if( h == list.hnd[1] )
{
result = true;
return MeshSet::ONE;
}
else
{
result = false;
return MeshSet::TWO;
}
case MeshSet::MANY: {
EntityHandle *i, *j, *p;
i = std::find( list.ptr[0], list.ptr[1], h );
if( i == list.ptr[1] )
{
result = false;
return MeshSet::MANY;
}
result = true;
p = list.ptr[1] - 1;
while( i != p )
{
j = i + 1;
*i = *j;
i = j;
}
int size = p - list.ptr[0];
if( size == 2 )
{
p = list.ptr[0];
list.hnd[0] = p[0];
list.hnd[1] = p[1];
free( p );
return MeshSet::TWO;
}
else
{
list.ptr[0] = (EntityHandle*)realloc( list.ptr[0], size * sizeof( EntityHandle ) );
list.ptr[1] = list.ptr[0] + size;
return MeshSet::MANY;
}
}
}
return MeshSet::ZERO;
}
int MeshSet::add_parent( EntityHandle parent )
{
int result = 0;
mParentCount = insert_in_vector( (Count)mParentCount, parentMeshSets, parent, result );
return result;
}
int MeshSet::add_child( EntityHandle child )
{
int result = 0;
mChildCount = insert_in_vector( (Count)mChildCount, childMeshSets, child, result );
return result;
}
int MeshSet::remove_parent( EntityHandle parent )
{
int result = 0;
mParentCount = remove_from_vector( (Count)mParentCount, parentMeshSets, parent, result );
return result;
}
int MeshSet::remove_child( EntityHandle child )
{
int result = 0;
mChildCount = remove_from_vector( (Count)mChildCount, childMeshSets, child, result );
return result;
}
/*****************************************************************************************
* Flag Conversion Operations *
*****************************************************************************************/
ErrorCode MeshSet::convert( unsigned flg, EntityHandle my_handle, AEntityFactory* adj )
{
ErrorCode rval = MB_SUCCESS;
if( ( mFlags & MESHSET_TRACK_OWNER ) && !( flg & MESHSET_TRACK_OWNER ) )
rval = remove_adjacencies( my_handle, adj );
else if( !( mFlags & MESHSET_TRACK_OWNER ) && ( flg & MESHSET_TRACK_OWNER ) )
rval = create_adjacencies( my_handle, adj );
if( MB_SUCCESS != rval ) return rval;
if( !( mFlags & MESHSET_ORDERED ) && ( flg & MESHSET_ORDERED ) )
{
size_t datalen;
EntityHandle* data = get_contents( datalen );
if( datalen )
{
std::vector< EntityHandle > list( datalen );
memcpy( &list[0], data, datalen * sizeof( EntityHandle ) );
int num_ents = num_entities();
Count count = (Count)mContentCount;
data = resize_compact_list( count, contentList, num_ents );
mContentCount = count;
assert( list.size() % 2 == 0 );
std::vector< EntityHandle >::iterator i = list.begin();
while( i != list.end() )
{
EntityHandle h = *i;
++i;
EntityHandle e = *i;
++i;
for( ; h <= e; ++h )
{
*data = h;
++data;
}
}
}
}
else if( ( mFlags & MESHSET_ORDERED ) && !( flg & MESHSET_ORDERED ) )
{
size_t datalen;
EntityHandle* data = get_contents( datalen );
if( datalen )
{
std::vector< EntityHandle > ranges;
convert_to_ranges( data, datalen, ranges );
Count count = (Count)mContentCount;
data = resize_compact_list( count, contentList, ranges.size() );
mContentCount = count;
memcpy( data, &ranges[0], ranges.size() * sizeof( EntityHandle ) );
}
}
return MB_SUCCESS;
}
ErrorCode MeshSet::create_adjacencies( EntityHandle my_handle, AEntityFactory* adj )
{
ErrorCode rval = MB_SUCCESS;<--- Variable 'rval' is assigned a value that is never used.
;
size_t count;
const EntityHandle* const ptr = get_contents( count );
const EntityHandle* const end = ptr + count;
if( vector_based() )
{
for( const EntityHandle* i = ptr; i != end; ++i )
{
rval = adj->add_adjacency( *i, my_handle, false );
if( MB_SUCCESS != rval )
{
for( const EntityHandle* j = ptr; j != i; ++j )
adj->remove_adjacency( *j, my_handle );
return rval;
}
}
}
else
{
assert( 0 == count % 2 );
for( const EntityHandle* i = ptr; i != end; i += 2 )
{
for( EntityHandle h = i[0]; h <= i[1]; ++h )
{
rval = adj->add_adjacency( h, my_handle, false );
if( MB_SUCCESS != rval )
{
for( EntityHandle j = i[0]; j < h; ++j )
adj->remove_adjacency( j, my_handle );
for( const EntityHandle* j = ptr; j != i; j += 2 )
for( EntityHandle k = j[0]; k <= j[1]; ++k )
adj->remove_adjacency( k, my_handle );
return rval;
}
}
}
}
return MB_SUCCESS;
}
ErrorCode MeshSet::remove_adjacencies( EntityHandle my_handle, AEntityFactory* adj )
{
size_t count;
const EntityHandle* const ptr = get_contents( count );
const EntityHandle* const end = ptr + count;
if( vector_based() )
{
for( const EntityHandle* i = ptr; i != end; ++i )
adj->remove_adjacency( *i, my_handle );
}
else
{
assert( 0 == count % 2 );
for( const EntityHandle* i = ptr; i != end; i += 2 )
for( EntityHandle h = i[0]; h <= i[1]; ++h )
adj->remove_adjacency( h, my_handle );
}
return MB_SUCCESS;
}
/*****************************************************************************************
* Contents Modifiction Methods *
*****************************************************************************************/
static EntityHandle* resize_compact_list( MeshSet::Count& count, MeshSet::CompactList& clist, size_t new_list_size )
{
if( count <= 2 )
{
if( new_list_size <= 2 )
{
count = (MeshSet::Count)new_list_size;
return clist.hnd;
}
else
{
EntityHandle* list = (EntityHandle*)malloc( new_list_size * sizeof( EntityHandle ) );
list[0] = clist.hnd[0];
list[1] = clist.hnd[1];
clist.ptr[0] = list;
clist.ptr[1] = list + new_list_size;
count = MeshSet::MANY;
return list;
}
}
else if( new_list_size > 2 )
{
if( new_list_size > (size_t)( clist.ptr[1] - clist.ptr[0] ) )
clist.ptr[0] = (EntityHandle*)realloc( clist.ptr[0], new_list_size * sizeof( EntityHandle ) );
clist.ptr[1] = clist.ptr[0] + new_list_size;
count = MeshSet::MANY;
return clist.ptr[0];
}
else
{
EntityHandle* list = clist.ptr[0];
clist.hnd[0] = list[0];
clist.hnd[1] = list[1];
free( list );
count = (MeshSet::Count)new_list_size;
return clist.hnd;
}
}
typedef std::pair< EntityHandle, EntityHandle > MeshSetRange;
class MeshSetRComp
{
public:
bool operator()( const MeshSetRange& r, const MeshSetRange& h )
{
return r.second < h.first;
}
};
template < typename pair_iter_t >
inline ErrorCode range_tool< pair_iter_t >::ranged_insert_entities( MeshSet::Count& count,
MeshSet::CompactList& clist,
pair_iter_t begin,
pair_iter_t end,
EntityHandle my_handle,
AEntityFactory* adj )
{
// first pass:
// 1) merge existing ranges
// 2) count number of new ranges that must be inserted
EntityHandle* list_ptr;
size_t list_size;
if( count < MeshSet::MANY )
{
list_ptr = clist.hnd;
list_size = count;
}
else
{
list_ptr = clist.ptr[0];
list_size = clist.ptr[1] - clist.ptr[0];
}
MeshSetRange* list = reinterpret_cast< MeshSetRange* >( list_ptr );
assert( 0 == list_size % 2 );
assert( 2 * sizeof( EntityHandle ) == sizeof( MeshSetRange ) );
list_size /= 2;
MeshSetRange* const list_end = list + list_size;
MeshSetRange *list_read = list, *list_write = list;
pair_iter_t i = begin;
// count number of range pairs that are straight insertions
// (don't overlap any range pair in the current set) that
// could not be inserted during the first pass.
size_t insert_count = 0;
// merge lists
while( i != end )
{
// find first range that intersects the current input range
// do binary search if no holes in current set contents
if( list_read == list_write )
{
// subtract one from i->first because if it is one greater
// then the the last value of some block, then we want that
// block to append to.
MeshSetRange tmp;
tmp.first = i->first - 1;
tmp.second = i->second;
list_write = std::lower_bound( list_read, list_end, tmp, MeshSetRComp() );
list_read = list_write;
}
// otherwise shift down until we find where we find a range block
// that intersects
else
while( list_read != list_end && list_read->second + 1 < i->first )
{
*list_write = *list_read;
++list_write;
++list_read;
}
// handle any straight insertions of range blocks
for( ; i != end && ( list_read == list_end || i->second + 1 < list_read->first ); ++i )
{
// If we haven't removed any range pairs, we don't have space to
// insert here. Defer the insertion until later.
if( list_read == list_write )
{
++insert_count;
}
else
{
if( adj )
for( EntityHandle j = i->first; j <= i->second; ++j )
adj->add_adjacency( j, my_handle, false );
list_write->first = i->first;
list_write->second = i->second;
++list_write;
}
}
// merge all the stuff that gets merged into a single range pair
// from both the input list and the existing set data
if( list_read != list_end )
{
MeshSetRange working = *list_read; // copy because might be the same as list_write
++list_read;
// Check if we need to prepend to the existing block.
// We only need to check this for the first input range because
// after this working.first will always be the first possible handle
// in the merged set of ranges.
if( i != end && i->first < working.first && i->second + 1 >= working.first )
{
if( adj )
for( EntityHandle h = i->first; h < working.first; ++h )
adj->add_adjacency( h, my_handle, false );
working.first = i->first;
}
// Now append from the input list and the remaining set contents
// until we've consolidated all overlapping/touching ranges.
bool done = false;
while( !done )
{
// does next set contents range touch working range?
bool set_overlap = list_read != list_end && list_read->first <= working.second + 1;
// does next input range touch working range?
bool inp_overlap = i != end && i->first <= working.second + 1;
// if both ranges touch...
if( inp_overlap && set_overlap )
{
// if next set range is contained in working, skip it
if( list_read->second <= working.second ) ++list_read;
// if next input range is contained in working, skip it
else if( i->second <= working.second )
++i;
// Otherwise set the working end to the smaller of the two
// ends: either the next set end or the next input end.
// We want the smaller of the two because the larger might
// intersect additional ranges in the other list.
else if( list_read->second <= i->second )
{
working.second = list_read->second;
++list_read;
}
else
{
working.second = i->second;
++i;
}
}
// If only the input range intersect the current working range...
else if( inp_overlap )
{
// Would it be more efficient to just extent 'working' to
// the end of the current input range? We'd end up adding
// adjacencies for for entities that are already in the tracking
// set and therefore already have the adjacency.
EntityHandle last = i->second;
if( list_read != list_end && list_read->first < last )
last = list_read->first - 1;
else
++i;
if( last > working.second )
{
if( adj )
for( EntityHandle h = working.second + 1; h <= last; ++h )
adj->add_adjacency( h, my_handle, false );
working.second = last;
}
}
else if( set_overlap )
{
if( working.second < list_read->second ) working.second = list_read->second;
++list_read;
}
else
{
done = true;
}
}
assert( list_write < list_read );
*list_write = working;
++list_write;
}
}
// shuffle down entries to fill holes
if( list_read == list_write )
list_read = list_write = list_end;<--- list_read is assigned
else
while( list_read < list_end )
{
*list_write = *list_read;
++list_read;
++list_write;
}
// adjust allocated array size
const size_t occupied_size = list_write - list;
const size_t new_list_size = occupied_size + insert_count;
list_ptr = resize_compact_list( count, clist, 2 * new_list_size );
// done?
if( !insert_count ) return MB_SUCCESS;
list = reinterpret_cast< MeshSetRange* >( list_ptr );
// Second pass: insert non-mergable range pairs
// All range pairs in the input are either completely disjoint from
// the ones in the mesh set and must be inserted or are entirely contained
// within a range pair in the mesh set.
assert( begin != end ); // can't have items to insert if given empty input list
pair_iter_t ri = end;
--ri;
list_write = list + new_list_size - 1;
list_read = list + occupied_size - 1;<--- list_read is overwritten
for( ; list_write >= list; --list_write )
{
if( list_read >= list )
{
while( ri->first >= list_read->first && ri->second <= list_read->second )
{
assert( ri != begin );
--ri;
}
if( list_read->first > ri->second )
{
*list_write = *list_read;
--list_read;
continue;
}
}
assert( insert_count > 0 );
if( adj )
for( EntityHandle h = ri->first; h <= ri->second; ++h )
adj->add_adjacency( h, my_handle, false );
list_write->first = ri->first;
list_write->second = ri->second;
// don't have reverse iterator, so check before decrement
// if insert_count isn't zero, must be more in range
if( 0 == --insert_count )
{
assert( list_read == list_write - 1 );
break;
}
else
{
--ri;
}
}
assert( !insert_count );
return MB_SUCCESS;
}
template < typename pair_iter_t >
inline ErrorCode range_tool< pair_iter_t >::ranged_remove_entities( MeshSet::Count& count,
MeshSet::CompactList& clist,
pair_iter_t begin,
pair_iter_t end,
EntityHandle my_handle,
AEntityFactory* adj )
{
// first pass:
// 1) remove (from) existing ranges
// 2) count number of ranges that must be split
ptrdiff_t split_count = 0;
EntityHandle* list;
size_t list_size;
if( count < MeshSet::MANY )
{
list = clist.hnd;
list_size = count;
}
else
{
list = clist.ptr[0];
list_size = clist.ptr[1] - clist.ptr[0];
}
EntityHandle* list_write = list;
EntityHandle *const list_end = list + list_size, *list_read = list;
pair_iter_t i = begin;
while( list_read != list_end && i != end )
{
while( i != end && i->second < list_read[0] )
++i;
if( i == end ) break;
// if there are holes in the current array, shuffle blocks
// down until we find the next block to remove
if( list_read != list_write )
{
while( list_read != list_end && i->second < list_read[0] )
{
list_write[0] = list_read[0];
list_write[1] = list_read[1];
list_write += 2;
list_read += 2;
}
}
// otherwise do a binary search
else
{
list_write = std::lower_bound( list_write, list_end, i->first );
// if in middle of range block (odd index), back up to start of block
list_write -= ( list_write - list ) % 2;
list_read = list_write;
}
// if everything remaning is past end of set contents...
if( list_read == list_end ) break;
// skip any remove pairs that aren't in the list
if( i->second < list_read[0] )
{
++i;
continue;
}
// Begin by assuming that we will keep the entire block
list_write[0] = list_read[0];
list_write[1] = list_read[1];
list_read += 2;
for( ; i != end && i->first <= list_write[1]; ++i )
{
if( i->first <= list_write[0] )
{
// remove whole block
if( i->second >= list_write[1] )
{
if( adj )
for( EntityHandle h = list_write[0]; h <= list_write[1]; ++h )
adj->remove_adjacency( h, my_handle );
list_write -= 2;
break;
}
// remove from start of block
else if( i->second >= list_write[0] )
{
if( adj )
for( EntityHandle h = list_write[0]; h <= i->second; ++h )
adj->remove_adjacency( h, my_handle );
list_write[0] = i->second + 1;
}
}
else if( i->first <= list_write[1] )
{
// remove from end of block
if( i->second >= list_write[1] )
{
if( adj )
for( EntityHandle h = i->first; h <= list_write[1]; ++h )
adj->remove_adjacency( h, my_handle );
list_write[1] = i->first - 1;
// list_write += 2;
break;
}
// split block
else
{
if( adj )
for( EntityHandle h = i->first; h <= i->second; ++h )
adj->remove_adjacency( h, my_handle );
if( list_read - list_write <= 2 )
{
++split_count;
continue;
}
else
{
list_write[3] = list_write[1];
list_write[1] = i->first - 1;
list_write[2] = i->second + 1;
list_write += 2;
}
}
}
}
list_write += 2;
}
// shuffle down entries to fill holes
if( list_read == list_write )
list_read = list_write = list_end;<--- list_read is assigned
else
while( list_read < list_end )
{
list_write[0] = list_read[0];
list_write[1] = list_read[1];
list_read += 2;
list_write += 2;
}
// adjust allocated array size
const size_t occupied_size = list_write - list;
const size_t new_list_size = occupied_size + 2 * split_count;
list = resize_compact_list( count, clist, new_list_size );
// done?
if( !split_count ) return MB_SUCCESS;
// Second pass: split range pairs
// All range pairs in the input are either already removed or
// require one of the existing range pairs to be split
assert( begin != end ); // can't have ranges to split if given empty input list
pair_iter_t ri = end;
--ri;
list_write = list + new_list_size - 2;
list_read = list + occupied_size - 2;<--- list_read is overwritten
for( ; list_write >= list; list_write -= 2 )
{
if( list_read >= list )
{
while( ri->second > list_read[1] )
{
assert( ri != begin );
--ri;
}
if( list_read[0] > ri->second )
{
list_write[0] = list_read[0];
list_write[1] = list_read[1];
list_read -= 2;
continue;
}
}
assert( split_count > 0 );
list_write[0] = ri->second + 1;
list_write[1] = list_read[1];
list_read[1] = ri->first - 1;
// don't have reverse iterator, so check before decrement
// if insert_count isn't zero, must be more in range
if( 0 == --split_count )
{
assert( list_read == list_write - 2 );
break;
}
else
{
--ri;
}
}
assert( !split_count );
return MB_SUCCESS;
}
template < typename pair_iter_t >
inline ErrorCode range_tool< pair_iter_t >::vector_insert_entities( MeshSet::Count& count,
MeshSet::CompactList& clist,
pair_iter_t begin,
pair_iter_t end,
EntityHandle my_handle,
AEntityFactory* adj )
{
const size_t init_size = count < MeshSet::MANY ? (int)count : clist.ptr[1] - clist.ptr[0];
size_t add_size = 0;
for( pair_iter_t i = begin; i != end; ++i )
add_size += i->second - i->first + 1;
EntityHandle* list = resize_compact_list( count, clist, init_size + add_size );
EntityHandle* li = list + init_size;
for( pair_iter_t i = begin; i != end; ++i )
{
for( EntityHandle h = i->first; h <= i->second; ++h )
{
if( adj ) adj->add_adjacency( h, my_handle, false );
*li = h;
++li;
}
}
return MB_SUCCESS;
}
static ErrorCode vector_remove_range( MeshSet::Count& count,
MeshSet::CompactList& clist,
const Range& range,
EntityHandle my_handle,
AEntityFactory* adj )
{
EntityHandle* list;
size_t list_size;
if( count < MeshSet::MANY )
{
list = clist.hnd;
list_size = count;
}
else
{
list = clist.ptr[0];
list_size = clist.ptr[1] - clist.ptr[0];
}
const EntityHandle* const list_end = list + list_size;
EntityHandle* list_write = list;
for( const EntityHandle* list_read = list; list_read != list_end; ++list_read )
{
if( range.find( *list_read ) == range.end() )
{ // keep
*list_write = *list_read;
++list_write;
}
else if( adj )
{
adj->remove_adjacency( *list_read, my_handle );
}
}
resize_compact_list( count, clist, list_write - list );
return MB_SUCCESS;
}
static ErrorCode vector_remove_ranges( MeshSet::Count& count,
MeshSet::CompactList& clist,
const EntityHandle* pair_list,
size_t num_pairs,
EntityHandle my_handle,
AEntityFactory* adj )
{
EntityHandle* list;
size_t list_size;
if( count < MeshSet::MANY )
{
list = clist.hnd;
list_size = count;
}
else
{
list = clist.ptr[0];
list_size = clist.ptr[1] - clist.ptr[0];
}
const EntityHandle *const list_end = list + list_size, *const input_end = pair_list + 2 * num_pairs;
EntityHandle* list_write = list;
for( const EntityHandle* list_read = list; list_read != list_end; ++list_read )
{
const EntityHandle* ptr = std::lower_bound( pair_list, input_end, *list_read );
if( ( ptr != input_end && ( *ptr == *list_read || ( ptr - pair_list ) % 2 ) ) && // if in delete list
std::find( list_read + 1, list_end, *list_read ) == list_end )
{ // and is last occurance in list
// only remove adj if no previous occurance
if( adj && std::find( list, list_write, *list_read ) == list_write )
adj->remove_adjacency( *list_read, my_handle );
}
else
{
*list_write = *list_read;
++list_write;
}
}
resize_compact_list( count, clist, list_write - list );
return MB_SUCCESS;
}
static ErrorCode vector_remove_vector( MeshSet::Count& count,
MeshSet::CompactList& clist,
const EntityHandle* vect,
size_t vect_size,
EntityHandle my_handle,
AEntityFactory* adj )
{
EntityHandle* list;
size_t list_size;
if( count < MeshSet::MANY )
{
list = clist.hnd;
list_size = count;
}
else
{
list = clist.ptr[0];
list_size = clist.ptr[1] - clist.ptr[0];
}
const EntityHandle *const list_end = list + list_size, *const input_end = vect + vect_size;
EntityHandle* list_write = list;
for( const EntityHandle* list_read = list; list_read != list_end; ++list_read )
{
if( std::find( vect, input_end, *list_read ) != input_end && // if in delete list
std::find( list_read + 1, list_end, *list_read ) == list_end )
{ // and is last occurance in list
// only remove adj if no previous occurance?
if( adj ) // && std::find(list, list_write, *list_read) == list_write)
adj->remove_adjacency( *list_read, my_handle );
}
else
{
*list_write = *list_read;
++list_write;
}
}
resize_compact_list( count, clist, list_write - list );
return MB_SUCCESS;
}
static ErrorCode vector_insert_vector( MeshSet::Count& count,
MeshSet::CompactList& clist,
const EntityHandle* vect,
size_t vect_size,
EntityHandle my_handle,
AEntityFactory* adj )
{
const size_t orig_size = count < MeshSet::MANY ? (int)count : clist.ptr[1] - clist.ptr[0];
EntityHandle* list = resize_compact_list( count, clist, orig_size + vect_size );
if( adj )
for( size_t i = 0; i < vect_size; ++i )
adj->add_adjacency( vect[i], my_handle, false );
memcpy( list + orig_size, vect, sizeof( EntityHandle ) * vect_size );
return MB_SUCCESS;
}
ErrorCode MeshSet::insert_entity_ranges( const EntityHandle* range_vect,
size_t len,
EntityHandle my_h,
AEntityFactory* adj )
{
typedef const std::pair< EntityHandle, EntityHandle >* pair_vect_t;
pair_vect_t pair_vect = reinterpret_cast< pair_vect_t >( range_vect );
MeshSet::Count count = static_cast< MeshSet::Count >( mContentCount );
ErrorCode rval;
if( !vector_based() )
rval = range_tool< pair_vect_t >::ranged_insert_entities( count, contentList, pair_vect, pair_vect + len / 2,
my_h, tracking() ? adj : 0 );
else
rval = range_tool< pair_vect_t >::vector_insert_entities( count, contentList, pair_vect, pair_vect + len / 2,
my_h, tracking() ? adj : 0 );
mContentCount = count;
return rval;
}
ErrorCode MeshSet::insert_entity_ranges( const Range& range, EntityHandle my_h, AEntityFactory* adj )
{
ErrorCode rval;
MeshSet::Count count = static_cast< MeshSet::Count >( mContentCount );
if( !vector_based() )
rval = range_tool< Range::const_pair_iterator >::ranged_insert_entities(
count, contentList, range.const_pair_begin(), range.const_pair_end(), my_h, tracking() ? adj : 0 );
else
rval = range_tool< Range::const_pair_iterator >::vector_insert_entities(
count, contentList, range.const_pair_begin(), range.const_pair_end(), my_h, tracking() ? adj : 0 );
mContentCount = count;
return rval;
}
ErrorCode MeshSet::remove_entity_ranges( const EntityHandle* range_vect,
size_t len,
EntityHandle my_h,
AEntityFactory* adj )
{
ErrorCode rval;
MeshSet::Count count = static_cast< MeshSet::Count >( mContentCount );
if( vector_based() )
rval = vector_remove_ranges( count, contentList, range_vect, len / 2, my_h, tracking() ? adj : 0 );
else
{
typedef const std::pair< EntityHandle, EntityHandle >* pair_vect_t;
pair_vect_t pair_vect = reinterpret_cast< pair_vect_t >( range_vect );
rval = range_tool< pair_vect_t >::ranged_remove_entities( count, contentList, pair_vect, pair_vect + len / 2,
my_h, tracking() ? adj : 0 );
}
mContentCount = count;
return rval;
}
ErrorCode MeshSet::remove_entity_ranges( const Range& range, EntityHandle my_h, AEntityFactory* adj )
{
ErrorCode rval;
MeshSet::Count count = static_cast< MeshSet::Count >( mContentCount );
if( vector_based() )
rval = vector_remove_range( count, contentList, range, my_h, tracking() ? adj : 0 );
else
rval = range_tool< Range::const_pair_iterator >::ranged_remove_entities(
count, contentList, range.const_pair_begin(), range.const_pair_end(), my_h, tracking() ? adj : 0 );
mContentCount = count;
return rval;
}
ErrorCode MeshSet::intersect( const MeshSet* other, EntityHandle my_handle, AEntityFactory* adj )
{
ErrorCode rval;
if( !vector_based() && !other->vector_based() )
{
size_t other_count = 0;
const EntityHandle* other_vect = other->get_contents( other_count );
if( !other_count ) return clear( my_handle, adj );
assert( 0 == other_count % 2 );
std::vector< EntityHandle > compliment;
compliment.reserve( other_count + 4 );
if( *other_vect > 0 )
{
compliment.push_back( 0 );
compliment.push_back( *other_vect - 1 );
}
++other_vect;
const EntityHandle* const other_end = other_vect + other_count - 2;
for( ; other_vect < other_end; other_vect += 2 )
{
compliment.push_back( other_vect[0] + 1 );
compliment.push_back( other_vect[1] - 1 );
}
if( *other_vect < ~(EntityHandle)0 )
{
compliment.push_back( *other_vect + 1 );
compliment.push_back( ~(EntityHandle)0 );
}
return remove_entity_ranges( &compliment[0], compliment.size(), my_handle, adj );
}
else
{
Range my_ents, other_ents;
rval = get_entities( my_ents );
if( MB_SUCCESS != rval ) return rval;
rval = other->get_entities( other_ents );<--- Variable 'rval' is assigned a value that is never used.
return remove_entities( moab::subtract( my_ents, other_ents ), my_handle, adj );
}
}
static void convert_to_ranges( const EntityHandle* vect_in, size_t vect_in_len, std::vector< EntityHandle >& vect_out )
{
vect_out.reserve( 2 * vect_in_len );
vect_out.resize( vect_in_len );
std::copy( vect_in, vect_in + vect_in_len, vect_out.begin() );
std::sort( vect_out.begin(), vect_out.end() );
vect_out.erase( std::unique( vect_out.begin(), vect_out.end() ), vect_out.end() );
// duplicate all entries
vect_out.resize( vect_out.size() * 2 );
for( long i = vect_out.size() - 1; i >= 0; --i )
vect_out[i] = vect_out[i / 2];
// compact adjacent ranges
std::vector< EntityHandle >::iterator r = vect_out.begin(), w = vect_out.begin();
while( r != vect_out.end() )
{
*w = *r;
++w;
++r;
*w = *r;
++r;
while( r != vect_out.end() && *w + 1 == *r )
{
++r;
*w = *r;
++r;
}
++w;
}
// remove extra space
vect_out.erase( w, vect_out.end() );
}
ErrorCode MeshSet::insert_entity_vector( const EntityHandle* vect, size_t len, EntityHandle my_h, AEntityFactory* adj )
{
MeshSet::Count count = static_cast< MeshSet::Count >( mContentCount );
ErrorCode rval;
if( vector_based() )
rval = vector_insert_vector( count, contentList, vect, len, my_h, tracking() ? adj : 0 );
else
{
std::vector< EntityHandle > rangevect;
convert_to_ranges( vect, len, rangevect );
typedef const std::pair< EntityHandle, EntityHandle >* pair_vect_t;
pair_vect_t pair_vect = ( rangevect.empty() ) ? NULL : reinterpret_cast< pair_vect_t >( &rangevect[0] );
rval = range_tool< pair_vect_t >::ranged_insert_entities(
count, contentList, pair_vect, pair_vect + rangevect.size() / 2, my_h, tracking() ? adj : 0 );
}
mContentCount = count;
return rval;
}
ErrorCode MeshSet::remove_entity_vector( const EntityHandle* vect, size_t len, EntityHandle my_h, AEntityFactory* adj )
{
MeshSet::Count count = static_cast< MeshSet::Count >( mContentCount );
ErrorCode rval;
if( vector_based() )
rval = vector_remove_vector( count, contentList, vect, len, my_h, tracking() ? adj : 0 );
else
{
std::vector< EntityHandle > rangevect;
convert_to_ranges( vect, len, rangevect );
typedef const std::pair< EntityHandle, EntityHandle >* pair_vect_t;
pair_vect_t pair_vect = ( rangevect.empty() ) ? NULL : reinterpret_cast< pair_vect_t >( &rangevect[0] );
rval = range_tool< pair_vect_t >::ranged_remove_entities(
count, contentList, pair_vect, pair_vect + rangevect.size() / 2, my_h, tracking() ? adj : 0 );
}
mContentCount = count;
return rval;
}
ErrorCode MeshSet::replace_entities( EntityHandle my_handle,
const EntityHandle* old_entities,
const EntityHandle* new_entities,
size_t num_ents,
AEntityFactory* adjfact )
{
if( vector_based() )
{
ErrorCode result = MB_SUCCESS;
size_t count;
EntityHandle* vect = get_contents( count );
EntityHandle* const vect_end = vect + count;
for( size_t i = 0; i < num_ents; ++i )
{
EntityHandle* p = std::find( vect, vect_end, old_entities[i] );
if( p == vect_end )
{
result = MB_ENTITY_NOT_FOUND;
}
else
do
{
if( tracking() )
{
adjfact->remove_adjacency( *p, my_handle );
adjfact->add_adjacency( new_entities[i], my_handle, false );
}
*p = new_entities[i];
p = std::find( p + 1, vect_end, old_entities[i] );
} while( p != vect_end );
}
return result;
}
else
{
ErrorCode r1 = remove_entities( old_entities, num_ents, my_handle, adjfact );
ErrorCode r2 = add_entities( new_entities, num_ents, my_handle, adjfact );
return ( MB_SUCCESS == r2 ) ? r1 : r2;
}
}
/*****************************************************************************************
* Misc. Methods *
*****************************************************************************************/
unsigned long MeshSet::get_memory_use() const
{
unsigned long result = 0;
if( mParentCount == MANY ) result += parentMeshSets.ptr[1] - parentMeshSets.ptr[0];
if( mChildCount == MANY ) result += childMeshSets.ptr[1] - childMeshSets.ptr[0];
if( mContentCount == MANY ) result += contentList.ptr[1] - contentList.ptr[0];
return sizeof( EntityHandle ) * result;
}
} // namespace moab
|