-
Notifications
You must be signed in to change notification settings - Fork 313
Multi-dimensional array added #256
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
Changes from 11 commits
6adc171
0147664
41e4126
6a51719
bf55961
c736ee5
65365ec
ed73085
c84fd4d
96fe82f
464c05f
7ea7a43
d35900f
a8b3340
5507c58
ecd5d20
4b04734
7211a98
d8db935
d6c1f14
bcb2ee6
8e1b86f
db6fd14
e7a10cb
0dc9c6c
388abf1
f176f46
b842d54
cce3f28
a25e465
3427792
427fd5b
c306463
19ceaad
7027671
e691e93
17ee7a9
806965e
a204a64
b8db3e5
7a780c7
9b502d7
176afd9
8a6bba6
2abae14
5145abd
182f8f7
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||
|---|---|---|---|---|---|---|
| @@ -1,16 +1,19 @@ | ||||||
| from pydatastructs.utils.misc_util import _check_type, NoneType | ||||||
|
|
||||||
| __all__ = [ | ||||||
| 'OneDimensionalArray', | ||||||
| 'DynamicOneDimensionalArray' | ||||||
| 'OneDimensionalArray', | ||||||
| 'MultiDimensionalArray', | ||||||
| 'DynamicOneDimensionalArray' | ||||||
| ] | ||||||
|
|
||||||
|
|
||||||
| class Array(object): | ||||||
| ''' | ||||||
| Abstract class for arrays in pydatastructs. | ||||||
| ''' | ||||||
| pass | ||||||
|
|
||||||
|
|
||||||
| class OneDimensionalArray(Array): | ||||||
| ''' | ||||||
| Represents one dimensional arrays. | ||||||
|
|
@@ -68,18 +71,18 @@ class OneDimensionalArray(Array): | |||||
| def __new__(cls, dtype=NoneType, *args, **kwargs): | ||||||
| if dtype is NoneType or len(args) not in (1, 2): | ||||||
| raise ValueError("1D array cannot be created due to incorrect" | ||||||
| " information.") | ||||||
| " information.") | ||||||
| obj = Array.__new__(cls) | ||||||
| obj._dtype = dtype | ||||||
| if len(args) == 2: | ||||||
| if _check_type(args[0], list) and \ | ||||||
| _check_type(args[1], int): | ||||||
| _check_type(args[1], int): | ||||||
| for i in range(len(args[0])): | ||||||
| if _check_type(args[0][i], dtype) is False: | ||||||
| args[0][i] = dtype(args[0][i]) | ||||||
| size, data = args[1], [arg for arg in args[0]] | ||||||
| elif _check_type(args[1], list) and \ | ||||||
| _check_type(args[0], int): | ||||||
| _check_type(args[0], int): | ||||||
| for i in range(len(args[1])): | ||||||
| if _check_type(args[1][i], dtype) is False: | ||||||
| args[1][i] = dtype(args[1][i]) | ||||||
|
|
@@ -89,7 +92,7 @@ def __new__(cls, dtype=NoneType, *args, **kwargs): | |||||
| "expected type of data is list/tuple.") | ||||||
| if size != len(data): | ||||||
| raise ValueError("Conflict in the size %s and length of data %s" | ||||||
| %(size, len(data))) | ||||||
| % (size, len(data))) | ||||||
| obj._size, obj._data = size, data | ||||||
|
|
||||||
| elif len(args) == 1: | ||||||
|
|
@@ -102,7 +105,7 @@ def __new__(cls, dtype=NoneType, *args, **kwargs): | |||||
| if _check_type(args[0][i], dtype) is False: | ||||||
| args[0][i] = dtype(args[0][i]) | ||||||
| obj._size, obj._data = len(args[0]), \ | ||||||
| [arg for arg in args[0]] | ||||||
| [arg for arg in args[0]] | ||||||
czgdp1807 marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||||||
| else: | ||||||
| raise TypeError("Expected type of size is int and " | ||||||
| "expected type of data is list/tuple.") | ||||||
|
|
@@ -133,13 +136,108 @@ 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. | ||||||
| size: int | ||||||
| The number of elements in the array. | ||||||
|
|
||||||
| Raises | ||||||
| ====== | ||||||
| IndexError | ||||||
| Index goes out of boundaries | ||||||
| ValueError | ||||||
| When there's no dimensions or the dimension size is 0 | ||||||
| TypeError | ||||||
| An argument is not what expected | ||||||
|
|
||||||
| 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.2 | ||||||
| >>> arr[3][0][0] | ||||||
| 7 | ||||||
|
|
||||||
| References | ||||||
| ========== | ||||||
|
|
||||||
| .. [1] https://en.wikipedia.org/wiki/Array_data_structure#Multidimensional_arrays | ||||||
| ''' | ||||||
| __slots__ = ['_size', '_data', '_dtype'] | ||||||
|
|
||||||
| def __new__(cls, dtype=NoneType, *args, **kwargs): | ||||||
| if dtype is NoneType or len(args) == (0): | ||||||
czgdp1807 marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||||||
| raise ValueError("array cannot be created due to incorrect" | ||||||
JeanPierreMR marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||||||
| " information.") | ||||||
| dimensions = list(args) | ||||||
|
||||||
| dimensions = list(args) | |
| dimensions = args |
JeanPierreMR marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
JeanPierreMR marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
czgdp1807 marked this conversation as resolved.
Show resolved
Hide resolved
czgdp1807 marked this conversation as resolved.
Show resolved
Hide resolved
JeanPierreMR marked this conversation as resolved.
Show resolved
Hide resolved
Outdated
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Irrelevant change.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
+1
Uh oh!
There was an error while loading. Please reload this page.