Queue 1.1 - library

Share your advanced PureBasic knowledge/code with the community.
Froggerprogger
Enthusiast
Enthusiast
Posts: 423
Joined: Fri Apr 25, 2003 5:22 pm
Contact:

Queue 1.1 - library

Post by Froggerprogger »

[edit]
I updated to version 1.1:

+ fixed a string-memory-hole during clean-up and dequeue/delete (strings are set to "" now)
+ added Queue_DequeueToString() - I just forgot it before. Similar to Queue_GetValToString()
(Returns a formatted string containing information on the active element of any type)
+ Change Queue Dequeue*()'s behaviour of setting the active element. It keeps now the active element
or resets the queue if it was the (previous) head. So you might mix up Queue_Next() with Queue_Dequeue()
without running into trouble.
+ added Queue_SetVal*() - functions to assign a new value of any type to the active element. (Just for
your convenience - you might further use the (faster) syntax e.g.:
*myVar.Variant_PB = Queue_GetActElement() : *myVar\l = 1234 : *myVar\type = #Long
+ added some more examples
+ added #Queue_GetResVersion = 1.1 to check against Queue_VersionInfo()
[/edit]




Hi!
I just finished the Queue-library.

These procedures give you all you need to handle dynamic data. You can use just the 'pure' queue-functionality (enqueue/dequeue) or the 'active element'-browsing for functionality same as lists. Further you can insert and delete elements anywhere inside this queue.

As elements you might use byte/word/long/float/string-values, or structures.
You can arbitrary mix them inside a queue.

You might have a look here (library and source):

http://www.2mal2mal.de/public/purebasic/libs

(Browse for the files in the subdirectory or download the self-named zip)
Last edited by Froggerprogger on Mon Jul 12, 2004 7:49 pm, edited 1 time in total.
%1>>1+1*1/1-1!1|1&1<<$1=1
User avatar
NoahPhense
Addict
Addict
Posts: 1999
Joined: Thu Oct 16, 2003 8:30 pm
Location: North Florida

Re: Queue 1.0 - library

Post by NoahPhense »

two words: freakin sweet

Nice work!

- np

Code: Select all

q = Queue_Create()

Queue_EnqueueS(q, "1")
Queue_EnqueueS(q, "2")
Queue_EnqueueS(q, "3")
Queue_EnqueueS(q, "4")
Queue_EnqueueS(q, "5")
Queue_EnqueueS(q, "6")

Debug Str(Queue_GetNumElems(q)) + " elements left in queue" ; there are no more elements in queue

Debug Queue_DequeueS(q)
Debug Queue_DequeueS(q)
Debug Queue_DequeueS(q)
Debug Queue_DequeueS(q)
Debug Queue_DequeueS(q)
Debug Queue_DequeueS(q)

Debug Str(Queue_GetNumElems(q)) + " elements left in queue" ; there are no more elements in queue
Unless you're already making one.. I can make a linked chm file for you.
Just send me ALL the functions in a text file..

*EDIT*

One question:

Code: Select all

q = Queue_Create()

Queue_EnqueueS(q, "apples")
Queue_EnqueueS(q, "oranges")
Queue_EnqueueS(q, "pears")
Queue_EnqueueS(q, "bananas")
Queue_EnqueueS(q, "grapes")
Queue_EnqueueS(q, "peaches")

Debug Str(Queue_GetNumElems(q)) + " elements left in queue" ; there are no more elements in queue

Queue_Reset(q)

While Queue_Next(q)
  Debug Queue_DequeueS(q)
Wend

; Debug Queue_DequeueS(q)

Queue_Destroy(q, 0)
What am I doing wrong? Queue_Next seems to work ok in your example.
But for me, it's not letting me grab the last element, unless I remove that
;comment..?

- np
User avatar
NoahPhense
Addict
Addict
Posts: 1999
Joined: Thu Oct 16, 2003 8:30 pm
Location: North Florida

Re: Queue 1.0 - library

Post by NoahPhense »

Queue_Sort() - for strings..

Had a little burst of energy this morning... thought I'd tool with your lib.

Code: Select all

q = Queue_Create()

Queue_EnqueueS(q, "apples")
Queue_EnqueueS(q, "oranges")
Queue_EnqueueS(q, "pears")
Queue_EnqueueS(q, "bananas")
Queue_EnqueueS(q, "grapes")
Queue_EnqueueS(q, "peaches")

Procedure Queue_Sort(queue)
  l = Queue_GetNumElems(queue)-1:Dim temp.s(l):Queue_Reset(queue)
  x = 0:While Queue_Next(queue):temp(x) = Queue_DequeueS(queue):x+1:Wend
  temp(x) = Queue_DequeueS(queue):SortArray(temp(), 0, 0, l)
  Queue_Clear(queue, 0):Queue_Reset(queue)
  For xx = 0 To l:Queue_EnqueueS(queue, temp(xx)):Next xx
  Dim temp.s(0);release the array
EndProcedure

Queue_Sort(q)

; list the queue
Queue_Reset(q)
While Queue_Next(q)
  Debug Queue_DequeueS(q)
Wend
Debug Queue_DequeueS(q) ; still have to shoot last one out like this

Queue_Destroy(q, 0)
End
- np
Froggerprogger
Enthusiast
Enthusiast
Posts: 423
Joined: Fri Apr 25, 2003 5:22 pm
Contact:

Post by Froggerprogger »

Unless you're already making one.. I can make a linked chm file for you.
Just send me ALL the functions in a text file..
A help. chm would be pretty cool. Inside the package there's the Queue.pb containing all functions. Behind each of them there's a comment describing in short words exactly what this function does and what values are expected for the parameters.


What am I doing wrong? Queue_Next seems to work ok in your example.
Don't use Queue_Next() with Queue_Dequeue(). Otherwise you are mixing the both concepts. I'll try to explain:
This queue includes two different concepts:
1. pure queue
2. list

One difference is, that a pure queue doesn't have any 'active element' like a list. In a list you can reset and go to next / previous element and read the value at the active element (+insert/delete).
In a pure queue you can only enqueue and dequeue (+insert/delete).
Dequeue means, that the head element will be deleted, totally independant of the active element.

So if you want do 'dequeue' all elements, you can do this e.g. by

Code: Select all

While Queue_GetNumElems(q)  ; check if there are still elements in queue
  Debug Queue_DequeueS(q)   ; dequeue the head
Wend
It's even faster than doing with Queue_Next(), because Queue_GetNumElems(q) just returns the value numElems of the Queue-Structure, so you might write alternatively:

Code: Select all

*p.queue = Queue_Create()
Queue_EnqueueS(*p, "TEST1")
Queue_EnqueueS(*p, "TEST2")
Queue_EnqueueS(*p, "TEST3")

While *p\numElems  ; check if there are still elements in queue
  Debug Queue_DequeueS(*p)   ; dequeue the head
Wend
another way (as fast as with Queue_GetNumElems()) is the flolowing, which should help understanding it:

Code: Select all

While Queue_SelectHead(q) ; returns pointer to headelem or 0, so check if there is still a head object
  Debug Queue_DequeueS(q) ; dequeue it
Wend 

This is how to handle the pure queue-part.
The Reset/Next/Prev/Select - functions let this queue behave like a list. You can navigate through the files and read or write their values.
So you can get all values by:

Code: Select all

Queue_Reset(q)
While Queue_Next(q)
  Debug Queue_GetValS(q)
Wend
without removing them. (In a pure queue you can only read the values by dequeue, so it will be removed)
Or e.g. by:

Code: Select all

Queue_Reset(q)
Repeat
  *r.Variant_PB = Queue_Next(q)
  If *r
    debug *r\s
  Endif
Until *r = 0
You can parse forward and immediately later backwards:

Code: Select all

Queue_Reset(q)
While Queue_Next(q)
  Debug Queue_GetValS(q)
Wend
While Queue_Prev(q)
  Debug Queue_GetValS(q)
Wend
Hope that made some things more clear.
Of course it would be nice, when Queue_Next() would work with Queue_Dequeue(), but unfortunately it doesn't.

((
Here the reason:
Queue_Reset() sets active element in front of head.
Queue_Next() sets now the head as the active element
Queue_Dequeue() removes the head and sets it the active element.
|| :
Queue_Next() sets now the head's fellow as active one
Queue_Dequeue() removes the head and sets it the active element.
: ||
After some loops Queue_Next() sets the active element to 0, though there is still one element in queue.

))



Queue_Sort() - for strings..
Yes, a good idea ! But it would only work if all elements have the same type. A user callback-sort would be quite good for this. Then you could sort structures, too, and you might compare bytes/words/longs/floats with each other.

Another thing that I noticed is, there's no
Queue_DequeueToString() at the moment, though there is a Queue_GetValToString(). This is a pretty useful command for mixed-type-queues.
I'll catch up this.

Further I could implement Queue_SetVal*() - functions. Atm you can only overwrite values using the Variant_PB-structure.

These are some things for the ToDo-list.
(But not today - I have to take a shower an' go to a bash now 8) )
Last edited by Froggerprogger on Sat Jul 10, 2004 7:10 pm, edited 1 time in total.
%1>>1+1*1/1-1!1|1&1<<$1=1
User avatar
NoahPhense
Addict
Addict
Posts: 1999
Joined: Thu Oct 16, 2003 8:30 pm
Location: North Florida

..

Post by NoahPhense »

Frogman wrote:Of course it would be nice, when Queue_Next() would work with Queue_Dequeue(), but unfortunately it doesn't.
Nah.. that's what linked lists are for.. ;) With your Que.. you've made
less typing for us.. ;) It's all good.

Enjoy the bash!

- np
Froggerprogger
Enthusiast
Enthusiast
Posts: 423
Joined: Fri Apr 25, 2003 5:22 pm
Contact:

Post by Froggerprogger »

While thniking about the Next/Dequeue - thing a bit longer I had the following idea:

Queue_Dequeue() needn't have to change the active element (which is part of the other concept) to the new head.
It was just an design-idea, so that you can use convenient Queue_GetVal*() immediately after Queue_Dequeue() on the new head.

As an alternative Queue_Dequeue() could reset the list always. Then

Code: Select all

While Queue_Next()
  Queue_Dequeue*()
Wend
would work. But it's not soo fine I think.

It would be better to leave the active element as it is, so long it makes sense, so here's the idea :

Queue_Dequeue() should leave the active element as it is. (e.g. if element ID 5 is active, after Dequeue() the active element will be still the same, (but on ID 4). This makes sense.
And when the active element was the head itself (which will be removed by Dequeue()) then the list will be resetted, so the active element is 0 !
-> The combination of Next / Dequeue would work then.


Thanks for this hint, I'll change it tomorrow !
... & so forget about some parts of my above blahblah :)
%1>>1+1*1/1-1!1|1&1<<$1=1
Froggerprogger
Enthusiast
Enthusiast
Posts: 423
Joined: Fri Apr 25, 2003 5:22 pm
Contact:

Post by Froggerprogger »

OK - tomorrow was yesterday, but today I made the update. :wink:
%1>>1+1*1/1-1!1|1&1<<$1=1
User avatar
NoahPhense
Addict
Addict
Posts: 1999
Joined: Thu Oct 16, 2003 8:30 pm
Location: North Florida

..

Post by NoahPhense »

Froggerprogger wrote:OK - tomorrow was yesterday, but today I made the update. :wink:
Getting now..

- np
Post Reply