Lisp - Fill Pointers in Arrays



Fill-Pointer is very useful construct while dealing with adjustable arrays. A fill pointers helps to manage the active portion of an array. It allows to treat a large array, like it is a smaller while touching the physical storage.

What is fill pointer?

Fill pointer is a logical marker to mark the size of an array.

  • Consider an array of 100 slots where only first 5 elements are present. Using fill pointer, we can set the array as of size 5 easily.

  • Fill pointer can always be lesser or same as an array size.

  • A fill pointer denotes the active length of an array. Accessing an index more than fill pointer may return a garbage value.

When to use a fill pointer?

We can utilize a fill pointer in many useful scenarios like following−

  • Efficient Resizing− In case we need to reduce size of an array, we can simply set the fill pointer to a lesser value without removing element or reallocating memory.

  • Dynamic Data− In case of dynamic/adjustable arrays, where we need to add/remove elements frequently, fill pointer is very useful to program such activities.

  • Controlled Access− List functions on arrays works on active elements only which we can specify using fill-pointer. This helps to limit the scope of array operations.

Fill Pointer Operations

Creating array with Fill Pointer

Following code shows how to create an adjustable array of size 5 with each element initialized with nil.

; define an adjustable array
(defvar my-dynamic-array (make-array 5 :adjustable t :fill-pointer t :initial-element nil))

Getting Fill Pointer

Following code shows how to get the current fill pointer. fill-pointer returns the current fill pointer of the array.

(fill-pointer my-dynamic-array)

Setting Fill Pointer

Following code shows how to set the fill pointer. Using setf we can set the fill pointer as required.

; set a new value to fill pointer
(setf (fill-pointer my-dynamic-array) 3)

Checking Fill Pointer

Following code shows how to check if an array supports fill pointer or not. array-has-fill-pointer-p function checks the provided array for fill-pointer.

; check if array has fill pointer
(array-has-fill-pointer-p my-dynamic-array)

Example - Fill Pointer

Following example shows the usage of Fill Pointer.

main.lisp

; define an adjustable array
(defvar my-dynamic-array (make-array 5 :adjustable t :fill-pointer t :initial-element nil))

; add values to the array 
(setf (aref my-dynamic-array 0) 'a)  
(setf (aref my-dynamic-array 1) 'b)  
(setf (aref my-dynamic-array 2) 'c)  
(setf (aref my-dynamic-array 3) 'd)
(setf (aref my-dynamic-array 4) 'e)

 ; Output: #(A B C D E)
(print my-dynamic-array)        

(terpri)
; get current fill pointer
(write(fill-pointer my-dynamic-array))

; set a new value to fill pointer
(setf (fill-pointer my-dynamic-array) 3)

(terpri)
; get current fill pointer
(write(fill-pointer my-dynamic-array))

 ; Output: #(A B C)
(print my-dynamic-array)        

(terpri)
; check if array has fill pointer
(write (array-has-fill-pointer-p my-dynamic-array))

Output

When you execute the code, it returns the following result −

#(A B C D E) 
5
3
#(A B C) 
T
Advertisements