-
Notifications
You must be signed in to change notification settings - Fork 47
/
concurrent_vector.h
executable file
·1971 lines (1800 loc) · 81.1 KB
/
concurrent_vector.h
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
/***
* ==++==
*
* Copyright (c) Microsoft Corporation. All rights reserved.
* Microsoft would like to acknowledge that this concurrency data structure implementation
* is based on the Intel implementation of its Threading Building Blocks ("Intel Material").
*
* ==--==
* =+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+
*
* concurrent_vector.h
*
* =-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
****/
/*
Intel Material Copyright 2005-2008 Intel Corporation. All Rights Reserved.
*/
#pragma once
#include <crtdefs.h>
#include <memory>
#include <iterator>
#include <limits>
#include <algorithm>
#include <cstring>
#include <crtdbg.h>
#include <concrt.h>
#define _PPL_CONTAINER
#if !(defined (_M_X64) || defined (_M_IX86) || defined (_M_ARM) || defined (_M_ARM64))
#error ERROR: Concurrency Runtime is supported only on X64, X86, ARM, and ARM64 architectures.
#endif /* !(defined (_M_X64) || defined (_M_IX86) || defined (_M_ARM) || defined (_M_ARM64)) */
#if defined (_M_CEE)
#error ERROR: Concurrency Runtime is not supported when compiling /clr.
#endif /* defined (_M_CEE) */
#pragma pack(push,_CRT_PACKING)
#pragma warning (push)
#pragma warning (disable: 4510 4512 4610) // disable warnings for compiler unable to generate constructor
#pragma push_macro("new")
#undef new
/// <summary>
/// The <c>Concurrency</c> namespace provides classes and functions that give you access to the Concurrency Runtime,
/// a concurrent programming framework for C++. For more information, see <see cref="Concurrency Runtime"/>.
/// </summary>
/**/
namespace Concurrency
{
template<typename _Ty, class _Ax = ::std::allocator<_Ty>>
class concurrent_vector;
namespace details
{
// Bad allocation marker.
#define _BAD_ALLOC_MARKER reinterpret_cast<void*>(63)
// Base class of concurrent vector implementation.
class _Concurrent_vector_base_v4
{
protected:
// Basic types declarations.
typedef size_t _Segment_index_t;
typedef size_t _Size_type;
// Size constants
static const _Segment_index_t _Default_initial_segments = 1; // 2 initial items
// Number of slots for segment's pointers inside the class
static const _Segment_index_t _Pointers_per_short_table = 3; // to fit into 8 words of entire structure
static const _Segment_index_t _Pointers_per_long_table = sizeof(_Segment_index_t) * 8; // one segment per bit
// Segment pointer. Can be zero-initialized.
struct _Segment_t
{
void* _My_array;
};
// Data fields
// allocator function pointer
void* (__cdecl *_My_vector_allocator_ptr)(_Concurrent_vector_base_v4 &, size_t);
// embedded storage of segment pointers
_Segment_t _My_storage[_Pointers_per_short_table];
// Methods
_Concurrent_vector_base_v4()
{
_My_early_size = 0;
_My_first_block = 0; // here is not _Default_initial_segments
_My_vector_allocator_ptr = nullptr;
for( _Segment_index_t _I = 0; _I < _Pointers_per_short_table; _I++)
_My_storage[_I]._My_array = nullptr;
_My_segment = _My_storage;
}
_CONCRTIMP ~_Concurrent_vector_base_v4();
_CONCRTIMP static _Segment_index_t __cdecl _Segment_index_of( _Size_type _Index );
static _Segment_index_t _Segment_base( _Segment_index_t _K )
{
return (_Segment_index_t(1)<<_K & ~_Segment_index_t(1));
}
static _Segment_index_t _Segment_base_index_of( _Segment_index_t &_Index )
{
_Segment_index_t _K = _Segment_index_of( _Index );
_Index -= _Segment_base(_K);
return _K;
}
static _Size_type _Segment_size( _Segment_index_t _K )
{
return _Segment_index_t(1)<<_K; // fake value for _K==0
}
// An operation on an n-element array starting at begin.
typedef void (__cdecl *_My_internal_array_op1)(void* _Begin, _Size_type _N );
// An operation on n-element destination array and n-element source array.
typedef void (__cdecl *_My_internal_array_op2)(void* _Dst, const void* _Src, _Size_type _N );
// Internal structure for shrink_to_fit().
struct _Internal_segments_table
{
_Segment_index_t _First_block;
void* _Table[_Pointers_per_long_table];
};
_CONCRTIMP void _Internal_reserve( _Size_type _N, _Size_type _Element_size, _Size_type _Max_size );
_CONCRTIMP _Size_type _Internal_capacity() const;
void _Internal_grow( _Size_type _Start, _Size_type _Finish, _Size_type _Element_size, _My_internal_array_op2 _Init, const void *_Src );
_Size_type _Internal_grow_segment( const _Size_type _Start, _Size_type _Finish, _Size_type _Element_size, _Segment_t** _PPSegment, _Size_type* _PSegStart, _Size_type* _PSegFinish );
_CONCRTIMP _Size_type _Internal_grow_by( _Size_type _Delta, _Size_type _Element_size, _My_internal_array_op2 _Init, const void *_Src );
_CONCRTIMP void* _Internal_push_back( _Size_type _Element_size, _Size_type& _Index );
_CONCRTIMP _Segment_index_t _Internal_clear( _My_internal_array_op1 _Destroy );
void _Internal_truncate( _Size_type _Old_size, _Size_type _New_size, _Size_type _Element_size, _My_internal_array_op1 _Destroy);
_CONCRTIMP void* _Internal_compact( _Size_type _Element_size, void *_Table, _My_internal_array_op1 _Destroy, _My_internal_array_op2 _Copy );
_CONCRTIMP void _Internal_copy( const _Concurrent_vector_base_v4& _Src, _Size_type _Element_size, _My_internal_array_op2 _Copy );
_CONCRTIMP void _Internal_assign( const _Concurrent_vector_base_v4& _Src, _Size_type _Element_size,
_My_internal_array_op1 _Destroy, _My_internal_array_op2 _Assign, _My_internal_array_op2 _Copy );
_CONCRTIMP void _Internal_throw_exception(_Size_type) const;
_CONCRTIMP void _Internal_swap(_Concurrent_vector_base_v4&);
_CONCRTIMP void _Internal_resize( _Size_type _New_size, _Size_type _Element_size, _Size_type _Max_size, _My_internal_array_op1 _Destroy, _My_internal_array_op2 _Init, const void* _Src);
_CONCRTIMP _Size_type _Internal_grow_to_at_least_with_result( _Size_type _New_size, _Size_type _Element_size, _My_internal_array_op2 _Init, const void *_Src );
// Count of segments in the first block.
_Subatomic<_Size_type> _My_first_block;
// Requested size of vector.
_Subatomic<_Size_type> _My_early_size;
// Pointer to the segments table.
_Subatomic<_Segment_t*> _My_segment;
private:
// Private functionality.
class _Helper;
friend class _Helper;
};
typedef _Concurrent_vector_base_v4 _Concurrent_vector_base;
// Meets requirements of a forward iterator for STL.*/
/** _Value is either the _Ty or const _Ty type of the container. */
template<typename _Container, typename _Value>
class _Vector_iterator
{
// concurrent_vector over which we are iterating.
_Container* _My_vector;
// Index into the vector.
size_t _My_index;
// Caches _My_vector->_Internal_subscript(_My_index)
/** NULL if cached value is not available */
mutable _Value* _My_item;
template<typename _C, typename _Ty>
friend _Vector_iterator<_C,_Ty> operator+( ptrdiff_t _Offset, const _Vector_iterator<_C,_Ty>& _Vec );
template<typename _C, typename _Ty, typename _U>
friend bool operator==( const _Vector_iterator<_C,_Ty>&, const _Vector_iterator<_C,_U>& );
template<typename _C, typename _Ty, typename _U>
friend bool operator<( const _Vector_iterator<_C,_Ty>&, const _Vector_iterator<_C,_U>& );
template<typename _C, typename _Ty, typename _U>
friend ptrdiff_t operator-( const _Vector_iterator<_C,_Ty>&, const _Vector_iterator<_C,_U>& );
template<typename _C, typename _U>
friend class ::Concurrency::details::_Vector_iterator;
template<typename _Ty, class _Ax>
friend class ::Concurrency::concurrent_vector;
_Vector_iterator( const _Container& _Vec, size_t _Index, void* _Ptr = nullptr )
: _My_vector(const_cast<_Container*>(&_Vec)),
_My_index(_Index),
_My_item(static_cast<_Value*>(_Ptr))
{
}
public:
// Default constructor
_Vector_iterator()
: _My_vector(nullptr), _My_index(~size_t(0)), _My_item(nullptr)
{
}
_Vector_iterator( const _Vector_iterator<_Container,typename _Container::value_type>& _Other )
: _My_vector(_Other._My_vector),
_My_index(_Other._My_index),
_My_item(_Other._My_item)
{
}
_Vector_iterator operator+( ptrdiff_t _Offset ) const
{
return _Vector_iterator( *_My_vector, _My_index+_Offset );
}
_Vector_iterator& operator+=( ptrdiff_t _Offset )
{
_My_index+=_Offset;
_My_item = nullptr;
return *this;
}
_Vector_iterator operator-( ptrdiff_t _Offset ) const
{
return _Vector_iterator( *_My_vector, _My_index-_Offset );
}
_Vector_iterator& operator-=( ptrdiff_t _Offset )
{
_My_index-=_Offset;
_My_item = nullptr;
return *this;
}
_Value& operator*() const
{
_Value* _Item = _My_item;
if( !_Item )
_Item = _My_item = &_My_vector->_Internal_subscript(_My_index);
_CONCRT_ASSERT( _Item==&_My_vector->_Internal_subscript(_My_index)); // corrupt cache
return *_Item;
}
_Value& operator[]( ptrdiff_t _K ) const
{
return _My_vector->_Internal_subscript(_My_index+_K);
}
_Value* operator->() const
{
return &operator*();
}
// Pre increment
_Vector_iterator& operator++()
{
size_t _K = ++_My_index;
if( _My_item )
{
// Following test uses 2's-complement wizardry.
if( (_K& (_K-2))==0 )
{
// _K is a power of two that is at least _K-2.
_My_item= nullptr;
}
else
{
++_My_item;
}
}
return *this;
}
// Pre decrement
_Vector_iterator& operator--()
{
_CONCRT_ASSERT( _My_index>0 ); // operator--() applied to iterator already at beginning of concurrent_vector.
size_t _K = _My_index--;
if( _My_item )
{
// Following test uses 2's-complement wizardry.
if( (_K& (_K-2))==0 )
{
// k is a power of two that is at least k-2.
_My_item= nullptr;
}
else
{
--_My_item;
}
}
return *this;
}
// Post increment
_Vector_iterator operator++(int)
{
_Vector_iterator _Result = *this;
operator++();
return _Result;
}
// Post decrement
_Vector_iterator operator--(int)
{
_Vector_iterator _Result = *this;
operator--();
return _Result;
}
// STL support
typedef ptrdiff_t difference_type;
typedef _Value value_type;
typedef _Value* pointer;
typedef _Value& reference;
typedef ::std::random_access_iterator_tag iterator_category;
};
template<typename _Container, typename _Ty>
_Vector_iterator<_Container,_Ty> operator+( ptrdiff_t _Offset, const _Vector_iterator<_Container,_Ty>& _Vec )
{
return _Vector_iterator<_Container,_Ty>( *_Vec._My_vector, _Vec._My_index+_Offset );
}
template<typename _Container, typename _Ty, typename _U>
bool operator==( const _Vector_iterator<_Container,_Ty>& _I, const _Vector_iterator<_Container,_U>& _J )
{
return _I._My_index==_J._My_index && _I._My_vector == _J._My_vector;
}
template<typename _Container, typename _Ty, typename _U>
bool operator!=( const _Vector_iterator<_Container,_Ty>& _I, const _Vector_iterator<_Container,_U>& _J )
{
return !(_I==_J);
}
template<typename _Container, typename _Ty, typename _U>
bool operator<( const _Vector_iterator<_Container,_Ty>& _I, const _Vector_iterator<_Container,_U>& _J )
{
return _I._My_index<_J._My_index && _I._My_vector == _J._My_vector;
}
template<typename _Container, typename _Ty, typename _U>
bool operator>( const _Vector_iterator<_Container,_Ty>& _I, const _Vector_iterator<_Container,_U>& _J )
{
return _J<_I;
}
template<typename _Container, typename _Ty, typename _U>
bool operator>=( const _Vector_iterator<_Container,_Ty>& _I, const _Vector_iterator<_Container,_U>& _J )
{
return !(_I<_J);
}
template<typename _Container, typename _Ty, typename _U>
bool operator<=( const _Vector_iterator<_Container,_Ty>& _I, const _Vector_iterator<_Container,_U>& _J )
{
return !(_J<_I);
}
template<typename _Container, typename _Ty, typename _U>
ptrdiff_t operator-( const _Vector_iterator<_Container,_Ty>& _I, const _Vector_iterator<_Container,_U>& _J )
{
return ptrdiff_t(_I._My_index)-ptrdiff_t(_J._My_index);
}
template<typename _Ty, class _Ax>
class _Allocator_base
{
public:
typedef typename ::std::allocator_traits<_Ax>::template
rebind_alloc<_Ty> _Allocator_type;
using _Allocator_traits = ::std::allocator_traits<_Allocator_type>;
_Allocator_type _My_allocator;
_Allocator_base()
: _My_allocator()
{
}
_Allocator_base(const _Allocator_type &_Al)
: _My_allocator(_Al)
{
}
};
} // namespace details
/// <summary>
/// The <c>concurrent_vector</c> class is a sequence container class that allows random access to any element.
/// It enables concurrency-safe append, element access, iterator access, and iterator traversal operations.
/// </summary>
/// <typeparam name="_Ty">
/// The data type of the elements to be stored in the vector.
/// </typeparam>
/// <typeparam name="_Ax">
/// The type that represents the stored allocator object that encapsulates details about the allocation and
/// deallocation of memory for the concurrent vector. This argument is optional and the default value is
/// <c>allocator<</c><typeparamref name="_Ty"/><c>></c>.
/// </typeparam>
/// <remarks>
/// For detailed information on the <c>concurrent_vector</c> class, see <see cref="Parallel Containers and Objects"/>.
/// </remarks>
/// <seealso cref="Parallel Containers and Objects"/>
/**/
template<typename _Ty, class _Ax>
class concurrent_vector: protected details::_Allocator_base<_Ty, _Ax>,
private details::_Concurrent_vector_base_v4
{
private:
typedef concurrent_vector<_Ty, _Ax> _Myt;
using typename details::_Allocator_base<_Ty, _Ax>::_Allocator_traits;
template<typename _C, typename _U>
friend class details::_Vector_iterator;
public:
/// <summary>
/// A type that counts the number of elements in a concurrent vector.
/// </summary>
/**/
typedef details::_Concurrent_vector_base_v4::_Size_type size_type;
/// <summary>
/// A type that represents the allocator class for the concurrent vector.
/// </summary>
/**/
typedef typename details::_Allocator_base<_Ty, _Ax>::_Allocator_type allocator_type;
/// <summary>
/// A type that represents the data type stored in a concurrent vector.
/// </summary>
/**/
typedef _Ty value_type;
/// <summary>
/// A type that provides the signed distance between two elements in a concurrent vector.
/// </summary>
/**/
typedef ptrdiff_t difference_type;
/// <summary>
/// A type that provides a reference to an element stored in a concurrent vector.
/// </summary>
/**/
typedef _Ty& reference;
/// <summary>
/// A type that provides a reference to a <c>const</c> element stored in a concurrent vector for reading and
/// performing <c>const</c> operations.
/// </summary>
/**/
typedef const _Ty& const_reference;
/// <summary>
/// A type that provides a pointer to an element in a concurrent vector.
/// </summary>
/**/
typedef _Ty *pointer;
/// <summary>
/// A type that provides a pointer to a <c>const</c> element in a concurrent vector.
/// </summary>
/**/
typedef const _Ty *const_pointer;
/// <summary>
/// A type that provides a random-access iterator that can read any element in a concurrent vector. Modification of an
/// element using the iterator is not concurrency-safe.
/// </summary>
/**/
typedef details::_Vector_iterator<concurrent_vector,_Ty> iterator;
/// <summary>
/// A type that provides a random-access iterator that can read a <c>const</c> element in a concurrent vector.
/// </summary>
/**/
typedef details::_Vector_iterator<concurrent_vector,const _Ty> const_iterator;
/// <summary>
/// A type that provides a random-access iterator that can read any element in a reversed concurrent vector. Modification of an
/// element using the iterator is not concurrency-safe.
/// </summary>
/**/
typedef ::std::reverse_iterator<iterator> reverse_iterator;
/// <summary>
/// A type that provides a random-access iterator that can read any <c>const</c> element in the concurrent vector.
/// </summary>
/**/
typedef ::std::reverse_iterator<const_iterator> const_reverse_iterator;
/// <summary>
/// Constructs a concurrent vector.
/// </summary>
/// <param name="_Al">
/// The allocator class to use with this object.
/// </param>
/// <remarks>
/// All constructors store an allocator object <paramref name="_Al"/> and initialize the vector.
/// <para>The first constructor specify an empty initial vector and explicitly specifies the allocator type.
/// to be used.</para>
/// <para>The second and third constructors specify a copy of the concurrent vector <paramref name="_Vector"/>.</para>
/// <para>The fourth constructor specifies a move of the concurrent vector <paramref name="_Vector"/>.</para>
/// <para>The fifth constructor specifies a repetition of a specified number (<paramref name="_N"/>) of elements of the default
/// value for class <typeparamref name="_Ty"/>.</para>
/// <para>The sixth constructor specifies a repetition of (<paramref name="_N"/>) elements of value <paramref name="_Item"/>.</para>
/// <para>The last constructor specifies values supplied by the iterator range [<paramref name="_Begin"/>, <paramref name="_End"/>).</para>
/// </remarks>
/**/
explicit concurrent_vector(const allocator_type &_Al = allocator_type())
: details::_Allocator_base<_Ty, _Ax>(_Al)
{
_My_vector_allocator_ptr = &_Internal_allocator;
}
/// <summary>
/// Constructs a concurrent vector.
/// </summary>
/// <param name="_Vector">
/// The source <c>concurrent_vector</c> object to copy or move elements from.
/// </param>
/// <remarks>
/// All constructors store an allocator object <paramref name="_Al"/> and initialize the vector.
/// <para>The first constructor specify an empty initial vector and explicitly specifies the allocator type.
/// to be used.</para>
/// <para>The second and third constructors specify a copy of the concurrent vector <paramref name="_Vector"/>.</para>
/// <para>The fourth constructor specifies a move of the concurrent vector <paramref name="_Vector"/>.</para>
/// <para>The fifth constructor specifies a repetition of a specified number (<paramref name="_N"/>) of elements of the default
/// value for class <typeparamref name="_Ty"/>.</para>
/// <para>The sixth constructor specifies a repetition of (<paramref name="_N"/>) elements of value <paramref name="_Item"/>.</para>
/// <para>The last constructor specifies values supplied by the iterator range [<paramref name="_Begin"/>, <paramref name="_End"/>).</para>
/// </remarks>
/**/
concurrent_vector( const concurrent_vector& _Vector)
: details::_Allocator_base<_Ty, _Ax>(_Vector.get_allocator())
{
_My_vector_allocator_ptr = &_Internal_allocator;
_Internal_copy(_Vector, sizeof(_Ty), &_Copy_array);
}
/// <summary>
/// Constructs a concurrent vector.
/// </summary>
/// <typeparam name="M">
/// The allocator type of the source vector.
/// </typeparam>
/// <param name="_Vector">
/// The source <c>concurrent_vector</c> object to copy or move elements from.
/// </param>
/// <param name="_Al">
/// The allocator class to use with this object.
/// </param>
/// <remarks>
/// All constructors store an allocator object <paramref name="_Al"/> and initialize the vector.
/// <para>The first constructor specify an empty initial vector and explicitly specifies the allocator type.
/// to be used.</para>
/// <para>The second and third constructors specify a copy of the concurrent vector <paramref name="_Vector"/>.</para>
/// <para>The fourth constructor specifies a move of the concurrent vector <paramref name="_Vector"/>.</para>
/// <para>The fifth constructor specifies a repetition of a specified number (<paramref name="_N"/>) of elements of the default
/// value for class <typeparamref name="_Ty"/>.</para>
/// <para>The sixth constructor specifies a repetition of (<paramref name="_N"/>) elements of value <paramref name="_Item"/>.</para>
/// <para>The last constructor specifies values supplied by the iterator range [<paramref name="_Begin"/>, <paramref name="_End"/>).</para>
/// </remarks>
/**/
template<class M>
concurrent_vector( const concurrent_vector<_Ty, M>& _Vector, const allocator_type& _Al = allocator_type() )
: details::_Allocator_base<_Ty, _Ax>(_Al)
{
_My_vector_allocator_ptr = &_Internal_allocator;
_Internal_copy(_Vector._Internal_vector_base(), sizeof(_Ty), &_Copy_array);
}
/// <summary>
/// Constructs a concurrent vector.
/// </summary>
/// <param name="_Vector">
/// The source <c>concurrent_vector</c> object to copy or move elements from.
/// </param>
/// <remarks>
/// All constructors store an allocator object <paramref name="_Al"/> and initialize the vector.
/// <para>The first constructor specify an empty initial vector and explicitly specifies the allocator type.
/// to be used.</para>
/// <para>The second and third constructors specify a copy of the concurrent vector <paramref name="_Vector"/>.</para>
/// <para>The fourth constructor specifies a move of the concurrent vector <paramref name="_Vector"/>.</para>
/// <para>The fifth constructor specifies a repetition of a specified number (<paramref name="_N"/>) of elements of the default
/// value for class <typeparamref name="_Ty"/>.</para>
/// <para>The sixth constructor specifies a repetition of (<paramref name="_N"/>) elements of value <paramref name="_Item"/>.</para>
/// <para>The last constructor specifies values supplied by the iterator range [<paramref name="_Begin"/>, <paramref name="_End"/>).</para>
/// </remarks>
/**/
concurrent_vector( concurrent_vector && _Vector)
: details::_Allocator_base<_Ty, _Ax>(_Vector.get_allocator())
{
_My_vector_allocator_ptr = &_Internal_allocator;
_Concurrent_vector_base_v4::_Internal_swap(_Vector._Internal_vector_base());
}
/// <summary>
/// Constructs a concurrent vector.
/// </summary>
/// <param name="_N">
/// The initial size of the <c>concurrent_vector</c> object.
/// </param>
/// <remarks>
/// All constructors store an allocator object <paramref name="_Al"/> and initialize the vector.
/// <para>The first constructor specify an empty initial vector and explicitly specifies the allocator type.
/// to be used.</para>
/// <para>The second and third constructors specify a copy of the concurrent vector <paramref name="_Vector"/>.</para>
/// <para>The fourth constructor specifies a move of the concurrent vector <paramref name="_Vector"/>.</para>
/// <para>The fifth constructor specifies a repetition of a specified number (<paramref name="_N"/>) of elements of the default
/// value for class <typeparamref name="_Ty"/>.</para>
/// <para>The sixth constructor specifies a repetition of (<paramref name="_N"/>) elements of value <paramref name="_Item"/>.</para>
/// <para>The last constructor specifies values supplied by the iterator range [<paramref name="_Begin"/>, <paramref name="_End"/>).</para>
/// </remarks>
/**/
explicit concurrent_vector(size_type _N)
{
_My_vector_allocator_ptr = &_Internal_allocator;
if ( !_N ) return;
_Internal_reserve(_N, sizeof(_Ty), max_size()); _My_early_size = _N;
_CONCRT_ASSERT( _My_first_block == _Segment_index_of(_N-1)+1 );
_Initialize_array(static_cast<_Ty*>(_My_segment[0]._My_array), nullptr, _N);
}
/// <summary>
/// Constructs a concurrent vector.
/// </summary>
/// <param name="_N">
/// The initial capacity of the <c>concurrent_vector</c> object.
/// </param>
/// <param name="_Item">
/// The value of elements in the constructed object.
/// </param>
/// <param name="_Al">
/// The allocator class to use with this object.
/// </param>
/// <remarks>
/// All constructors store an allocator object <paramref name="_Al"/> and initialize the vector.
/// <para>The first constructor specify an empty initial vector and explicitly specifies the allocator type.
/// to be used.</para>
/// <para>The second and third constructors specify a copy of the concurrent vector <paramref name="_Vector"/>.</para>
/// <para>The fourth constructor specifies a move of the concurrent vector <paramref name="_Vector"/>.</para>
/// <para>The fifth constructor specifies a repetition of a specified number (<paramref name="_N"/>) of elements of the default
/// value for class <typeparamref name="_Ty"/>.</para>
/// <para>The sixth constructor specifies a repetition of (<paramref name="_N"/>) elements of value <paramref name="_Item"/>.</para>
/// <para>The last constructor specifies values supplied by the iterator range [<paramref name="_Begin"/>, <paramref name="_End"/>).</para>
/// </remarks>
/**/
concurrent_vector(size_type _N, const_reference _Item, const allocator_type& _Al = allocator_type())
: details::_Allocator_base<_Ty, _Ax>(_Al)
{
_My_vector_allocator_ptr = &_Internal_allocator;
_Internal_assign( _N, _Item );
}
/// <summary>
/// Constructs a concurrent vector.
/// </summary>
/// <typeparam name="_InputIterator">
/// The type of the input iterator.
/// </typeparam>
/// <param name="_Begin">
/// Position of the first element in the range of elements to be copied.
/// </param>
/// <param name="_End">
/// Position of the first element beyond the range of elements to be copied.
/// </param>
/// <param name="_Al">
/// The allocator class to use with this object.
/// </param>
/// <remarks>
/// All constructors store an allocator object <paramref name="_Al"/> and initialize the vector.
/// <para>The first constructor specify an empty initial vector and explicitly specifies the allocator type.
/// to be used.</para>
/// <para>The second and third constructors specify a copy of the concurrent vector <paramref name="_Vector"/>.</para>
/// <para>The fourth constructor specifies a move of the concurrent vector <paramref name="_Vector"/>.</para>
/// <para>The fifth constructor specifies a repetition of a specified number (<paramref name="_N"/>) of elements of the default
/// value for class <typeparamref name="_Ty"/>.</para>
/// <para>The sixth constructor specifies a repetition of (<paramref name="_N"/>) elements of value <paramref name="_Item"/>.</para>
/// <para>The last constructor specifies values supplied by the iterator range [<paramref name="_Begin"/>, <paramref name="_End"/>).</para>
/// </remarks>
/**/
template<class _InputIterator>
concurrent_vector(_InputIterator _Begin, _InputIterator _End, const allocator_type &_Al = allocator_type())
: details::_Allocator_base<_Ty, _Ax>(_Al)
{
_My_vector_allocator_ptr = &_Internal_allocator;
_Internal_assign(_Begin, _End, static_cast<_Is_integer_tag<::std::numeric_limits<_InputIterator>::is_integer> *>(0) );
}
/// <summary>
/// Assigns the contents of another <c>concurrent_vector</c> object to this one. This method is not concurrency-safe.
/// </summary>
/// <param name="_Vector">
/// The source <c>concurrent_vector</c> object.
/// </param>
/// <returns>
/// A reference to this <c>concurrent_vector</c> object.
/// </returns>
/**/
concurrent_vector& operator=( const concurrent_vector& _Vector )
{
if( this != &_Vector )
_Concurrent_vector_base_v4::_Internal_assign(_Vector, sizeof(_Ty), &_Destroy_array, &_Assign_array, &_Copy_array);
return *this;
}
/// <summary>
/// Assigns the contents of another <c>concurrent_vector</c> object to this one. This method is not concurrency-safe.
/// </summary>
/// <typeparam name="M">
/// The allocator type of the source vector.
/// </typeparam>
/// <param name="_Vector">
/// The source <c>concurrent_vector</c> object.
/// </param>
/// <returns>
/// A reference to this <c>concurrent_vector</c> object.
/// </returns>
/**/
template<class M>
concurrent_vector& operator=( const concurrent_vector<_Ty, M>& _Vector )
{
if( static_cast<void*>( this ) != static_cast<const void*>( &_Vector ) )
{
_Concurrent_vector_base_v4::_Internal_assign(_Vector._Internal_vector_base(),
sizeof(_Ty), &_Destroy_array, &_Assign_array, &_Copy_array);
}
return *this;
}
/// <summary>
/// Assigns the contents of another <c>concurrent_vector</c> object to this one. This method is not concurrency-safe.
/// </summary>
/// <param name="_Vector">
/// The source <c>concurrent_vector</c> object.
/// </param>
/// <returns>
/// A reference to this <c>concurrent_vector</c> object.
/// </returns>
/**/
concurrent_vector& operator=( concurrent_vector && _Vector )
{
if( static_cast<void*>( this ) != static_cast<const void*>( &_Vector ) )
{
_Concurrent_vector_base_v4::_Internal_swap(_Vector._Internal_vector_base());
_Vector.clear();
}
return *this;
}
/// <summary>
/// Grows this concurrent vector by <paramref name="_Delta"/> elements. This method is concurrency-safe.
/// </summary>
/// <param name="_Delta">
/// The number of elements to append to the object.
/// </param>
/// <returns>
/// An iterator to first item appended.
/// </returns>
/// <remarks>
/// If <paramref name="_Item"/> is not specified, the new elements are default constructed.
/// </remarks>
/**/
iterator grow_by( size_type _Delta )
{
return iterator(*this, _Delta ? _Internal_grow_by( _Delta, sizeof(_Ty), &_Initialize_array, nullptr ) : _My_early_size);
}
/// <summary>
/// Grows this concurrent vector by <paramref name="_Delta"/> elements. This method is concurrency-safe.
/// </summary>
/// <param name="_Delta">
/// The number of elements to append to the object.
/// </param>
/// <param name="_Item">
/// The value to initialize the new elements with.
/// </param>
/// <returns>
/// An iterator to first item appended.
/// </returns>
/// <remarks>
/// If <paramref name="_Item"/> is not specified, the new elements are default constructed.
/// </remarks>
/**/
iterator grow_by( size_type _Delta, const_reference _Item )
{
return iterator(*this, _Delta ? _Internal_grow_by( _Delta, sizeof(_Ty), &_Initialize_array_by, static_cast<const void*>(&_Item) ) : _My_early_size);
}
/// <summary>
/// Grows this concurrent vector until it has at least <paramref name="_N"/> elements. This method is concurrency-safe.
/// </summary>
/// <param name="_N">
/// The new minimum size for the <c>concurrent_vector</c> object.
/// </param>
/// <returns>
/// An iterator that points to beginning of appended sequence, or to the element at index <paramref name="_N"/> if no
/// elements were appended.
/// </returns>
/**/
iterator grow_to_at_least( size_type _N )
{
size_type _M = 0;
if( _N )
{
_M = _Internal_grow_to_at_least_with_result( _N, sizeof(_Ty), &_Initialize_array, nullptr );
if( _M > _N )
_M = _N;
}
return iterator(*this, _M);
};
/// <summary>
/// Appends the given item to the end of the concurrent vector. This method is concurrency-safe.
/// </summary>
/// <param name="_Item">
/// The value to be appended.
/// </param>
/// <returns>
/// An iterator to item appended.
/// </returns>
/**/
iterator push_back( const_reference _Item )
{
size_type _K;
void *_Ptr = _Internal_push_back(sizeof(_Ty), _K);
_Internal_loop_guide _Loop(1, _Ptr);
_Loop._Init(&_Item);
return iterator(*this, _K, _Ptr);
}
/// <summary>
/// Appends the given item to the end of the concurrent vector. This method is concurrency-safe.
/// </summary>
/// <param name="_Item">
/// The value to be appended.
/// </param>
/// <returns>
/// An iterator to item appended.
/// </returns>
/**/
iterator push_back( _Ty &&_Item )
{
size_type _K;
void *_Ptr = _Internal_push_back(sizeof(_Ty), _K);
new (_Ptr) _Ty( ::std::move(_Item));
return iterator(*this, _K, _Ptr);
}
/// <summary>
/// Provides access to the element at the given index in the concurrent vector. This method is concurrency-safe for read operations,
/// and also while growing the vector, as long as the you have ensured that the value <paramref name="_Index"/> is less than
/// the size of the concurrent vector.
/// </summary>
/// <param name="_Index">
/// The index of the element to be retrieved.
/// </param>
/// <returns>
/// A reference to the item at the given index.
/// </returns>
/// <remarks>
/// The version of <c>operator []</c> that returns a non-<c>const</c> reference cannot be used to concurrently write to the element
/// from different threads. A different synchronization object should be used to synchronize concurrent read and write operations
/// to the same data element.
/// <para>No bounds checking is performed to ensure that <paramref name="_Index"/> is a valid index into the concurrent vector.</para>
/// </remarks>
/**/
reference operator[]( size_type _Index )
{
return _Internal_subscript(_Index);
}
/// <summary>
/// Provides read access to element at the given index in the concurrent vector. This method is concurrency-safe for read operations,
/// and also while growing the vector, as long as the you have ensured that the value <paramref name="_Index"/> is less than
/// the size of the concurrent vector.
/// </summary>
/// <param name="_Index">
/// The index of the element to be retrieved.
/// </param>
/// <returns>
/// A <c>const</c> reference to the item at the given index.
/// </returns>
/// <remarks>
/// The version of <c>operator []</c> that returns a non-<c>const</c> reference cannot be used to concurrently write to the element
/// from different threads. A different synchronization object should be used to synchronize concurrent read and write operations
/// to the same data element.
/// <para>No bounds checking is performed to ensure that <paramref name="_Index"/> is a valid index into the concurrent vector.</para>
/// </remarks>
/**/
const_reference operator[]( size_type _Index ) const
{
return _Internal_subscript(_Index);
}
/// <summary>
/// Provides access to the element at the given index in the concurrent vector. This method is concurrency-safe for read operations,
/// and also while growing the vector, as long as you have ensured that the value <paramref name="_Index"/> is less than
/// the size of the concurrent vector.
/// </summary>
/// <param name="_Index">
/// The index of the element to be retrieved.
/// </param>
/// <returns>
/// A reference to the item at the given index.
/// </returns>
/// <remarks>
/// The version of the function <c>at</c> that returns a non-<c>const</c> reference cannot be used to concurrently write to the element
/// from different threads. A different synchronization object should be used to synchronize concurrent read and write operations
/// to the same data element.
/// <para>The method throws <c>out_of_range</c> if <paramref name="_Index"/> is greater than or equal to the size of the concurrent vector,
/// and <c>range_error</c> if the index is for a broken portion of the vector. For details on how a vector can become broken,
/// see <see cref="Parallel Containers and Objects"/>.</para>
/// </remarks>
/**/
reference at( size_type _Index )
{
return _Internal_subscript_with_exceptions(_Index);
}
/// <summary>
/// Provides access to the element at the given index in the concurrent vector. This method is concurrency-safe for read operations,
/// and also while growing the vector, as long as you have ensured that the value <paramref name="_Index"/> is less than
/// the size of the concurrent vector.
/// </summary>
/// <param name="_Index">
/// The index of the element to be retrieved.
/// </param>
/// <returns>
/// A <c>const</c> reference to the item at the given index.
/// </returns>
/// <remarks>
/// The version of the function <c>at</c> that returns a non-<c>const</c> reference cannot be used to concurrently write to the element
/// from different threads. A different synchronization object should be used to synchronize concurrent read and write operations
/// to the same data element.
/// <para>The method throws <c>out_of_range</c> if <paramref name="_Index"/> is greater than or equal to the size of the concurrent vector,
/// and <c>range_error</c> if the index is for a broken portion of the vector. For details on how a vector can become broken,
/// see <see cref="Parallel Containers and Objects"/>.</para>
/// </remarks>
/**/
const_reference at( size_type _Index ) const
{
return _Internal_subscript_with_exceptions(_Index);
}
/// <summary>
/// Returns the number of elements in the concurrent vector. This method is concurrency-safe.
/// </summary>
/// <returns>
/// The number of elements in this <c>concurrent_vector</c> object.
/// </returns>
/// <remarks>
/// The returned size is guaranteed to include all elements appended by calls to the function <c>push_back</c>,
/// or grow operations that have completed prior to invoking this method. However, it may also include elements
/// that are allocated but still under construction by concurrent calls to any of the growth methods.
/// </remarks>
/**/
size_type size() const
{
size_type _Sz = _My_early_size;
size_type _Cp = _Internal_capacity();
return _Cp < _Sz ? _Cp : _Sz;
}
/// <summary>
/// Tests if the concurrent vector is empty at the time this method is called. This method is concurrency-safe.
/// </summary>
/// <returns>
/// <c>true</c> if the vector was empty at the moment the function was called, <c>false</c> otherwise.
/// </returns>
/**/
bool empty() const
{
return !_My_early_size;
}
/// <summary>
/// Returns the maximum size to which the concurrent vector can grow without having to allocate more memory.
/// This method is concurrency-safe.
/// </summary>
/// <returns>
/// The maximum size to which the concurrent vector can grow without having to allocate more memory.