golang的vector

Go语言中使用切片(slice)实现一个Vector容器_weixin_30467087的博客-CSDN博客

package vector

import (
    "reflect"
)

// 小写 只能通过工厂函数创建
type vector struct {
    values []interface{}
}

// 创建工厂函数
func New(cap int) *vector {
    this := new(vector)
    this.values = make([]interface{}, 0, cap)

    return this
}

func (this *vector) IsEmpty() bool {
    return len(this.values) == 0
}

// 元素数量
func (this *vector) Size() int {
    return len(this.values)
}

// 追加单个元素
func (this *vector) Append(value interface{}) bool {
    this.values = append(this.values, value)
    return true
}

// 追加元素切片
func (this *vector) AppendAll(values []interface{}) bool {
    if values == nil || len(values) < 1 {
        return false
    }
    this.values = append(this.values, values...)
    return true
}

// 插入单个元素
func (this *vector) Insert(index int, value interface{}) bool {
    if index < 0 || index >= len(this.values) {
        return false
    }
    this.values = append(this.values[:index], append([]interface{}{value}, this.values[index:]...)...)
    return true
}

// 插入元素切片
func (this *vector) InsertAll(index int, values []interface{}) bool {
    if index < 0 || index >= len(this.values) || values == nil || len(values) < 1 {
        return false
    }
    this.values = append(this.values[:index], append(values, this.values[index:]...)...)
    return true
}

// 移除
func (this *vector) Remove(index int) bool {
    if index < 0 || index >= len(this.values) {
        return false
    }
    // 重置为 nil 防止内存泄漏
    this.values[index] = nil
    this.values = append(this.values[:index], this.values[index+1:]...)
    return true
}

// 范围移除 从 fromIndex(包含) 到 toIndex(不包含) 之间的元素
func (this *vector) RemoveRange(fromIndex, toIndex int) bool {
    if fromIndex < 0 || fromIndex >= len(this.values) || toIndex > len(this.values) || fromIndex > toIndex {
        return false
    }
    // 重置为 nil 防止内存泄漏
    for i := fromIndex; i < toIndex; i++ {
        this.values[i] = nil
    }
    this.values = append(this.values[:fromIndex], this.values[toIndex:]...)
    return true
}

// 全部移除
func (this *vector) RemoveAll() {
    // 重置为 nil 防止内存泄漏
    for i := 0; i < this.Size(); i++ {
        this.values[i] = nil
    }
    this.values = this.values[0:0]
}

func (this *vector) getIndex(value interface{}) int {
    for i := 0; i < len(this.values); i++ {
        if reflect.DeepEqual(this.values[i], value) {
            return i
        }
    }
    return -1
}

// 是否存在该元素值
func (this *vector) Contains(value interface{}) bool {
    return this.getIndex(value) >= 0
}

// 获取元素值第一次出现的索引
func (this *vector) IndexOf(value interface{}) int {
    return this.getIndex(value)
}

// 获取元素值最后一次出现的索引
func (this *vector) LastIndexOf(value interface{}) int {
    for i := len(this.values) - 1; i >= 0; i-- {
        if reflect.DeepEqual(this.values[i], value) {
            return i
        }
    }
    return -1
}

// 得到索引对应的元素值
func (this *vector) GetValue(index int) interface{} {
    if index < 0 || index >= len(this.values) {
        return nil
    }
    return this.values[index]
}

// 设置值
func (this *vector) SetValue(index int, value interface{}) bool {
    if index < 0 || index >= len(this.values) {
        return false
    }
    this.values[index] = value
    return true
}

func (this *vector) ToArray() []interface{} {
    dst := make([]interface{}, this.Size())
    copy(dst, this.values)
    return dst
}

func testVector() {
    v := vector.New(5)
    for i := 0; i < 6; i++ {
        v.Append(i)
    }
    fmt.Println(*v)
    fmt.Println(v.IsEmpty())
    v.Insert(1, 10)
    v.Insert(20, 20)
    fmt.Println(*v)
    v.Remove(-1)
    v.Remove(1)
    v.Remove(100)
    fmt.Println(*v)
    fmt.Println(v.IndexOf(3))
    fmt.Println(v.IndexOf(300))
    fmt.Println(v.GetValue(1))
    fmt.Println(v.GetValue(100))
    v.SetValue(-1, -1)
    v.SetValue(1, 11)
    v.SetValue(100, 101)
    fmt.Println(*v)

    v.RemoveAll()
    fmt.Println(v.IsEmpty())
    fmt.Println(v.Size())

    v.Append(22)
    fmt.Println(*v)
}

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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值