1. Learn the data structure array in PHP

Time:2020-10-22

Note: the code uses the automatic loading of composer class, and adopts the psr4 naming standard. If it is not used, the definition of namespace can be required

1. Implementation logic

<?php
/**
 *Content: implementation of array
 * create: 2020-10-20
 */
namespace ArrayBundle;
class BaseArray
{
     /**
     *Array current length
     * @var int
     */
    protected $size;
    
     /**
     *The total capacity of the array
     * @var int
     */
     protected $capacity;
     
     /**
     *The contents of the array
     * @var array
     */
     protected $data;
     
     /**
     *Initializing array contents
     * BaseArray constructor.
     * @param int $capacity
     */
     public function __construct(int $capacity = 100)
     { 
        If ($capacity < = 0) exit ('failed to initialize array! The total length of array must be greater than 0! ).
        $this->capacity = $capacity;
        $this->size = 0;
        $this->data = [];
     }
     
     /**
     *Print content
     * @return string
     */
     public function __toString()
     { 
        $result = sprintf('Array: size = %d, capacity = %d'. PHP_EOL, $this->size, $this->capacity);
        $result .= '[';
        for ($i = 0; $i < $this->size; $i++) {
            $result .= $this->data[$i];
            if ($i != $this->size - 1) {
                $result .= ',';
            } 
        } 
        $result .= ']';
        return $result;
     }
     
     /**
     *Determine whether the array is empty
     * @return bool
     */
     public function isEmpty(): bool
     {
        return $this->size == 0;
     }
     
     /**
     *Determine whether the array is full
     * @return bool
     */
     public function isFull(): bool
     {
        return $this->size == $this->capacity;
     }
     
     /**
     *Adjust the size of the array
     *@ param float $ratio is smaller than 1 to reduce, and greater than 1 to expand
     */
     public function resize(float $ratio): void
     {
         if ($ratio > 1) {
            if ($this->size < $this->capacity) return;
         } elseif ($ratio < 1) {
            if ($this->size > $this->capacity / 2) return;
         } else {
            return;
         } 
         $this->capacity = $this->capacity * $ratio;
     }
     /**
     *Insert element
     *The index inserted by @ param int $Index
     *@ param mixed $value inserted value
     * @return void
     */
     public function add(int $index, $value): void
     {
         //If the current index is greater than the maximum capacity value
         if ($index > $this->capacity) return;
         //If the array is full, expand it
         if ($this->isFull()) {
            $this->resize(2);
         } 
         for ($i = $this->size - 1; $i >= $index; $i--) {
            $this->data[$i + 1] = $this->data[$i];
         } 
         //Insert array
         $this->data[$index] = $value;
         //Current length auto increment
         $this->size++;
         return;
     }
     
     /**
     *Insert element in head
     * @param mixed $value
     * @return void
     */
     public function addFirst($value): void
     {
        $this->add(0, $value);
     }
     
     /**
     *Insert element at end
     * @param mixed $value
     * @return void
     */
     public function addLast($value): void
     {
        $this->add($this->size, $value);
     }
     
     /**
     *Delete element
     *@ param int $Index deleted index
     * @return bool
     */
     public function del(int $index): bool
     {
         //Determine whether it is empty or whether the index is within the current length
         if ($this->isEmpty() || $this->size <= $index) return false;
         //Advances the value one bit forward, starting with the subscript index to be deleted, until the end
         for ($i = $index; $i < $this->size - 1; $i++) {
            $this->data[$i] = $this->data[$i + 1];
         }// set the value of the last bit to null
         $this->data[$this->size - 1] = null;
         $this->size--;
         $this->resize(0.5);
         return true;
     }
     
     /**
     *Update the specified field
     *@ param int $index index index index
     * @param mixed $value
     * @return bool
     */
     public function update(int $index, $value): bool
     {
         //Determine whether it is empty or whether the index is within the current length
         if ($this->isEmpty() || $this->size <= $index) return false;
         if ($this->search($value) == -1) return false;
         $this->data[$index] = $value;
         return true;
     }
     
     /**
     *Replacement value
     *The value of the @ param mixed $SearchValue query
     *@ param mixed $newvalue new value
     */
     public function replace($searchValue, $newValue): void
     {
        for ($i = 0; $i < $this->size; $i++) {
            if ($this->data[$i] == $searchValue) {
                $this->data[$i] = $newValue;
            } 
        } 
     }
     
     /**
     *Search by value
     * @param $value
     * @return int
     */
     public function search($value): int
     {
        if ($this->isEmpty()) return -1;
        for ($i = 0; $i < $this->size; $i++) {
            if ($this->data[$i] == $value) return $i;
        } 
        return -1;
     }
 }

2. Execution logic

<?php
require_once __DIR__. '/../vendor/autoload.php';
$array = new ArrayBundleBaseArray(1);
//Insert 5 elements from the end
$array->addLast('a');
$array->addLast('b');
$array->addLast('c');
$array->addLast('d');
$array->addLast('e');
//Insert 3 elements from the beginning
$array->addFirst('z');
$array->addFirst('z');
$array->addFirst('z');
//Delete the first three elements
$array->del(0);
$array->del(0);
$array->del(0);
//Print results
echo $array;

3. Printing results

Array: size = 5, capacity = 8
[a,b,c,d,e]

Recommended Today

Comparison and analysis of Py = > redis and python operation redis syntax

preface R: For redis cli P: Redis for Python get ready pip install redis pool = redis.ConnectionPool(host=’39.107.86.223′, port=6379, db=1) redis = redis.Redis(connection_pool=pool) Redis. All commands I have omitted all the following commands. If there are conflicts with Python built-in functions, I will add redis Global command Dbsize (number of returned keys) R: dbsize P: print(redis.dbsize()) […]