have a whack at improving my arraybuffer_add sub

General FreeBASIC programming questions.
agamemnus
Posts: 1842
Joined: Jun 02, 2005 4:48

have a whack at improving my arraybuffer_add sub

Postby agamemnus » Nov 21, 2007 17:02

I wanted to make a function that could change the size of my unknown-final-size UINTEGER array "every once in a while", instead of each time I added a value to it. I made an arraybuffer_add function which does this. Thanks to anonymous1337, 1000101, and voodooattack for some early issues I had.

Some improvements that might be nice:
* A way to use array types other than UINTEGER.
* A better search algorithm for searching for an already-existing array in the "for i = 1 to n" loop. (eg: B+)
* A function that is somehow tied to all the ways an array can be removed from memory to clean up the "arraybuffer.pointerlist" and "arraybuffer.virtualsizelist" (or, perhaps a run-once-in-a-while "cleanup" function?)

Here is the code, which I put into "arraybuffer.bi":

Code: Select all

declare sub arraybuffer_add (curarray() as uinteger, byval valuetoadd as uinteger)
declare sub arraybuffer_normalize (curarray() as uinteger)

namespace arraybuffer
 dim shared as uinteger buffersize, sub_initialized
 dim shared pointerlist() as uinteger ptr
 dim shared virtualsizelist() as uinteger
end namespace

arraybuffer.buffersize = 512
arraybuffer.sub_initialized = 0

sub arraybuffer_add (curarray() as uinteger, byval valuetoadd as uinteger)
dim as integer i, n, m
dim p as integer ptr

p = @curarray(0)
if arraybuffer.sub_initialized = 0 then
arraybuffer.sub_initialized = 1
redim arraybuffer.pointerlist(0 to 0)
redim arraybuffer.virtualsizelist(0 to 0)
goto one
end if
n = ubound(arraybuffer.pointerlist)


for i = 0 to n
if p = arraybuffer.pointerlist(i) then
goto two
end if
next i
redim preserve arraybuffer.pointerlist(0 to i)
redim preserve arraybuffer.virtualsizelist(0 to i)

one:
arraybuffer.pointerlist(i) = p
two:

if arraybuffer.virtualsizelist(i) + lbound(curarray) >= ubound(curarray) then
n = lbound(curarray) + ubound(curarray) + arraybuffer.buffersize - 1
redim preserve curarray(lbound(curarray) to n)
arraybuffer.pointerlist(i) = @curarray(0)
end if

curarray(arraybuffer.virtualsizelist(i)) = valuetoadd
arraybuffer.virtualsizelist(i) +=1
end sub



sub arraybuffer_normalize (curarray() as uinteger)
if arraybuffer.sub_initialized = 0 then exit sub
dim as integer i, n, m
dim p as integer ptr

p = @curarray(0)

n = ubound(arraybuffer.pointerlist)


for i = 0 to n
if p = arraybuffer.pointerlist(i) then
redim preserve curarray(lbound(curarray) to lbound(curarray) + arraybuffer.virtualsizelist(i) - 1)
end if
next i

end sub


Thank you and HAPPY THANKSGIVING! YAY....!
Last edited by agamemnus on Nov 27, 2007 18:18, edited 2 times in total.
anonymous1337
Posts: 5494
Joined: Sep 12, 2005 20:06
Location: California

Postby anonymous1337 » Nov 21, 2007 18:50

Well about the duplicated definition, make sure that the array is declared to be dynamic (which I believe you can't have in TYPE's...try a pointer instead).

Code: Select all

dim as integer myArray(0)

redim preserve myArray( 0 to 9 )
Won't work. When you say myArray(0), you're telling the compiler some important information about it. It's not a dynamic array.

Code: Select all

dim as integer myarray()

redim preserve myarray( 0 to 9 )
Works.

Code: Select all

type foo
  dim as integer myArray()
end type

dim as foo myFoo
redim preserve myFoo.myArray(0 to 9)
Won't work. No dynamic arrays within types. Use a pointer instead and resize that. I believe FBext may be able to solve some of these problems for you. . . It's a great lib.
agamemnus
Posts: 1842
Joined: Jun 02, 2005 4:48

Postby agamemnus » Nov 21, 2007 20:29

Thanks.

It's a namespace, not a TYPE, btw, so just removing "(0)" and fixing various other small issues fixed things. I will post this once I am done debugging (and describe it) so that others could have a whack at improving my function..

EDIT: I posted the completed function/sub...
cha0s
Site Admin
Posts: 5317
Joined: May 27, 2005 6:42
Location: Illinois
Contact:

Postby cha0s » Nov 21, 2007 23:21

How many gotos does it take do do a memory pool..... =P
agamemnus
Posts: 1842
Joined: Jun 02, 2005 4:48

Postby agamemnus » Nov 22, 2007 0:39

Hehe, nice one... I believe I said "have a whack at improving" the sub, though. =D
RockTheSchock
Posts: 228
Joined: Mar 12, 2006 16:25

I don't understand your code

Postby RockTheSchock » Nov 23, 2007 17:17

Sorry, but what does your code? I guess, it's only extending the Array if it's needed. Why you don't use a linked list of arrays and from time to time you make one bigger Array copying and erasing the others in the linked list.

If you use OOP you could implement some cool things overloading operators.
agamemnus
Posts: 1842
Joined: Jun 02, 2005 4:48

Re: I don't understand your code

Postby agamemnus » Nov 25, 2007 3:57

RockTheSchock wrote:Sorry, but what does your code [do]? I guess, it's only extending the Array if it's needed.


Right, but in chunks so that it doesn't keep redimming the array.

Why you don't use a linked list of arrays and from time to time you make one bigger Array copying and erasing the others in the linked list.


I suppose that would be more efficient, but how would I use it and not have to modify the array's structure at the same time? Might be possible but over my head.

If you use OOP you could implement some cool things overloading operators.


I don't know enough OOP for that, and I don't think FB does either. :\
anonymous1337
Posts: 5494
Joined: Sep 12, 2005 20:06
Location: California

Re: I don't understand your code

Postby anonymous1337 » Nov 25, 2007 4:02

agamemnus wrote:
If you use OOP you could implement some cool things overloading operators.


I don't know enough OOP for that, and I don't think FB does either. :\
It sure does! ^.^;;
notthecheatr
Posts: 1759
Joined: May 23, 2007 21:52
Location: Cut Bank, MT
Contact:

Postby notthecheatr » Nov 25, 2007 4:07

Off gourse vee haff zee overloading of operators!!! Zis ees verr verr you-zeffull.
agamemnus
Posts: 1842
Joined: Jun 02, 2005 4:48

Postby agamemnus » Nov 27, 2007 18:17

I found a few bugs and added a "normalization" function (to get rid of "empty" spots)

Return to “General”

Who is online

Users browsing this forum: No registered users and 3 guests