Skip to content

Commit f66b00f

Browse files
committed
add readme.md
1 parent a497629 commit f66b00f

File tree

1 file changed

+200
-0
lines changed

1 file changed

+200
-0
lines changed

README.md

Lines changed: 200 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,200 @@
1+
# NumpyDeque Python Module
2+
3+
<p align="left">
4+
<img src="https://github.com/ScottBoyce-Python/NumpyDeque/actions/workflows/python-pytest.yml/badge.svg" alt="Build Status" height="20">
5+
</p>
6+
7+
8+
9+
NumpyDeque is a `numpy.ndarray` based double-ended queue (`deque`) with a maximum size. The deque has an initial size of zero and grows as values are added. When the deque has `maxsize` values and another value is added (`put` or `putleft`), the value on the opposite end is dropped. The underlying buffer allows fast addition and removal of values from the deque on both ends.
10+
11+
This double-ended queue is efficiently done by using a padded-buffer array. When an operation results no more padding the buffer performs an internal shift to restore the padding buffer space. The padding can give priority to specific operations, where more padding is given for faster `put` operations (adding to the end), or for `putleft` operations (adding to start).
12+
13+
## Features
14+
15+
- The deque has most of the `collections.deque` methods.
16+
- The deque can use any `nump.ndarray` methods..
17+
- Supports fast append and pop operations on both ends of the deque.
18+
19+
## Installation
20+
Ensure that `numpy` is installed in your environment. If not, you can install it using:
21+
(note, this module was only tested against `numpy>2.0`)
22+
23+
```bash
24+
pip install numpy
25+
```
26+
27+
To install the module
28+
29+
```bash
30+
pip install --upgrade git+https://github.com/ScottBoyce-Python/NumpyDeque.git
31+
```
32+
33+
or you can clone the respository with
34+
```bash
35+
git clone https://github.com/ScottBoyce-Python/NumpyDeque.git
36+
```
37+
and then move the file `NumpyDeque/NumpyDeque.py` to wherever you want to use it.
38+
39+
40+
## Usage
41+
42+
Below are examples showcasing how to create and interact with a `NumpyDeque`.
43+
44+
### Creating a Deque
45+
46+
```python
47+
import numpy as np
48+
from NumpyDeque import NumpyDeque
49+
50+
# Create an empty deque that stores up to 10 float64 numbers
51+
d = NumpyDeque(maxsize=10, dtype=np.float64)
52+
53+
# Create a deque with 5 int64 zeros (the deque is initialized to maxsize with 0).
54+
d = NumpyDeque(maxsize=5, fill=0, dtype=np.int64)
55+
56+
# Create a deque from an array. Its maxsize is automatically set to 5.
57+
d = NumpyDeque.array([1, 2, 3, 4, 5])
58+
59+
# Create a deque from an array. Its maxsize is set to 5.
60+
d = NumpyDeque.array([1, 2, 3], 5)
61+
62+
```
63+
64+
### Adding to Right of The Deque
65+
66+
```python
67+
d = NumpyDeque(maxsize=5, dtype=np.int64)
68+
69+
# Put a value to the right on the deque
70+
d.put(5)
71+
d.put(7)
72+
d.put(9)
73+
print(d) # Output: NumpyDeque([5, 7, 9])
74+
d.put(11)
75+
d.put(13)
76+
print(d) # Output: NumpyDeque([5, 7, 9, 11, 13])
77+
d.put(15) # 5 is dropped
78+
print(d) # Output: NumpyDeque([7, 9, 11, 13, 15])
79+
80+
d.putter([1, 2, 3])
81+
print(d) # Output: NumpyDeque([13, 15, 1, 2, 3])
82+
83+
d.putter([-1, -2, -3, -4, -5, -6, -7])
84+
print(d) # Output: NumpyDeque([-3, -4, -5, -6, -7])
85+
86+
d.putter([1, 2, 3, 4, 5])
87+
print(d) # Output: NumpyDeque([1, 2, 3, 4, 5])
88+
```
89+
90+
### Adding to Left of The Deque
91+
92+
```python
93+
d = NumpyDeque(maxsize=5, dtype=np.int64)
94+
95+
# Put a value to the right on the deque
96+
d.putleft(5)
97+
d.putleft(7)
98+
d.putleft(9)
99+
print(d) # Output: NumpyDeque([9, 7, 5])
100+
d.putleft(11)
101+
d.putleft(13)
102+
print(d) # Output: NumpyDeque([13, 11, 9, 7, 5])
103+
d.putleft(15) # 5 is dropped
104+
print(d) # Output: NumpyDeque([15, 13, 11, 9, 7])
105+
106+
d.putterleft([1, 2, 3])
107+
print(d) # Output: NumpyDeque([3, 2, 1, 15, 13])
108+
109+
d.putterleft([-1, -2, -3, -4, -5, -6, -7])
110+
print(d) # Output: NumpyDeque([-7, -6, -5, -4, -3])
111+
112+
d.putter([1, 2, 3, 4, 5])
113+
print(d) # Output: NumpyDeque([5, 4, 3, 2, 1])
114+
```
115+
116+
### Removing Elements
117+
118+
```python
119+
d = NumpyDeque.array([1, 2, 3, 4, 5, 6, 7, 8, 9, 10])
120+
121+
# Remove and return the last element
122+
print(d) # Output: NumpyDeque([1, 2, 3, 4, 5, 6, 7, 8, 9, 10])
123+
rightmost_value = d.pop()
124+
print(d) # Output: NumpyDeque([1, 2, 3, 4, 5, 6, 7, 8, 9])
125+
print(rightmost_value)# Output: 10
126+
127+
# Remove and return the first element
128+
leftmost_value = d.popleft()
129+
print(d) # Output: NumpyDeque([2, 3, 4, 5, 6, 7, 8, 9])
130+
print(leftmost_value) # Output: 1
131+
132+
# Remove and return the third element
133+
third_value = d.drop(2)
134+
print(d) # Output: NumpyDeque([2, 3, 5, 6, 7, 8, 9])
135+
print(third_value)# Output: 4
136+
137+
# If the number 8 and 1 are found, remove the first appearance
138+
d.remove(8)
139+
print(d) # Output: NumpyDeque([2, 3, 5, 6, 7, 9])
140+
d.remove(1) # Nothing happens
141+
print(d) # Output: NumpyDeque([2, 3, 5, 6, 7, 9])
142+
```
143+
144+
### Slicing
145+
146+
```python
147+
d = NumpyDeque.array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9], maxsize=10)
148+
149+
# Slice behaves like NumPy arrays, but be careful with indexes
150+
print( d[1:4] ) # Output: [1, 2, 3]
151+
152+
d[1:3] = [-1, -2] # Output: [2, 3, 4]
153+
print(d) # Output: NumpyDeque([0, -1, -2, 3, 4, 5, 6, 7, 8, 9])
154+
155+
# Note that values move once maxsize is exceeded
156+
print( d[2] ) # Output: -2
157+
d.put(10)
158+
print(d) # Output: NumpyDeque([-1, -2, 3, 4, 5, 6, 7, 8, 9, 10])
159+
print( d[2] ) # Output: 3
160+
d.put(11)
161+
print(d) # Output: NumpyDeque([-2, 3, 4, 5, 6, 7, 8, 9, 10, 11])
162+
print( d[2] ) # Output: 4
163+
d.putleft(99)
164+
print(d) # Output: NumpyDeque([99, -2, 3, 4, 5, 6, 7, 8, 9, 10])
165+
print( d[2] ) # Output: 3
166+
167+
#Be careful about the size
168+
d = NumpyDeque(maxsize=5)
169+
d.put(5)
170+
d.put(4)
171+
d.put(3)
172+
print(d) # Output: NumpyDeque([5, 4, 3])
173+
print( d[3] ) # Raises index error!!!
174+
```
175+
176+
## Testing
177+
178+
This project uses `pytest` and `pytest-xdist` for testing. Tests are located in the `tests` folder. To run tests, install the required packages and execute the following command:
179+
180+
```bash
181+
pip install pytest pytest-xdist
182+
183+
pytest # run all tests, note options are set in the pyproject.toml file
184+
```
185+
186+
&nbsp;
187+
188+
Note, that the [pyproject.toml](pyproject.toml) contains the flags used for pytest.
189+
190+
191+
192+
## License
193+
194+
This project is licensed under the MIT License. See the [LICENSE](LICENSE) file for details.
195+
196+
## Contributing
197+
Contributions are welcome! Please open an issue or submit a pull request for any improvements or bug fixes.
198+
199+
## Author
200+
Scott E. Boyce

0 commit comments

Comments
 (0)