PureBasic Forum
http://forums.purebasic.com/english/

Dim Array with Data
http://forums.purebasic.com/english/viewtopic.php?f=12&t=73027
Page 1 of 1

Author:  mk-soft [ Sat Jun 15, 2019 12:19 pm ]
Post subject:  Dim Array with Data

Create an array with start values and change the values via DataSection.
At time not for strings.

Update v1.04
- Bugfix

Update v1.05
- Change: Remove parameter type
- Change: ArrayName now with type of array
- Added: DimWithString(...) and EndDimWithString()
- Added: LoadArrayString(...) and EndLoadArrayString()

Code:
;-TOP

; Comment : Dim Array with Data
; Author  : mk-soft
; Version : v1.05
; Created : 15.06.2019
; Updated :

; *********************************************************

EnableExplicit

CompilerIf Not Defined(__ArrayAddr, #PB_Variable)
  Threaded __ArrayAddr
  Threaded __ArraySize
  Threaded __ArrayIndex
  Threaded *__ArrayAddr.string
CompilerEndIf

; ----

Macro DimWithData(ArrayName, Size)
  Dim ArrayName(Size)
  __ArrayAddr = @ArrayName()
  DataSection
  __Begin_NewArrayData_#MacroExpandedCount:
EndMacro
 
Macro EndDimWithData()
  __End_NewArrayData_#MacroExpandedCount:
  EndDataSection
  CopyMemory(?__Begin_NewArrayData_#MacroExpandedCount, __ArrayAddr, ?__End_NewArrayData_#MacroExpandedCount - ?__Begin_NewArrayData_#MacroExpandedCount)
EndMacro

; ----

Macro LoadArrayData(ArrayName)
  __ArrayAddr = @ArrayName()
  DataSection
  __Begin_ArrayData_#MacroExpandedCount:
EndMacro

Macro EndLoadArrayData()
  __End_ArrayData_#MacroExpandedCount:
  EndDataSection
  CopyMemory(?__Begin_ArrayData_#MacroExpandedCount, __ArrayAddr, ?__End_ArrayData_#MacroExpandedCount - ?__Begin_ArrayData_#MacroExpandedCount)
EndMacro

; ----

Macro DimWithString(ArrayName, Size)
  Dim ArrayName(Size)
  *__ArrayAddr = @ArrayName()
  __ArraySize = ArraySize(ArrayName())
  DataSection
  __Begin_NewStringArray_#MacroExpandedCount:
EndMacro
 
Macro EndDimWithString()
  __End_NewStringArray_#MacroExpandedCount:
  EndDataSection
  Restore __Begin_NewStringArray_#MacroExpandedCount
  For __ArrayIndex = 0 To __ArraySize
    Read.s *__ArrayAddr\s
    *__ArrayAddr + SizeOf(integer)
  Next
EndMacro

; ----

Macro LoadArrayString(ArrayName)
  *__ArrayAddr = @ArrayName()
  __ArraySize = ArraySize(ArrayName())
  DataSection
  __Begin_StringArray_#MacroExpandedCount:
EndMacro

Macro EndLoadArrayString()
  __End_StringArray_#MacroExpandedCount:
  EndDataSection
  Restore __Begin_StringArray_#MacroExpandedCount
  For __ArrayIndex = 0 To __ArraySize
    Read.s *__ArrayAddr\s
    *__ArrayAddr + SizeOf(integer)
  Next
EndMacro

; *********************************************************

;-Example

CompilerIf #PB_Compiler_IsMainFile
 
  Define index
 
  Debug "Init Array..."
  Global DimWithData(Values.f, 5) ; <-- Don´t forget ArrayName with type float (.f)
    Data.f 1.1, 1.2, 1.3, 1.4, 1.5, 1.6
  EndDimWithData()
 
  For index = 0 To 5
    Debug StrF(Values(index), 2)
  Next
  Debug "----"
 
  Debug "Change Data 2"
  ReDim Values(7)
  LoadArrayData(Values)
    Data.f 2.1, 2.2, 2.3, 2.4, 2.5, 2.6, 2.7, 2.8
  EndLoadArrayData()
 
  For index = 0 To 7
    Debug StrF(Values(index), 2)
  Next
  Debug "----"
 
  Debug "Change Data 3"
  ReDim Values(3)
  LoadArrayData(Values)
    Data.f 3.1, 3.2, 3.3, 3.4
  EndLoadArrayData()
 
  For index = 0 To 3
    Debug StrF(Values(index), 2)
  Next
  Debug "----"
 
  Debug "Init String Array..."
  Global DimWithString(sVal.s, 3) ; <-- Don´t forget ArrayName with type string (.s)
    Data.s "Hello " , "World!", "I Like Purebasic", ""
  EndDimWithString()
 
  For index = 0 To 3
    Debug "Index " + index + " = " + sVal(index)
  Next
  Debug "----"
 
  Debug "Update String Array..."
  LoadArrayString(sVal)
    Data.s "One" , "Two", "Three", "Four"
  EndLoadArrayString()
 
  For index = 0 To 3
    Debug "Index " + index + " = " + sVal(index)
  Next
  Debug "----"
 
 
CompilerEndIf

Author:  mk-soft [ Sat Jun 15, 2019 12:46 pm ]
Post subject:  Re: Dim Array with Data

Code:
;-Example Multi-Array

CompilerIf #PB_Compiler_IsMainFile
 
  Define a, b
 
  Dim iVal(4, 1)
 
  LoadArrayData(iVal)
  Data.i 11, 12
  Data.i 21, 22
  Data.i 31, 32
  Data.i 41, 42
  EndLoadArrayData()
 
  For a = 0 To 4
    For b = 0 To 1
      Debug "" + a + ":" + b + " = " + iVal(a,b)
    Next
  Next
 
CompilerEndIf

Author:  blueb [ Sat Jun 15, 2019 1:37 pm ]
Post subject:  Re: Dim Array with Data

Very nice source and examples. Thanks :D

Author:  mk-soft [ Sat Jun 15, 2019 1:55 pm ]
Post subject:  Re: Dim Array with Data

Thanks blueb,

But is not for Strings. I work on it...

Author:  Olliv [ Sat Jun 15, 2019 3:38 pm ]
Post subject:  Re: Dim Array with Data

Done
We can remove "Data.I" prefix.

Author:  mk-soft [ Sat Jun 15, 2019 7:05 pm ]
Post subject:  Re: Dim Array with Data

Olliv wrote:
Done
We can remove "Data.I" prefix.

:wink:

Author:  mk-soft [ Sat Jun 15, 2019 7:06 pm ]
Post subject:  Re: Dim Array with Data

Update v1.05
- Change: Remove parameter type
- Change: ArrayName now with type of array
- Added: DimWithString(...) and EndDimWithString()
- Added: LoadArrayString(...) and EndLoadArrayString()

Now Strings supported :wink:

Page 1 of 1 All times are UTC + 1 hour
Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group
http://www.phpbb.com/