Download the PHP package adambenovic/shipmonk-sorted-linked-list without Composer
On this page you can find all versions of the php package adambenovic/shipmonk-sorted-linked-list. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Download adambenovic/shipmonk-sorted-linked-list
More information about adambenovic/shipmonk-sorted-linked-list
Files in adambenovic/shipmonk-sorted-linked-list
Package shipmonk-sorted-linked-list
Short Description A type-safe sorted linked list for PHP 8.4+ that holds either int or string values, maintaining sorted order on insertion.
License MIT
Informations about the package shipmonk-sorted-linked-list
SortedLinkedList
A type-safe sorted linked list library for PHP 8.4+. Holds either int or string values (never both in the same instance) and maintains ascending order on every insertion.
Requirements
- PHP 8.4 or higher
Installation
Quick Start
Once the first value is inserted, the type is locked:
Creating Lists
Typed Convenience Classes
For stricter static analysis, use the type-specific classes directly:
API
| Method | Description | Complexity |
|---|---|---|
insert($value): void |
Add a value, maintaining sorted order | O(n) |
remove($value): bool |
Remove the first occurrence | O(n) |
contains($value): bool |
Check if a value exists | O(n) |
first(): int\|string |
Get the smallest (first) element | O(1) |
last(): int\|string |
Get the largest (last) element | O(1) |
toArray(): array |
Get all values as a sorted array | O(n) |
isEmpty(): bool |
Check if the list is empty | O(1) |
clear(): void |
Remove all elements | O(1) |
count(): int |
Get the number of elements | O(1) |
filter(callable): static |
Create a new filtered list | O(n) |
merge(self): static |
Merge two lists into a new one | O(n+m) |
getValueType(): ?ValueType |
Get the detected/declared value type | O(1) |
Counting and Iteration
All list classes implement Countable, IteratorAggregate, JsonSerializable, and Stringable:
Filtering
Merging
Merges two sorted lists of the same type in O(n+m) time:
Duplicates
Duplicate values are allowed. remove() removes only the first occurrence:
String Sorting
StringSortedLinkedList and SortedLinkedList (when holding strings) use byte-level comparison (strcmp), which follows UTF-8 byte order rather than locale-aware collation. This means:
- Uppercase letters sort before lowercase (e.g.,
"Banana"before"apple") - Multi-byte characters (e.g.,
"ä","ñ") sort after all ASCII characters
If you need locale-sensitive ordering, consider using PHP's intl extension (Collator class).
Exceptions
| Exception | Parent | When |
|---|---|---|
TypeMismatchException |
\InvalidArgumentException |
Inserting a value of the wrong type |
EmptyListException |
\UnderflowException |
Calling first() or last() on an empty list |
\InvalidArgumentException |
-- | Merging two lists of different types |
Architecture
The library uses the Template Method pattern:
SortedLinkedListInterface-- defines the public contract with@templategenericsAbstractSortedLinkedList-- implements the sorted insertion algorithm, delegates type validation and comparison to subclassesSortedLinkedList-- the primary entry point with auto-detection of value typeIntSortedLinkedList/StringSortedLinkedList-- convenience classes with narrowed return types forfirst()andlast()
Development
License
MIT