From 2c7cc34e74470e5de1529fefbc72a04193a44e8e Mon Sep 17 00:00:00 2001 From: Utkarsh Mishra <35002267+utkarsh0702@users.noreply.github.com> Date: Sat, 28 Dec 2019 23:38:26 +0530 Subject: [PATCH 01/13] Create --- pydatastructs/miscellaneous_data_structures/queue.py | 1 + 1 file changed, 1 insertion(+) create mode 100644 pydatastructs/miscellaneous_data_structures/queue.py diff --git a/pydatastructs/miscellaneous_data_structures/queue.py b/pydatastructs/miscellaneous_data_structures/queue.py new file mode 100644 index 000000000..8b1378917 --- /dev/null +++ b/pydatastructs/miscellaneous_data_structures/queue.py @@ -0,0 +1 @@ + From 4a37e4b812b1f213d53ada7a94fd61bc0f70bf91 Mon Sep 17 00:00:00 2001 From: Utkarsh Mishra <35002267+utkarsh0702@users.noreply.github.com> Date: Sat, 28 Dec 2019 23:38:58 +0530 Subject: [PATCH 02/13] Delete queue.py --- pydatastructs/miscellaneous_data_structures/queue.py | 1 - 1 file changed, 1 deletion(-) delete mode 100644 pydatastructs/miscellaneous_data_structures/queue.py diff --git a/pydatastructs/miscellaneous_data_structures/queue.py b/pydatastructs/miscellaneous_data_structures/queue.py deleted file mode 100644 index 8b1378917..000000000 --- a/pydatastructs/miscellaneous_data_structures/queue.py +++ /dev/null @@ -1 +0,0 @@ - From 161ac686b7f98dfaabf93352f5cd0b4f3104fb0e Mon Sep 17 00:00:00 2001 From: Utkarsh Mishra <35002267+utkarsh0702@users.noreply.github.com> Date: Sun, 29 Dec 2019 00:54:31 +0530 Subject: [PATCH 03/13] queue.py Added queue.py code --- .../miscellaneous_data_structures/queue.py | 72 +++++++++++++++++++ 1 file changed, 72 insertions(+) create mode 100644 pydatastructs/miscellaneous_data_structures/queue.py diff --git a/pydatastructs/miscellaneous_data_structures/queue.py b/pydatastructs/miscellaneous_data_structures/queue.py new file mode 100644 index 000000000..0f34b0066 --- /dev/null +++ b/pydatastructs/miscellaneous_data_structures/queue.py @@ -0,0 +1,72 @@ +from pydatastructs.linear_data_structures import OneDimensionalArray + +_check_type = lambda a, t: isinstance(a, t) +NoneType = type(None) +rear= -1 + +class Queue(objects): + def __new__(clas, implementation='array', **kwargs): + if implementation == 'array': + return ArrayQueue( + kwargs.get('maxsize', None), + kwargs.get('front', -1), + kwargs.get('rear', -1), + kwargs.get('item', 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, **kwargs): + raise NotImplementedError("This is an abstract method.") + + def __len__(self, **kwargs): + raise NotImplementedError("This is an abstract method.") + + +class ArrayQueue(Queue): + __slots__ = ['maxsize','front','rear','item','dtype'] + def __new__(cls, maxsize = None, front = -1, rear = -1, item = None, dtype = int): + if not _check_type(maxsize, int): + raise ValueError("maxsize is missing.") + if not _check_type(front, int): + raise TypeError("front is not of type int.") + if not _check_type(rear, int): + raise TypeError("rear is not of type int.") + if item is None: + item= OneDimensionalArray(dtype, maxsize) + if not _check_type(item, OneDimensionalArray): + raise ValueError("item is not of OneDimensionalArray type") + if item._size > maxsize: + raise ValueError("Overflow, size of item %s is greater than maxsize, %s"%(items._size, maxsize)) + + obj = object.__new__(clas) + obj.maxsize, obj.front, obj.rear, obj.item, obj.dtype = maxsize, front, rear, item, items._dtype + return obj + + def append(self, x): + if self.rear == self.maxsize-1: + raise ValueError("Queue is full.") + else: + if self.front == -1: + self.front=0 + self.rear +=1 + self.item[self.rear]= self.dtype(x) + + def popleft(self): + if (self.front == -1) and (self.front > self.rear): + raise ValueError("Queue is empty.") + self.top -= 1 + r = self.item[self.front] + self.item[self.front] = None + self.front += 1 + return r + + def __len__(self): + i=0 + count=0 + while(self.item[i] is not None): + i+=1 + count+=1 + return count From 2442914155635dbebe6f66b5fa7bbd74638b77a2 Mon Sep 17 00:00:00 2001 From: Utkarsh Mishra <35002267+utkarsh0702@users.noreply.github.com> Date: Sun, 29 Dec 2019 01:09:56 +0530 Subject: [PATCH 04/13] Update __init__.py --- pydatastructs/miscellaneous_data_structures/__init__.py | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/pydatastructs/miscellaneous_data_structures/__init__.py b/pydatastructs/miscellaneous_data_structures/__init__.py index 09edacdce..3bc15898a 100644 --- a/pydatastructs/miscellaneous_data_structures/__init__.py +++ b/pydatastructs/miscellaneous_data_structures/__init__.py @@ -2,9 +2,14 @@ from . import ( stack, + queue, ) from .stack import ( Stack, ) +from .queue import ( + Queue, +) __all__.extend(stack.__all__) +__all__.extend(queue.__all__) From 6449369ed2e71a8ab1de1d1bc4281a8c76a9bf8a Mon Sep 17 00:00:00 2001 From: Utkarsh Mishra <35002267+utkarsh0702@users.noreply.github.com> Date: Sun, 29 Dec 2019 01:11:56 +0530 Subject: [PATCH 05/13] Update queue.py --- pydatastructs/miscellaneous_data_structures/queue.py | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/pydatastructs/miscellaneous_data_structures/queue.py b/pydatastructs/miscellaneous_data_structures/queue.py index 0f34b0066..ad98eb837 100644 --- a/pydatastructs/miscellaneous_data_structures/queue.py +++ b/pydatastructs/miscellaneous_data_structures/queue.py @@ -1,5 +1,9 @@ from pydatastructs.linear_data_structures import OneDimensionalArray +__all__ = [ + 'Queue' +] + _check_type = lambda a, t: isinstance(a, t) NoneType = type(None) rear= -1 From 7c444d2d44c7c04f0efa77c22075e3e57f85610c Mon Sep 17 00:00:00 2001 From: Utkarsh Mishra <35002267+utkarsh0702@users.noreply.github.com> Date: Mon, 30 Dec 2019 11:04:37 +0530 Subject: [PATCH 06/13] Updated queue.py file with Dynamic One Dimensional Array --- .../miscellaneous_data_structures/queue.py | 44 +++++++------------ 1 file changed, 17 insertions(+), 27 deletions(-) diff --git a/pydatastructs/miscellaneous_data_structures/queue.py b/pydatastructs/miscellaneous_data_structures/queue.py index ad98eb837..d0873e215 100644 --- a/pydatastructs/miscellaneous_data_structures/queue.py +++ b/pydatastructs/miscellaneous_data_structures/queue.py @@ -1,4 +1,4 @@ -from pydatastructs.linear_data_structures import OneDimensionalArray +from pydatastructs.linear_data_structures import DynamicOneDimensionalArray __all__ = [ 'Queue' @@ -14,9 +14,9 @@ def __new__(clas, implementation='array', **kwargs): return ArrayQueue( kwargs.get('maxsize', None), kwargs.get('front', -1), - kwargs.get('rear', -1), kwargs.get('item', None), - kwargs.get('dtype', int)) + kwargs.get('dtype', int), + kwargs.get('count', int)) raise NotImplementedError("%s hasn't been implemented yet."%(implementation)) def append(self, *args, **kwargs): @@ -30,47 +30,37 @@ def __len__(self, **kwargs): class ArrayQueue(Queue): - __slots__ = ['maxsize','front','rear','item','dtype'] - def __new__(cls, maxsize = None, front = -1, rear = -1, item = None, dtype = int): + __slots__ = ['maxsize','front','item','dtype','count'] + def __new__(clas, maxsize = None, front = -1, item = None, dtype = int, count=0): if not _check_type(maxsize, int): raise ValueError("maxsize is missing.") if not _check_type(front, int): raise TypeError("front is not of type int.") - if not _check_type(rear, int): - raise TypeError("rear is not of type int.") if item is None: - item= OneDimensionalArray(dtype, maxsize) - if not _check_type(item, OneDimensionalArray): - raise ValueError("item is not of OneDimensionalArray type") - if item._size > maxsize: - raise ValueError("Overflow, size of item %s is greater than maxsize, %s"%(items._size, maxsize)) + item= DynamicOneDimensionalArray(dtype, maxsize) + if not _check_type(item, DynamicOneDimensionalArray): + raise ValueError("item is not of DynamicOneDimensionalArray type") obj = object.__new__(clas) - obj.maxsize, obj.front, obj.rear, obj.item, obj.dtype = maxsize, front, rear, item, items._dtype + obj.maxsize, obj.front, obj.item, obj.dtype, obj.count = maxsize, front, item, items._dtype, count return obj def append(self, x): - if self.rear == self.maxsize-1: - raise ValueError("Queue is full.") - else: if self.front == -1: self.front=0 - self.rear +=1 - self.item[self.rear]= self.dtype(x) + self.item.apped[self.dtype(x)] + count+=1 def popleft(self): - if (self.front == -1) and (self.front > self.rear): + if (self.front == -1): raise ValueError("Queue is empty.") - self.top -= 1 r = self.item[self.front] - self.item[self.front] = None + self.item.delete[self.front] self.front += 1 return r def __len__(self): - i=0 - count=0 - while(self.item[i] is not None): - i+=1 - count+=1 - return count + if self.front== -1: + return 0 + else: + return (self.count- self.front) From 18f92e5a891a9ba6ee37b8a88c666e3161ea8171 Mon Sep 17 00:00:00 2001 From: Utkarsh Mishra <35002267+utkarsh0702@users.noreply.github.com> Date: Wed, 1 Jan 2020 17:53:20 +0530 Subject: [PATCH 07/13] Added queue.py --- pydatastructs/miscellaneous_data_structures/__init__.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/pydatastructs/miscellaneous_data_structures/__init__.py b/pydatastructs/miscellaneous_data_structures/__init__.py index 3bc15898a..3ccfd8ea8 100644 --- a/pydatastructs/miscellaneous_data_structures/__init__.py +++ b/pydatastructs/miscellaneous_data_structures/__init__.py @@ -8,8 +8,9 @@ from .stack import ( Stack, ) +__all__.extend(stack.__all__) + from .queue import ( Queue, ) -__all__.extend(stack.__all__) __all__.extend(queue.__all__) From 928082d4cd82d0c90cba861d60452f00534b9a6d Mon Sep 17 00:00:00 2001 From: Utkarsh Mishra <35002267+utkarsh0702@users.noreply.github.com> Date: Wed, 1 Jan 2020 19:27:48 +0530 Subject: [PATCH 08/13] Added Dynamic 1d array queue --- .../miscellaneous_data_structures/queue.py | 38 ++++++++----------- 1 file changed, 15 insertions(+), 23 deletions(-) diff --git a/pydatastructs/miscellaneous_data_structures/queue.py b/pydatastructs/miscellaneous_data_structures/queue.py index d0873e215..a222586a7 100644 --- a/pydatastructs/miscellaneous_data_structures/queue.py +++ b/pydatastructs/miscellaneous_data_structures/queue.py @@ -12,11 +12,9 @@ class Queue(objects): def __new__(clas, implementation='array', **kwargs): if implementation == 'array': return ArrayQueue( - kwargs.get('maxsize', None), kwargs.get('front', -1), kwargs.get('item', None), - kwargs.get('dtype', int), - kwargs.get('count', int)) + kwargs.get('dtype', int)) raise NotImplementedError("%s hasn't been implemented yet."%(implementation)) def append(self, *args, **kwargs): @@ -25,42 +23,36 @@ def append(self, *args, **kwargs): def popleft(self, **kwargs): raise NotImplementedError("This is an abstract method.") - def __len__(self, **kwargs): - raise NotImplementedError("This is an abstract method.") - class ArrayQueue(Queue): - __slots__ = ['maxsize','front','item','dtype','count'] - def __new__(clas, maxsize = None, front = -1, item = None, dtype = int, count=0): - if not _check_type(maxsize, int): - raise ValueError("maxsize is missing.") + __slots__ = ['front','item','dtype','count'] + def __new__(clas, front = -1, item = None, dtype = int): if not _check_type(front, int): raise TypeError("front is not of type int.") if item is None: - item= DynamicOneDimensionalArray(dtype, maxsize) - if not _check_type(item, DynamicOneDimensionalArray): - raise ValueError("item is not of DynamicOneDimensionalArray type") + item= DynamicOneDimensionalArray(dtype, 0) + else: + item= DynamicOneDimensionalArray(dtype, item) obj = object.__new__(clas) - obj.maxsize, obj.front, obj.item, obj.dtype, obj.count = maxsize, front, item, items._dtype, count + obj.front, obj.item, obj.dtype = front, item, items._dtype return obj def append(self, x): if self.front == -1: self.front=0 - self.item.apped[self.dtype(x)] - count+=1 + self.item.append(x) def popleft(self): if (self.front == -1): raise ValueError("Queue is empty.") - r = self.item[self.front] - self.item.delete[self.front] + r = dc(self.item[self.front]) + self.item.delete(self.front) self.front += 1 return r - def __len__(self): - if self.front== -1: - return 0 - else: - return (self.count- self.front) + def __str__(self): + """ + Used for printing. + """ + return str(self.item._data) From 6e31b53deed9cb2e482f708a9ea59be1e3b2a25c Mon Sep 17 00:00:00 2001 From: Utkarsh Mishra <35002267+utkarsh0702@users.noreply.github.com> Date: Wed, 1 Jan 2020 19:33:20 +0530 Subject: [PATCH 09/13] added dynamic 1d array to queue From fa2ee9ac1543597cfbbeb642e1bd2b564d14e3f1 Mon Sep 17 00:00:00 2001 From: Utkarsh Mishra <35002267+utkarsh0702@users.noreply.github.com> Date: Wed, 1 Jan 2020 22:14:05 +0530 Subject: [PATCH 10/13] Updated the queue.py acc. to Dynamic 1d --- .../miscellaneous_data_structures/queue.py | 22 +++++++++++++------ 1 file changed, 15 insertions(+), 7 deletions(-) diff --git a/pydatastructs/miscellaneous_data_structures/queue.py b/pydatastructs/miscellaneous_data_structures/queue.py index a222586a7..1f8e7da09 100644 --- a/pydatastructs/miscellaneous_data_structures/queue.py +++ b/pydatastructs/miscellaneous_data_structures/queue.py @@ -6,13 +6,11 @@ _check_type = lambda a, t: isinstance(a, t) NoneType = type(None) -rear= -1 class Queue(objects): def __new__(clas, implementation='array', **kwargs): if implementation == 'array': return ArrayQueue( - kwargs.get('front', -1), kwargs.get('item', None), kwargs.get('dtype', int)) raise NotImplementedError("%s hasn't been implemented yet."%(implementation)) @@ -23,25 +21,29 @@ def append(self, *args, **kwargs): def popleft(self, **kwargs): raise NotImplementedError("This is an abstract method.") + def __len__(self, **kwargs): + raise NotImplementedError("This is an abstract method.") + class ArrayQueue(Queue): - __slots__ = ['front','item','dtype','count'] - def __new__(clas, front = -1, item = None, dtype = int): - if not _check_type(front, int): - raise TypeError("front is not of type int.") + __slots__ = ['item','dtype'] + front= -1 + count= 0 + def __new__(clas, item = None, dtype = int): if item is None: item= DynamicOneDimensionalArray(dtype, 0) else: item= DynamicOneDimensionalArray(dtype, item) obj = object.__new__(clas) - obj.front, obj.item, obj.dtype = front, item, items._dtype + obj.item, obj.dtype = item, items._dtype return obj def append(self, x): if self.front == -1: self.front=0 self.item.append(x) + self.count+=1 def popleft(self): if (self.front == -1): @@ -50,6 +52,12 @@ def popleft(self): self.item.delete(self.front) self.front += 1 return r + + def __len__(self): + if (self.front == -1): + return 0 + else: + return (self.count- self.front) def __str__(self): """ From 8a295ff44fe68f79985144c98d72f06a393f638e Mon Sep 17 00:00:00 2001 From: Utkarsh Mishra <35002267+utkarsh0702@users.noreply.github.com> Date: Wed, 1 Jan 2020 22:21:26 +0530 Subject: [PATCH 11/13] Update __init__.py --- pydatastructs/miscellaneous_data_structures/__init__.py | 6 +----- 1 file changed, 1 insertion(+), 5 deletions(-) diff --git a/pydatastructs/miscellaneous_data_structures/__init__.py b/pydatastructs/miscellaneous_data_structures/__init__.py index 12321f19c..421291405 100644 --- a/pydatastructs/miscellaneous_data_structures/__init__.py +++ b/pydatastructs/miscellaneous_data_structures/__init__.py @@ -1,11 +1,7 @@ __all__ = [] from . import ( - stack, - - queue, - - binomial_trees + queue ) From 36c10b971de3fd67c462ececc7154acc5037cb9e Mon Sep 17 00:00:00 2001 From: Utkarsh Mishra <35002267+utkarsh0702@users.noreply.github.com> Date: Thu, 2 Jan 2020 17:39:58 +0530 Subject: [PATCH 12/13] Update __init__.py --- pydatastructs/miscellaneous_data_structures/__init__.py | 2 ++ 1 file changed, 2 insertions(+) diff --git a/pydatastructs/miscellaneous_data_structures/__init__.py b/pydatastructs/miscellaneous_data_structures/__init__.py index 421291405..fe36a1bb9 100644 --- a/pydatastructs/miscellaneous_data_structures/__init__.py +++ b/pydatastructs/miscellaneous_data_structures/__init__.py @@ -1,6 +1,8 @@ __all__ = [] from . import ( + stack, + binomial_trees, queue ) From 46820079da0471d11868d57c535b25b24834a0ec Mon Sep 17 00:00:00 2001 From: Utkarsh Mishra <35002267+utkarsh0702@users.noreply.github.com> Date: Thu, 2 Jan 2020 17:41:59 +0530 Subject: [PATCH 13/13] Update __init__.py --- pydatastructs/miscellaneous_data_structures/__init__.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/pydatastructs/miscellaneous_data_structures/__init__.py b/pydatastructs/miscellaneous_data_structures/__init__.py index fe36a1bb9..d23ae4fc2 100644 --- a/pydatastructs/miscellaneous_data_structures/__init__.py +++ b/pydatastructs/miscellaneous_data_structures/__init__.py @@ -13,11 +13,11 @@ __all__.extend(binomial_trees.__all__) from .stack import ( - Stack, + Stack ) __all__.extend(stack.__all__) from .queue import ( - Queue, + Queue ) __all__.extend(queue.__all__)