Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
26 commits
Select commit Hold shift + click to select a range
5919602
added queue.py
iamrajiv Jan 16, 2020
a6b81bf
changes made to __init.py__
iamrajiv Jan 16, 2020
bfb8158
added tests for Queue.py
iamrajiv Jan 16, 2020
c277a02
added new line at end of __init.py__
iamrajiv Jan 16, 2020
87dfc60
added queue size
iamrajiv Jan 16, 2020
81a6fed
corrected tests
iamrajiv Jan 16, 2020
643a336
corrected tests
iamrajiv Jan 16, 2020
7fcd05d
corrected tests
iamrajiv Jan 16, 2020
378046b
corrected tests and queue.py
iamrajiv Jan 16, 2020
94f4c05
corrected tests and queue.py
iamrajiv Jan 16, 2020
466d66f
corrected tests and queue.py
iamrajiv Jan 16, 2020
3ca4ba9
corrected tests and queue.py
iamrajiv Jan 16, 2020
b6c7a0c
corrected tests and queue.py
iamrajiv Jan 16, 2020
2b11c96
corrected tests and queue.py
iamrajiv Jan 16, 2020
7a8262c
corrected tests and queue.py
iamrajiv Jan 16, 2020
f3a7c3d
implemented DynamicOneDimensionalArray of queue and improved code qua…
iamrajiv Jan 31, 2020
df81e1e
implemented DynamicOneDimensionalArray of queue and improved code qua…
iamrajiv Jan 31, 2020
8606d2b
remove unnecessary line
iamrajiv Jan 31, 2020
7418af0
improved tests
iamrajiv Feb 3, 2020
3845d67
removed init
iamrajiv Feb 3, 2020
7452720
added front and rear attributes
iamrajiv Feb 5, 2020
c143412
corrected the code syntax
iamrajiv Feb 5, 2020
80c1607
corrected the code syntax
iamrajiv Feb 5, 2020
9a0d81b
queue corrected, tests to be verified
czgdp1807 Feb 12, 2020
540c060
added tests, ready for merge
czgdp1807 Feb 14, 2020
0b20f54
updated authors
czgdp1807 Feb 14, 2020
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 7 additions & 6 deletions AUTHORS
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
Gagandeep Singh<singh.23@iitj.ac.in>
Kartikei Mittal<kartikeimittal@gmail.com>
Umesh<23umesh.here@gmail.com>
Rohan Singh<singh.77@iitj.ac.in>
Tarun Singh Tomar<tomartarun2001@gmail.com>
Saptashrungi Birajdar<saptashrungib@gmail.com>
Gagandeep Singh <singh.23@iitj.ac.in>
Kartikei Mittal <kartikeimittal@gmail.com>
Umesh <23umesh.here@gmail.com>
Rohan Singh <singh.77@iitj.ac.in>
Tarun Singh Tomar <tomartarun2001@gmail.com>
Saptashrungi Birajdar <saptashrungib@gmail.com>
Rajiv Ranjan Singh <rajivperfect007@gmail.com>
5 changes: 5 additions & 0 deletions pydatastructs/miscellaneous_data_structures/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,3 +14,8 @@
Stack,
)
__all__.extend(stack.__all__)

from .queue import (
Queue,
)
__all__.extend(queue.__all__)
124 changes: 124 additions & 0 deletions pydatastructs/miscellaneous_data_structures/queue.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,124 @@
from pydatastructs.linear_data_structures import DynamicOneDimensionalArray
from pydatastructs.utils.misc_util import NoneType
from copy import deepcopy as dc

__all__ = [
'Queue'
]

class Queue(object):
"""Representation of queue data structure.

Parameters
==========

implementation : str
Implementation to be used for queue.
By default, 'array'
Currently only supports 'array'
implementation.
items : list/tuple
Optional, by default, None
The inital items in the queue.
For array implementation.
dtype : A valid python type
Optional, by default NoneType if item
is None, otherwise takes the data
type of DynamicOneDimensionalArray
For array implementation.

Examples
========

>>> from pydatastructs import Queue
>>> q = Queue()
>>> q.append(1)
>>> q.append(2)
>>> q.append(3)
>>> q.popleft()
1
>>> len(q)
2

References
==========

.. [1] https://en.wikipedia.org/wiki/Queue_(abstract_data_type)
"""

def __new__(cls, implementation='array', **kwargs):
if implementation == 'array':
return ArrayQueue(
kwargs.get('items', None),
kwargs.get('dtype', int))
raise NotImplementedError(
"%s hasn't been implemented yet."%(implementation))

def append(self, *args, **kwargs):
raise NotImplementedError(
"This is an abstract method.")

def popleft(self, *args, **kwargs):
raise NotImplementedError(
"This is an abstract method.")

@property
def is_empty(self):
return None

class ArrayQueue(Queue):

__slots__ = ['front']

def __new__(cls, items=None, dtype=NoneType):
if items is None:
items = DynamicOneDimensionalArray(dtype, 0)
else:
dtype = type(items[0])
items = DynamicOneDimensionalArray(dtype, items)
obj = object.__new__(cls)
obj.items, obj.front = items, -1
if items.size == 0:
obj.front = -1
else:
obj.front = 0
return obj

def append(self, x):
if self.is_empty:
self.front = 0
self.items._dtype = type(x)
self.items.append(x)

def popleft(self):
if self.is_empty:
raise ValueError("Queue is empty.")
return_value = dc(self.items[self.front])
front_temp = self.front
if self.front == self.rear:
self.front = -1
else:
if (self.items._num - 1)/self.items._size < \
self.items._load_factor:
self.front = 0
else:
self.front += 1
self.items.delete(front_temp)
return return_value

@property
def rear(self):
return self.items._last_pos_filled

@property
def is_empty(self):
return self.__len__() == 0

def __len__(self):
return self.items._num

def __str__(self):
_data = []
for i in range(self.front, self.rear + 1):
_data.append(self.items._data[i])
return str(_data)
23 changes: 11 additions & 12 deletions pydatastructs/miscellaneous_data_structures/stack.py
Original file line number Diff line number Diff line change
@@ -1,15 +1,13 @@
from pydatastructs.linear_data_structures import DynamicOneDimensionalArray
from pydatastructs.utils.misc_util import _check_type, NoneType
from copy import deepcopy as dc

__all__ = [
'Stack'
]

_check_type = lambda a, t: isinstance(a, t)
NoneType = type(None)

class Stack(object):
"""Respresentation of stack data structure
"""Representation of stack data structure

Parameters
==========
Expand All @@ -24,13 +22,13 @@ class Stack(object):
The inital items in the stack.
For array implementation.
dtype : A valid python type
Optional, by default int if item
Optional, by default NoneType if item
is None, otherwise takes the data
type of OneDimensionalArray
type of DynamicOneDimensionalArray
For array implementation.

Example
=======
Examples
========

>>> from pydatastructs import Stack
>>> s = Stack()
Expand Down Expand Up @@ -74,19 +72,20 @@ def peek(self):

class ArrayStack(Stack):

__slots__ = ['items', 'dtype']
__slots__ = ['items']

def __new__(cls, items=None, dtype=int):
def __new__(cls, items=None, dtype=NoneType):
if items is None:
items = DynamicOneDimensionalArray(dtype, 0)
else:
items = DynamicOneDimensionalArray(dtype, items)
obj = object.__new__(cls)
obj.items, obj.dtype = \
items, items._dtype
obj.items = items
return obj

def push(self, x):
if self.is_empty:
self.items._dtype = type(x)
self.items.append(x)

def pop(self):
Expand Down
20 changes: 20 additions & 0 deletions pydatastructs/miscellaneous_data_structures/tests/test_queue.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
from pydatastructs.miscellaneous_data_structures import Queue
from pydatastructs.utils.raises_util import raises

def test_Queue():

q1 = Queue(implementation='array', items=[0])
q1.append(1)
q1.append(2)
q1.append(3)
assert str(q1) == '[0, 1, 2, 3]'
assert len(q1) == 4
assert q1.popleft() == 0
assert q1.popleft() == 1
assert len(q1) == 2
assert q1.popleft() == 2
assert q1.popleft() == 3
assert len(q1) == 0

q1 = Queue()
raises(ValueError, lambda: q1.popleft())