..array element removal..
BlitzMax Forums/BlitzMax Programming/..array element removal..
| ||
..what would be most efficient way of removing desired element of an array..array contain integers only.. |
| ||
The most efficient way would be to use a TList or TMap instead - arrays are not the best choice where the data needs to be changed around/removed. But if you insist on arrays, slicing is probably the best way. I don't often use it myself but its well documented. You might as read up on it yourself rather than me find the info and repeat it all to you. |
| ||
..slices are fine, i was just wondering is there anything else to consider..because when im passing trough array in order to get specific data and Tlist, i found that actually arrays are faster..so i was wondering what techniques you guys using for such thing.. |
| ||
You could set empty elements to a preset dummy value (such as smallest possible integer) and simply ignore any elements with this value. If order in the array is not important, you could swap the unwanted element with the last element in the list and maintain your own counter of how long the array is. A TMap is between an array and TList for both removing a specific element and getting a specific element. |
| ||
When I really want speed, then I use a slot/garbage system (frankly builds upon what Czar suggested):Global data:Int[MAX_DATA] Global datacount:Int Global datapeak:Int Global garbage:Int[MAX_GARBAGE] Global garbagecount:Int Function AddData:Int( content:Int ) Local slot:Int If garbagecount garbagecount:- 1 slot = garbage[ garbagecount ] Else slot = datapeak datapeak:+ 1 End If data[ slot ] = content datacount:+ 1 Return slot End Function Function RemoveData( slot:int ) data[ slot ] = DUMMY_VALUE garbage[ garbagecount ] = slot garbagecount:+ 1 datacount:- 1 End Function |
| ||
you can create second array, same size, bytes only, read that array first, if 1 then that field in other array i used. Set to 0 if you want to "remove" data from other array. You increase memory usage a bit but not so much :) |
| ||
If you are going into complex solutions such as support arrays, you might as well create a TInt type with only a single integer field, and store those in the array. Then you can use Null as your dummy value! |
| ||
@Polan: Surprisingly, 4 bytes will be allocated for each byte var, so they dont get you any mem benefit. Some speed benefit in some situations though. I guess. |
| ||
Surprisingly, 4 bytes will be allocated for each byte var Not in an array : SuperStrict Local bytes:Byte[] = New Byte[256] Local bp:Byte Ptr = bytes For Local i:Int = 0 Until bytes.length bp[0] = 1 bp:+ 1 Next ' reset bp = bytes ' display first 4 bytes (hex) Print Hex(Int Ptr(bp)[0]) will be 256 bytes big, plus 4 for the object reference (which I believe precedes it)... Of course, if you mean a byte Field, then yes, they are int aligned, I believe. |
| ||
Beanage, the reason is a 32-bit processor goes through data 32-bit chunks at a time. That's 4 bytes and an Int is 4 bytes, and so the fastest data type. A byte is 8-bits and so it's actually more awkward for the processor to handle than an Int, even though it's smaller. Is a BlitzMax Long faster on a 64-bit processor? |