PSet

这段代码实现了一个PSet,即Positive Set,是GSet的扩展。PSet是一个集合,支持添加和删除元素,但是不支持重复元素。PSet的实现是通过两个GSet来实现的,一个GSet存储添加的元素,另一个GSet存储删除的元素。value函数返回PSet中的元素,add函数向PSet中添加元素,rem函数从PSet中删除元素,merge函数将两个PSet合并。其中,GSet是一个Grow-only Set,即只支持添加元素,不支持删除和修改元素。

PSet是一个集合,支持添加和删除元素,但是不支持重复元素。下面是各个函数的功能:

  • PSet():创建一个空的PSet。
  • zero():创建一个空的PSet。
  • value(a: PSet):返回PSet中的元素。
  • add(a: PSet, value):向PSet中添加元素。
  • rem(a: PSet, value):从PSet中删除元素。
  • merge(a: PSet, b: PSet):将两个PSet合并。
import GSet

def PSet():
return (GSet.zero(), GSet.zero()) def zero():
return PSet() def value(a: PSet):
add, rem = a
return add - rem def add(a: PSet, value):
add, rem = a
return add.add(value) def rem(a: PSet, value):
add, rem = a
return rem.add(value) def merge(a: PSet, b: PSet):
add_a, rem_a = a
add_b, rem_b = b
return (GSet.merge(add_a, add_b), GSet.merge(rem_a, rem_b))
import java.util.HashSet;
import java.util.Set; public class PSet {
public static Set<Integer> value(PSetTuple a) {
Set<Integer> add = GSet.value(a.add);
Set<Integer> rem = GSet.value(a.rem);
Set<Integer> res = new HashSet<>(add);
res.removeAll(rem);
return res;
} public static PSetTuple add(PSetTuple a, int value) {
Set<Integer> add = GSet.add(a.add, value);
Set<Integer> rem = a.rem;
return new PSetTuple(add, rem);
} public static PSetTuple rem(PSetTuple a, int value) {
Set<Integer> add = a.add;
Set<Integer> rem = GSet.add(a.rem, value);
return new PSetTuple(add, rem);
} public static PSetTuple merge(PSetTuple a, PSetTuple b) {
Set<Integer> add_a = a.add;
Set<Integer> rem_a = a.rem;
Set<Integer> add_b = b.add;
Set<Integer> rem_b = b.rem;
Set<Integer> add = GSet.merge(add_a, add_b);
Set<Integer> rem = GSet.merge(rem_a, rem_b);
return new PSetTuple(add, rem);
} public static PSetTuple zero() {
return new PSetTuple(GSet.zero(), GSet.zero());
} public static class PSetTuple {
public Set<Integer> add;
public Set<Integer> rem; public PSetTuple(Set<Integer> add, Set<Integer> rem) {
this.add = add;
this.rem = rem;
}
}
} class GSet {
public static Set<Integer> value(Set<Integer> a) {
return a;
} public static Set<Integer> add(Set<Integer> a, int value) {
Set<Integer> res = new HashSet<>(a);
res.add(value);
return res;
} public static Set<Integer> merge(Set<Integer> a, Set<Integer> b) {
Set<Integer> res = new HashSet<>(a);
res.addAll(b);
return res;
} public static Set<Integer> zero() {
return new HashSet<>();
}
}

实现一个CRDT工具库——PSet的更多相关文章

  1. 如何手写一个js工具库?同时发布到npm上

    自从工作以来,写项目的时候经常需要手写一些方法和引入一些js库 JS基础又十分重要,于是就萌生出自己创建一个JS工具库并发布到npm上的想法 于是就创建了一个名为learnjts的项目,在空余时间也写 ...

  2. [js高手之路] 跟GhostWu一起封装一个字符串工具库-架构篇(1)

    所谓字符串工具库就是利用javascript面向对象的知识封装一个常用的字符串处理方法库,首先给这个库起个名字,好吧就叫ghostwu.js. 看下ghostwu.js的整体架构: ; (functi ...

  3. [js高手之路] 跟GhostWu一起封装一个字符串工具库-扩展trim,trimLeft,trimRight方法(2)

    我们接着上一篇的继续,在上一篇我们完成了工具库的架构,本文扩展字符串去空格的方法, 一共有3个 1,trimLeft: 去除字符串左边的空格 2,trimRight: 去除字符串右边的空格 3,tri ...

  4. [js高手之路] 跟GhostWu一起封装一个字符串工具库-扩展字符串位置方法(4)

    本文,我们接着之前的框架继续扩展,这次扩展了一共有5个与字符串位置相关的方法 between( left, right ) 返回两个字符串之间的内容, 如果第二个参数没有传递,返回的是找到的第一个参数 ...

  5. [js高手之路] 跟GhostWu一起封装一个字符串工具库-扩展camelize与dasherize方法(3)

    在此之前,我们已经完成了4个方法: trimLeft, trimRight, trim, capitalize 本文,我们扩展驼峰式与下划线转化这两个对称的方法 camelize: 把空格,下划线,中 ...

  6. 一个PHP高性能、多并发、restful的工具库(基于multi_curl)

    This is high performance curl wrapper written in pure PHP. It's compatible with PHP 5.4+ and HHVM. N ...

  7. Underscore.js工具库

    Underscore 是一个 JavaScript 工具库,它提供了一整套函数式编程的实用功能,但是没有扩展任何 JavaScript 内置对象. 他解决了这个问题:“如果我面对一个空白的 HTML ...

  8. JavaScript工具库

    jPublic 交流QQ群:1017567122 前言 在我们开发项目的时候,无论项目规模大小,在所难免会写一些工具型函数来解决一些问题,随着项目开发和维护的时间越来越长,这些工具型函数会越来越多,同 ...

  9. 手把手教你使用Rollup打包📦并发布自己的工具库🔧

    DevUI是一支兼具设计视角和工程视角的团队,服务于华为云DevCloud平台和华为内部数个中后台系统,服务于设计师和前端工程师. 官方网站:devui.design Ng组件库:ng-devui(欢 ...

  10. 自己的一个LESS工具函数库

    自己大概在一年前开始使用LESS编写样式,现在感觉不用LESS都不会写样式了.现在写静态页面完全离不开LESS与Zen Coding,我可以不用什么IDE,但这两个工具却必须要,当然也强烈推荐看到这篇 ...

随机推荐

  1. html input 属性

    一:disabled disabled 属性规定应该禁用 input 元素,被禁用的 input 元素,不可编辑,不可复制,不可选择,不能接收焦点,后台也不会接收到传值.设置后文字的颜色会变成灰色.d ...

  2. Vue3 + Vue Router 4.x 添加过渡动效报错

    1. 报错信息 2. 报错原因 检查页面代码发现动效出错页面为多根节点,修改后动效正常 <template> <div> <div>xxx</div> ...

  3. Leetcode——二分法bisect_left,bisect_right

    !前提--列表有序 case 1 如果列表中没有元素x,那么bisect_left(ls, x)和bisec_right(ls, x)返回相同的值,该值是x在ls中"合适的插入点索引,使得数 ...

  4. 查看Linux 日志

    # 直接定位到第100行 less +100g xx.log   # 定位到最后一行 less +GG xx.log   # 定位到第100个字节的位置 less +100P xx.log   # 直 ...

  5. Spring系列之验证-14

    目录 Java Bean 验证 Bean 验证概述 配置 Bean 验证提供程序 注入验证器 配置一个`DataBinder` Spring MVC 3 验证 Java Bean 验证 Bean 验证 ...

  6. 第14章 身份验证:使用Identity将用户添加到应用程序(ASP.NET Core in Action, 2nd Edition)

    本章包括 ASP.NET Core中web应用程序的身份验证工作原理 使用ASP.NET Core标识系统创建项目 向现有web应用添加用户功能 自定义默认ASP.NET Core标识UI 像ASPN ...

  7. vue导出文件

    /**导出 */ async toExcel() { // let result = await this.axios({ // method: 'get', // url: `issdc-manag ...

  8. AES加密 php7版本 openssl_encrypt 遇到的坑

    与前端对接api ,解密不了前端加密的数据. 问题描述: 1.前端用 cryptojs  加密的 密钥是24位 , 2.后端用的php7的 openssl_encrypt  同密钥来进行解密,发现解密 ...

  9. re相关正则表达式(re.sub、re.I 、re.S、re.M)

    re.I 表示忽略大小写 re.S 表示全文匹配 re.M 表示全文拼配行尾段位的字符或者数字,影响^和$ re.sub 表示替换 使用方法: re.sub(pattern, repl, string ...

  10. 十大经典排序之快速排序(C++实现)

    快速排序 通过一趟排序将待排序列分割成两部分,其中一部分记录的关键字均比另一部分记录的关键字小.之后分别对这两部分记录继续进行排序,以达到整个序列有序的目的. 思路: (1)选择基准:从数列中挑出一个 ...