FIFO, LIFO, OQ
Project description
Introduction
This repository contains the code for the queues package. The package
provides three data structures, all with a similar API:
- Stacks (LIFO Queues)
- FIFO Queues
- Ordered Queues
All three data structures are bounded in order to provide more predictable memory usage.
Usage
import queues
Stacks
Initialization
Stacks must be initialized with the initial maximum stack size:
>>> s = Stack(100)
Providing a maximum size that is less than or equal to 0 results in an error.
You can determine the number of items currently on the stack by calling the
size method:
>>> s.size()
Insertion
Inserting a value into a stack is accomplished by using the push method
provided by the stack. The result value for push should be a boolean that
signals whether the insertion was successful.
>>> s.push(42) # pushes the integer (42) onto the stack
True
The size of a stack should increase by 1 after every push:
>>> s.size()
10
>>> s.push(42)
True
>>> s.size()
11
Removal
Getting something from the stack is done using the pop method, which takes no
arguments. The result of pop should be the most recently pushed value:
>>> s.push(42)
True
>>> s.pop()
42
Calling pop on a non-empty stack should reduce the size by 1.
Calling pop on an empty stack should result in None.
Multiple removal
You can remove multiple items from the stack at once using the pops method,
which takes an integer as its argument. The result of pops is a list of all
the popped elements.
>>> s.push(42)
True
>>> s.push(84)
True
>>> s.pops(2)
[84, 42]
It is okay to call pops with a number larger than the size of the stack, in
which case the resulting list will include all the elements of the stack and no
more.
Calling pops(n) on a non-empty stack should reduce the size by n or reduce
the size to 0.
Calling pops on an empty stack should result in the empty list.
FIFO Queues
Initialization
Queues must be initialized with the initial maximum size:
>>> q = FIFO(100)
Providing a maximum size that is less than or equal to 0 results in an error.
You can determine the number of items currently in the queue by calling the
size method:
>>> q.size()
Insertion
Inserting a value into the queue is accomplished by using the insert method
provided by the queue. The result value for insert should be a boolean that
signals whether the insertion was successful.
>>> q.insert(42) # adds the integer (42) to the end of the queue
True
The size of a queue should increase by 1 after every insert:
>>> q.size()
10
>>> q.insert(42)
True
>>> q.size()
11
Removal
Getting something from the queue is done using the get method, which takes no
arguments. The result of get should be the oldest value in the queue:
>>> q.insert(42)
True
>>> q.insert(84)
True
>>> q.get()
42
Calling get on a non-empty queue should reduce the size by 1.
Calling get on an empty queue should result in None.
Multiple removal
You can remove multiple items from the queue at once using the gets method,
which takes an integer as its argument. The result of gets is a list of all
the retrieved elements.
>>> q.insert(42)
True
>>> q.insert(84)
True
>>> q.gets(100000000)
[84, 42]
It is okay to call gets with a number larger than the size of the queue, in
which case the resulting list will include all the elements of the queue and no
more.
Calling gets(n) on a non-empty queue should reduce the size by n or reduce
the size to 0.
Calling gets on an empty queue should result in the empty list.
Ordered Queues
Ordered Queues store all of their elements in ascending order.
The data structure provides the same methods as the FIFO Queue, but the behavior is slightly different.
Initialization
OQs must be initialized with the initial maximum size:
>>> q = OQ(100)
Providing a maximum size that is less than or equal to 0 results in an error.
You can determine the number of items currently in the queue by calling the
size method:
>>> q.size()
Insertion
Inserting a value into the queue is accomplished by using the insert method
provided by the queue. The result value for insert should be a boolean that
signals whether the insertion was successful.
>>> q.insert(42) # adds the integer (42) to the queue
True
The size of a queue should increase by 1 after every insert:
>>> q.size()
10
>>> q.insert(42)
True
>>> q.size()
11
Removal
Getting something from the queue is done using the get method, which takes no
arguments. The result of get should be the smallest integer in the queue.
>>> q.insert(42)
True
>>> q.insert(21)
True
>>> q.insert(84)
True
>>> q.get()
21
Calling get on a non-empty queue should reduce the size by 1.
Calling get on an empty queue should result in None.
Multiple removal
You can remove multiple items from the queue at once using the gets method,
which takes an integer as its argument. The result of gets is a list of all
the retrieved elements.
>>> q.insert(42)
True
>>> q.insert(21)
True
>>> q.insert(84)
True
>>> q.gets(2)
[21, 42]
It is okay to call gets with a number larger than the size of the queue, in
which case the resulting list will include all the elements of the queue and no
more.
Calling gets(n) on a non-empty queue should reduce the size by n or reduce
the size to 0.
Calling gets on an empty queue should result in the empty list.
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file P_T_P1_package-0.0.1.tar.gz.
File metadata
- Download URL: P_T_P1_package-0.0.1.tar.gz
- Upload date:
- Size: 8.6 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/5.0.0 CPython/3.12.0
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
8d830a5ee7ac17050779acb99ef51916f511fff3c08a48a57584375cc47341c2
|
|
| MD5 |
e2ac7e5dcfaad5c8c7a26dbe054e7297
|
|
| BLAKE2b-256 |
32e79f8068aef88736db7e164d8e48fd3a363549c606359bdc38f753cd3d0a49
|
File details
Details for the file P_T_P1_package-0.0.1-py3-none-any.whl.
File metadata
- Download URL: P_T_P1_package-0.0.1-py3-none-any.whl
- Upload date:
- Size: 9.2 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/5.0.0 CPython/3.12.0
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
0e75bfa852a54da73892f8330a220eff86d7ae05098560b7fedffc230d0877d4
|
|
| MD5 |
d3d6bfb3d5188c03c2f0d815fa8f5aed
|
|
| BLAKE2b-256 |
2a50ebe1b451c0cee7b766aa0dfc55ea34c742ecc3b207bb64d1c69cfc5e32cb
|