obernard/linkedlist 问题修复 & 功能扩展

解决BUG、新增功能、兼容多环境部署,快速响应你的开发需求

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

obernard/linkedlist

Composer 安装命令:

composer require obernard/linkedlist

包简介

Data Storage based on linkedlist

README 文档

README

Linked List implementation in PHP.

Installation

composer require obernard/linkedlist

Usage of final class LifoList

Final classes like LifoList are given as examples of implementation of abstract linked-list classes but extending AbstractSinglyLinkedList or AbstractDoublyLinkedList offers much more coding potentials.

Create an empty list and add nodes.

$stack = new Obernard\LinkedList\Collection\LifoList;

$stack->push('hello');
$stack->push(1);
$stack->push(['test1', 'test2']);

foreach ($stack as $key, $value) {
    // do something 
}

$l = $stack->length() // 3

$node = $stack->pop(); // ['test1', 'test2']

$l = $stack->length() // 2

$node = $stack->pop(); // 1

$l = $stack->length() // 1

Usage of Abstract classes

Abstract singly-linked list supports the use of abstract doubly-linked nodes but as a good practice use singly linked nodes inside singly-linked lists.

Your concrete list class links instances of your concrete node classe. Concrete list classes may create node objects - like LifoList class does - or may not.

In this example, MyList class does not create nodes by itself:

// MyList.php

class MyList extends AbstractSinglyLinkedList {
    
}

// MyNode.php
class MyNode extends AbstractSinglyLinkedNode {

    public $data;
    public function __construct($data) {
        $this->data = $data;
    }

    // IterableNodeInterface getValue method:
    public function getValue() {
        return $this->data;
    }

}

// program.php

$list= new MyList();

$list->pushToHead(new MyNode("test1"))->pushToHead(new MyNode("test2"));

foreach ($list as $key => $value) {
    // do something with $value string (returned by MyNode->getValue()) and $key (MyNode->getKey())
}

A Node classe implements IterableNodeInterface getKey and getValue methods through AbstractNode. You may need to replace one or the other.

  • getValue method determines what is returned as value when iterating the list.

In above example, we decide that foreach statement iterate over $data node property.

If you want to iterate over Node objects, do not replace getValue because AbstractNode->getValue() already returns $this.

  • getKey method determines what is returned as key when iterating the list. AbstractNode->getkey() argument $index is binded with the iterator position index. But you can replace the method and make it return whatever suites your needs.

@see AbstractCommonList key() and current() methods to see how the magic works.

Dialogue between nodes

An interesting design pattern is to make nodes communicate through the list.

See AbstractNode distanceToLastNode() method as an example of inter-nodes communication:

// AbstractNode.php

    /**
     *  Returns the node's rank beginning at tail node (ie at the end).
     *  !! Time complexity is O(n) !!
     *  @return int 
     */
    public function distanceToLastNode():int {
        if ($this->isLast()) // if you Node are the last node just say 0
            return 0;
        else {
            // just ask your next node for its rank and increment 
            $nextNodeRrank=$this->next->distanceToLastNode();    
            return ++$nextNodeRrank; 
        }
    }

This design pattern is based on recursivity.

The time complexity of such recursive methods is 0(n) where n is the number of nodes.

If your configuration has xdebug module enabled, the use of such recursive function calls may raise the following error if your list length get close to 256:

Error: Maximum function nesting level of '256' reached, aborting!    

If you don't want to modify your xdebug config by increasing the xdebug.max_nesting_level setting, just don't use that pattern design for long lists.

Be carefull not to use recursive communication methods between nodes when iterating over nodes because the time complexity would be O(n2) leading to very poor performance.

// very low 0(n) < perf < 0(n^2)
for ($i=0; $i<$list->length(); $i++) {
    $list->head($i);
}
// very very low perf ~ O(n^2)  ...
for ($i=0; $i<$list->length(); $i++) {
    $list->head($i)->distanceToLastNode();
}

Tests

Run composer test.

Contributing

Improvements are welcome! Feel free to submit pull requests.

Licence

MIT

Copyright (c) 2021 Olivier BERNARD

obernard/linkedlist 适用场景与选型建议

obernard/linkedlist 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 13.6k 次下载、GitHub Stars 达 0, 最近一次更新时间为 2021 年 11 月 25 日, 在 PHP 生态内属于活跃度较高的组件。

它主要适用于以下技术方向: 「iterator」 「collection」 「LinkedList」 「SinglyLinkedList」 「DoublyLinkedList」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。

我们在过去多个企业项目中使用过 obernard/linkedlist 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。

围绕 obernard/linkedlist 我们能提供哪些服务?
定制开发 / 二次开发

基于 obernard/linkedlist 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。

BUG 修复 & 性能优化

线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。

项目外包 & 长期维护

承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。

yvsm@zunyunkeji.com QQ:316430983 微信:yvsm316 西安尊云信息科技 · 专注 PHP / Go / 分布式系统研发

统计信息

  • 总下载量: 13.6k
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 0
  • 点击次数: 12
  • 依赖项目数: 0
  • 推荐数: 0

GitHub 信息

  • Stars: 0
  • Watchers: 1
  • Forks: 0
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2021-11-25