GoDSGo 数据结构包

联合创作 · 2023-09-28 11:22

GoDS 是一个 Go 语言实现的各种数据结构的工具包,包括:

  • Containers (Sets, Lists, Stacks, Maps, Trees),
  • Sets (HashSet, TreeSet, LinkedHashSet),
  • Lists (ArrayList, SinglyLinkedList, DoublyLinkedList),
  • Stacks (LinkedListStack, ArrayStack),
  • Maps (HashMap, TreeMap, HashBidiMap, TreeBidiMap, LinkedHashMap),
  • Trees (RedBlackTree, AVLTree, BTree, BinaryHeap),
  • Comparators,
  • Iterators,

示例代码:

package main

import (
	"github.com/emirpasic/gods/lists/arraylist"
	"github.com/emirpasic/gods/utils"
)

func main() {
	list := arraylist.New()
	list.Add("a")                         // ["a"]
	list.Add("c", "b")                    // ["a","c","b"]
	list.Sort(utils.StringComparator)     // ["a","b","c"]
	_, _ = list.Get(0)                    // "a",true
	_, _ = list.Get(100)                  // nil,false
	_ = list.Contains("a", "b", "c")      // true
	_ = list.Contains("a", "b", "c", "d") // false
	list.Swap(0, 1)                       // ["b","a",c"]
	list.Remove(2)                        // ["b","a"]
	list.Remove(1)                        // ["b"]
	list.Remove(0)                        // []
	list.Remove(0)                        // [] (ignored)
	_ = list.Empty()                      // true
	_ = list.Size()                       // 0
	list.Add("a")                         // ["a"]
	list.Clear()                          // []
	list.Insert(0, "b")                   // ["b"]
	list.Insert(0, "a")                   // ["a","b"]
}

 

浏览 7
点赞
评论
收藏
分享

手机扫一扫分享

编辑 分享
举报
评论
图片
表情
推荐
点赞
评论
收藏
分享

手机扫一扫分享

编辑 分享
举报