B4R Library [module] Queue list

Queue is a FIFO collection. You can add elements to the end of the queue and get the first item.

Access the first item with Queue.FirstItem.

Example:
B4X:
Private Sub AppStart
   Serial1.Initialize(115200)
   Log("AppStart")
   Queue.AddBytes("Item 1")
   Queue.AddBytes("Item 222")
   Queue.AddBytes("Item 3333")
   Queue.AddBytes("Item 44")
   Queue.AddBytes("Item 555555")
   Queue.RemoveFirst
   Queue.AddBytes("Item 612312")
   Queue.AddBytes("Item 77777777777")
   Queue.AddBytes("Item 88")
   Queue.AddBytes("Item 99")
   Dim str() As Byte = "Item #"
   For i = 1 To 10
     Queue.AddBytes(str)
     Queue.AddBytesToLastItem(NumberFormat(i, 0, 0))
   Next
   Do While Queue.Size > 0
     Log(Queue.FirstItem)
     Queue.RemoveFirst
   Loop
   Log("Size: ", Queue.Size)
End Sub

V1.10 released with a new AddBytesToLastItem method. This is a very useful method which allows building an item with multiple calls instead of using JoinBytes or JoinStrings which require more memory.
 

Attachments

  • Queue.zip
    1.7 KB · Views: 757
Last edited:

max123

Well-Known Member
Licensed User
Longtime User
Many thanks for this code module!

I've tried this code and works well:
B4X:
Sub Process_Globals
    'These global variables will be declared once when the application starts.
    'Public variables can be accessed from all modules.
    Public Serial1 As Serial
    Private bc As ByteConverter
End Sub

Private Sub AppStart
    Serial1.Initialize(115200)
    Log(" ") : Log("AppStart")
    Queue.AddBytes("Item 1")
    Queue.AddBytes("Item 222")
    Queue.AddBytes("Item 3333")
    Queue.AddBytes("Item 44")
    Queue.AddBytes("Item 555555")
    Queue.RemoveFirst
    Queue.AddBytes("Item 612312")
    Queue.AddBytes("Item 77777777777")
    Queue.AddBytes("Item 88")
    Queue.AddBytes("Item 99")
    Queue.AddBytes("Item 612312")
    Queue.AddBytes("Item 77777777777")
    Queue.AddBytes("Item 88")
    Queue.AddBytes("Item 99")

    For i = 1 To 30
        Queue.AddBytes(bc.StringToBytes(JoinStrings(Array As String("This is a join number ", i))))
    Next
  
    Log("Size before dequeue: ", Queue.Size, " items")
  
    Do While Queue.Size > 0
        Log(Queue.FirstItem)
        Queue.RemoveFirst
    Loop
  
    Log("Size after dequeue: ", Queue.Size, " items")
End Sub
also I incremented the Queue module buffer size:
B4X:
Private buffer(1000) As Byte
also I incremented the StackBufferSize (I see that not only I need to increase Queue module buffer size, but both) :
B4X:
#StackBufferSize: 2000
and added this line to the bottom of AddBytes function, just to track a stack buffer size after I add more bytes:
B4X:
Log("StackBufferUsage: ", StackBufferUsage)
Final results:
AppStart
StackBufferUsage: 8
StackBufferUsage: 16
StackBufferUsage: 24
StackBufferUsage: 32
StackBufferUsage: 40
StackBufferUsage: 48
StackBufferUsage: 56
StackBufferUsage: 64
StackBufferUsage: 72
StackBufferUsage: 80
StackBufferUsage: 88
StackBufferUsage: 96
StackBufferUsage: 104
StackBufferUsage: 156
StackBufferUsage: 208
StackBufferUsage: 260
StackBufferUsage: 312
StackBufferUsage: 364
StackBufferUsage: 416
StackBufferUsage: 468
StackBufferUsage: 520
StackBufferUsage: 572
StackBufferUsage: 628
StackBufferUsage: 684
StackBufferUsage: 740
StackBufferUsage: 796
StackBufferUsage: 852
StackBufferUsage: 908
StackBufferUsage: 964
StackBufferUsage: 1020
StackBufferUsage: 1076
StackBufferUsage: 1132
StackBufferUsage: 1188
StackBufferUsage: 1244
StackBufferUsage: 1300
StackBufferUsage: 1356
StackBufferUsage: 1412
StackBufferUsage: 1468
StackBufferUsage: 1524
StackBufferUsage: 1580
StackBufferUsage: 1636
StackBufferUsage: 1692
StackBufferUsage: 1748
Size before dequeue: 42 items
Item 222
Item 3333
Item 44
Item 555555
Item 612312
Item 77777777777
Item 88
Item 99
Item 612312
Item 77777777777
Item 88
Item 99
This is a join number 1
This is a join number 2
This is a join number 3
This is a join number 4
This is a join number 5
This is a join number 6
This is a join number 7
This is a join number 8
This is a join number 9
This is a join number 10
This is a join number 11
This is a join number 12
This is a join number 13
This is a join number 14
This is a join number 15
This is a join number 16
This is a join number 17
This is a join number 18
This is a join number 19
This is a join number 20
This is a join number 21
This is a join number 22
This is a join number 23
This is a join number 24
This is a join number 25
This is a join number 26
This is a join number 27
This is a join number 28
This is a join number 29
This is a join number 30
Size after dequeue: 0 items
 
Last edited:

max123

Well-Known Member
Licensed User
Longtime User
I have no questions on this, this is not a question, just I tried it and like to put my contribute on this forum. ;)

Maybe you just can help me to know how to change this line to use JoinBytes and not JoinStrings, I've tried but I can't see on the log the number, just I see ASCII rappresentation character, tried with ByteConverter but without success:
B4X:
Queue.AddBytes(bc.StringToBytes(JoinStrings(Array As String("This is a join number ", i))))
And you helped me another time with this:
You need to increase the #StackBufferSize because of the JoinStrings call
JoinStrings should be avoided whenever possible.

Many thanks.
 
Last edited:

max123

Well-Known Member
Licensed User
Longtime User
Do you intend Stack module that works as LIFO?
 

janderkan

Well-Known Member
Licensed User
Longtime User
I found something strange adding 12 bytes.
There is no problem if the bytes added contains a byte with value zero.
But if the first byte has the value zero, then .FirstItem returns an array with length 1 and first byte with value 0.
And I can't C.
 

janderkan

Well-Known Member
Licensed User
Longtime User
As always it was an error 40.
Next time I make the small test program before I write to you:)
 
Last edited:
Top