Implementation of Dynamic Array in Python
What is a dynamic array?
A dynamic array is similar to an array, but with the difference that its size can be dynamically modified at runtime. Don’t need to specify how much large an array beforehand. The elements of an array occupy a contiguous block of memory, and once created, its size cannot be changed. A dynamic array can, once the array is filled, allocate a bigger chunk of memory, copy the contents from the original array to this new space, and continue to fill the available slots.
We’ll be using a built in library called ctypes of python . Check out the documentation for more info, but its basically going to be used here as a raw array from the ctypes module.
A quick note on public vs private methods, we can use an underscore _ before the method name to keep it non-public.
For example:
Python
class M( object ):
def public( self ):
print 'Use Tab to see me !'
def _private( self ):
print "You won't be able to Tab to see me !"
|
Output:
Use Tab to see me!
Output:
You won't be able to see me!
Dynamic Array Logic Implementation:
The key is to provide means to grows an array A that stores the elements of a list. We can’t actually grow the array, its capacity is fixed. If an element is appended to a list at a time, when the underlying array is full, we need to perform following steps.
- Allocate a new array B with larger capacity (A commonly used rule for the new array is to have twice the capacity of the existing array )
- Set B[i]=A[i], for i=0 to n-1 where n denotes the current no of items.
- Set A=B that is, we hence forth use B as the array of supporting list.
- Insert new element in the new array.
Dynamic Array Code Implementation:
Python3
import ctypes
class DynamicArray( object ):
def __init__( self ):
self .n = 0
self .capacity = 1
self .A = self .make_array( self .capacity)
def __len__( self ):
return self .n
def __getitem__( self , k):
if not 0 < = k < self .n:
return IndexError( 'K is out of bounds !' )
return self .A[k]
def append( self , ele):
if self .n = = self .capacity:
self ._resize( 2 * self .capacity)
self .A[ self .n] = ele
self .n + = 1
def insertAt( self , item, index):
if index < 0 or index > self .n:
print ( "please enter appropriate index.." )
return
if self .n = = self .capacity:
self ._resize( 2 * self .capacity)
for i in range ( self .n - 1 , index - 1 , - 1 ):
self .A[i + 1 ] = self .A[i]
self .A[index] = item
self .n + = 1
def delete( self ):
if self .n = = 0 :
print ( "Array is empty deletion not Possible" )
return
self .A[ self .n - 1 ] = 0
self .n - = 1
def removeAt( self , index):
if self .n = = 0 :
print ( "Array is empty deletion not Possible" )
return
if index < 0 or index > = self .n:
return IndexError( "Index out of bound....deletion not possible" )
if index = = self .n - 1 :
self .A[index] = 0
self .n - = 1
return
for i in range (index, self .n - 1 ):
self .A[i] = self .A[i + 1 ]
self .A[ self .n - 1 ] = 0
self .n - = 1
def _resize( self , new_cap):
B = self .make_array(new_cap)
for k in range ( self .n):
B[k] = self .A[k]
self .A = B
self .capacity = new_cap
def make_array( self , new_cap):
return (new_cap * ctypes.py_object)()
arr = DynamicArray()
arr.append( 1 )
arr.append( 2 )
arr.append( 3 )
print ( len (arr))
print (arr[ 1 ])
print (arr[ 2 ])
arr.removeAt( 2 )
print ( len (arr))
print (arr[ 1 ])
|
Output:
3
2
3
2
2
Last Updated :
12 May, 2023
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...