collection包1.1.0都升级了什么功能
collection包1.1.0都升级了什么功能
jianfengye/collection(https://github.com/jianfengye/collection) 这个包喜迎第一个子版本升级,从1.0.1升级到了1.1.0。这次还是做了不少改动的。
支持int32
这个需求是这个issue提出的: https://github.com/jianfengye/collection/issues/10
主要是在protobuf 生成的go代码里面是int32,int64的。
增加一个类型的数组其实是很方便的事情了,只需要写一个Int32Collection的struct, 基于AbsCollection,实现几个必要的函数就可以了。
package collection
import (
"errors"
"fmt"
)
type Int32Collection struct {
AbsCollection
objs []int32
}
func compareInt32(i interface{}, i2 interface{}) int {
int1 := i.(int32)
int2 := i2.(int32)
if int1 > int2 {
return 1
}
if int1 < int2 {
return -1
}
return 0
}
// NewInt32Collection create a new Int32Collection
func NewInt32Collection(objs []int32) *Int32Collection {
arr := &Int32Collection{
objs: objs,
}
arr.AbsCollection.Parent = arr
arr.SetCompare(compareInt32)
return arr
}
...
实现延迟复制
这个是有一个读者在公众号留言提醒的。之前1.0.1版本的collection在new的时候直接将slice进行copy一份,是出于安全的考虑,Collection的使用一定不能修改到原有的slice。现在1.1.0在newCollection的时候并不复制slice,而是在需要对slice进行乱序或者变更操作的时候进行一次Copy操作。而我把Copy操作的时间也放到各个具体实现类中了。
于是ICollection多实现了一个Copy方法,它会把当前Collection的Slice复制一份出来。然后在AbsCollection中记录一个是否已经拷贝的标记,isCopied,对于那些对原数组进行操作的方法会根据这个标记,如果之前没有复制,就复制一份,再进行操作
func (arr *AbsCollection) Insert(index int, obj interface{}) ICollection {
if arr.Err() != nil {
return arr
}
if arr.Parent == nil {
panic("no parent")
}
if arr.isCopied == false {
arr.Copy()
arr.isCopied = true
}
return arr.Parent.Insert(index, obj)
}
这样就实现了延迟拷贝的功能。
实现了SetIndex的方法
这个方法和Index方法是对应的,将数组的某个元素进行设置。
这个方法的具体实现也在实现类中实现了,特别是对ObjCollection的SetIndex实现还是需要reflect进行绕的,其他的COllection不需要使用反射。
func (arr *ObjCollection) SetIndex(i int, val interface{}) ICollection {
arr.objs.Index(i).Set(reflect.ValueOf(val))
return arr
}
Sort实现了快速排序
这个是这个issue提出的 https://github.com/jianfengye/collection/issues/9
之前的Sort我是使用冒泡排序实现的,确实效率有欠考虑。
这次将Sort进行了快排实现。由于已经又了SetIndex, Index, 等方法,所以可以这个快排可以直接在AbsCollection中实现就行了。
func (arr *AbsCollection) qsort(left, right int, isAscOrder bool) {
tmp := arr.Index(left)
p := left
i, j := left, right
for i <= j {
for j >= p {
c, err := arr.Index(j).Compare(tmp)
if err != nil {
arr.SetErr(err)
return
}
if isAscOrder && c >= 0 {
j--
continue
}
if !isAscOrder && c <= 0 {
j--
continue
}
break
}
if j >= p {
t, _ := arr.Index(j).ToInterface()
arr.SetIndex(p, t)
p = j
}
for i <= p {
c, err := arr.Index(i).Compare(tmp)
if err != nil {
arr.SetErr(err)
return
}
if isAscOrder && c <= 0 {
i++
continue
}
if !isAscOrder && c >= 0 {
i++
continue
}
break
}
if i <= p {
t, _ := arr.Index(i).ToInterface()
arr.SetIndex(p, t)
p = i
}
}
t, _ := tmp.ToInterface()
arr.SetIndex(p, t)
if p-left > 1 {
arr.qsort(left, p-1, isAscOrder)
}
if right-p > 1 {
arr.qsort(p+1, right, isAscOrder)
}
}
func (arr *AbsCollection) Sort() ICollection {
if arr.Err() != nil {
return arr
}
if arr.compare == nil {
return arr.SetErr(errors.New("sort: compare must be set"))
}
if arr.isCopied {
arr.qsort(0, arr.Count()-1, true)
return arr
}
arr.Copy()
return arr.Sort()
}
compare函数进行传递
之前IMix的compare函数一定都需要调用SetCompare才能设置,现在如果这个IMix是从Collection进行创建的,比如Collection.Index(xx) IMix, 返回的IMix就直接将Collection中设置的compare函数直接传递过来。
这样在使用过程中方便了不少。
我也将各个类型的compare函数都整理在具体实现类的头部
func compareInt32(i interface{}, i2 interface{}) int
func compareInt64(i interface{}, i2 interface{}) int
func compareInt(i interface{}, i2 interface{}) int
func compareString(a interface{}, b interface{}) int
...
总结
1.1.0版本主要是根据issue反馈修复了一些使用和性能上的优化点。总体觉得已经可以发一个小版本了,于是打上了1.1.0的tag。
该项目目前也有277个star了,欢迎在业务上试用 jianfengye/collection(https://github.com/jianfengye/collection) 这个包,有问题请直接提issue,我会尽快响应。
collection包1.1.0都升级了什么功能的更多相关文章
- struts2.3.16所需的基本的jar包---------SSH升级包不是整体全部都升级的
struts2.3.16所需的基本的jar包 jar包放多了就报Exception什么Unable to load....上网搜了半天也没有能解决的 下面所说的jar包放到WEB-INF/lib以 ...
- Nuget包CommonServiceLocator从1.0.3升级到2.0.4时MvvmLight的ViewModelLocator初始化SimpleIoc.Default格式不匹配问题
原文:Nuget包CommonServiceLocator从1.0.3升级到2.0.4时MvvmLight的ViewModelLocator初始化SimpleIoc.Default格式不匹配问题 把旧 ...
- rpm包安装的nginx热升级
文章目录一.本地环境基本介绍二.yum升级命令说明三.升级好nginx后如何不中断业务切换3.1.nginx相关的信号说明3.2.在线热升级nginx可执行文件程序一.本地环境基本介绍本次测试环境,是 ...
- rac 10g 10.2.0.1升级到10.2.0.5具体解释
RAC 10.2.0.1 升级到 10.2.0.5 一. 准备: Patch 包:p8202632_10205_LINUX.zip 节点数:3个节点 RAC1 RAC2 ...
- Linux下Oracle 10.2.0.1升级到10.2.0.4总结
最近部署测试环境时,将测试环境ORACLE数据库从10.2.0.1升级到了10.2.0.4,顺便整理记录一下升级过程. 实验环境: 操作系统:Oracle Linux Server release 5 ...
- Oracle数据库版本10.2.0.1升级到10.2.0.3(转)
Oracle数据库版本10.2.0.1升级到10.2.0.3 1.停止OEM/isqlplus/监听/DB实例 $ emctl stop dbconsole $ isqlplusctl stop $ ...
- Oracle11.2.0.1升级到11.2.0.3
Oracle数据库升级也并非简单的事,这篇博客,博主对Oracle那点事做了较详细的介绍: http://blog.itpub.net/9599/viewspace-473003/ 我还属于Oracl ...
- CDH 版本 6.0.1 升级到 6.2.0 当前最新版本(CentOS 7.x)
前文「CDH CM版本 6.0.1 升级到 CM 6.2.0 当前最新版本(CentOS 7.x)」 承接上文,当我们完成 CM 6.2.0 的升级之后,我们已经相当于完成了80% minor 的升级 ...
- windows使用zip包安装mysql8.0.12
1.前言 在windows下有两种安装mysql的方式,一种是msi的方式,一种是使用zip包的安装方式.通常都是用msi的方式,毕竟不需要敲命令,只用图形界面就可以完成安装.zip包的安装方式也很简 ...
随机推荐
- QT5:第八章 信号与槽机制
一.简介 QT编程中信号与槽用于处理界面各个组件的交互,类似与MFC的消息循环和绑定 注意:在使用信号与槽的类中,必须在类的定义中加入宏定义Q_OBJECT 信号(Signal)就是在特定情况下被发射 ...
- CF-1100 E Andrew and Taxi
CF-1100E Andrew and Taxi https://codeforces.com/contest/1100/problem/E 知识点: 二分 判断图中是否有环 题意: 一个有向图,每边 ...
- GIMP的Path的合并于复制
1/Path的复制不能像图层一样简单的复制粘贴,只有通过merge的方法实现: 使要合并的Path处于可见状态,右击Path工具栏: 合并前与合并后比较: 2/向不同文件复制Path: 到另外一个 ...
- a标签中javascript和void
<body> <a href="javascript:;">点了无反应</a> <a href="javascript:void ...
- verilog behavioral modeling--procedural continous assignment(不用)
assign / deassgin force /release the procedural continuous assignments(using keywords assign and for ...
- centos7 安装rabbitmq rabbitmq-c以及amqp扩展 详细篇
自己鼓捣了一晚上总算整明白了,有几个坑分享给小伙伴,希望能帮到你 前期准备 安装erlang 下载rpm包地址:https://github.com/rabbitmq/erlang-rpm (注意er ...
- Oracle中创建触发器示例及注意事项
1.oracle 中创建触发器示例 CREATE TABLE "CONCEPT"."FREQUENCYMODIFYLOG" ( "FREQUENCYI ...
- mysql 面安装配置
解压MySQL压缩包 将以下载的MySQL压缩包解压到自定义目录下,我的解压目录是: "D:\Program Files\MySQL\mysql-5.6.13-win32&quo ...
- Linux下二进制文件安装MySQL
MySQL 下载地址:https://dev.mysql.com/downloads/mysql/ 并按如下方式选择来下载安装包. 1. 设置配置文件/etc/my.cnmore /etc/my.cn ...
- linux下防火墙iptables原理及使用
iptables简介 netfilter/iptables(简称为iptables)组成Linux平台下的包过滤防火墙,与大多数的Linux软件一样,这个包过滤防火墙是免费的,它可以代替昂贵的商业防火 ...