DataStructure provides Map and Vector data structures.
DataStructure is available through Packagist and the repository source is at chevere/data-structure.
composer require chevere/data-structure
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);
The withPut
method is used to put value(s) to the Map.
$map = $map
->withPut(
foo: $foo,
bar: $bar
);
The count
method returns the number of keys mapped.
$map->count();
// 2
The keys
method is used to retrieve the map keys as an array.
$map->keys();
// ['foo', 'bar']
The has
method is used to check if the Map contains the given key(s).
$map->has('foo'); // true
$map->has('notFound'); // false
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');
The get
method is used to retrieve the Map value for the given key.
$foo = $map->get('foo');
$bar = $map->get('bar');
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,);
The count
method returns the number of keys in the vector.
$vector->count();
// 2
The keys
method is used to retrieve the map keys as an array.
$map->keys();
// [0, 1]
The withPush
method is used to add one or more elements to the end of the sequence.
$with = $vector->withPush($value,);
The withSet
method is used to set the value at the given position.
$with = $vector->withSet(0, $value);
The withUnshift
method is used to prepend one or more elements at the beginning of the sequence.
$with = $vector->withUnshift($value,);
The withInsert
method is used to insert values at a given position.
$with = $vector->withInsert($pos, ...$values);
The withRemove
method is used to remove one or more values at a given position.
$with = $vector->withRemove($pos,);
The has
method is used to check if the Vector contains the given value(s).
$vector->has($value); // true
$vector->has($notFound); // false
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);
The get
method is used to retrieve the Vector value at the given position.
$value = $vector->get($pos);
The find
method is used to find the position for the given value.
$pos = $vector->find($value);
The contains
method is used to check if the Vector contains the given value(s).
$vector->contains($value); // bool
Documentation is available at chevere.org.
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.