Arrays of data structures

Keywords: PHP github

Definition

php's array is a dynamic data structure by default, while the underlying array should open up a fixed storage space in memory to store a continuous piece of data, so we can only use SplFixed Array to limit the memory capacity.

Code

github address: array (Arrays)

class Arrays
{
    private $data;
    private $size;
    private $capacity;
    // Constructor, capacity capacity of the incoming array constructs Array, capacity of the default array = 10
    public function __construct(int $capacity = 10)
    {
        $this->data = new SplFixedArray($capacity);
        $this->capacity = $capacity;
        $this->size = 0;
    }
    // Gets the number of elements in an array
    public function getSize():int
    {
        return $this->size;
    }
     // Whether the return array is empty
    public function isEmpty():bool
    {
        return $this->size == 0;
    }
    // Capture the capacity of an array
    public function getCapacity():int
    {
        return $this->data->getSize();
    }
    // Insert a new element e O(n) at the index location
    public function add(int $index,int $e)
    {
        if($index <0 || $index > $this->size){
            throw new Exception("index is illegal");
        }
        if ($this->size == $this->capacity){
            $this->resize(2 * $this->capacity);
        }
        for ($i=$this->size -1 ; $i >=$index; $i--) {
            $this->data[$i + 1] = $this->data[$i];
        }
        $this->data[$index] = $e;
        $this->size ++;

    }
    // Add a new element O(1) to all elements
    public function addLast($e)
    {
        $this->add($this->size,$e);
    }
    // Add a new element O(n) before all elements
    public function addFirst($e)
    {
        $this->add(0,$e);
    }
    // Get the element O(1) at the index location
    public function get($index)
    {
        if($index <0 || $index > $this->size){
            throw new Exception("index is illegal");
        }
        return $this->data[$index];
    }
    public function getLast(){
        return $this->get($this->size-1);
    }
    public function getFirst(){
        return $this->get(0);
    }
    // Modify the index index location to e O(1)
    public function set($index,$e)
    {
        if($index <0 || $index > $this->size){
            throw new Exception("index is illegal");
        }
        $this->data[$index] = $e;
    }
    // Find if there is an element e O(n) in the array
    public function contains($e):bool
    {
        for ($i=0; $i < $this->size; $i++) {
            if($this->data[$i] == $e){
                return true;
            }
        }
        return false;
    }
    // Find the index of the element e in the array, and return - 1 O(n) if there is no element e.
    public function find($e):int
    {
        for ($i=0; $i < $this->size; $i++) {
            if($this->data[$i] == $e){
                return $i;
            }
        }
        return -1;
    }
    // Remove the element in the index position from the array and return the deleted element
    public function remove($index)
    {
        if($index <0 || $index > $this->size){
            throw new Exception("index is illegal");
        }
        $ret = $this->data[$index];

        for ($i=$index+1; $i < $this->size; $i++) {
            $this->data[$i-1] = $this->data[$i];
        }
        $this->size--;
        unset($this->data[$this->size]);
        if($this->size == $this->capacity / 4 && $this->capacity /2 != 0){
            $this->resize($this->capacity/2);
        }
        return $ret;
    }
     // Delete the last element from the array and return the deleted element
    public function removeLast()
    {
        $this->remove($this->size-1);
    }
    // Delete the first element from the array and return the deleted element
    public function removeFirst()
    {
        $this->remove(0);
    }
     // Delete element e from the array
    public function removeElement($e)
    {
        $index = $this->find($e);
        if($index != -1){
            $this->remove($index);
        }
    }
    // Change the capacity of array space to the size of new Capacity
    private function resize($newCapacity)
    {
        $newData = (new self($newCapacity))->data;
        for($i=0;$i<$this->size;$i++){
            $newData[$i] = $this->data[$i];
        }
        $this->data = $newData;
        $this->capacity = $newCapacity;
    }

    public function __toString()
    {
        $str = sprintf("\nArray: size = %d , capacity = %d\n",$this->size,$this->getCapacity());
        $str.='[';
        for($i = 0 ; $i < $this->size; $i ++){
            $str.= $i;
            if($i != $this->size - 1){
                $str.= ", ";
            }
        }
        $str.="]";
        return $str;
    }
}

Time complexity

operation Notes Time complexity Explain
add Adding elements O(n) In the worst case, all elements should be relocated
addFirst Add elements to the header O(n) Worst case
addLast Add elements at the tail O(1) Best case
get Gets the specified location element O(1) Data is contiguous, and elements can be obtained directly from the location.
getFirst Get the first element O(1) Ditto
getLast Get the end element O(1) Ditto
set Modify the element at the specified index location O(1) Ditto
contains Find if there are elements in an array O(1) Need to lookup
find Find the index of the element in the array O(1) Need to lookup
remove Delete the specified element O(n) In the worst case, all elements should be relocated
removeFirst Delete the first element O(n) Worst case
removeLast Delete the end element O(1) Best case
removeElement Delete the specified element O(1) Need to lookup

summary

Arrays are the most basic data structures, so understanding the principles and operation time complexity of arrays is the basis for learning data structures and algorithms.

Welcome to scan the two-dimensional code below, and continue to pay attention to:

Internet Engineer (id:phpstcn), we study together and make progress together

Posted by Pantho on Sun, 28 Jul 2019 21:18:30 -0700