Giter VIP home page Giter VIP logo

typed-array's Introduction

Linna Logo

Linna dotenv Logo

Tests Quality Gate Status PDS Skeleton PHP 8.1

About

This package provide typed arrays for php as extension of native ArrayObject.

Requirements

This package require php 8.1

Installation

With composer:

composer require linna/typed-array

Classes

Name Native Type Handled Description
ArrayOfArrays array
ArrayOfBooleans bool
ArrayOfCallable callable
ArrayOfClasses any existing class passed as first argument in constructor as ::class
ArrayOfFloats float
ArrayOfIntegers int
ArrayOfObjects object
ArrayOfStrings string

Usage

use Linna\TypedArrayObject\ArrayOfIntegers;
use Linna\TypedArrayObject\ArrayOfClasses;

//correct, only int passed to constructor.
$intArray = new ArrayOfIntegers([1, 2, 3, 4]);

//correct, int assigned
$intArray[] = 5;
//throw InvalidArgumentException, string assigned, int expected.
$intArray[] = 'a';

//correct, int used
$intArray->append(5);
//throw InvalidArgumentException, string used, int expected.
$intArray->append('a');

//throw InvalidArgumentException, mixed array passed to constructor.
$otherIntArray = new ArrayOfIntegers([1, 'a', 3, 4]);

//correct, only Foo class instances passed to constructor.
$fooArray = new ArrayOfClasses(Foo::class, [
    new Foo(),
    new Foo()
]);

//correct, Foo() instance assigned.
$fooArray[] = new Foo();
//throw InvalidArgumentException, Bar() instance assigned.
$fooArray[] = new Bar();

//correct, Foo() instance used.
$fooArray->append(new Foo());
//throw InvalidArgumentException, Bar() instance used, Foo() instance expected.
$fooArray->append(new Bar());

//throw InvalidArgumentException, mixed array of instances passed to constructor.
$otherFooArray = new ArrayOfClasses(Foo::class, [
    new Foo(),
    new Bar()
]);

Note: Allowed types are: array, bool, callable, float, int, object, string and all existing classes.

Performance consideration for v3.0

Compared to previous versions of the library, this version is a bit faster because every types has it own class. Do milliseconds really matters?

Array Speed Test

Performance consideration for v2.0

Compared to first version of the library, this version is a bit slower because after merging TypedObjectArray with TypedArray, there are more code that be executed when new instance is created and on assign operations.

Array Speed Test

Performance consideration for v1.0

Compared to the parent class ArrayObject typed arrays are slower on writing approximately from 6x to 8x. The slowness is due to not native __construct() and not native offsetSet().
Other operations do not have a speed difference with the native ArrayObject.

use Linna\TypedArray;

//slower from 6x to 8x.
$array = new TypedArray('int', [1, 2, 3, 4]);
$array[] = 5;

//other operations, fast as native.
//for example:
$arrayElement = $array[0];
$elements = $array->count();

Array Speed Test View the speed test script on gist.

typed-array's People

Contributors

peter279k avatar s3b4stian avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar

Forkers

peter279k sifex

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.