Click here to Skip to main content
13,861,061 members
Rate this:
 
Please Sign up or sign in to vote.
See more:
Hey i was just looking for a class to construct array and adjust it with elements.And i found an array constructor , But there are few lines of codes which i did,nt understand so i am looking for someone who can explain me what,s happening at that code and what that does and why is that line of code written there.I will write "Make me understand" before that codeline which i don,t understand So LET,S BEGIN...


import ctypes

class DynamicArray(object):
'''
DYNAMIC ARRAY CLASS (Similar to Python List)
'''

def __init__(self):
self.n = 0 # Count actual elements (Default is 0)
self.capacity = 1 # Default Capacity
self.A = self.make_array(self.capacity)

def __len__(self):
"""
Return number of elements sorted in array
"""
return self.n

def __getitem__(self,k):
"""
Return element at index k
"""
if not 0 <= k <self.n:
return IndexError('K is out of bounds!') # Check it k index is in bounds of array

return self.A[k] #Retrieve from array at index k

def append(self, ele):
"""
Add element to end of the array
"""
if self.n == self.capacity:
self._resize(2*self.capacity) #Double capacity if not enough room

"Make me understand" self.A[self.n] = ele #Set self.n index to element
"Make me understand" self.n += 1

def _resize(self,new_cap):
"""
Resize internal array to capacity new_cap
"""

B = self.make_array(new_cap) # New bigger array

for k in range(self.n): # Reference all existing values
B[k] = self.A[k]

"Make me understand" self.A = B # Call A the new bigger array
"Make me understand" self.capacity = new_cap # Reset the capacity

def make_array(self,new_cap):
"""
Returns a new array with new_cap capacity
"""
"Make me understand" return (new_cap * ctypes.py_object)()

What I have tried:

Although I first came to here because i hope here are some good people who can be my coding mentor..
Posted
Updated 19-Jan-19 0:01am

1 solution

Rate this: bad
 
good
Please Sign up or sign in to vote.

Solution 1

def append(self, ele):
    """
    Add element to end of the array
    """
    if self.n == self.capacity:
    self._resize(2*self.capacity) #Double capacity if not enough room
    
    self.A[self.n] = ele #Set self.n index to element --> 1
    self.n += 1          #                            --> 2

1. The parameter object ele will be stored in the array A at offset n. If the value of n is greater than the number of elements of A-1 then an exception will be thrown.
2. The value of n is increased by 1, in order to point to the next free element in the array A.

B = self.make_array(new_cap) # New bigger array

for k in range(self.n): # Reference all existing values
B[k] = self.A[k]

self.A = B # Call A the new bigger array      --> 1
self.capacity = new_cap # Reset the capacity  --> 2

def make_array(self,new_cap):
    """
    Returns a new array with new_cap capacity
    """
    return (new_cap * ctypes.py_object)() #   --> 3

1. Set the reference A to point to the newly created array B. So all references will access the data in the new array.
2. Set the variable capacity to the value of the parameter new_cap. That is just replacing one value in a variable with another (similar to i = 5).
3. The method make_array is creating a new raw array that can hold up to new_cap ctypes.py_object objects. The array reference is then passed back to the caller of the method, as referred to in lines 1 and 2.

See also The Python Tutorial — Python 3.7.2 documentation[^].
   

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
Top Experts
Last 24hrsThis month


Advertise | Privacy | Cookies | Terms of Service
Web02 | 2.8.190214.1 | Last Updated 19 Jan 2019
Copyright © CodeProject, 1999-2019
All Rights Reserved.
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100