php 自增 数组,1. 数据结构(PHP实现) -- 数组

说明:代码使用了composer类的自动加载,并采用psr4命名规范,如不使用可取消namespace的定义

1. 实现逻辑<?php

/**

* content: 数组的实现

* create: 2020-10-20

*/

namespace ArrayBundle;

class BaseArray

{

/**

* 数组当前长度

* @var int

*/

protected $size;

/**

* 数组的总容量

* @var int

*/

protected $capacity;

/**

* 数组的内容

* @var array

*/

protected $data;

/**

* 初始化数组内容

* BaseArray constructor.

* @param int $capacity

*/

public function __construct(int $capacity = 100)

{

if ($capacity <= 0) exit('初始化数组失败!数组总长度必须大于0!');

$this->capacity = $capacity;

$this->size = 0;

$this->data = [];

}

/**

* 打印内容

* @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;

}

/**

* 判断数组是否为空

* @return bool

*/

public function isEmpty(): bool

{

return $this->size == 0;

}

/**

* 判断数组是否已经存满

* @return bool

*/

public function isFull(): bool

{

return $this->size == $this->capacity;

}

/**

* 调整数组的容量大小

* @param float $ratio 小于1为缩小,大于1为扩大

*/

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;

}

/**

* 插入元素

* @param int $index 插入的索引

* @param mixed $value 插入的值

* @return void

*/

public function add(int $index, $value): void

{

// 如果当前的索引 大于 最大的容量值就返回

if ($index > $this->capacity) return;

// 如果数组存满了就扩容

if ($this->isFull()) {

$this->resize(2);

}

for ($i = $this->size - 1; $i >= $index; $i--) {

$this->data[$i + 1] = $this->data[$i];

}

// 插入数组

$this->data[$index] = $value;

// 当前长度自增

$this->size++;

return;

}

/**

* 在头部插入元素

* @param mixed $value

* @return void

*/

public function addFirst($value): void

{

$this->add(0, $value);

}

/**

* 在末尾插入元素

* @param mixed $value

* @return void

*/

public function addLast($value): void

{

$this->add($this->size, $value);

}

/**

* 删除元素

* @param int $index 删除的索引

* @return mixed

*/

public function del(int $index)

{

// 判断是否为空 或 索引是否在当前长度内

if ($this->isEmpty() || $index < 0 || $index >= $this->size) return null;

// 获取删除的值

$deleteValue = $this->data[$index];

// 将值往前推进一位, 初始值为需要删除的下标索引,直到最后

for ($i = $index; $i < $this->size - 1; $i++) {

$this->data[$i] = $this->data[$i + 1];

}

// 将最后一位的值设为null

$this->data[$this->size - 1] = null;

$this->size--;

$this->resize(0.5);

return $deleteValue;

}

/**

* 更新指定字段

* @param int $index 索引下标

* @param mixed $value

* @return bool

*/

public function update(int $index, $value): bool

{

// 判断是否为空 或 索引是否在当前长度内

if ($this->isEmpty() || $this->size <= $index) return false;

if ($this->search($value) == -1) return false;

$this->data[$index] = $value;

return true;

}

/**

* 替换值

* @param mixed $searchValue 查询的值

* @param mixed $newValue 新的值

*/

public function replace($searchValue, $newValue): void

{

for ($i = 0; $i < $this->size; $i++) {

if ($this->data[$i] == $searchValue) {

$this->data[$i] = $newValue;

}

}

}

/**

* 根据值查询搜因

* @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;

}

/**

* 获取参数

* @param int $index

* @return mixed

*/

public function get(int $index)

{

if ($index < 0 || $index >= $this->size) exit('获取错误!索引下标不能小于0 或 不能大于等于当前长度!');

return $this->data[$index];

}

/**

* 获取第一个值

* @return mixed

*/

public function getFirst()

{

return $this->get(0);

}

/**

* 获取最后一个值

* @return mixed

*/

public function getLast()

{

return $this->get($this->size - 1);

}

}

2. 执行逻辑<?php

require_once __DIR__. '/../vendor/autoload.php';

$array = new ArrayBundleBaseArray(1);

// 从末尾插入5个元素

$array->addLast('a');

$array->addLast('b');

$array->addLast('c');

$array->addLast('d');

$array->addLast('e');

// 从开头插入3个元素

$array->addFirst('z');

$array->addFirst('z');

$array->addFirst('z');

// 删除前面3个元素

$array->del(0);

$array->del(0);

$array->del(0);

// 打印结果

echo $array;

3. 打印结果Array: size = 5, capacity = 8

[a,b,c,d,e]

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值