Overview
TheCircularArray class extends the Array class to provide a circular (ring) buffer implementation. When the array reaches capacity, new elements wrap around and overwrite the oldest elements. This is useful for implementing fixed-size buffers, recent history tracking, and circular queues.
Special Methods
- Index Operator:
array[index]- Access elements by index (logical, not physical) - Assignment:
array[index] = value- Set values at specific indices (logical, not physical)
Key Differences from Array
- Wrap-around behavior: When full,
append()overwrites the oldest element instead of raising an exception - Logical indexing: Index 0 always refers to the oldest element, regardless of physical position
- Fixed capacity: Like
Array, but handles overflow gracefully
Inheritance
CircularArray inherits from Array and overrides key methods to provide circular behavior:
- Inherits:
__len__,__repr__,__eq__,is_empty,capacity,shift_right,shift_left,from_list - Overrides:
__init__,__getitem__,__setitem__,append,insert,delete,to_list - Adds:
raw_view
Constructor
__init__(contents=None, capacity: int=10)
Initialize the circular array with optional contents and a fixed capacity.
An optional iterable to fill the array with initial values. If the length of contents exceeds the specified capacity, only the last
capacity elements will be retained due to wrap-around.The fixed size of the circular array. Determines the maximum number of elements that can be stored before wrap-around occurs.
Implementation Details
Implementation Details
The constructor maintains an internal
_start index that tracks the position of the oldest element. This allows the array to wrap around efficiently without shifting all elements.Methods (Overridden)
append(element)
Append an element to the circular array. If appending exceeds capacity, it will wrap around and overwrite the oldest element.
The element to append to the array.
Difference from Array
Difference from Array
Unlike
Array.append() which raises an exception when capacity is exceeded, CircularArray.append() overwrites the oldest element and advances the start pointer.insert(index: int, element)
Insert an element at a specified index, shifting existing elements to the right.
The logical index at which to insert the element. Must be between 0 and count (inclusive).
The element to insert.
IndexError: If the index is out of boundsException: If the array is full
Difference from Array
Difference from Array
Unlike
Array.insert(), this method accounts for the circular nature of the array and uses modulo arithmetic to handle the wrap-around when shifting elements.delete(index: int)
Delete an element at a specified index, shifting subsequent elements to the left.
The logical index of the element to delete. Must be between 0 and count - 1.
IndexError: If the index is out of bounds
Difference from Array
Difference from Array
This method accounts for the circular nature of the array and uses modulo arithmetic to handle the wrap-around when shifting elements to the left.
__getitem__(index: int)
Retrieve the element at the specified logical index. Enables the use of bracket notation array[index].
The logical index of the element to retrieve. Index 0 always refers to the oldest element. Must be between 0 and count - 1.
The element at the specified logical index.
IndexError: If the index is out of bounds
Logical vs Physical Indexing
Logical vs Physical Indexing
The circular array maintains a
_start pointer to track the oldest element. When you access array[0], it translates to the physical index (self._start + 0) % capacity. This ensures that logical index 0 always refers to the oldest element, regardless of where it’s physically stored.__setitem__(index: int, value)
Set a new value at the specified logical index. Enables the use of bracket notation array[index] = value.
The logical index at which to set the value. Must be between 0 and count - 1.
The new value to assign.
IndexError: If the index is out of bounds
to_list() -> list
Convert the circular array’s elements to a standard Python list in logical order.
A list containing the elements of the array in logical order (oldest to newest), regardless of their physical positions.
Difference from Array
Difference from Array
Unlike
Array.to_list() which simply slices the internal array, CircularArray.to_list() must iterate through the elements using modulo arithmetic to produce them in the correct logical order.Additional Methods
raw_view() -> list
Return a raw view of the internal array, showing the physical storage layout.
The internal array as-is, including the physical positions and any
None values.Use Case
Use Case
This method is primarily useful for debugging and understanding how the circular array stores elements internally. It shows the actual physical layout rather than the logical order.
Inherited Methods
The following methods are inherited from theArray class and work identically:
Utility Methods
is_empty() -> bool- Check if the array is emptycapacity() -> int- Get the total capacityfrom_list(mylist: list)- Create a CircularArray from a Python list (classmethod)
Helper Methods
shift_right(start: int)- Shift elements right from a start indexshift_left(start: int)- Shift elements left from a start indexextend(array)- Append multiple elements (uses overriddenappend)
Special Methods
__len__() -> int- Get the number of elements usinglen(array)__repr__() -> str- String representation of the array__eq__(other) -> bool- Compare arrays for equality based on logical order
View Full Documentation
View Full Documentation
For detailed documentation of inherited methods, see the Array reference page.
Attributes
count
The number of elements currently stored in the array. This value is capped at the capacity.
_start
Internal attribute tracking the physical index of the oldest element in the circular array. This is updated automatically when wrap-around occurs.
Internal Implementation Detail
Internal Implementation Detail
While
_start is accessible, it’s an internal implementation detail primarily used by the class methods. Direct modification of this attribute is not recommended.Performance Characteristics
Time Complexity Summary
| Operation | Time Complexity |
|---|---|
append | O(1) |
extend | O(m) where m is elements to add |
insert | O(n) |
delete | O(n) |
__getitem__ | O(1) |
__setitem__ | O(1) |
is_empty | O(1) |
capacity | O(1) |
to_list | O(n) |
raw_view | O(1) |
Space Complexity
TheCircularArray maintains O(capacity) space complexity, which is fixed at initialization. Unlike DynamicArray, it never grows or shrinks.