DataStructure

Got an idea for enhancing PureBasic? New command(s) you'd like to see?
BorisTheOld
Enthusiast
Enthusiast
Posts: 542
Joined: Tue Apr 24, 2012 5:08 pm
Location: Ontario, Canada

Re: DataStructure

Post 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.
For ten years Caesar ruled with an iron hand, then with a wooden foot, and finally with a piece of string.
~ Spike Milligan
bosker
Enthusiast
Enthusiast
Posts: 105
Joined: Fri Jan 08, 2010 11:04 pm
Location: Hampshire, UK

Re: DataStructure

Post 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
User avatar
skywalk
Addict
Addict
Posts: 4211
Joined: Wed Dec 23, 2009 10:14 pm
Location: Boston, MA

Re: DataStructure

Post 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.
The nice thing about standards is there are so many to choose from. ~ Andrew Tanenbaum
BorisTheOld
Enthusiast
Enthusiast
Posts: 542
Joined: Tue Apr 24, 2012 5:08 pm
Location: Ontario, Canada

Re: DataStructure

Post 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.
For ten years Caesar ruled with an iron hand, then with a wooden foot, and finally with a piece of string.
~ Spike Milligan
davido
Addict
Addict
Posts: 1890
Joined: Fri Nov 09, 2012 11:04 pm
Location: Uttoxeter, UK

Re: DataStructure

Post by davido »

bosker:

Very, very clever! :D
DE AA EB
User avatar
Michael Vogel
Addict
Addict
Posts: 2798
Joined: Thu Feb 09, 2006 11:27 pm
Contact:

Re: DataStructure

Post by Michael Vogel »

bosker wrote:Well, everybody else is necro-posting :)
[...]
I love that :wink:
Post Reply