I have a folder array that contains up to +1.000.000 entries.
Dim folderStack() as FolderItem
Now I need to remove X entries at the start and Y entries at the end.
Cutting the end isn’t difficult:
If I need to cut the last 5.000 and total entries is 10.000)
Redim folderStack(5000)
For removing entries at the start, is a bit more work. You need to copy the ones you want to keep to another array and then copy it back.
When there are a lot of entries, this is slow when you have a lot of entries.
Anyone has a better way doing this?
FWW with Swift you have ArraySlice which works incredible fast (even for more than 1.000.000 entries is <0.1s). I don’t think Xojo has a similar
Another thought: how about using Array.SortWith() https://documentation.xojo.com/api/language/sort.htmlWith and sort the array so the to-be-deleted items are now at the end of the array, and then use Redim() to truncate the array? This might be faster (it might also be much, much slower).
sub FastSliceArray(extends a() as folderItem, startIndex as integer, endIndex as integer)
#pragma DisableBackgroundTasks
dim i as integer = 0
for j as integer = startIndex to endIndex
a(i) = a(j) ' shift item
i = i + 1 ' remember index of last shifted item
next
redim a(i-1) ' truncate
end sub
[quote=453952:@Kem Tekinay]
As an aside, it’s a shame that you can’t declare a general array parameter, because this would work with any type of array.
Sub MyArrayFunction (Extends arr() As Array) // if only...
[/quote] huh …
In Go, a slice is a reference type, so the “sub set” sliced is just a view of the real complete array, and that’s why it is so fast to grab a slice of an array.
Wow ! Great and some clever ideas!
Will do some tests for sure.
BTW I do like the boolean flag method. This way I can even undo things.
Thank you all for the valuable input!
API2 comes to mind …
I think this is the same with Swift ArraySlice.
[quote=453966:@Rick Araujo]A “sliceable array” could be a Xojo class with .append() .index() etc and .slice(start, end) which returns a new slice with that subset.
[/quote]