-
Notifications
You must be signed in to change notification settings - Fork 32
/
Copy pathNativeLinkedList.cs
4722 lines (4253 loc) · 137 KB
/
NativeLinkedList.cs
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 file="NativeLinkedList.cs" company="Jackson Dunstan">
// Copyright (c) Jackson Dunstan. See LICENSE.md.
// </copyright>
//-----------------------------------------------------------------------
using System;
using System.Collections;
using System.Collections.Generic;
using System.Diagnostics;
using System.Runtime.InteropServices;
using Unity.Collections;
using Unity.Collections.LowLevel.Unsafe;
namespace JacksonDunstan.NativeCollections
{
/// <summary>
/// The state of a <see cref="NativeLinkedList{T}"/>. Shared among instances
/// of the struct via a pointer to unmanaged memory. This has no type
/// parameters, so it can be used by all list types.
/// </summary>
[StructLayout(LayoutKind.Sequential)]
internal unsafe struct NativeLinkedListState
{
/// <summary>
/// Each node's value. Indices correspond with nextIndexes.
/// </summary>
internal void* m_Values;
/// <summary>
/// Each node's next node index. Indices correspond with values.
/// </summary>
internal int* m_NextIndexes;
/// <summary>
/// Each node's previous node index. Indices correspond with values.
/// </summary>
internal int* m_PrevIndexes;
/// <summary>
/// Index of the first node in the list or -1 if there are no nodes in
/// the list
/// </summary>
internal int m_HeadIndex;
/// <summary>
/// Index of the last node in the list or -1 if there are no nodes in
/// the list
/// </summary>
internal int m_TailIndex;
/// <summary>
/// Number of nodes contained
/// </summary>
internal int m_Length;
/// <summary>
/// Number of nodes that can be contained
/// </summary>
internal int m_Capacity;
/// <summary>
/// Version of enumerators that are valid for this list. This starts at
/// 1 and increases by one with each change that invalidates the list's
/// enumerators.
/// </summary>
internal int m_Version;
/// <summary>
/// Allocator used to create the backing arrays
/// </summary>
internal Allocator m_Allocator;
}
/// <summary>
/// A doubly-linked list native collection.
/// </summary>
///
/// <typeparam name="T">
/// Type of nodes in the list. Must be blittable.
/// </typeparam>
[NativeContainer]
[NativeContainerSupportsMinMaxWriteRestriction]
[DebuggerDisplay("Length = {Length}. Capacity = {Capacity}")]
[DebuggerTypeProxy(typeof(NativeLinkedListDebugView<>))]
[StructLayout(LayoutKind.Sequential)]
public unsafe struct NativeLinkedList<T>
: IEnumerable<T>
, IDisposable
#if CSHARP_7_3_OR_NEWER
where T : unmanaged
#else
where T : struct
#endif
{
/// <summary>
/// An enumerator for <see cref="NativeLinkedList{T}"/>
/// </summary>
[StructLayout(LayoutKind.Sequential)]
public struct Enumerator
: IEnumerator<T>
, IEquatable<Enumerator>
{
/// <summary>
/// Index of the node
/// </summary>
internal int m_Index;
/// <summary>
/// Version of the list that this enumerator is valid for
/// </summary>
internal readonly int m_Version;
/// <summary>
/// List to iterate
/// </summary>
internal NativeLinkedList<T> m_List;
/// <summary>
/// Create the enumerator for a particular node
/// </summary>
///
/// <param name="list">
/// List to iterate
/// </param>
///
/// <param name="index">
/// Index of the node. Out-of-bounds values are OK.
/// </param>
///
/// <param name="version">
/// Version of the list that this enumerator is valid for
/// </param>
internal Enumerator(
NativeLinkedList<T> list,
int index,
int version)
{
m_Index = index;
m_Version = version;
m_List = list;
}
/// <summary>
/// Get the list this enumerator is for.
/// </summary>
///
/// <value>
/// The list this enumerator is for. It is not necessarily usable.
/// For example, if this enumerator was initialized with the default
/// constructor then the list returned will not be usable.
/// </value>
public NativeLinkedList<T> List
{
get
{
return m_List;
}
}
/// <summary>
/// Get an enumerator to the next node.
///
/// This operation requires read access in general and access to the
/// node if the enumerator is valid.
///
/// This operation is O(1).
/// </summary>
///
/// <returns>
/// If this enumerator is valid, an enumerator to the next node or
/// an invalid enumerator if at the tail node. If this enumerator is
/// invalid but was constructed via the non-default constructor and
/// the list it enumerates has not been disposed and has not
/// invalidated this enumerator, an enumerator to the head node.
/// Otherwise, an invalid enumerator.
/// </returns>
public Enumerator Next
{
get
{
m_List.RequireReadAccess();
// The version matches
if (m_Version == m_List.m_State->m_Version)
{
// Still within the list. The enumerator is valid.
if (m_Index >= 0 && m_Index < m_List.m_State->m_Length)
{
// Return the next node
m_List.RequireParallelForAccess(m_Index);
return new Enumerator(
m_List,
m_List.m_State->m_NextIndexes[m_Index],
m_Version);
}
// Not within the list. Return the head.
return new Enumerator(
m_List,
m_List.m_State->m_HeadIndex,
m_Version);
}
// No valid list
return this;
}
}
/// <summary>
/// Get an enumerator to the previous node
///
/// This operation requires read access in general and access to the
/// node if the enumerator is valid.
///
/// This operation is O(1).
/// </summary>
///
/// <returns>
/// If this enumerator is valid, an enumerator to the previous node
/// or an invalid enumerator if at the head node. If this enumerator
/// is invalid but was constructed via the non-default constructor
/// and the list it enumerates has not been disposed and has not
/// invalidated this enumerator, an enumerator to the tail node.
/// Otherwise, an invalid enumerator.
/// </returns>
public Enumerator Prev
{
get
{
m_List.RequireReadAccess();
// The version matches
if (m_Version == m_List.m_State->m_Version)
{
// Still within the list. The enumerator is valid.
if (m_Index >= 0 && m_Index < m_List.m_State->m_Length)
{
// Return the previous node
m_List.RequireParallelForAccess(m_Index);
return new Enumerator(
m_List,
m_List.m_State->m_PrevIndexes[m_Index],
m_Version);
}
// Not within the list. Return the tail.
return new Enumerator(
m_List,
m_List.m_State->m_TailIndex,
m_Version);
}
// No valid list
return this;
}
}
/// <summary>
/// If this enumerator is valid, move to the next node or
/// invalidate this enumerator if at the tail node. If this
/// enumerator is invalid but was constructed via the non-default
/// constructor and the list it enumerates has not been disposed and
/// has not invalidated this enumerator, move to the head node.
/// Otherwise, this function has no effect.
///
/// This operation requires read access in general and access to the
/// node if the enumerator is valid.
///
/// This operation is O(1).
/// </summary>
///
/// <returns>
/// If this enumerator is valid
/// </returns>
public bool MoveNext()
{
m_List.RequireReadAccess();
// The version matches
if (m_Version == m_List.m_State->m_Version)
{
// Still within the list. The enumerator is valid.
if (m_Index >= 0 && m_Index < m_List.m_State->m_Length)
{
// Go to the next node
m_List.RequireParallelForAccess(m_Index);
m_Index = m_List.m_State->m_NextIndexes[m_Index];
return m_Index >= 0;
}
// Not within the list. Go to the head.
m_Index = m_List.m_State->m_HeadIndex;
return true;
}
// Already invalid
return false;
}
/// <summary>
/// If this enumerator is valid, move to the previous node or
/// invalidate this enumerator if at the head node. If this
/// enumerator is invalid but was constructed via the non-default
/// constructor and the list it enumerates has not been disposed and
/// has not invalidated this enumerator, move to the tail node.
/// Otherwise, this function has no effect.
///
/// This operation requires read access in general and access to the
/// node if the enumerator is valid.
///
/// This operation is O(1).
/// </summary>
///
/// <returns>
/// If this enumerator is valid
/// </returns>
public bool MovePrev()
{
m_List.RequireReadAccess();
// The version matches
if (m_Version == m_List.m_State->m_Version)
{
// Still within the list. The enumerator is valid.
if (m_Index >= 0 && m_Index < m_List.m_State->m_Length)
{
// Go to the previous node
m_List.RequireParallelForAccess(m_Index);
m_Index = m_List.m_State->m_PrevIndexes[m_Index];
return m_Index >= 0;
}
// Not within the list. Go to the tail.
m_Index = m_List.m_State->m_TailIndex;
return true;
}
// Already invalid
return false;
}
/// <summary>
/// If this enumerator is valid, move to the next node a given
/// number of times or invalidate this enumerator if either at the
/// tail node or the tail node is moved beyond while moving next. If
/// this enumerator is invalid but was constructed via the
/// non-default constructor and the list it enumerates has not been
/// disposed and has not invalidated this enumerator, move to the
/// head node. Otherwise, this function has no effect.
///
/// This operation requires read access in general and access to the
/// node if the enumerator is valid.
///
/// This operation is O(N) where N is the given number of moves.
/// </summary>
///
/// <returns>
/// If this enumerator is valid
/// </returns>
public bool MoveNext(int numSteps)
{
m_List.RequireReadAccess();
// The version matches
if (m_Version == m_List.m_State->m_Version)
{
for (; numSteps > 0; numSteps--)
{
// Still within the list. The enumerator is valid.
if (m_Index >= 0 && m_Index < m_List.m_State->m_Length)
{
// Go to the next node
m_List.RequireParallelForAccess(m_Index);
m_Index = m_List.m_State->m_NextIndexes[m_Index];
}
else
{
// Not within the list. Go to the tail.
m_Index = m_List.m_State->m_HeadIndex;
}
}
return m_Index >= 0;
}
// Already invalid
return false;
}
/// <summary>
/// If this enumerator is valid, move to the previous node a given
/// number of times or invalidate this enumerator if either at the
/// head node or the head node moved beyond while moving previous.
/// If this enumerator is invalid but was constructed via the
/// non-default constructor and the list it enumerates has not been
/// disposed and has not invalidated this enumerator, move to the
/// tail node. Otherwise, this function has no effect.
///
/// This operation requires read access in general and access to the
/// node if the enumerator is valid.
///
/// This operation is O(N) where N is the given number of moves.
/// </summary>
///
/// <returns>
/// If this enumerator is valid
/// </returns>
public bool MovePrev(int numSteps)
{
m_List.RequireReadAccess();
// The version matches
if (m_Version == m_List.m_State->m_Version)
{
for (; numSteps > 0; numSteps--)
{
// Still within the list. The enumerator is valid.
if (m_Index >= 0 && m_Index < m_List.m_State->m_Length)
{
// Go to the previous node
m_List.RequireParallelForAccess(m_Index);
m_Index = m_List.m_State->m_PrevIndexes[m_Index];
}
else
{
// Not within the list. Go to the tail.
m_Index = m_List.m_State->m_TailIndex;
}
}
return m_Index >= 0;
}
// Already invalid
return false;
}
/// <summary>
/// Get the distance between this iterator and a given iterator that
/// is further towards the tail.
///
/// This operation requires read access in general and access to all
/// nodes between the node this enumerator refers to and the given
/// enumerator, inclusive, if this enumerator is valid and the given
/// enumerator is valid for the same list as this enumerator.
///
/// This operation is O(N) where N is the length of the list.
/// </summary>
///
/// <param name="other">
/// Enumerator to get the distance to. Must be valid for the same
/// list this enumerator is for.
/// </param>
///
/// <returns>
/// The number of nodes between this enumerator and the given
/// enumerator if both are valid for the same list and the given
/// enumerator is either the same enumerator as this enumerator or
/// is toward the tail compared to this enumerator. Otherwise, a
/// negative value.
/// </returns>
public int GetDistance(Enumerator other)
{
m_List.RequireReadAccess();
// Can't compare invalid enumerators or enumerators for
// different lists
if (!IsValid || !other.IsValidFor(m_List))
{
return -1;
}
// Keep moving next to find the given enumerator
int distance = 0;
int index = m_Index;
do
{
// Reached the tail
if (index < 0)
{
return -1;
}
// Reached the enumerator to find
if (index == other.m_Index)
{
return distance;
}
// Move next
m_List.RequireParallelForAccess(index);
index = m_List.m_State->m_NextIndexes[index];
distance++;
}
while (true);
}
/// <summary>
/// Check if an enumerator is valid
///
/// This operation requires read access unless the enumerator was
/// initialized with the default constructor.
///
/// This operation is O(1).
/// </summary>
///
/// <returns>
/// If the given enumerator is valid
/// </returns>
public bool IsValid
{
get
{
if (m_List.m_State == null)
{
return false;
}
m_List.RequireReadAccess();
return m_Index >= 0
&& m_Index < m_List.m_State->m_Length
&& m_Version == m_List.m_State->m_Version;
}
}
/// <summary>
/// Check if an enumerator is valid for a given list
///
/// This operation requires read access unless the enumerator was
/// initialized with the default constructor.
///
/// This operation is O(1).
/// </summary>
///
/// <param name="list">
/// List to check if the enumerator is valid for
/// </param>
///
/// <returns>
/// If the enumerator is valid for the given list
/// </returns>
public bool IsValidFor(NativeLinkedList<T> list)
{
if (m_List.m_State == null)
{
return false;
}
m_List.RequireReadAccess();
return m_Index >= 0
&& m_List.m_State == list.m_State
&& m_Index < m_List.m_State->m_Length
&& m_Version == m_List.m_State->m_Version;
}
/// <summary>
/// Check if two enumerators refer to the same node lists.
///
/// This operation requires read access to both enumerators' lists
/// unless either list was initialized with the default constructor.
///
/// This operation is O(1).
/// </summary>
///
/// <param name="a">
/// First enumerator to compare
/// </param>
///
/// <param name="b">
/// Second enumerator to compare
/// </param>
///
/// <returns>
/// If the given enumerators refer to the same node and neither
/// enumerator is invalid.
/// </returns>
public static bool operator ==(Enumerator a, Enumerator b)
{
// Enumerators without a valid list can't be equal
if (a.m_List.m_State == null || b.m_List.m_State == null)
{
return false;
}
a.m_List.RequireReadAccess();
b.m_List.RequireReadAccess();
return a.IsValid
&& b.IsValid
&& a.m_Index == b.m_Index
&& a.m_List.m_State == b.m_List.m_State;
}
/// <summary>
/// Check if two enumerators refer to different nodes.
///
/// This operation requires read access to both enumerators' lists
/// unless either list was initialized with the default constructor.
///
/// This operation is O(1).
/// </summary>
///
/// <param name="a">
/// First enumerator to compare
/// </param>
///
/// <param name="b">
/// Second enumerator to compare
/// </param>
///
/// <returns>
/// If the given enumerators refer to different nodes or either
/// enumerator is invalid.
/// </returns>
public static bool operator !=(Enumerator a, Enumerator b)
{
// Enumerators without a valid list can't be equal
if (a.m_List.m_State == null || b.m_List.m_State == null)
{
return true;
}
a.m_List.RequireReadAccess();
b.m_List.RequireReadAccess();
return !a.IsValid
|| !b.IsValid
|| a.m_Index != b.m_Index
|| a.m_List.m_State != b.m_List.m_State;
}
/// <summary>
/// Check if this enumerator refer to the same node as another
/// enumerator.
///
/// This operation requires read access to both enumerators' lists
/// unless either list was initialized with the default constructor.
///
/// This operation is O(1).
/// </summary>
///
/// <param name="e">
/// Enumerator to compare with
/// </param>
///
/// <returns>
/// If the given enumerator refers to the same node as this
/// enumerator and is of the same type and neither enumerator is
/// invalid.
/// </returns>
public bool Equals(Enumerator e)
{
return this == e;
}
/// <summary>
/// Check if this enumerator refer to the same node as another
/// enumerator.
///
/// This operation requires read access to both enumerators' lists
/// unless either list was initialized with the default constructor.
///
/// This operation is O(1).
/// </summary>
///
/// <param name="obj">
/// Enumerator to compare with
/// </param>
///
/// <returns>
/// If the given enumerator refers to the same node as this
/// enumerator and is of the same type and neither enumerator is
/// invalid.
/// </returns>
public override bool Equals(object obj)
{
return obj is Enumerator && this == (Enumerator)obj;
}
/// <summary>
/// Get a hash code for this enumerator. If the enumerator is
/// mutated such as by calling <see cref="MoveNext()"/>, the
/// returned hash code will no longer match values returned by
/// subsequent calls to this function.
///
/// This operation has no access requirements on the enumerator's
/// associated list.
///
/// This operation is O(1).
/// </summary>
///
/// <returns>
/// A hash code for this enumerator
/// </returns>
public override int GetHashCode()
{
// Suppress "non-readonly field" warning because we don't have a
// readonly-only way to generate a hash code since Index is
// mutable to comply with the IEnumerator interface.
#pragma warning disable RECS0025
return m_Index;
#pragma warning restore RECS0025
}
/// <summary>
/// Set the ParallelFor safety check ranges of the list this
/// enumerator is for. This is used for automated testing purposes
/// only.
/// </summary>
///
/// <param name="minIndex">
/// The minimum index that can safely be accessed. This is zero
/// outside of a job and in a regular, non-ParallelFor job but set
/// higher by ParallelFor jobs.
/// </param>
///
/// <param name="maxIndex">
/// The maximum index that can safely be accessed. This is equal to
/// (m_Length-1) outside of a job and in a regular, non-ParallelFor
/// job but set lower by ParallelFor jobs.
/// </param>
[Conditional("ENABLE_UNITY_COLLECTIONS_CHECKS")]
public void TestUseOnlySetParallelForSafetyCheckRange(
int minIndex = -1,
int maxIndex = -1)
{
#if ENABLE_UNITY_COLLECTIONS_CHECKS
m_List.m_MinIndex = minIndex;
m_List.m_MaxIndex = maxIndex;
#endif
}
/// <summary>
/// Set whether both read and write access should be allowed for the
/// enumerator's list. This is used for automated testing purposes
/// only.
/// </summary>
///
/// <param name="allowReadOrWriteAccess">
/// If both read and write access should be allowed for the
/// enumerator's list
/// </param>
[Conditional("ENABLE_UNITY_COLLECTIONS_CHECKS")]
public void TestUseOnlySetAllowReadAndWriteAccess(
bool allowReadOrWriteAccess)
{
#if ENABLE_UNITY_COLLECTIONS_CHECKS
AtomicSafetyHandle.SetAllowReadOrWriteAccess(
m_List.m_Safety,
allowReadOrWriteAccess);
#endif
}
/// <summary>
/// Dispose the enumerator. This operation has no effect and exists
/// only to satisfy the requirements of <see cref="IDisposable"/>.
///
/// This operation has no access requirements on the enumerator's
/// associated list.
///
/// This operation is O(1).
/// </summary>
public void Dispose()
{
}
/// <summary>
/// Reset the enumerator to the head of the list if it wasn't
/// created using the default constructor.
///
/// This operation requires read access to the list.
///
/// This operation is O(1).
/// </summary>
public void Reset()
{
m_List.RequireReadAccess();
m_Index = m_List.m_State->m_HeadIndex;
}
/// <summary>
/// Get or set a node's value.
///
/// This operation requires read access to the node for 'get' and
/// write access to the node for 'set'.
///
/// This operation is O(1).
/// </summary>
///
/// <value>
/// The node's value
/// </value>
public T Value
{
get
{
m_List.RequireReadAccess();
m_List.RequireParallelForAccess(m_Index);
return UnsafeUtility.ReadArrayElement<T>(
m_List.m_State->m_Values,
m_Index);
}
[WriteAccessRequired]
set
{
m_List.RequireWriteAccess();
m_List.RequireParallelForAccess(m_Index);
UnsafeUtility.WriteArrayElement(
m_List.m_State->m_Values,
m_Index,
value);
}
}
/// <summary>
/// Get or set a node's value. This is provided only for
/// compatibility with <see cref="IEnumerator{T}"/>. As such, there is
/// no 'set' for this property.
///
/// This operation requires read access to the node.
///
/// This operation is O(1).
/// </summary>
///
/// <value>
/// The node's value
/// </value>
public T Current
{
get
{
m_List.RequireReadAccess();
m_List.RequireParallelForAccess(m_Index);
return UnsafeUtility.ReadArrayElement<T>(
m_List.m_State->m_Values,
m_Index);
}
}
/// <summary>
/// Get a node's value. Prefer using the generic version of
/// <see cref="Current"/> as this will cause boxing when enumerating
/// value type node value. This is provided only for compatibility
/// with <see cref="IEnumerator"/>. As such, there is no 'set' for
/// this non-generic property.
///
/// This operation requires read access to the node.
///
/// This operation is O(1).
/// </summary>
///
/// <value>
/// The node's value or null if this enumerator is invalid.
/// </value>
object IEnumerator.Current
{
get
{
m_List.RequireReadAccess();
m_List.RequireParallelForAccess(m_Index);
return UnsafeUtility.ReadArrayElement<T>(
m_List.m_State->m_Values,
m_Index);
}
}
}
/// <summary>
/// State of the list or null if the list is created with the default
/// constructor or <see cref="Dispose"/> has been called. This is shared
/// among all instances of the list.
/// </summary>
[NativeDisableUnsafePtrRestriction]
private NativeLinkedListState* m_State;
// These fields are all required when safety checks are enabled
// They must have these exact types, names, and order
#if ENABLE_UNITY_COLLECTIONS_CHECKS
/// <summary>
/// Length of the list. Equal to the number of nodes currently stored.
/// This is set by ParallelFor jobs due to specifying
/// [NativeContainerSupportsMinMaxWriteRestriction].
/// </summary>
private int m_Length;
/// <summary>
/// The minimum index that can safely be accessed. This is zero outside
/// of a job and in a regular, non-ParallelFor job but set higher by
/// ParallelFor jobs due to specifying
/// [NativeContainerSupportsMinMaxWriteRestriction].
///
/// This field must immediately follow <see cref="m_Length"/>.
/// </summary>
private int m_MinIndex;
/// <summary>
/// The maximum index that can safely be accessed. This is equal to
/// (m_Length-1) outside of a job and in a regular, non-ParallelFor job
/// but set lower by ParallelFor jobs due to specifying
/// [NativeContainerSupportsMinMaxWriteRestriction].
///
/// This field must immediately follow <see cref="m_MinIndex"/>.
/// </summary>
private int m_MaxIndex;
/// <summary>
/// A handle to information about what operations can be safely
/// performed on the list at any given time.
/// </summary>
private AtomicSafetyHandle m_Safety;
/// <summary>
/// A handle that can be used to tell if the list has been disposed yet
/// or not, which allows for error-checking double disposal.
/// </summary>
[NativeSetClassTypeToNullOnSchedule]
private DisposeSentinel m_DisposeSentinel;
#endif
/// <summary>
/// Create the list with an initial capacity. It initially has no nodes.
///
/// This complexity of this operation is O(1) plus the allocator's
/// allocation complexity.
/// </summary>
///
/// <param name="capacity">
/// Initial capacity. If less than four, four is used.
/// </param>
///
/// <param name="allocator">
/// Allocator to allocate unmanaged memory with. Must be valid.
/// </param>
public NativeLinkedList(int capacity, Allocator allocator)
{
// Require a valid allocator
if (allocator <= Allocator.None)
{
throw new ArgumentException(
"Allocator must be Temp, TempJob or Persistent",
"allocator");
}
RequireBlittable();
// Insist on a minimum capacity
if (capacity < 4)
{
capacity = 4;
}
// Allocate the state. It is freed in Dispose().
m_State = (NativeLinkedListState*)UnsafeUtility.Malloc(
sizeof(NativeLinkedListState),
UnsafeUtility.AlignOf<NativeLinkedListState>(),
allocator);
// Create the backing arrays. There's no need to clear them since we
// make no assumptions about the contents anyways.
m_State->m_Values = UnsafeUtility.Malloc(
UnsafeUtility.SizeOf<T>() * capacity,
UnsafeUtility.AlignOf<T>(),
allocator
);
m_State->m_NextIndexes = (int*)UnsafeUtility.Malloc(
sizeof(int) * capacity,
UnsafeUtility.AlignOf<int>(),
allocator);
m_State->m_PrevIndexes = (int*)UnsafeUtility.Malloc(
sizeof(int) * capacity,
UnsafeUtility.AlignOf<int>(),
allocator);
// Store the allocator for future allocation and deallocation
// operations
m_State->m_Allocator = allocator;
// Initially empty with the given capacity
m_State->m_Length = 0;
m_State->m_Capacity = capacity;
// The list is empty so there is no head or tail
m_State->m_HeadIndex = -1;
m_State->m_TailIndex = -1;
// Version starts at one so that the default (0) is never used
m_State->m_Version = 1;
// Initialize safety ranges
#if ENABLE_UNITY_COLLECTIONS_CHECKS
m_Length = -1;
m_MinIndex = -1;
m_MaxIndex = -1;