Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Multi-dimensional array added #256

Merged
merged 47 commits into from
May 15, 2020
Merged
Show file tree
Hide file tree
Changes from 41 commits
Commits
Show all changes
47 commits
Select commit Hold shift + click to select a range
6adc171
Initial structure for multiDimensional Array
JeanPierreMR Apr 6, 2020
0147664
git ignore update
JeanPierreMR Apr 7, 2020
41e4126
Update arrays.py
JeanPierreMR Apr 9, 2020
6a51719
Changed MultiDimensionalArray
JeanPierreMR Apr 9, 2020
bf55961
Added Multi-dimensional array
JeanPierreMR Apr 9, 2020
c736ee5
Updated arrays.py
JeanPierreMR Apr 9, 2020
65365ec
Update test_arrays.py
JeanPierreMR Apr 13, 2020
ed73085
Update arrays.py
JeanPierreMR Apr 15, 2020
c84fd4d
Update test_arrays.py
JeanPierreMR Apr 15, 2020
96fe82f
Update arrays.py
JeanPierreMR Apr 15, 2020
464c05f
Merge remote-tracking branch 'upstream/master'
JeanPierreMR Apr 15, 2020
7ea7a43
Update arrays.py
JeanPierreMR Apr 15, 2020
d35900f
Adding Multidimensional array
JeanPierreMR Apr 17, 2020
a8b3340
Update Multidimensional aray
JeanPierreMR Apr 17, 2020
5507c58
Update arrays.py
JeanPierreMR Apr 17, 2020
ecd5d20
Merge branch 'MultiDimensionalArrayPlain'
JeanPierreMR Apr 17, 2020
4b04734
Update arrays.py
JeanPierreMR Apr 17, 2020
7211a98
Update pydatastructs/linear_data_structures/__init__.py
JeanPierreMR Apr 18, 2020
d8db935
Update pydatastructs/linear_data_structures/arrays.py
JeanPierreMR Apr 18, 2020
d6c1f14
Update arrays.py
JeanPierreMR Apr 18, 2020
bcb2ee6
Apply suggestions from code review
JeanPierreMR Apr 18, 2020
8e1b86f
Updated test for multi dimensional array
JeanPierreMR Apr 18, 2020
db6fd14
Update arrays.py
JeanPierreMR Apr 18, 2020
e7a10cb
Update test_arrays.py
JeanPierreMR Apr 18, 2020
0dc9c6c
Minor update
JeanPierreMR Apr 18, 2020
388abf1
Minor changes
JeanPierreMR Apr 20, 2020
f176f46
Apply suggestions from code review
JeanPierreMR Apr 30, 2020
b842d54
Update arrays.py
JeanPierreMR Apr 30, 2020
cce3f28
Update arrays.py
JeanPierreMR Apr 30, 2020
a25e465
Update test_arrays.py
JeanPierreMR May 4, 2020
3427792
Update test_code_quality.py
JeanPierreMR May 4, 2020
427fd5b
Update pydatastructs/linear_data_structures/arrays.py
JeanPierreMR May 4, 2020
c306463
Update pydatastructs/linear_data_structures/arrays.py
JeanPierreMR May 4, 2020
19ceaad
Merge branch 'master' of https://github.com/JeanPierreMR/pydatastructs
JeanPierreMR May 4, 2020
7027671
Merge branch 'master' into master
JeanPierreMR May 4, 2020
e691e93
Apply suggestions from code review
czgdp1807 May 5, 2020
17ee7a9
Apply suggestions from code review
czgdp1807 May 5, 2020
806965e
Apply suggestions from code review
czgdp1807 May 5, 2020
a204a64
Apply suggestions from code review
czgdp1807 May 5, 2020
b8db3e5
Update pydatastructs/linear_data_structures/arrays.py
JeanPierreMR May 5, 2020
7a780c7
Update arrays.py
JeanPierreMR May 5, 2020
9b502d7
Apply suggestions from code review
czgdp1807 May 6, 2020
176afd9
Apply suggestions from code review
czgdp1807 May 6, 2020
8a6bba6
Update arrays.py
JeanPierreMR May 9, 2020
2abae14
Update test_arrays.py
JeanPierreMR May 9, 2020
5145abd
code ready
czgdp1807 May 15, 2020
182f8f7
ready for merge
czgdp1807 May 15, 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
3 changes: 2 additions & 1 deletion pydatastructs/linear_data_structures/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,8 @@

from .arrays import (
OneDimensionalArray,
DynamicOneDimensionalArray
DynamicOneDimensionalArray,
MultiDimensionalArray
)
__all__.extend(arrays.__all__)

Expand Down
128 changes: 126 additions & 2 deletions pydatastructs/linear_data_structures/arrays.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
from pydatastructs.utils.misc_util import _check_type, NoneType

__all__ = [
'OneDimensionalArray',
'DynamicOneDimensionalArray'
'OneDimensionalArray',
'MultiDimensionalArray',
'DynamicOneDimensionalArray'
]

class Array(object):
Expand Down Expand Up @@ -139,6 +140,129 @@ def __len__(self):
def __str__(self):
return str(self._data)

class MultiDimensionalArray(Array):
"""
Represents a multi-dimensional array.

Parameters
==========

dtype: type
A valid object type.
*args: int
The dimensions of the array.

Raises
======

IndexError
Index goes out of boundaries, or
the number of index given is not
the same as the number of dimensions.
ValueError
When there's no dimensions or the
dimension size is 0.

Examples
========

>>> from pydatastructs import MultiDimensionalArray as MDA
>>> arr = MDA(int, 5, 6, 9)
>>> arr.fill(32)
>>> arr[3, 0, 0]
32
>>> arr[3, 0, 0] = 7
>>> arr[3, 0, 0]
7

References
==========

.. [1] https://en.wikipedia.org/wiki/Array_data_structure#Multidimensional_arrays

"""
__slots__ = ['_shape', '_data', '_dtype']

def __new__(cls, dtype=NoneType, *args, **kwargs):
if dtype is NoneType or not args:
raise ValueError("Array cannot be created due to incorrect"
" information.")
if len(args) == 1:
obj = Array.__new__(cls)
obj._dtype = dtype
obj._shape = [1]
czgdp1807 marked this conversation as resolved.
Show resolved Hide resolved
obj._data = [None] * args[0]
return obj
czgdp1807 marked this conversation as resolved.
Show resolved Hide resolved

czgdp1807 marked this conversation as resolved.
Show resolved Hide resolved

dimensions = args
for dimension in dimensions:
if dimension < 1:
raise ValueError("Array cannot be created due to incorrect"
" dimensions.")
n_dimensions = len(dimensions)
d_sizes = []
index = 0
while n_dimensions > 1:
size = dimensions[index]
for i in range(index, len(dimensions) - 1):
size = size * dimension
d_sizes.append(size)
n_dimensions -= 1
index += 1
d_sizes.append(dimensions[index])
d_sizes.append(1)
obj = Array.__new__(cls)
obj._dtype = dtype
obj._shape = d_sizes
czgdp1807 marked this conversation as resolved.
Show resolved Hide resolved
obj._data = [None] * obj._shape[1] * dimensions[0]
return obj
czgdp1807 marked this conversation as resolved.
Show resolved Hide resolved

JeanPierreMR marked this conversation as resolved.
Show resolved Hide resolved
@classmethod
def methods(cls):
return ['__new__', '__getitem__', '__setitem__', 'fill']

def __getitem__(self, indices):
self._compare_shape(indices)
if isinstance(indices, int):
return self._data[indices]
position = 0
for i in range(0, len(indices)):
position += self._shape[i+1] * indices[i]
return self._data[position]

def __setitem__(self, indices, element):
self._compare_shape(indices)
if isinstance(indices, int):
self._data[indices] = element
else:
position = 0
for i in range(0, len(indices)):
position += self._shape[i+1] * indices[i]
JeanPierreMR marked this conversation as resolved.
Show resolved Hide resolved
self._data[position] = element

def _compare_shape(self, indices):
if isinstance(indices, int):
if len(self._shape) > 1:
raise IndexError("Shape mismatch, current shape is %s" % self._shape)
return
if len(indices) != len(self._shape) - 1:
raise IndexError("Shape mismatch, current shape is %s" % self._shape)
for i in range(len(indices)):
if indices[i] > self._shape[i]:
raise IndexError("Index out of range.")

def __str__(self):
return str(self._data)

def fill(self, element):
element = self._dtype(element)
for i in range(len(self._data)):
self._data[i] = element

JeanPierreMR marked this conversation as resolved.
Show resolved Hide resolved
@property
def shape(self):
return self._shape

class DynamicArray(Array):
"""
Expand Down
30 changes: 29 additions & 1 deletion pydatastructs/linear_data_structures/tests/test_arrays.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
from pydatastructs.linear_data_structures import (
OneDimensionalArray, DynamicOneDimensionalArray)
OneDimensionalArray, DynamicOneDimensionalArray, MultiDimensionalArray)
from pydatastructs.utils.raises_util import raises


JeanPierreMR marked this conversation as resolved.
Show resolved Hide resolved
Expand All @@ -21,6 +21,34 @@ def test_OneDimensionalArray():
assert raises(TypeError, lambda: ODA(int, set([1, 2, 3])))
assert raises(ValueError, lambda: ODA(int, 3, [1]))


def test_MultiDimensionalArray():
array = MultiDimensionalArray(int, 5, 9, 3, 8)
czgdp1807 marked this conversation as resolved.
Show resolved Hide resolved
array.fill(5)
array[1, 3, 2, 5] = 2.0
assert array
assert array[1, 3, 2, 5] == 2.0
assert array[1, 3, 0, 5] == 5
assert array[1, 2, 2, 5] == 5
assert array[2, 3, 2, 5] == 5
assert raises(IndexError, lambda: array[5])
assert raises(IndexError, lambda: array[4, 10])
assert raises(IndexError, lambda: array[-1])
assert raises(ValueError, lambda: MultiDimensionalArray())
assert raises(ValueError, lambda: MultiDimensionalArray(int))
assert raises(TypeError, lambda: MultiDimensionalArray(int, 5, 6, ""))
array = MultiDimensionalArray(int, 3, 2, 2)
array.fill(1)
array[0, 0, 0] = 0
array[0, 0, 1] = 0
array[1, 0, 0] = 0
array[2, 1, 1] = 0
assert str(array._data) == '[0, 0, 1, 1, 0, 1, 1, 1, 1, 1, 1, 0]'
czgdp1807 marked this conversation as resolved.
Show resolved Hide resolved
array = MultiDimensionalArray(int, 4)
array.fill(5)
array[3] = 3
assert array[3] == 3

def test_DynamicOneDimensionalArray():
DODA = DynamicOneDimensionalArray
A = DODA(int, 0)
Expand Down
2 changes: 1 addition & 1 deletion pydatastructs/utils/tests/test_code_quality.py
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ def _apis():
pyds.DoublyLinkedList, pyds.SinglyLinkedList,
pyds.SinglyCircularLinkedList,
pyds.DoublyCircularLinkedList,
pyds.OneDimensionalArray,
pyds.OneDimensionalArray, pyds.MultiDimensionalArray,
pyds.DynamicOneDimensionalArray,
pyds.trees.BinaryTree, pyds.BinarySearchTree,
pyds.AVLTree, pyds.SplayTree, pyds.BinaryTreeTraversal,
Expand Down