pwm/bitset 问题修复 & 功能扩展

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

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

pwm/bitset

Composer 安装命令:

composer require pwm/bitset

包简介

A simple Bit Set implementation

README 文档

README

Build Status codecov Maintainability Test Coverage License: MIT

A simple bit set implementation for compact storage of sets of values. The library itself is stateless providing only pure mapping functions.

Table of Contents

Requirements

PHP 7.1+

Installation

$ composer require pwm/bitset

Usage

In this example we are going to use a bit set to put users in groups.

Let's create a Group class. It has a list of groups, named G1 to G5 for this example, and a map between groups and binary values of powers of 2 provided by BitSet. Finally it has 2 functions that map between group names and bit values.

class Group
{
    const G1 = 'Group 1';
    const G2 = 'Group 2';
    const G3 = 'Group 3';
    const G4 = 'Group 4';
    const G5 = 'Group 5';

    private const MAP = [
        self::G1 => BitSet::B1,
        self::G2 => BitSet::B2,
        self::G3 => BitSet::B3,
        self::G4 => BitSet::B4,
        self::G5 => BitSet::B5,
    ];

    public static function fromGroups(array $groups): array
    {
        return array_values(array_intersect_key(self::MAP, array_flip($groups)));
    }

    public static function toGroups(array $bitValues): array
    {
        return array_values(array_intersect_key(array_flip(self::MAP), array_flip($bitValues)));
    }
}

Let's also create a User class. A user can belong to groups. The $groups property starts from 0 that represents the empty set. It has 5 methods to manipulate its groups, corresponding to BitSet's 5 functions: get(), set(), add(), remove() and has().

class User
{
    private $groups = BitSet::EMPTY;

    public function getGroups(): array
    {
        return Group::toGroups(BitSet::get($this->groups));
    }

    public function setGroups(array $groups): void
    {
        $this->groups = BitSet::set(Group::fromGroups($groups));
    }

    public function addGroups(array $groups): void
    {
        $this->groups = BitSet::add($this->groups, Group::fromGroups($groups));
    }

    public function removeGroups(array $groups): void
    {
        $this->groups = BitSet::remove($this->groups, Group::fromGroups($groups));
    }

    public function hasGroup(string $group): bool
    {
        $a = Group::fromGroups([$group]);
        return isset($a[0])
            ? BitSet::has($this->groups, $a[0])
            : false;
    }
}

Now we can use the above the following way:

$user = new User();
$user->setGroups([Group::G1, Group::G2]);

assert($user->getGroups() === [Group::G1, Group::G2]);
assert($user->hasGroup(Group::G1) === true);

$user->addGroups([Group::G4, Group::G5]);
$user->removeGroups([Group::G1, Group::G4]);

assert($user->getGroups() === [Group::G2, Group::G5]);
assert($user->hasGroup(Group::G1) === false);

How it works

As an example let's take the numbers 1, 4 and 8. Their sum is 13. Pretty uninteresting so far. But let's look at their binary representations: 0b1, 0b100 and 0b1000 respectively. Their sum 13 is 0b1101. See how the base-2 representations of 1, 4 and 8 "fill unique slots" in 13's, leaving only one slot unset? This is because 1, 4 and 8 are all powers of 2 so their binary representations starts with a 1 followed by zero or more 0s. 0b1 = 2^0 = 1, 0b100 = 2^2 = 4, 0b1000 = 2^3 = 8. Their sum: 2^0 + 2^2 + 2^3 = 0b1 + 0b100 + 0b1000 = 0b1101 = 13. Now, say in a game, we could call 1 "north", 2 "south", 4 "east" and 8 "west" and then 13 could mean that our character can move all directions from its current position except south as 2 (0b10) is not in 13 (0b1101).

The idea behind a bit set is using a base-2 representation of a number to singal the presence or absence of values by having its bits set to 1 or 0. The values themselves are also numbers with the restriction that they must be powers of 2 starting from 2^0 and in general, depending on the underlying system, going up to 2^32 or 2^64. For example on a 64 bit system one can store up to 64 unique values in a bit set.

The BitSet library itself is a collection of 5 pure functions that map between values and their bit set:

set(array $values): int

Maps a list of unique positive integers to a single integer, the bit set, that is their sum. All integers in the input list must be powers of 2.

get(int $bitSet): array

Maps an integer, the bit set, to a list of unique positive integers. All integers in the output list must be powers of 2.

add(int $bitSet, array $values): int

Adds a list of unique positive integers to a bit set. All integers in the list must be powers of 2.

remove(int $bitSet, array $values): int

Subtracts a list of unique positive integers from a bit set. All integers in the list must be powers of 2.

has(int $bitSet, int $value): bool

Predicate that tells whether an element is in a bit set or not.

Tests

$ vendor/bin/phpunit
$ composer phpcs
$ composer phpstan

Changelog

Click here

Licence

MIT

pwm/bitset 适用场景与选型建议

pwm/bitset 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 4 次下载、GitHub Stars 达 5, 最近一次更新时间为 2017 年 07 月 09 日, 在 PHP 生态内属于活跃度较高的组件。

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

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

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

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

BUG 修复 & 性能优化

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

项目外包 & 长期维护

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

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

统计信息

  • 总下载量: 4
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 5
  • 点击次数: 3
  • 依赖项目数: 0
  • 推荐数: 0

GitHub 信息

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

其他信息

  • 授权协议: MIT
  • 更新时间: 2017-07-09