List type and arrays

General FreeBASIC programming questions.
marcov
Posts: 2964
Joined: Jun 16, 2005 9:45
Location: Eindhoven, NL
Contact:

Re: List type and arrays

Postby marcov » Dec 19, 2017 10:37

paul doe wrote:
Josep Roca wrote:We can do it ourselves, but because FB does not support templates, we would need to have procedures or classes for each kind of data type.

Unfortunately, yes. But depending on what paradigm one favors (OOP/Procedural) this may or may not be an issue. If (like me) you favor the OOP paradigm, you simply wrap the class with the datatype you want (the flexible way). Or, you can do some sort of crude 'templating' using macros (the efficient way).


The trouble is that arrays do support all value types (random structs) and OOP wrapper classes usually limit to reference types only.
You need generics if you want to make typesafe containers that are not by ref only.

To circumvent similar limitations, in an old job however (in the pre generics Delphi era) we had a system that parsed the source files with business objects though, and automatically generate source for various container types.

Having a language parser as a library is a really strong tool in such cases.
jj2007
Posts: 1470
Joined: Oct 23, 2016 15:28
Location: Roma, Italia
Contact:

Re: List type and arrays

Postby jj2007 » Dec 19, 2017 10:47

dodicat wrote:Does this give the same results with your test file (Biblical thing)

Thanks, dodicat. It looks similar, but

- your StringSplit uses " " to create a text array with 766108 elements (see below), while my Recall()-based code divides into 30383 lines (i.e. CrLf as separator)

- my Instr_(b$(ecx), "devil", 5) checks for each line if there is an occurrence of "devil" (5=case-insensitive, full word), while there are 3*instr(a(n),char) in your code but I have not fully understood what each of them does.

My idea of a string array is that each line of a text file constitutes one array element; that is more or less how a compiler would "see" a text file. But there are of course applications like dictionaries where whitespace is the more appropriate delimiter.

Code: Select all

 4077771      File length
 4077771      File length compare
Time to load into array  0.1324772974409996
 766108       array size before
Time to delete devil  0.3186197457243907
 765993       array size after
dodicat
Posts: 6498
Joined: Jan 10, 2006 20:30
Location: Scotland

Re: List type and arrays

Postby dodicat » Dec 19, 2017 10:58

Hello lost Zergling.
The StringSplit I admit looks a bit crazy.
A while back there was a kind of competition to see who could split a string into an array quickly.
The codes got faster post by post, but became more stupid looking each day.
This is the result.
The arraydelete I thought was perhaps easier on the eye.
One loop to find the size of the of the temporary array.
One loop to fill it.
Then copy back in to the original array via memcpy.
dodicat
Posts: 6498
Joined: Jan 10, 2006 20:30
Location: Scotland

Re: List type and arrays

Postby dodicat » Dec 19, 2017 11:02

Hello jj2007.
Yea, I wondered about that.
I was thinking of splitting by chr(10) to grab a line at a time, but changed my mind.
The devil is in the detail, either way.
jj2007
Posts: 1470
Joined: Oct 23, 2016 15:28
Location: Roma, Italia
Contact:

Re: List type and arrays

Postby jj2007 » Dec 19, 2017 11:03

> The devil is in the detail, either way.
I wonder where the smilies are ;-)

New version that mimics the behaviour of your code. Loading is a tick faster, eliminating the devil a tick slower ;-)
Lost Zergling
Posts: 322
Joined: Dec 02, 2011 22:51
Location: France

Re: List type and arrays

Postby Lost Zergling » Dec 19, 2017 14:06

@dodicat : thx indeed :-)
paul doe
Posts: 1225
Joined: Jul 25, 2017 17:22
Location: Argentina

Re: List type and arrays

Postby paul doe » Dec 19, 2017 15:15

marcov wrote:The trouble is that arrays do support all value types (random structs) and OOP wrapper classes usually limit to reference types only.
You need generics if you want to make typesafe containers that are not by ref only.

Yes, indeed. But as I stated before, this may or may not be an issue, depending mostly on how you treat data. If you look at the arrayList implementation I posted, you'll notice that's intentionally left vague. This is so because I keep data and objects that act or represent data segregated. How to wrap the class to support primitive data types is left as an exercise for the reader ;)
marcov wrote:To circumvent similar limitations, in an old job however (in the pre generics Delphi era) we had a system that parsed the source files with business objects though, and automatically generate source for various container types.

Having a language parser as a library is a really strong tool in such cases.

Yes, I agree too. I've coded similar utilities before (waaay back in the 90s), mostly to parse and add generics support for customized scripting languages. I'm not a fan of templating, though. I tend to favor abstract code, if you know what I mean (must be something I inherited from my Forth days haha)

EDIT: I edited the date, as I messed it up, sorry =D
Last edited by paul doe on Dec 19, 2017 20:38, edited 3 times in total.
dodicat
Posts: 6498
Joined: Jan 10, 2006 20:30
Location: Scotland

Re: List type and arrays

Postby dodicat » Dec 19, 2017 18:30

Strangely this seems to work for a general arraydelete.
It seemed far too simple to be effective.
String or numerical.
You can choose which type of string delete by un- commenting.
straight or instr or instr any if required.


Code: Select all



#macro arraydelete(array,discard)
scope
    dim as long ctr=lbound(array)-1
    for n as integer=lbound(array) to ubound(array)
        #if typeof(array)=string
        'if instr(array(n),discard)=0 then ctr+=1: array(ctr)=array(n)
        if (array(n)<>discard) then ctr+=1: array(ctr)=array(n)
    #else
    if array(n)<>discard then ctr+=1: array(ctr)=array(n)
        #endif
    next n
        redim preserve array(lbound(array) to ctr)
        end scope
#endmacro

redim as string b(3 to 100000)

for n as long=3 to 100000
    b(n)="AB"
    if n mod 10000=0 then b(n)="ABC"
next


b(5)="Hello"
b(95)="goodbye"

arraydelete(b,"AB")
for n as long=lbound(b) to ubound(b)
    print n,b(n)
next


redim as integer i(100000000)
for n as long=0 to 100000000
    i(n)=13
next
print
print
i(500000)=2017
i(600000)=2018
i(700000)=2019
dim as double t=timer
arraydelete(i,13)
print timer-t
for n as long=lbound(i) to ubound(i)
    print n,i(n)
next
print
sleep



 

Return to “General”

Who is online

Users browsing this forum: No registered users and 3 guests