chevere / data-structure
Map and Vector data structures
Installs: 4 932
Dependents: 5
Suggesters: 0
Security: 0
Stars: 1
Watchers: 0
Forks: 0
Open Issues: 1
Requires
- php: ^8.1
- chevere/message: ^1.0.0
Requires (Dev)
- phpstan/phpstan: ^1.9
- phpunit/phpunit: ^9.5
- symplify/easy-coding-standard: ^11.1
README
Summary
DataStructure provides Map and Vector data structures.
Installing
DataStructure is available through Packagist and the repository source is at chevere/data-structure.
composer require chevere/data-structure
Map
A Map is a sequential collection of key-value pairs. Keys can be of type integer
and string
.
Create a Map by passing named arguments of any type.
use Chevere\DataStructure\Map; $map = new Map(foo: $foo, bar: $bar);
Put Map values
The withPut
method is used to put value(s) to the Map.
$map = $map ->withPut( foo: $foo, bar: $bar );
Counting Map keys
The count
method returns the number of keys mapped.
$map->count(); // 2
Map keys
The keys
method is used to retrieve the map keys as an array.
$map->keys(); // ['foo', 'bar']
Has Map keys
The has
method is used to check if the Map contains the given key(s).
$map->has('foo'); // true $map->has('notFound'); // false
Assert has Map keys
The assertHas
method is used to assert if the Map contains the given key(s). It throws an exception when failing to assert.
$map->assertHas('foo'); $map->assertHas('notFound');
Get Map value
The get
method is used to retrieve the Map value for the given key.
$foo = $map->get('foo'); $bar = $map->get('bar');
Vector
A Vector is a sequence of values of any type. Keys are of type integer.
Create a Vector by passing values.
use Chevere\DataStructure\Vector; $vector = new Vector($value1, $value2,);
Counting Vector keys
The count
method returns the number of keys in the vector.
$vector->count(); // 2
Vector keys
The keys
method is used to retrieve the map keys as an array.
$map->keys(); // [0, 1]
Push Vector values
The withPush
method is used to add one or more elements to the end of the sequence.
$with = $vector->withPush($value,);
Set Vector values
The withSet
method is used to set the value at the given position.
$with = $vector->withSet(0, $value);
Unshift Vector values
The withUnshift
method is used to prepend one or more elements at the beginning of the sequence.
$with = $vector->withUnshift($value,);
Insert Vector values
The withInsert
method is used to insert values at a given position.
$with = $vector->withInsert($pos, ...$values);
Remove Vector values
The withRemove
method is used to remove one or more values at a given position.
$with = $vector->withRemove($pos,);
Has Vector values
The has
method is used to check if the Vector contains the given value(s).
$vector->has($value); // true $vector->has($notFound); // false
Assert Vector has values
The assertHas
method is used to assert if the Vector contains the given value(s). It throws an exception when failing to assert.
$vector->assertHas($value);
Get Vector value
The get
method is used to retrieve the Vector value at the given position.
$value = $vector->get($pos);
Find values
The find
method is used to find the position for the given value.
$pos = $vector->find($value);
Contains values
The contains
method is used to check if the Vector contains the given value(s).
$vector->contains($value); // bool
Documentation
Documentation is available at chevere.org.
License
Copyright Rodolfo Berrios A.
Chevere is licensed under the Apache License, Version 2.0. See LICENSE for the full license text.
Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.