Recommend this page to a friend! |
Download .zip |
Info | Example | View files (5) | Download .zip | Reputation | Support forum | Blog | Links |
Last Updated | Ratings | Unique User Downloads | Download Rankings | |||||
2022-10-21 (7 days ago) | Not enough user ratings | Total: 97 | All time: 9,698 This week: 149 |
Version | License | PHP version | Categories | |||
circular-buffer 1.0.2 | GNU General Publi... | 5.5 | PHP 5, Data types |
Description | Author | |
This class can store values in a circular buffer array. |
The CircularBuffer class implements a circular buffer, ie a buffer that can store a limited number of items. If you add items that exceed the capacity of the circular buffer, then the older items will be "forgotten" to leave room to the ones you want to add.
Circular buffers are often used when you have to face the following trade-offs :
As time goes by, you can accept to discard some of the oldest data you're storing into the circular buffer
## Creating a circular buffer ##
Creating a circular buffer is easy ; just fix yourself a limit on the number of items you want to store :
$buffer = new CircularBuffer ( 1000 ) ; // The circular buffer will be able to store at most 1000 elements
Adding items is easy ; just use the circular buffer as an array to append your latest items :
$buffer [] = "This is a log message" ;
Since the CircularBuffer class implements the ArrayAccess, Coutable and IteratorAggregate interface, you can perform the following operations :
The CircularBuffer class constructor has 3 different signatures, described in the following sections :
Creates a circular buffer that is able to store $size elements.
Creates a circular buffer and initializes it with the contents of $array.
The size of the circular buffer will be the size of the specified array.
Creates a circular buffer and initializes it with the contents of $array.
The circular buffer will have $size size elements. If $size is less than the number of items in $array, the circular buffer will be initialized with its first $size elements. If $size is greater, then emty elements will be added to the circular buffer.
Empties the contents of the circular buffer.
Files |
File | Role | Description |
---|---|---|
CircularBuffer.phpclass | Class | Class source |
example.php | Example | Example script |
LICENSE | Lic. | License text |
NOTICE | Data | Auxiliary data |
README.md | Doc. | Documentation |
Version Control | Unique User Downloads | Download Rankings | |||||||||||||||
100% |
|
|
Applications that use this package |
If you know an application of this package, send a message to the author to add a link here.