Skip to content

πŸ“š SlimAPI - Data Structures

License

Notifications You must be signed in to change notification settings

slimapi/data-structure

Repository files navigation

πŸ“š SlimAPI - Data Structures

PHP Version Release License Build Code Coverage PHPStan

A collection of reusable and efficient data structures for various use cases.

Data Structure Status
Sorted Linked List βœ… Implemented
Other structures (coming soon) ⏳ Planned

πŸ”— SlimAPI\DataStructure\SortedLinkedList

A linked list that automatically keeps your values sorted. Supports integers or strings.

✨ Features

  • Automatic Sorting: Always keeps elements sorted.
  • Customizable Direction: Sort in ascending (default) or descending order.
  • Duplicate Prevention: Automatically ensures only unique elements are stored in the list.
  • Operations: Insert O(n), remove O(n), count O(1), and iterate O(n) with ease.

πŸ“š Usage Examples

βœ… Adding Items to the List

Values are inserted while maintaining the sort order.

$list = new SortedLinkedList();
$list->insert(3); // [3]
$list->insert(1); // [1, 3]
$list->insert(2); // [1, 2, 3]

πŸ”„ Changing Sort Direction

You can control the sorting order by specifying Direction::ASC (default) or Direction::DESC.

$list = new SortedLinkedList(Direction::DESC);
$list->insert(3); // [3]
$list->insert(1); // [3, 1]
$list->insert(2); // [3, 2, 1]

❌ Removing Items

Easily remove any value from the list.

$list->remove(2); // [3, 1]

πŸ”’ Counting and Iterating

The list is both countable and iterable, perfect for large datasets.

echo $list->count(); // 2

foreach ($list->toGenerator() as $item) {
    echo $item; // Outputs 3, then 1
}

πŸ“¦ Installation

Add the dependency to your project:

composer require slimapi/data-structure

πŸ› οΈ Local Development & Testing

Clone the repo, then use the power of make to simplify your workflow:

make help  # See all available commands
make run   # Start the app container
make test  # Run tests and code checkers

πŸ“œ License

This project is licensed under the terms specified in the LICENSE file.

🌟 Get Involved

We welcome contributions and suggestions! Please report any issues in the issue tracker.