charlang/example/byteQueue.char
Copy
// use newObj function to create a byteQueue object, with capacity of 6 b1 := newObj("byteQueue", 6) pl("[%v] %#v %v", -1, b1, b1) // no loop since size is 0 now for j := 0; j < b1.Size(); j++ { item1 := b1.Get(j) prf(" %v", item1) } // get item while size = 0, will get error return value pl(" F: %v L: %v", b1.Get(), b1.Get(-1)) // zero-length list pl("List: %#v %v", b1.GetList(), b1.GetList()) // all these removing actions will return error pln("remove 0:", b1.Remove(0)) pln("remove last:", b1.Remove(b1.Size())) pln("remove 3:", b1.Remove(3)) // push 20 bytes in the queue for i := 0; i < 20; i++ { b1.Push(byte(i)) pl("[%v] %v", i, b1) for j := 0; j < b1.Size(); j++ { // "Get" an item in the queue will not remove it from the queue item1 := b1.Get(j) prf(" %v", item1) } pln() // show the first(get with no parameters) and last(get with -1 as index) item of the queue pl(" F: %v L: %v", b1.Get(), b1.Get(-1)) pl("List: %#v %v", b1.GetList(), b1.GetList()) } // insert a byte at the beginning of the queue b1.Insert(0, byte(100)) pl("-1 [%v] %v %v", 999, b1, b1.GetInfo()) for j := 0; j < b1.Size(); j++ { item1 := b1.Get(j) prf(" %v", item1) } pln() pl(" F: %v L: %v", b1.Get(), b1.Get(-1)) // insert a byte before the item with index of 3(which starts with 0) of the queue b1.Insert(3, byte(103)) pl("-2 [%v] %v %v", 999, b1, b1.GetInfo()) for j := 0; j < b1.Size(); j++ { item1 := b1.Get(j) prf(" %v", item1) } pln() pl(" F: %v L: %v", b1.Get(), b1.Get(-1)) // insert/append a byte at the end of the queue b1.Insert(b1.Size()-1, byte(109)) pl("-3 [%v] %v %v", 999, b1, b1.GetInfo()) for j := 0; j < b1.Size(); j++ { item1 := b1.Get(j) prf(" %v", item1) } pln() pl(" F: %v L: %v", b1.Get(), b1.Get(-1)) pl("List: %#v %v", b1.GetList(), b1.GetList()) // remove the first item(with index 0) b1.Remove(0) pl("remove0 [%v] %v %v", 999, b1, b1.GetInfo()) for j := 0; j < b1.Size(); j++ { item1 := b1.Get(j) prf(" %v", item1) } pln() pl(" F: %v L: %v", b1.Get(), b1.Get(-1)) pl("List: %#v %v", b1.GetList(), b1.GetList()) // remove last item/byte of the queue b1.Remove(b1.Size() - 1) pl("removeLast [%v] %v %v", 999, b1, b1.GetInfo()) for j := 0; j < b1.Size(); j++ { item1 := b1.Get(j) prf(" %v", item1) } pln() pl(" F: %v L: %v", b1.Get(), b1.Get(-1)) pl("List: %#v %v", b1.GetList(), b1.GetList()) // remove the item with index 2 b1.Remove(2) pl("remove2 [%v] %v %v", 999, b1, b1.GetInfo()) for j := 0; j < b1.Size(); j++ { item1 := b1.Get(j) prf(" %v", item1) } pln() pl(" F: %v L: %v", b1.Get(), b1.Get(-1)) pl("List: %#v %v", b1.GetList(), b1.GetList()) // pick items cnt1 := 0 for { // Pick action will remove item from the queue a1 := b1.Pick() // break while no items if isErr(a1) { break } cnt1++ pl("got %v", a1) pl("[%v] %v S: %v", cnt1, b1, b1.Size()) }