Delete from array?

BlitzMax Forums/BlitzMax Programming/Delete from array?

Drekinn(Posted 2009) [#1]
(Tried to locate existing posts on this but found nothing.)

Is it possible to delete items from an array?
Example:

arr:int[1,2,3,4,5]

How to remove the 3?


Any help would be appreciated.


Bremer(Posted 2009) [#2]
Check "slices" in the documentation, that should help you do what you are after.


xlsior(Posted 2009) [#3]
Slices don't work with multi-dimensional arrays, only single-dimension.... So the answer is probably "Can't be done" (unless you create a new array and copy the desired contents over)


_Skully(Posted 2009) [#4]
I think what he meant was:

Local arr:Int[]=[1,2,3,4,5]

arr=arr[..2]+arr[3..]

For ct=0 To 3
	Print arr[ct]
Next



xlsior(Posted 2009) [#5]
Ah. Makes sense, I was misinterpreting the question.


JoshK(Posted 2009) [#6]
Isn't this pretty inefficient, because you are allocating 2 new chunks of memory (slow) when all you really need is one memcopy (instant)?


_Skully(Posted 2009) [#7]
Yes.. but I didn't put a tonne of thought into it either :) 3 kids running around... you know ;)


JoshK(Posted 2009) [#8]
I don't think there is presently any other way of doing this. A memcopy will mess up GC for an array of objects. There should be something like this:
arr=arr[..3..]

or:
arr=arr[..3..5..]


_Skully(Posted 2009) [#9]
If he's just storing int's then using a bank with memory copy would be quicker (I think)


Brucey(Posted 2009) [#10]
If he's just storing int's then using a bank with memory copy would be quicker (I think)

Only as long as you are remembering that your "bank" will be virtually shorter - since you are shifting a whole block down a bit. What would you do with the now empty end bit? (other than setting the values to 0, I suppose).


_Skully(Posted 2009) [#11]
or set the first int as the length


JoshK(Posted 2009) [#12]
You can use memcopy with object arrays, but it will mess up the GC, of course, so don't do it.


Czar Flavius(Posted 2009) [#13]
What is this array being used for? Perhaps a linked list would be more appropriate?

I'd personally swap the 3 with the end 5, then either resize downwards one, or have a variable to keep track of the "usable length". Assuming the order of elements in the array was unimportant..


ImaginaryHuman(Posted 2009) [#14]
I suggest trying the self-optimizing arrays that I sort of invented ... basically like this:

Local MyArray:Int[50]
Local Space:Int=50
Local Items:Int=0

'Add an item...
If Items=Space then ResliceTheArrayToMakeItBigger() 'and add to Space
MyArray[Items]=Whatever
Items:+1

'Remove item 3 from an array, assuming it already contains 12 items...
'Space will = 50
'Items will = 12
MyArray[3]=MyArray[Items-1]
Items:-1


So basically when you add to the array you add after the previous item, at the end. When you remove something from the array, you copy the end item and overwrite the removed item with it, then decrease the number of items.

So long as you don't need your array to be sorted it'll work.

If you need your array to stay sorted, I suggest associating it with an array of indexes.


JoshK(Posted 2009) [#15]
Even if you do a conventional slice, you still need an extra array to keep track of the indexes, if you want to be able to directly remove items. And you need another array with the real and fake indexes sorted the opposite way.

Does anyone have something like this written already?