Mirage Source
Adjusting an array - Printable Version

+- Mirage Source (https://mirage-engine.uk/forums)
+-- Forum: Mirage Source (Nostalgia) (https://mirage-engine.uk/forums/forumdisplay.php?fid=61)
+--- Forum: Archive (2006-2011) (https://mirage-engine.uk/forums/forumdisplay.php?fid=18)
+---- Forum: Programming (https://mirage-engine.uk/forums/forumdisplay.php?fid=24)
+----- Forum: Visual Basic 6 (https://mirage-engine.uk/forums/forumdisplay.php?fid=32)
+----- Thread: Adjusting an array (/showthread.php?tid=2773)



Adjusting an array - GIAKEN - 29-04-2009

Here's some code to adjust an array. This could mean boundless / dynamic arrays...

The problem with them is for example:

Player(1 To 1)

When a player logs in it fills the first data, then a second comes in and you do ReDim Player(1 To UBound(Player) + 1) and you just have to loop through the UBound of the Player array...however, here is where adjusting an array comes in.

Say for example player 2 leaves, you just do ReDim Preserve Player(1 To UBound(Player) - 1)...well what if Player 1 leaves? If you just get rid of the last part of the array it will leave the player 1 data and clear the player 2...so you have to switch the array around. Here's a form giving an example of rearranging an array I made:

http://mayhem.auburnflame.com/frmArrayTest.frm


Re: Adjusting an array - Jacob - 29-04-2009

You should learn to use "CopyMemory" api and copy the parts of the array.

Later tonight I'll write up a bit of code to show how it's done.


Re: Adjusting an array - GIAKEN - 29-04-2009

Yeah I'm sure there's better ways...I never use CopyMemory / ZeroMemory because I haven't really studied up on them yet.


Re: Adjusting an array - Jacob - 30-04-2009

Here's a real quick class that will use a byte array. You can insert, insert at certain indexes and remove at certain indexes.

I kinda tested it, if you see any problems let me know.

Code:
' **************
' * By : Dugor *
' **************

Option Explicit

' Our copy memory API call
Private Declare Sub CopyMemory Lib "Kernel32.dll" Alias "RtlMoveMemory" (Destination As Any, Source As Any, ByVal Length As Long)

Private bArr() As Byte      ' A byte array we are using
Private Size As Long        ' The size of the array

Private Sub Class_Initialize()
    Size = -1
End Sub

' Will insert the value at the end of the array
Public Sub Insert(ByRef Value As Byte)
    Size = Size + 1
    ReDim Preserve bArr(Size)
    bArr(Size) = Value
End Sub

' Will insert the value at the index you specify
' It will then move the rest of the array down
Public Sub InsertAt(ByRef Index As Long, ByRef Value As Byte)
    If Index < 0 Then Exit Sub
    
    ' If the index you're trying to insert at is above our size
    ' just insert it at the end
    If Index > Size Then
        Insert Value
    Else
        Size = Size + 1
        ReDim Preserve bArr(Size)
        CopyMemory bArr(Index + 1), bArr(Index), Size - Index
        bArr(Index) = Value
    End If
End Sub

' Will remove a part of the array
Public Sub Remove(ByRef Index As Long)
    If Index < 0 Then Exit Sub
    
    ' If this is the last element we have to handle it different
    If Index = Size Then
        Size = Size - 1
        ReDim Preserve bArr(Size)
    Else
        If Index > Size Then Exit Sub
        CopyMemory bArr(Index), bArr(Index + 1), Count - Index
        Size = Size - 1
        ReDim Preserve bArr(Size)
    End If
End Sub

Public Function Count() As Long
    Count = (UBound(bArr) - LBound(bArr)) + 1
End Function

Public Sub Display()
Dim i As Long
    Debug.Print "Amount of items in array: ", Count
    For i = LBound(bArr) To UBound(bArr)
        Debug.Print "Index:"; i, "Value:", bArr(i)
    Next
End Sub

This is a quick example of how to use it.
Code:
Dim t As New clsArray
    
    t.Insert 1
    t.Insert 2
    t.Insert 8
    t.InsertAt 1, 5
    t.Display
    
    t.Remove 3
    t.Display
    
    t.InsertAt 4, 10
    t.Display

This is the output:
Quote:Amount of items in array: 4
Index: 0 Value: 1
Index: 1 Value: 5
Index: 2 Value: 2
Index: 3 Value: 8
Amount of items in array: 3
Index: 0 Value: 1
Index: 1 Value: 5
Index: 2 Value: 2
Amount of items in array: 4
Index: 0 Value: 1
Index: 1 Value: 5
Index: 2 Value: 2
Index: 3 Value: 10



Re: Adjusting an array - Robin - 29-05-2009

The memory API is so fucking easy to use, and so much faster, I don't see why anyone wouldn't learn it.

I'll check your code out later tonight, see if it's better than the one I use myself.