pythonic-fp.queues 3.0.0__py3-none-any.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Potentially problematic release.
This version of pythonic-fp.queues might be problematic. Click here for more details.
- pythonic_fp/queues/__init__.py +43 -0
- pythonic_fp/queues/de.py +190 -0
- pythonic_fp/queues/fifo.py +150 -0
- pythonic_fp/queues/lifo.py +152 -0
- pythonic_fp/queues/py.typed +0 -0
- pythonic_fp_queues-3.0.0.dist-info/METADATA +49 -0
- pythonic_fp_queues-3.0.0.dist-info/RECORD +9 -0
- pythonic_fp_queues-3.0.0.dist-info/WHEEL +4 -0
- pythonic_fp_queues-3.0.0.dist-info/licenses/LICENSE +190 -0
|
@@ -0,0 +1,43 @@
|
|
|
1
|
+
# Copyright 2023-2024 Geoffrey R. Scheller
|
|
2
|
+
#
|
|
3
|
+
# Licensed under the Apache License, Version 2.0 (the "License");
|
|
4
|
+
# you may not use this file except in compliance with the License.
|
|
5
|
+
# You may obtain a copy of the License at
|
|
6
|
+
#
|
|
7
|
+
# http://www.apache.org/licenses/LICENSE-2.0
|
|
8
|
+
#
|
|
9
|
+
# Unless required by applicable law or agreed to in writing, software
|
|
10
|
+
# distributed under the License is distributed on an "AS IS" BASIS,
|
|
11
|
+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
12
|
+
# See the License for the specific language governing permissions and
|
|
13
|
+
# limitations under the License.
|
|
14
|
+
|
|
15
|
+
"""Developer Tools - Queue based data structures
|
|
16
|
+
|
|
17
|
+
These are modeled after Python builtins. The initializers take at most one
|
|
18
|
+
iterable while factory functions stand in for Python syntax.
|
|
19
|
+
|
|
20
|
+
Queue types:
|
|
21
|
+
|
|
22
|
+
- *class* dtools.queues.fifo.FIFOQueue: First-In-First-Out Queue
|
|
23
|
+
- *class* dtools.queues.lifo.LIFOQueue: Last-In-First-Out Queue
|
|
24
|
+
- *class* dtools.queues.de.DEQueue: Double-Ended Queue
|
|
25
|
+
|
|
26
|
+
Create queues from specific values:
|
|
27
|
+
|
|
28
|
+
- *function* dtools.queues.fifo.fifo_queue: Create a FIFOQueue
|
|
29
|
+
|
|
30
|
+
- from function's arguments pushed on in natural FIFO order
|
|
31
|
+
|
|
32
|
+
- *function* dtools.queues.lifo.lifo_queue: Create a LIFOQueue
|
|
33
|
+
|
|
34
|
+
- from function's arguments pushed on in natural LIFO order
|
|
35
|
+
|
|
36
|
+
- *function* dtools.queues.de.de_queue: Create a DEQueue
|
|
37
|
+
|
|
38
|
+
- from function's arguments pushed on from the right
|
|
39
|
+
"""
|
|
40
|
+
|
|
41
|
+
__author__ = 'Geoffrey R. Scheller'
|
|
42
|
+
__copyright__ = 'Copyright (c) 2023-2025 Geoffrey R. Scheller'
|
|
43
|
+
__license__ = 'Apache License 2.0'
|
pythonic_fp/queues/de.py
ADDED
|
@@ -0,0 +1,190 @@
|
|
|
1
|
+
# Copyright 2023-2024 Geoffrey R. Scheller
|
|
2
|
+
#
|
|
3
|
+
# Licensed under the Apache License, Version 2.0 (the "License");
|
|
4
|
+
# you may not use this file except in compliance with the License.
|
|
5
|
+
# You may obtain a copy of the License at
|
|
6
|
+
#
|
|
7
|
+
# http://www.apache.org/licenses/LICENSE-2.0
|
|
8
|
+
#
|
|
9
|
+
# Unless required by applicable law or agreed to in writing, software
|
|
10
|
+
# distributed under the License is distributed on an "AS IS" BASIS,
|
|
11
|
+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
12
|
+
# See the License for the specific language governing permissions and
|
|
13
|
+
# limitations under the License.
|
|
14
|
+
|
|
15
|
+
"""
|
|
16
|
+
### Double-Ended (DE) Queue
|
|
17
|
+
|
|
18
|
+
- stateful DE queue data structures with amortized O(1) pushes and pops each end
|
|
19
|
+
- obtaining length (number of elements) of a queue is an O(1) operation
|
|
20
|
+
- implemented in a "has-a" relationship with a Python list based circular array
|
|
21
|
+
- will resize itself larger as needed
|
|
22
|
+
|
|
23
|
+
"""
|
|
24
|
+
|
|
25
|
+
from __future__ import annotations
|
|
26
|
+
|
|
27
|
+
from collections.abc import Callable, Iterable, Iterator, Sequence
|
|
28
|
+
from typing import Never, overload, TypeVar
|
|
29
|
+
|
|
30
|
+
from pythonic_fp.circulararray import CA
|
|
31
|
+
from pythonic_fp.containers.maybe import MayBe as MB
|
|
32
|
+
|
|
33
|
+
|
|
34
|
+
__all__ = ['DEQueue', 'de_queue']
|
|
35
|
+
|
|
36
|
+
D = TypeVar('D')
|
|
37
|
+
|
|
38
|
+
|
|
39
|
+
class DEQueue[D]:
|
|
40
|
+
"""Double Ended Queue
|
|
41
|
+
|
|
42
|
+
- stateful Double-Ended (DEQueue) data structure
|
|
43
|
+
- order of initial data retained, as if pushed on from the right
|
|
44
|
+
"""
|
|
45
|
+
L = TypeVar('L')
|
|
46
|
+
R = TypeVar('R')
|
|
47
|
+
|
|
48
|
+
__slots__ = ('_ca',)
|
|
49
|
+
|
|
50
|
+
U = TypeVar('U')
|
|
51
|
+
|
|
52
|
+
def __init__(self, *dss: Iterable[D]) -> None:
|
|
53
|
+
if (size := len(dss)) < 2:
|
|
54
|
+
self._ca = CA(dss[0]) if size == 1 else CA()
|
|
55
|
+
else:
|
|
56
|
+
msg = f'DEQueue expects at most 1 iterable argument, got {size}'
|
|
57
|
+
raise TypeError(msg)
|
|
58
|
+
|
|
59
|
+
def __bool__(self) -> bool:
|
|
60
|
+
return len(self._ca) > 0
|
|
61
|
+
|
|
62
|
+
def __len__(self) -> int:
|
|
63
|
+
return len(self._ca)
|
|
64
|
+
|
|
65
|
+
def __eq__(self, other: object, /) -> bool:
|
|
66
|
+
if not isinstance(other, DEQueue):
|
|
67
|
+
return False
|
|
68
|
+
return self._ca == other._ca
|
|
69
|
+
|
|
70
|
+
@overload
|
|
71
|
+
def __getitem__(self, idx: int, /) -> D: ...
|
|
72
|
+
@overload
|
|
73
|
+
def __getitem__(self, idx: slice, /) -> Sequence[D]: ...
|
|
74
|
+
|
|
75
|
+
def __getitem__(self, idx: int | slice, /) -> D | Sequence[D] | Never:
|
|
76
|
+
if isinstance(idx, slice):
|
|
77
|
+
msg = 'dtool.restictive queues are not slicable by design'
|
|
78
|
+
raise NotImplementedError(msg)
|
|
79
|
+
return self._ca[idx]
|
|
80
|
+
|
|
81
|
+
def __iter__(self) -> Iterator[D]:
|
|
82
|
+
return iter(list(self._ca))
|
|
83
|
+
|
|
84
|
+
def __reversed__(self) -> Iterator[D]:
|
|
85
|
+
return reversed(list(self._ca))
|
|
86
|
+
|
|
87
|
+
def __repr__(self) -> str:
|
|
88
|
+
if len(self) == 0:
|
|
89
|
+
return 'DQ()'
|
|
90
|
+
return 'DQ(' + ', '.join(map(repr, self._ca)) + ')'
|
|
91
|
+
|
|
92
|
+
def __str__(self) -> str:
|
|
93
|
+
return '>< ' + ' | '.join(map(str, self)) + ' ><'
|
|
94
|
+
|
|
95
|
+
def copy(self) -> DEQueue[D]:
|
|
96
|
+
"""Return a shallow copy of the ``DEQueue``."""
|
|
97
|
+
return DEQueue(self._ca)
|
|
98
|
+
|
|
99
|
+
def pushl(self, *ds: D) -> None:
|
|
100
|
+
"""Push data onto left side (front) of ``DEQueue``."""
|
|
101
|
+
self._ca.pushl(*ds)
|
|
102
|
+
|
|
103
|
+
def pushr(self, *ds: D) -> None:
|
|
104
|
+
"""Push data onto right side (rear) of ``DEQueue``.
|
|
105
|
+
Like a Python List, does not return a value.
|
|
106
|
+
"""
|
|
107
|
+
self._ca.pushr(*ds)
|
|
108
|
+
|
|
109
|
+
def popl(self) -> MB[D]:
|
|
110
|
+
"""Pop Data from left side (front) of ``DEQueue``.
|
|
111
|
+
|
|
112
|
+
- pop left most item off of queue, return item in a maybe monad
|
|
113
|
+
- returns an empty ``MB()`` if queue is empty
|
|
114
|
+
"""
|
|
115
|
+
if self._ca:
|
|
116
|
+
return MB(self._ca.popl())
|
|
117
|
+
return MB()
|
|
118
|
+
|
|
119
|
+
def popr(self) -> MB[D]:
|
|
120
|
+
"""Pop Data from right side (rear) of ``DEQueue``.
|
|
121
|
+
|
|
122
|
+
- pop right most item off of queue, return item in a maybe monad
|
|
123
|
+
- returns an empty ``MB()`` if queue is empty
|
|
124
|
+
"""
|
|
125
|
+
if self._ca:
|
|
126
|
+
return MB(self._ca.popr())
|
|
127
|
+
return MB()
|
|
128
|
+
|
|
129
|
+
def peakl(self) -> MB[D]:
|
|
130
|
+
"""Peak left side of ``DEQueue``.
|
|
131
|
+
|
|
132
|
+
- return left most value in a maybe monad
|
|
133
|
+
- does not consume the item
|
|
134
|
+
- returns an empty ``MB()`` if queue is empty
|
|
135
|
+
"""
|
|
136
|
+
if self._ca:
|
|
137
|
+
return MB(self._ca[0])
|
|
138
|
+
return MB()
|
|
139
|
+
|
|
140
|
+
def peakr(self) -> MB[D]:
|
|
141
|
+
"""Peak right side of ``DEQueue``.
|
|
142
|
+
|
|
143
|
+
- return right most value in a maybe monad
|
|
144
|
+
- does not consume the item
|
|
145
|
+
- returns an empty ``MB()`` if queue is empty
|
|
146
|
+
"""
|
|
147
|
+
if self._ca:
|
|
148
|
+
return MB(self._ca[-1])
|
|
149
|
+
return MB()
|
|
150
|
+
|
|
151
|
+
def foldl[L](self, f: Callable[[L, D], L], initial: L | None = None, /) -> MB[L]:
|
|
152
|
+
"""Reduce left to right with ``f`` using an optional initial value.
|
|
153
|
+
|
|
154
|
+
- note that when an initial value is not given then ``~L = ~D``
|
|
155
|
+
- if iterable empty & no initial value given, return ``MB()``
|
|
156
|
+
- traditional FP type order given for function ``f``
|
|
157
|
+
"""
|
|
158
|
+
if initial is None:
|
|
159
|
+
if not self._ca:
|
|
160
|
+
return MB()
|
|
161
|
+
return MB(self._ca.foldl(f, initial))
|
|
162
|
+
|
|
163
|
+
def foldr[R](self, f: Callable[[D, R], R], initial: R | None = None, /) -> MB[R]:
|
|
164
|
+
"""Reduce right to left with ``f`` using an optional initial value.
|
|
165
|
+
|
|
166
|
+
- note that when an initial value is not given then ``~R = ~D``
|
|
167
|
+
- if iterable empty & no initial value given, return ``MB()``
|
|
168
|
+
- traditional FP type order given for function ``f``
|
|
169
|
+
"""
|
|
170
|
+
if initial is None:
|
|
171
|
+
if not self._ca:
|
|
172
|
+
return MB()
|
|
173
|
+
return MB(self._ca.foldr(f, initial))
|
|
174
|
+
|
|
175
|
+
def map[U](self, f: Callable[[D], U], /) -> DEQueue[U]:
|
|
176
|
+
"""Map a function over ``DEQueue``.
|
|
177
|
+
|
|
178
|
+
- map the function ``f`` over the ``DEQueue``
|
|
179
|
+
|
|
180
|
+
- left to right
|
|
181
|
+
- retain original order
|
|
182
|
+
|
|
183
|
+
- returns a new instance
|
|
184
|
+
"""
|
|
185
|
+
return DEQueue(map(f, self._ca))
|
|
186
|
+
|
|
187
|
+
|
|
188
|
+
def de_queue[D](*ds: D) -> DEQueue[D]:
|
|
189
|
+
"""Create a ``DEQueue`` from the function arguments."""
|
|
190
|
+
return DEQueue(ds)
|
|
@@ -0,0 +1,150 @@
|
|
|
1
|
+
# Copyright 2023-2024 Geoffrey R. Scheller
|
|
2
|
+
#
|
|
3
|
+
# Licensed under the Apache License, Version 2.0 (the "License");
|
|
4
|
+
# you may not use this file except in compliance with the License.
|
|
5
|
+
# You may obtain a copy of the License at
|
|
6
|
+
#
|
|
7
|
+
# http://www.apache.org/licenses/LICENSE-2.0
|
|
8
|
+
#
|
|
9
|
+
# Unless required by applicable law or agreed to in writing, software
|
|
10
|
+
# distributed under the License is distributed on an "AS IS" BASIS,
|
|
11
|
+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
12
|
+
# See the License for the specific language governing permissions and
|
|
13
|
+
# limitations under the License.
|
|
14
|
+
|
|
15
|
+
"""First-In-Last-Out (FIFO) Queue"""
|
|
16
|
+
|
|
17
|
+
from __future__ import annotations
|
|
18
|
+
|
|
19
|
+
from collections.abc import Callable, Iterable, Iterator, Sequence
|
|
20
|
+
from typing import Never, overload, TypeVar
|
|
21
|
+
|
|
22
|
+
from pythonic_fp.circulararray import CA
|
|
23
|
+
from pythonic_fp.containers.maybe import MayBe as MB
|
|
24
|
+
|
|
25
|
+
|
|
26
|
+
__all__ = ['FIFOQueue', 'fifo_queue']
|
|
27
|
+
|
|
28
|
+
D = TypeVar('D')
|
|
29
|
+
|
|
30
|
+
|
|
31
|
+
class FIFOQueue[D]:
|
|
32
|
+
"""FIFO Queue
|
|
33
|
+
|
|
34
|
+
- stateful First-In-First-Out (FIFO) data structure
|
|
35
|
+
- initial data pushed on in natural FIFO order
|
|
36
|
+
"""
|
|
37
|
+
|
|
38
|
+
__slots__ = ('_ca',)
|
|
39
|
+
|
|
40
|
+
T = TypeVar('T')
|
|
41
|
+
U = TypeVar('U')
|
|
42
|
+
|
|
43
|
+
def __init__(self, *dss: Iterable[D]) -> None:
|
|
44
|
+
if (size := len(dss)) < 2:
|
|
45
|
+
self._ca = CA(dss[0]) if size == 1 else CA()
|
|
46
|
+
else:
|
|
47
|
+
msg = f'FIFOQueue expects at most 1 iterable argument, got {size}'
|
|
48
|
+
raise ValueError(msg)
|
|
49
|
+
|
|
50
|
+
def __bool__(self) -> bool:
|
|
51
|
+
return len(self._ca) > 0
|
|
52
|
+
|
|
53
|
+
def __len__(self) -> int:
|
|
54
|
+
return len(self._ca)
|
|
55
|
+
|
|
56
|
+
def __eq__(self, other: object, /) -> bool:
|
|
57
|
+
if not isinstance(other, FIFOQueue):
|
|
58
|
+
return False
|
|
59
|
+
return self._ca == other._ca
|
|
60
|
+
|
|
61
|
+
@overload
|
|
62
|
+
def __getitem__(self, idx: int, /) -> D: ...
|
|
63
|
+
@overload
|
|
64
|
+
def __getitem__(self, idx: slice, /) -> Sequence[D]: ...
|
|
65
|
+
|
|
66
|
+
def __getitem__(self, idx: int | slice, /) -> D | Sequence[D] | Never:
|
|
67
|
+
if isinstance(idx, slice):
|
|
68
|
+
msg = 'dtool.restictive queues are not slicable by design'
|
|
69
|
+
raise NotImplementedError(msg)
|
|
70
|
+
return self._ca[idx]
|
|
71
|
+
|
|
72
|
+
def __iter__(self) -> Iterator[D]:
|
|
73
|
+
return iter(list(self._ca))
|
|
74
|
+
|
|
75
|
+
def __repr__(self) -> str:
|
|
76
|
+
if len(self) == 0:
|
|
77
|
+
return 'FQ()'
|
|
78
|
+
return 'FQ(' + ', '.join(map(repr, self._ca)) + ')'
|
|
79
|
+
|
|
80
|
+
def __str__(self) -> str:
|
|
81
|
+
return '<< ' + ' < '.join(map(str, self)) + ' <<'
|
|
82
|
+
|
|
83
|
+
def copy(self) -> FIFOQueue[D]:
|
|
84
|
+
"""Return a shallow copy of the ``FIFOQueue``."""
|
|
85
|
+
return FIFOQueue(self._ca)
|
|
86
|
+
|
|
87
|
+
def push(self, *ds: D) -> None:
|
|
88
|
+
"""Push data onto ``FIFOQueue``, does not return a value."""
|
|
89
|
+
self._ca.pushr(*ds)
|
|
90
|
+
|
|
91
|
+
def pop(self) -> MB[D]:
|
|
92
|
+
"""Pop data from ``FIFOQueue``.
|
|
93
|
+
|
|
94
|
+
- pop item off queue, return item in a maybe monad
|
|
95
|
+
- returns an empty ``MB()`` if queue is empty
|
|
96
|
+
"""
|
|
97
|
+
if self._ca:
|
|
98
|
+
return MB(self._ca.popl())
|
|
99
|
+
return MB()
|
|
100
|
+
|
|
101
|
+
def peak_last_in(self) -> MB[D]:
|
|
102
|
+
"""Peak last data into ``FIFOQueue``.
|
|
103
|
+
|
|
104
|
+
- return a maybe monad of the last item pushed to queue
|
|
105
|
+
- does not consume the data
|
|
106
|
+
- if item already popped, return ``MB()``
|
|
107
|
+
"""
|
|
108
|
+
if self._ca:
|
|
109
|
+
return MB(self._ca[-1])
|
|
110
|
+
return MB()
|
|
111
|
+
|
|
112
|
+
def peak_next_out(self) -> MB[D]:
|
|
113
|
+
"""Peak next data out of ``FIFOQueue``.
|
|
114
|
+
|
|
115
|
+
- returns a maybe monad of the next item to be popped from the queue.
|
|
116
|
+
- does not consume it the item
|
|
117
|
+
- returns ``MB()`` if queue is empty
|
|
118
|
+
"""
|
|
119
|
+
if self._ca:
|
|
120
|
+
return MB(self._ca[0])
|
|
121
|
+
return MB()
|
|
122
|
+
|
|
123
|
+
def fold[T](self, f: Callable[[T, D], T], initial: T | None = None, /) -> MB[T]:
|
|
124
|
+
"""Reduce with ``f`` with an optional initial value.
|
|
125
|
+
|
|
126
|
+
- folds in natural FIFO Order (oldest to newest)
|
|
127
|
+
- note that when an initial value is not given then ``~L = ~D``
|
|
128
|
+
- if iterable empty & no initial value given, return ``MB()``
|
|
129
|
+
"""
|
|
130
|
+
if initial is None:
|
|
131
|
+
if not self._ca:
|
|
132
|
+
return MB()
|
|
133
|
+
return MB(self._ca.foldl(f, initial))
|
|
134
|
+
|
|
135
|
+
def map[U](self, f: Callable[[D], U], /) -> FIFOQueue[U]:
|
|
136
|
+
"""Map over the ``FIFOQueue``.
|
|
137
|
+
|
|
138
|
+
- map function ``f`` over the queue
|
|
139
|
+
|
|
140
|
+
- oldest to newest
|
|
141
|
+
- retain original order
|
|
142
|
+
|
|
143
|
+
- returns a new instance
|
|
144
|
+
"""
|
|
145
|
+
return FIFOQueue(map(f, self._ca))
|
|
146
|
+
|
|
147
|
+
|
|
148
|
+
def fifo_queue[D](*ds: D) -> FIFOQueue[D]:
|
|
149
|
+
"""Create a FIFOQueue from the arguments."""
|
|
150
|
+
return FIFOQueue(ds)
|
|
@@ -0,0 +1,152 @@
|
|
|
1
|
+
# Copyright 2023-2024 Geoffrey R. Scheller
|
|
2
|
+
#
|
|
3
|
+
# Licensed under the Apache License, Version 2.0 (the "License");
|
|
4
|
+
# you may not use this file except in compliance with the License.
|
|
5
|
+
# You may obtain a copy of the License at
|
|
6
|
+
#
|
|
7
|
+
# http://www.apache.org/licenses/LICENSE-2.0
|
|
8
|
+
#
|
|
9
|
+
# Unless required by applicable law or agreed to in writing, software
|
|
10
|
+
# distributed under the License is distributed on an "AS IS" BASIS,
|
|
11
|
+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
12
|
+
# See the License for the specific language governing permissions and
|
|
13
|
+
# limitations under the License.
|
|
14
|
+
|
|
15
|
+
"""
|
|
16
|
+
### Last-In-Last-Out (LIFO) Queue
|
|
17
|
+
|
|
18
|
+
- stateful LIFO queue data structures with amortized O(1) pushes and pops
|
|
19
|
+
- obtaining length (number of elements) of a queue is an O(1) operation
|
|
20
|
+
- implemented in a "has-a" relationship with a Python list based circular array
|
|
21
|
+
- will resize itself larger as needed
|
|
22
|
+
|
|
23
|
+
"""
|
|
24
|
+
|
|
25
|
+
from __future__ import annotations
|
|
26
|
+
|
|
27
|
+
from collections.abc import Callable, Iterable, Iterator, Sequence
|
|
28
|
+
from typing import Never, TypeVar, overload
|
|
29
|
+
|
|
30
|
+
from pythonic_fp.circulararray import CA
|
|
31
|
+
from pythonic_fp.containers.maybe import MayBe as MB
|
|
32
|
+
from pythonic_fp.fptools.function import swap
|
|
33
|
+
|
|
34
|
+
__all__ = ['LIFOQueue', 'lifo_queue']
|
|
35
|
+
|
|
36
|
+
D = TypeVar('D')
|
|
37
|
+
|
|
38
|
+
|
|
39
|
+
class LIFOQueue[D]:
|
|
40
|
+
"""LIFO Queue.
|
|
41
|
+
|
|
42
|
+
- stateful Last-In-First-Out (LIFO) data structure
|
|
43
|
+
- initial data pushed on in natural LIFO order
|
|
44
|
+
|
|
45
|
+
"""
|
|
46
|
+
|
|
47
|
+
__slots__ = ('_ca',)
|
|
48
|
+
|
|
49
|
+
T = TypeVar('T')
|
|
50
|
+
U = TypeVar('U')
|
|
51
|
+
|
|
52
|
+
def __init__(self, *dss: Iterable[D]) -> None:
|
|
53
|
+
if (size := len(dss)) < 2:
|
|
54
|
+
self._ca = CA(dss[0]) if size == 1 else CA()
|
|
55
|
+
else:
|
|
56
|
+
msg = f'LIFOQueue expects at most 1 iterable argument, got {size}'
|
|
57
|
+
raise TypeError(msg)
|
|
58
|
+
|
|
59
|
+
def __bool__(self) -> bool:
|
|
60
|
+
return len(self._ca) > 0
|
|
61
|
+
|
|
62
|
+
def __len__(self) -> int:
|
|
63
|
+
return len(self._ca)
|
|
64
|
+
|
|
65
|
+
def __eq__(self, other: object, /) -> bool:
|
|
66
|
+
if not isinstance(other, LIFOQueue):
|
|
67
|
+
return False
|
|
68
|
+
return self._ca == other._ca
|
|
69
|
+
|
|
70
|
+
@overload
|
|
71
|
+
def __getitem__(self, idx: int, /) -> D: ...
|
|
72
|
+
@overload
|
|
73
|
+
def __getitem__(self, idx: slice, /) -> Sequence[D]: ...
|
|
74
|
+
|
|
75
|
+
def __getitem__(self, idx: int | slice, /) -> D | Sequence[D] | Never:
|
|
76
|
+
if isinstance(idx, slice):
|
|
77
|
+
msg = 'dtool.restictive queues are not slicable by design'
|
|
78
|
+
raise NotImplementedError(msg)
|
|
79
|
+
return self._ca[idx]
|
|
80
|
+
|
|
81
|
+
def __iter__(self) -> Iterator[D]:
|
|
82
|
+
return reversed(list(self._ca))
|
|
83
|
+
|
|
84
|
+
def __repr__(self) -> str:
|
|
85
|
+
if len(self) == 0:
|
|
86
|
+
return 'LQ()'
|
|
87
|
+
return 'LQ(' + ', '.join(map(repr, self._ca)) + ')'
|
|
88
|
+
|
|
89
|
+
def __str__(self) -> str:
|
|
90
|
+
return '|| ' + ' > '.join(map(str, self)) + ' ><'
|
|
91
|
+
|
|
92
|
+
def copy(self) -> LIFOQueue[D]:
|
|
93
|
+
"""Return a shallow copy of the ``LIFOQueue``."""
|
|
94
|
+
return LIFOQueue(reversed(self._ca))
|
|
95
|
+
|
|
96
|
+
def push(self, *ds: D) -> None:
|
|
97
|
+
"""Push data onto ``LIFOQueue``, does not return a value."""
|
|
98
|
+
self._ca.pushr(*ds)
|
|
99
|
+
|
|
100
|
+
def pop(self) -> MB[D]:
|
|
101
|
+
"""Pop data from ``LIFOQueue``.
|
|
102
|
+
|
|
103
|
+
- pop item off of queue, return item in a maybe monad
|
|
104
|
+
- returns an empty ``MB()`` if queue is empty
|
|
105
|
+
|
|
106
|
+
"""
|
|
107
|
+
if self._ca:
|
|
108
|
+
return MB(self._ca.popr())
|
|
109
|
+
return MB()
|
|
110
|
+
|
|
111
|
+
def peak(self) -> MB[D]:
|
|
112
|
+
"""Peak next data out of ``LIFOQueue``.
|
|
113
|
+
|
|
114
|
+
- return a maybe monad of the next item to be popped from the queue
|
|
115
|
+
- does not consume the item
|
|
116
|
+
- returns ``MB()`` if queue is empty
|
|
117
|
+
|
|
118
|
+
"""
|
|
119
|
+
if self._ca:
|
|
120
|
+
return MB(self._ca[-1])
|
|
121
|
+
return MB()
|
|
122
|
+
|
|
123
|
+
def fold[T](self, f: Callable[[T, D], T], initial: T | None = None, /) -> MB[T]:
|
|
124
|
+
"""Reduce with ``f`` with an optional initial value.
|
|
125
|
+
|
|
126
|
+
- folds in natural LIFO Order (newest to oldest)
|
|
127
|
+
- note that when an initial value is not given then ``~T = ~D``
|
|
128
|
+
- if iterable empty & no initial value given, return ``MB()``
|
|
129
|
+
|
|
130
|
+
"""
|
|
131
|
+
if initial is None:
|
|
132
|
+
if not self._ca:
|
|
133
|
+
return MB()
|
|
134
|
+
return MB(self._ca.foldr(swap(f), initial))
|
|
135
|
+
|
|
136
|
+
def map[U](self, f: Callable[[D], U], /) -> LIFOQueue[U]:
|
|
137
|
+
"""Map Over the ``LIFOQueue``.
|
|
138
|
+
|
|
139
|
+
- map the function ``f`` over the queue
|
|
140
|
+
|
|
141
|
+
- newest to oldest
|
|
142
|
+
- retain original order
|
|
143
|
+
|
|
144
|
+
- returns a new instance
|
|
145
|
+
|
|
146
|
+
"""
|
|
147
|
+
return LIFOQueue(reversed(CA(map(f, reversed(self._ca)))))
|
|
148
|
+
|
|
149
|
+
|
|
150
|
+
def lifo_queue[D](*ds: D) -> LIFOQueue[D]:
|
|
151
|
+
"""Create a LIFOQueue from the arguments."""
|
|
152
|
+
return LIFOQueue(ds)
|
|
File without changes
|
|
@@ -0,0 +1,49 @@
|
|
|
1
|
+
Metadata-Version: 2.4
|
|
2
|
+
Name: pythonic-fp.queues
|
|
3
|
+
Version: 3.0.0
|
|
4
|
+
Summary: Developer Tools - Queue based data structures
|
|
5
|
+
Keywords: dqueue,fifo,lifo,queue
|
|
6
|
+
Author-email: "Geoffrey R. Scheller" <geoffrey@scheller.com>
|
|
7
|
+
Requires-Python: >=3.12
|
|
8
|
+
Description-Content-Type: text/markdown
|
|
9
|
+
Classifier: Development Status :: 5 - Production/Stable
|
|
10
|
+
Classifier: Framework :: Pytest
|
|
11
|
+
Classifier: Intended Audience :: Developers
|
|
12
|
+
Classifier: License :: OSI Approved :: Apache Software License
|
|
13
|
+
Classifier: Operating System :: OS Independent
|
|
14
|
+
Classifier: Programming Language :: Python :: 3.13
|
|
15
|
+
Classifier: Typing :: Typed
|
|
16
|
+
License-File: LICENSE
|
|
17
|
+
Requires-Dist: pythonic-fp.circulararray >= 5.0.0
|
|
18
|
+
Requires-Dist: pythonic-fp.containers >=2.0.0
|
|
19
|
+
Requires-Dist: pythonic-fp.fptools >=3.0.0
|
|
20
|
+
Project-URL: Changelog, https://grscheller.github.io/pythonic-fp-queues/html/changelog.html
|
|
21
|
+
Project-URL: Documentation, https://grscheller.github.io/pythonic-fp-queues/html/
|
|
22
|
+
Project-URL: Homepage, https://github.com/grscheller/pythonic-fp/blob/main/README.md
|
|
23
|
+
Project-URL: Source, https://github.com/grscheller/pythonic-fp-queues
|
|
24
|
+
|
|
25
|
+
# Pythonic FP - Queues
|
|
26
|
+
|
|
27
|
+
PyPI project
|
|
28
|
+
[pythonic-fp.queues](https://pypi.org/project/pythonic-fp.queues/)
|
|
29
|
+
Implementing 3 types of queue data structures.
|
|
30
|
+
|
|
31
|
+
| Class | Queue Type |
|
|
32
|
+
|:---------:|:------------------------ |
|
|
33
|
+
| FIFOQueue | First-In-First-Out Queue |
|
|
34
|
+
| LIFOQueue | Last-In-First-Out Queue |
|
|
35
|
+
| DEQueue | Double-Ended Queue |
|
|
36
|
+
|
|
37
|
+
This PyPI project is part of of the grscheller
|
|
38
|
+
[pythonic-fp namespace projects](https://github.com/grscheller/pythonic-fp/blob/main/README.md).
|
|
39
|
+
|
|
40
|
+
## Documentation
|
|
41
|
+
|
|
42
|
+
Documentation hosted on
|
|
43
|
+
[GitHub Pages](https://grscheller.github.io/pythonic-fp-queues/html).
|
|
44
|
+
|
|
45
|
+
## Copyright and License
|
|
46
|
+
|
|
47
|
+
Copyright (c) 2023-2025 Geoffrey R. Scheller. Licensed under the Apache
|
|
48
|
+
License, Version 2.0. See the LICENSE file for details.
|
|
49
|
+
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
pythonic_fp/queues/__init__.py,sha256=9gxtZ2HyHWVcpcTmTOc_ZNf4-WYGhRMOwhoOf64ulP8,1519
|
|
2
|
+
pythonic_fp/queues/de.py,sha256=d4mgLa5MMPxjzWSEbOhWv1RuJtMwViZR4dxPYKezsUk,5918
|
|
3
|
+
pythonic_fp/queues/fifo.py,sha256=IePckXP722Ky_MKZySawY-q9cFJ6XcMCa_eWXRrbPhI,4519
|
|
4
|
+
pythonic_fp/queues/lifo.py,sha256=LuA3sg5uTMZMXFrf3hTPVDDJ9jbmdKqZNv0zBjcCUrk,4553
|
|
5
|
+
pythonic_fp/queues/py.typed,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
|
|
6
|
+
pythonic_fp_queues-3.0.0.dist-info/licenses/LICENSE,sha256=IJMkVWQUVy-SonK6SMaMotDQ6Zb-lVi2bUTiGp1GUO8,10774
|
|
7
|
+
pythonic_fp_queues-3.0.0.dist-info/WHEEL,sha256=G2gURzTEtmeR8nrdXUJfNiB3VYVxigPQ-bEQujpNiNs,82
|
|
8
|
+
pythonic_fp_queues-3.0.0.dist-info/METADATA,sha256=svlf8vGt6_z1PwRAoyYG67cR2HbmxhnYCjkGFMtoSsI,1858
|
|
9
|
+
pythonic_fp_queues-3.0.0.dist-info/RECORD,,
|
|
@@ -0,0 +1,190 @@
|
|
|
1
|
+
Apache License
|
|
2
|
+
Version 2.0, January 2004
|
|
3
|
+
http://www.apache.org/licenses/
|
|
4
|
+
|
|
5
|
+
TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
|
|
6
|
+
|
|
7
|
+
1. Definitions.
|
|
8
|
+
|
|
9
|
+
"License" shall mean the terms and conditions for use, reproduction,
|
|
10
|
+
and distribution as defined by Sections 1 through 9 of this document.
|
|
11
|
+
|
|
12
|
+
"Licensor" shall mean the copyright owner or entity authorized by
|
|
13
|
+
the copyright owner that is granting the License.
|
|
14
|
+
|
|
15
|
+
"Legal Entity" shall mean the union of the acting entity and all
|
|
16
|
+
other entities that control, are controlled by, or are under common
|
|
17
|
+
control with that entity. For the purposes of this definition,
|
|
18
|
+
"control" means (i) the power, direct or indirect, to cause the
|
|
19
|
+
direction or management of such entity, whether by contract or
|
|
20
|
+
otherwise, or (ii) ownership of fifty percent (50%) or more of the
|
|
21
|
+
outstanding shares, or (iii) beneficial ownership of such entity.
|
|
22
|
+
|
|
23
|
+
"You" (or "Your") shall mean an individual or Legal Entity
|
|
24
|
+
exercising permissions granted by this License.
|
|
25
|
+
|
|
26
|
+
"Source" form shall mean the preferred form for making modifications,
|
|
27
|
+
including but not limited to software source code, documentation
|
|
28
|
+
source, and configuration files.
|
|
29
|
+
|
|
30
|
+
"Object" form shall mean any form resulting from mechanical
|
|
31
|
+
transformation or translation of a Source form, including but
|
|
32
|
+
not limited to compiled object code, generated documentation,
|
|
33
|
+
and conversions to other media types.
|
|
34
|
+
|
|
35
|
+
"Work" shall mean the work of authorship, whether in Source or
|
|
36
|
+
Object form, made available under the License, as indicated by a
|
|
37
|
+
copyright notice that is included in or attached to the work
|
|
38
|
+
(an example is provided in the Appendix below).
|
|
39
|
+
|
|
40
|
+
"Derivative Works" shall mean any work, whether in Source or Object
|
|
41
|
+
form, that is based on (or derived from) the Work and for which the
|
|
42
|
+
editorial revisions, annotations, elaborations, or other modifications
|
|
43
|
+
represent, as a whole, an original work of authorship. For the purposes
|
|
44
|
+
of this License, Derivative Works shall not include works that remain
|
|
45
|
+
separable from, or merely link (or bind by name) to the interfaces of,
|
|
46
|
+
the Work and Derivative Works thereof.
|
|
47
|
+
|
|
48
|
+
"Contribution" shall mean any work of authorship, including
|
|
49
|
+
the original version of the Work and any modifications or additions
|
|
50
|
+
to that Work or Derivative Works thereof, that is intentionally
|
|
51
|
+
submitted to Licensor for inclusion in the Work by the copyright owner
|
|
52
|
+
or by an individual or Legal Entity authorized to submit on behalf of
|
|
53
|
+
the copyright owner. For the purposes of this definition, "submitted"
|
|
54
|
+
means any form of electronic, verbal, or written communication sent
|
|
55
|
+
to the Licensor or its representatives, including but not limited to
|
|
56
|
+
communication on electronic mailing lists, source code control systems,
|
|
57
|
+
and issue tracking systems that are managed by, or on behalf of, the
|
|
58
|
+
Licensor for the purpose of discussing and improving the Work, but
|
|
59
|
+
excluding communication that is conspicuously marked or otherwise
|
|
60
|
+
designated in writing by the copyright owner as "Not a Contribution."
|
|
61
|
+
|
|
62
|
+
"Contributor" shall mean Licensor and any individual or Legal Entity
|
|
63
|
+
on behalf of whom a Contribution has been received by Licensor and
|
|
64
|
+
subsequently incorporated within the Work.
|
|
65
|
+
|
|
66
|
+
2. Grant of Copyright License. Subject to the terms and conditions of
|
|
67
|
+
this License, each Contributor hereby grants to You a perpetual,
|
|
68
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
|
69
|
+
copyright license to reproduce, prepare Derivative Works of,
|
|
70
|
+
publicly display, publicly perform, sublicense, and distribute the
|
|
71
|
+
Work and such Derivative Works in Source or Object form.
|
|
72
|
+
|
|
73
|
+
3. Grant of Patent License. Subject to the terms and conditions of
|
|
74
|
+
this License, each Contributor hereby grants to You a perpetual,
|
|
75
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
|
76
|
+
(except as stated in this section) patent license to make, have made,
|
|
77
|
+
use, offer to sell, sell, import, and otherwise transfer the Work,
|
|
78
|
+
where such license applies only to those patent claims licensable
|
|
79
|
+
by such Contributor that are necessarily infringed by their
|
|
80
|
+
Contribution(s) alone or by combination of their Contribution(s)
|
|
81
|
+
with the Work to which such Contribution(s) was submitted. If You
|
|
82
|
+
institute patent litigation against any entity (including a
|
|
83
|
+
cross-claim or counterclaim in a lawsuit) alleging that the Work
|
|
84
|
+
or a Contribution incorporated within the Work constitutes direct
|
|
85
|
+
or contributory patent infringement, then any patent licenses
|
|
86
|
+
granted to You under this License for that Work shall terminate
|
|
87
|
+
as of the date such litigation is filed.
|
|
88
|
+
|
|
89
|
+
4. Redistribution. You may reproduce and distribute copies of the
|
|
90
|
+
Work or Derivative Works thereof in any medium, with or without
|
|
91
|
+
modifications, and in Source or Object form, provided that You
|
|
92
|
+
meet the following conditions:
|
|
93
|
+
|
|
94
|
+
(a) You must give any other recipients of the Work or
|
|
95
|
+
Derivative Works a copy of this License; and
|
|
96
|
+
|
|
97
|
+
(b) You must cause any modified files to carry prominent notices
|
|
98
|
+
stating that You changed the files; and
|
|
99
|
+
|
|
100
|
+
(c) You must retain, in the Source form of any Derivative Works
|
|
101
|
+
that You distribute, all copyright, patent, trademark, and
|
|
102
|
+
attribution notices from the Source form of the Work,
|
|
103
|
+
excluding those notices that do not pertain to any part of
|
|
104
|
+
the Derivative Works; and
|
|
105
|
+
|
|
106
|
+
(d) If the Work includes a "NOTICE" text file as part of its
|
|
107
|
+
distribution, then any Derivative Works that You distribute must
|
|
108
|
+
include a readable copy of the attribution notices contained
|
|
109
|
+
within such NOTICE file, excluding those notices that do not
|
|
110
|
+
pertain to any part of the Derivative Works, in at least one
|
|
111
|
+
of the following places: within a NOTICE text file distributed
|
|
112
|
+
as part of the Derivative Works; within the Source form or
|
|
113
|
+
documentation, if provided along with the Derivative Works; or,
|
|
114
|
+
within a display generated by the Derivative Works, if and
|
|
115
|
+
wherever such third-party notices normally appear. The contents
|
|
116
|
+
of the NOTICE file are for informational purposes only and
|
|
117
|
+
do not modify the License. You may add Your own attribution
|
|
118
|
+
notices within Derivative Works that You distribute, alongside
|
|
119
|
+
or as an addendum to the NOTICE text from the Work, provided
|
|
120
|
+
that such additional attribution notices cannot be construed
|
|
121
|
+
as modifying the License.
|
|
122
|
+
|
|
123
|
+
You may add Your own copyright statement to Your modifications and
|
|
124
|
+
may provide additional or different license terms and conditions
|
|
125
|
+
for use, reproduction, or distribution of Your modifications, or
|
|
126
|
+
for any such Derivative Works as a whole, provided Your use,
|
|
127
|
+
reproduction, and distribution of the Work otherwise complies with
|
|
128
|
+
the conditions stated in this License.
|
|
129
|
+
|
|
130
|
+
5. Submission of Contributions. Unless You explicitly state otherwise,
|
|
131
|
+
any Contribution intentionally submitted for inclusion in the Work
|
|
132
|
+
by You to the Licensor shall be under the terms and conditions of
|
|
133
|
+
this License, without any additional terms or conditions.
|
|
134
|
+
Notwithstanding the above, nothing herein shall supersede or modify
|
|
135
|
+
the terms of any separate license agreement you may have executed
|
|
136
|
+
with Licensor regarding such Contributions.
|
|
137
|
+
|
|
138
|
+
6. Trademarks. This License does not grant permission to use the trade
|
|
139
|
+
names, trademarks, service marks, or product names of the Licensor,
|
|
140
|
+
except as required for reasonable and customary use in describing the
|
|
141
|
+
origin of the Work and reproducing the content of the NOTICE file.
|
|
142
|
+
|
|
143
|
+
7. Disclaimer of Warranty. Unless required by applicable law or
|
|
144
|
+
agreed to in writing, Licensor provides the Work (and each
|
|
145
|
+
Contributor provides its Contributions) on an "AS IS" BASIS,
|
|
146
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
|
|
147
|
+
implied, including, without limitation, any warranties or conditions
|
|
148
|
+
of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
|
|
149
|
+
PARTICULAR PURPOSE. You are solely responsible for determining the
|
|
150
|
+
appropriateness of using or redistributing the Work and assume any
|
|
151
|
+
risks associated with Your exercise of permissions under this License.
|
|
152
|
+
|
|
153
|
+
8. Limitation of Liability. In no event and under no legal theory,
|
|
154
|
+
whether in tort (including negligence), contract, or otherwise,
|
|
155
|
+
unless required by applicable law (such as deliberate and grossly
|
|
156
|
+
negligent acts) or agreed to in writing, shall any Contributor be
|
|
157
|
+
liable to You for damages, including any direct, indirect, special,
|
|
158
|
+
incidental, or consequential damages of any character arising as a
|
|
159
|
+
result of this License or out of the use or inability to use the
|
|
160
|
+
Work (including but not limited to damages for loss of goodwill,
|
|
161
|
+
work stoppage, computer failure or malfunction, or any and all
|
|
162
|
+
other commercial damages or losses), even if such Contributor
|
|
163
|
+
has been advised of the possibility of such damages.
|
|
164
|
+
|
|
165
|
+
9. Accepting Warranty or Additional Liability. While redistributing
|
|
166
|
+
the Work or Derivative Works thereof, You may choose to offer,
|
|
167
|
+
and charge a fee for, acceptance of support, warranty, indemnity,
|
|
168
|
+
or other liability obligations and/or rights consistent with this
|
|
169
|
+
License. However, in accepting such obligations, You may act only
|
|
170
|
+
on Your own behalf and on Your sole responsibility, not on behalf
|
|
171
|
+
of any other Contributor, and only if You agree to indemnify,
|
|
172
|
+
defend, and hold each Contributor harmless for any liability
|
|
173
|
+
incurred by, or claims asserted against, such Contributor by reason
|
|
174
|
+
of your accepting any such warranty or additional liability.
|
|
175
|
+
|
|
176
|
+
END OF TERMS AND CONDITIONS
|
|
177
|
+
|
|
178
|
+
Copyright (c) 2023-2025 Geoffrey R. Scheller
|
|
179
|
+
|
|
180
|
+
Licensed under the Apache License, Version 2.0 (the "License");
|
|
181
|
+
you may not use this file except in compliance with the License.
|
|
182
|
+
You may obtain a copy of the License at
|
|
183
|
+
|
|
184
|
+
http://www.apache.org/licenses/LICENSE-2.0
|
|
185
|
+
|
|
186
|
+
Unless required by applicable law or agreed to in writing, software
|
|
187
|
+
distributed under the License is distributed on an "AS IS" BASIS,
|
|
188
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
189
|
+
See the License for the specific language governing permissions and
|
|
190
|
+
limitations under the License.
|