Mirage Source
http://www.miragesource.net/forums/

Adjusting an array
http://www.miragesource.net/forums/viewtopic.php?f=143&t=5552
Page 1 of 1

Author:  GIAKEN [ Wed Apr 29, 2009 2:59 am ]
Post subject:  Adjusting an array

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

Author:  Jacob [ Wed Apr 29, 2009 6:44 pm ]
Post subject:  Re: Adjusting an array

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.

Author:  GIAKEN [ Wed Apr 29, 2009 9:34 pm ]
Post subject:  Re: Adjusting an array

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

Author:  Jacob [ Thu Apr 30, 2009 3:50 pm ]
Post subject:  Re: Adjusting an array

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


Author:  Lea [ Fri May 29, 2009 4:11 am ]
Post subject:  Re: Adjusting an array

copymemory and zeromemory = <3

Author:  Robin [ Fri May 29, 2009 5:03 pm ]
Post subject:  Re: Adjusting an array

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.

Page 1 of 1 All times are UTC
Powered by phpBB® Forum Software © phpBB Group
https://www.phpbb.com/