Open In App

Fill Pointers in LISP

Last Updated : 12 Dec, 2022
Improve
Improve
Like Article
Like
Save
Share
Report

A fill pointer is a special kind of pointer used in some Lisp implementations to indicate the end of the accessible part of a vector. It is not necessarily the end of the vector itself; the total size of the vector may be larger. The fill pointer can be moved with certain functions to make more or less of the vector accessible, effectively changing its size. Vectors are a data structure that can be used to store an ordered collection of elements. In many programming languages, including Lisp, vectors are implemented as arrays. An array is a contiguous block of memory that holds a fixed number of elements of the same type, each element in the array is accessed by its index, which is an integer offset from the beginning of the array.

The size of an array is determined when it is created and cannot be changed without creating a new array. This can be inefficient if the size of the data set stored in the array changes frequently. Vectors address this problem by allowing their size to be changed dynamically. A fill pointer is used to keep track of which parts of a vector have been initialized with data. It points to the next available slot in the vector, i.e., the place where new data will be inserted when it is added. When data is removed from a vector, the fill pointer moves accordingly so that it always points to the next available slot. Functions that add or remove data from vectors automatically update the fill pointer as needed.

How To Use Fill Pointers in LISP:

If you are working with arrays in LISP, you may need to use fill pointers. Fill pointers indicate the end of an array, and can be used to prevent overwriting data or to make sure that all data is properly stored.

To set a fill pointer, use the setf function:

Lisp




(setf my-array (make-array 10 :fill-pointer 5))


This sets the fill pointer of my-array to 5, indicating that only the first 5 elements of the array should be used. You can also use fill pointers when creating new arrays:

Lisp




(make-array 10 :fill-pointer 5)


When using fill pointers, you need to be careful not to overwrite existing data. If you try to write past the end of an array, you will get an error:

Lisp




(setf (aref my-array 5) 1) ;error!
Lisp>


In this case, you would need to increase the size of my-array before setting the element at index 5. You can do this with the adjust-array function:

Lisp




Lisp>
(adjust-array my-array :fill-pointer 10)


Now my-array can hold 10 elements, and we can set the element at index 5 without getting an error:

Lisp




(setf (aref my-array 5) 1)


Why you should use Fill Pointers in LISP

Fill pointers are a powerful tool in LISP that can help improve the performance of your code. By using fill pointers, you can ensure that your code only allocates the necessary amount of memory for each data structure. This can help reduce memory usage and improve the speed of your code.

How to get started with Fill Pointers in LISP

Fill pointers in LISP are a way of indicating the end of a variable-size array. They are often used when working with dynamic arrays, as they can be used to automatically increase the size of an array when more elements are added.

To get started with fill pointers in LISP, you first need to create a dynamic array. This can be done using the make-array function:

Lisp




(setf (aref my-array 5) 1)


Once you have created a dynamic array, you can then add elements to it using the setf function:

Lisp




(write(setf my-array (make-array 10 :fill-pointer 5)))
(setf (aref my-array 0) 1
(setf (aref my-array 1) 2
(write my-array)


Output:

 

Now that you have created a dynamic array and added some elements to it, you can set its fill pointer using the setf function: (setf (fill-pointer my-array) 10) This tells LISP that there are 10 elements currently in use in the array. You can also use the fill-pointer function to check what the current fill pointer is for an array: (fill-pointer my-array) Finally, you can remove elements from a dynamic array by resetting its fill



Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads