-
-
Notifications
You must be signed in to change notification settings - Fork 21.2k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Sync C# Array with Core #71786
Sync C# Array with Core #71786
Conversation
9da6899
to
7ef8cd0
Compare
// If we can retrieve the count of the collection without enumerating it | ||
// (e.g.: the collections is a List<T>), use it to resize the array once | ||
// instead of growing it as we add items. | ||
if (collection.TryGetNonEnumeratedCount(out int count)) | ||
{ | ||
Resize(Count + count); | ||
|
||
var enumerator = collection.GetEnumerator(); | ||
|
||
for (int i = 0; i < count; i++) | ||
{ | ||
enumerator.MoveNext(); | ||
this[count + i] = enumerator.Current; | ||
} | ||
|
||
return; | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Not sure if resizing the array instead of growing as the items are added makes that much of a difference and is worth the complexity.
7ef8cd0
to
5a08e2f
Compare
About |
4d0e4a8
to
13f2e74
Compare
I ended up adding |
c4210a0
to
45b5076
Compare
I removed the changes about readonly-ness since I'm not convinced it was the proper way to do it and don't want to back ourselves into a corner by implementing something we can't break compat with later. I'll extract it to a different PR (see #71986) so it can be merged separately and doesn't hold back this PR. |
45b5076
to
3f108ff
Compare
/// all nested arrays and dictionaries are duplicated and will not be shared with | ||
/// the original array. If <see langword="false"/>, a shallow copy is made and | ||
/// references to the original nested arrays and dictionaries are kept, so that | ||
/// modifying a sub-array or dictionary in the copy will also impact those | ||
/// referenced in the source array. Note that any <see cref="Object"/> derived | ||
/// elements will be shallow copied regardless of the <paramref name="deep"/> | ||
/// setting. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
/// all nested arrays and dictionaries are duplicated and will not be shared with | |
/// the original array. If <see langword="false"/>, a shallow copy is made and | |
/// references to the original nested arrays and dictionaries are kept, so that | |
/// modifying a sub-array or dictionary in the copy will also impact those | |
/// referenced in the source array. Note that any <see cref="Object"/> derived | |
/// elements will be shallow copied regardless of the <paramref name="deep"/> | |
/// setting. | |
/// all nested arrays and dictionaries are recursively duplicated and will not be shared with | |
/// the original array. If <see langword="false"/>, a shallow copy is made and | |
/// references to the original nested arrays and dictionaries are kept, so that | |
/// modifying a sub-array or dictionary in the copy will also impact those | |
/// referenced in the source array. Note that any <see cref="Object"/> derived | |
/// elements will not be copied regardless of the <paramref name="deep"/> | |
/// setting. |
Should probably be changed in the classref as well. At least the bit about objects being shallow copied is very miss understandable / being even more explicit then my suggestion may be even better.
9d254b1
to
d425e82
Compare
- Add `AddRange` method. - Add `Fill` method. - Add `Max` and `Min` methods. - Add `PickRandom` method. - Add `Reverse` method. - Add `RecursiveEqual` method. - Add `Sort` method. - Add `Slice` and `GetSliceRange` methods. - Add `IndexOf` overload that takes an index parameter. - Add `LastIndexOf` method. - Add `BinarySearch` method. - Add/update documentation.
d425e82
to
54f8fb0
Compare
Does our version of binary search return different values (to match Godot) than that of some core C# classes? If that's the case, to avoid confusion, I think it would be better to name our version |
The current implementation of I also don't think it's useful to provide a |
Thanks! |
Summary
AddRange
method.AddRange
(e.g.:List<T>
).append_array
. Added to Core in Add append_array() method to Array class #43398.Add
which is the equivalent of GDScript'sappend
.IEnumerable
is a Godot array and uses a single interop call; otherwise, falls back to iterating the collection.Fill
method.Max
andMin
methods.PickRandom
method.System.Random
instead.Reverse
method.Reverse
that reverses in place (e.g.:List<T>
).RecursiveEqual
method.==
and!=
operators which compare by value. These operators still compare by reference in .NET since that's likely the users expectation as all .NET collections work like this.SequenceEqual
to avoid confusing users with the behavior differences withEnumerable.SequenceEqual
(see Sync C# Array with Core #71786 (comment)).Slice
andGetSliceRange
methods.Slice
that returns a value of the same type (e.g.:List<T>
,Span
,ImmutableArray
, ...).Slice
method with the needed signature to get implicit Range support.GetSliceRange
which follows more closely the behavior of GDScript'sslice
method and allows to specify astep
anddeep
parameters.Slice
methods in .NET usually usestart
andlength
as parameter, but Godot'sslice
method usesstart
andend
which has a different meaning and could be confusing as overloads.Sort
method.Sort
that sorts in place (e.g.:List<T>
).IndexOf
method overload that takesindex
parameter.IndexOf
with the same parameters (e.g.:List<T>
).find
method also allows for an optionalindex
parameter.LastIndexOf
method.LastIndexOf
with the same overloads (e.g.:List<T>
).rfind
.IndexOf
which we use as the equivalent of GDScript'sfind
.BinarySearch
method.BinarySearch
(e.g.:List<T>
).Methods not added
Callable
(usually their name ends with_custom
, e.g.:sort_custom
,all
,any
).get_typed_builtin
,get_typed_class_name
,get_typed_script
,is_typed
,typed_assign
,set_typed
) since they feel very out of place in .NET.hash
.GetHashCode
instead.GD.Hash
as well, so maybe we don't want to implement it in theArray
class.bsearch
.I think this functionality is too specific and should probably be added as an extension rather than in the(I ended up implementing it asArray
class, if at all.BinarySearch
).count
because its name conflicts with theCount
property, we would have to think of a different name, it also doesn't seem as useful to me as the other methods.array.Count(x => x == value)
), although the performance won't be great because it would have to marshal every item.push_front
,push_back
,pop_front
,pop_back
,pop_at
).Add
,Insert
,Remove
andRemoveAt
so there doesn't seem to be much benefit in adding these methods.IList<T>
since those methods sound like they'd fit better in aQueue
orStack
collection.<
,<=
,>
,>=
) because they don't seem particularly useful or intuitive.