Page 2 of 2

Re: DataStructure

Posted: Sat Apr 20, 2013 4:38 pm
by BorisTheOld
This is an interesting request, but is probably not necessary, since there's a simple way of implementing it.

The 20,000+ items presumably already exist in a file which must be converted into source code. The Data Section would look something like this:

Code: Select all

DataSection
  IncludeFile "filename"
EndDataSection
We use a small program to convert the original data file into a series of Data statements, with the data converted into 4-byte hex constants, such as:

Code: Select all

Data.L $aabbccdd, $aabbccdd, etc
When our application starts, the encoded data in the DataSection is expanded into arrays for ease of processing.

It's a simple technique that can handle all types of data, and gets around the problem of having to provide customers with small data files as well as the executable code.

Re: DataStructure

Posted: Sun Apr 21, 2013 11:02 pm
by bosker
Well, everybody else is necro-posting :) ...

Since the original post in this thread, some things have been added to PB that make it possible to do what I think Michael wanted
without copying data. A few structures and macros is all it takes.
The following test code reveals all (I hope)...

Code: Select all

EnableExplicit

Structure TCity         ; a single city data
    Name.s
    Country.a
    x.f
    y.f
EndStructure

Structure TCities       ; a whole bunch of cities
    City.TCity[0]       ; this works for any number (but may be considered flaky)
EndStructure

Macro mCity(Name, Country, x, y)    ; Declare of a single city (keeps all data together)
    Data.i @ Name       ; address of name string
    Data.a Country
    Data.f x, y
EndMacro

; Test code
Define *Cities.TCities = ?CityData
Define i.l

OpenConsole()

Repeat
    With *Cities\City[i]
        If \Name <> "XXXX"
            PrintN(\Name + ", " + Str(\Country) + " at " + StrF(\x) + ", " + StrF(\y))
        Else
            Break
        EndIf
    EndWith

    i + 1
Forever

PrintN("Done!")

DataSection
CityData:
    mCity("Andorra la Vella",   1,  42.51,  1.51)
    mCity("Les Escaldes",       1,  42.50,  1.53)
    mCity("Abu Zabi",           2,  24.48, 54.37)
    mCity("Khawr Fakkan",       2,  25.34, 56.36)

    Data.i @"XXXX"              ; terminator (if you like)
EndDataSection

Re: DataStructure

Posted: Mon Apr 22, 2013 12:15 am
by skywalk
Thank you bosker! 8) Better to post later than never!
Hadn't thought to try a macro in a DataSection.
This solves an annoying problem I had with losing track of which position I was in while editing a data line.
Now with the macro, I get context help. Really cool.

Re: DataStructure

Posted: Mon Apr 22, 2013 2:39 am
by BorisTheOld
Of course, a more simple approach is to not use a DataSection to store the data, but to assign it directly to some other data structure, such as an array. It all depends on the application and the nature of the data.

The solution depends on many things. For instance, is the data in memory accessed sequentially or by one or more key fields? Will an array or map work best? Or should the data be written to a random access file for the duration of the program run?

The trick is not to get hung up on a particular approach, but to be creative and think outside the box.

Mostly, we write embedded data to indexed files in order to do keyed lookups, but for a few situations we embed binary data in a DataSection.

Re: DataStructure

Posted: Mon Apr 22, 2013 7:45 am
by davido
bosker:

Very, very clever! :D

Re: DataStructure

Posted: Mon Apr 22, 2013 6:39 pm
by Michael Vogel
bosker wrote:Well, everybody else is necro-posting :)
[...]
I love that :wink: