Go語言中使用切片(slice)實現一個Vector容器


Go語言中的切片(slice)和一些內置函數能實現其他語言容器類Array、Vector的功能,但是Go內置語言包container里只提供了list、heap、ring三種容器,缺少vector容器,在實際的項目中為使用方便有必要包裝一個vector,提供vector的常見功能。

C++、Java、C#等語言支持泛型,Go語言不支持泛型,可以用 interface{} 提供類似泛型的支持。下面是vector容器代碼

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
}

 

vector內部使用切片(slice)保存元素值,元素類型 interface{},泛型。getIndex()方法使用reflect.DeepEqual()函數對元素值作深度比較。上述代碼只提供了vector容器常見功能,更多的功能可以根據需要添加,比如有的時候需要對容器中的元素排序,則可以添加排序方法。

一些方法簡單返回bool值表示成功或失敗,可以根據需要增加一個error返回值指明失敗的詳細原因。

需要注意的事項是Remove、RemoveRange、RemoveAll方法內把元素移走時避免內存泄露。

 

下面是測試代碼

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)
}

 

測試代碼覆蓋了部分可能的情況,創建函數New的cap參數指明容器的初始容量。

上述測試代碼只寫了int類型的元素,其他string、float、array、map、struct、slice、vector等類型類似。


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM