{"id":13411498,"url":"https://github.com/liyue201/gostl","last_synced_at":"2025-05-14T06:13:47.916Z","repository":{"id":35190027,"uuid":"214554025","full_name":"liyue201/gostl","owner":"liyue201","description":"Data structure and algorithm library for go, designed to provide functions similar to C++ STL","archived":false,"fork":false,"pushed_at":"2025-01-03T10:05:57.000Z","size":377,"stargazers_count":1082,"open_issues_count":4,"forks_count":112,"subscribers_count":18,"default_branch":"master","last_synced_at":"2025-04-11T01:41:47.291Z","etag":null,"topics":["bitmap","bloomfilterr","deque","hamt","ketama","list","multiset","queue","rbtree","set","skiplist","sort","stack","stl","vector"],"latest_commit_sha":null,"homepage":"","language":"Go","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/liyue201.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2019-10-12T01:10:24.000Z","updated_at":"2025-04-10T20:25:11.000Z","dependencies_parsed_at":"2024-04-08T16:26:06.879Z","dependency_job_id":"fe908694-2e8a-4847-9caa-6685e276a9c0","html_url":"https://github.com/liyue201/gostl","commit_stats":null,"previous_names":[],"tags_count":4,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/liyue201%2Fgostl","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/liyue201%2Fgostl/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/liyue201%2Fgostl/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/liyue201%2Fgostl/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/liyue201","download_url":"https://codeload.github.com/liyue201/gostl/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":254083638,"owners_count":22011901,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["bitmap","bloomfilterr","deque","hamt","ketama","list","multiset","queue","rbtree","set","skiplist","sort","stack","stl","vector"],"created_at":"2024-07-30T20:01:14.086Z","updated_at":"2025-05-14T06:13:47.892Z","avatar_url":"https://github.com/liyue201.png","language":"Go","funding_links":[],"categories":["Data Structures and Algorithms","Data Structures","数据结构与算法","Generators","数据结构","Go","数据结构`go语言实现的数据结构与算法`","Data Integration Frameworks","Uncategorized"],"sub_categories":["Data Structure and Algorithm Collections","Advanced Console UIs","Standard CLI","数据结构和算法集合","标准 CLI"],"readme":"# GoSTL\n\n[![GoDoc](https://godoc.org/github.com/liyue201/gostl?status.svg)](https://godoc.org/github.com/liyue201/gostl)\n[![Goreportcard](https://goreportcard.com/badge/github.com/liyue201/gostl)](https://goreportcard.com/report/github.com/liyue201/gostl)\n[![Build Status](https://travis-ci.org/liyue201/gostl.svg?branch=master)](https://travis-ci.org/liyue201/gostl)\n[![Coverall](https://coveralls.io/repos/github/liyue201/gostl/badge.svg?branch=master)](https://coveralls.io/github/liyue201/gostl)\n[![License](https://img.shields.io/badge/license-MIT-brightgreen.svg)](/LICENSE)\n[![View examples](https://img.shields.io/badge/learn-examples-brightgreen.svg)](/examples)\n\nEnglish | [简体中文](./README_CN.md)\n\n## Introduction\nGoSTL is a data structure and algorithm library for go, designed to provide functions similar to C++ STL, but more powerful. Combined with the characteristics of go language, most of the data structures have realized goroutine-safe. When creating objects, you can specify whether to turn it on or not through configuration parameters.\n\n\n## Function list\n- data structure\n    - [slice](#slice)\n    - [array](#array)\n    - [vector](#vector)\n    - [list](#list)\n    - [deque](#deque)\n    - [queue](#queue)\n    - [priority_queue](#priority_queue)\n    - [stack](#stack)\n    - [rbtree(red_black_tree)](#rbtree)\n    - [map/multimap](#map)\n    - [set/multiset](#set)\n    - [bitmap](#bitmap)\n    - [bloom_filter](#bloom_filter)\n    - [hamt(hash_array_mapped_trie)](#hamt)\n    - [ketama](#ketama)\n    - [skiplist](#skiplist)\n- algorithm\n    - [sort(quick_sort)](#sort)\n    - [stable_sort(merge_sort)](#sort)\n    - [binary_search](#sort)\n    - [lower_bound](#sort)\n    - [upper_bound](#sort)\n    - [next_permutation](#next_permutation)\n    - [nth_element](#nth_element)\n    - [swap](#algo_op)\n    - [reverse](#algo_op)\n    - [count/count_if](#algo_op_const)\n    - [find/find_if](#algo_op_const)\n    - [min_element/max_element](#algo_op_const)\n    \n ## Examples\n\n ### \u003ca name=\"slice\"\u003eslice\u003c/a\u003e\nThe slice in this library is a wrapper of go native slice.\n\n ```go\npackage main\n\nimport (\n  \"fmt\"\n  \"github.com/liyue201/gostl/algorithm/sort\"\n  \"github.com/liyue201/gostl/ds/slice\"\n  \"github.com/liyue201/gostl/utils/comparator\"\n)\n\nfunc main() {\n  a := make([]int, 0)\n  a = append(a, 2)\n  a = append(a, 1)\n  a = append(a, 3)\n  fmt.Printf(\"%v\\n\", a)\n\n  wa := slice.NewSliceWrapper(a)\n\n  // sort in ascending\n  sort.Sort[int](wa.Begin(), wa.End(), comparator.IntComparator)\n  fmt.Printf(\"%v\\n\", a)\n\n  // sort in descending\n  sort.Sort[int](wa.Begin(), wa.End(), comparator.Reverse(comparator.IntComparator))\n  fmt.Printf(\"%v\\n\", a)\n}\n\n ```\n \n### \u003ca name=\"array\"\u003earray\u003c/a\u003e\nArray is a data structure with fixed length once it is created, which supports random access and iterator access.\n\n```go\npackage main\n\nimport (\n  \"fmt\"\n  \"github.com/liyue201/gostl/ds/array\"\n)\n\nfunc main() {\n  a := array.New[int](5)\n  for i := 0; i \u003c a.Size(); i++ {\n    a.Set(i, i+1)\n  }\n  for i := 0; i \u003c a.Size(); i++ {\n    fmt.Printf(\"%v \", a.At(i))\n  }\n\n  fmt.Printf(\"\\n\")\n  for iter := a.Begin(); iter.IsValid(); iter.Next() {\n    fmt.Printf(\"%v \", iter.Value())\n  }\n}\n\n```\n\n### \u003ca name=\"vector\"\u003evector\u003c/a\u003e\nVector is a kind of data structure whose size can be automatically expanded, which is realized by slice internally. Supports random access and iterator access.\n\n```go\npackage main\n\nimport (\n  \"fmt\"\n  \"github.com/liyue201/gostl/algorithm/sort\"\n  \"github.com/liyue201/gostl/ds/vector\"\n  \"github.com/liyue201/gostl/utils/comparator\"\n)\n\nfunc main() {\n  v := vector.New[int]()\n  v.PushBack(1)\n  v.PushBack(2)\n  v.PushBack(3)\n  for i := 0; i \u003c v.Size(); i++ {\n    fmt.Printf(\"%v \", v.At(i))\n  }\n  fmt.Printf(\"\\n\")\n\n  // sort in descending\n  sort.Sort[int](v.Begin(), v.End(), comparator.Reverse(comparator.IntComparator))\n  for iter := v.Begin(); iter.IsValid(); iter.Next() {\n    fmt.Printf(\"%v \", iter.Value())\n  }\n}\n\n```\n\n\n### \u003ca name=\"list\"\u003elist\u003c/a\u003e\n- simple list  \nSimple list is a one directional list, which supports inserting data from the head and tail, and only traversing data from the head.\n\n```go\npackage main\n\nimport (\n  \"fmt\"\n  \"github.com/liyue201/gostl/ds/list/simplelist\"\n)\n\nfunc main() {\n  l := simplelist.New[int]()\n  l.PushBack(1)\n  l.PushFront(2)\n  l.PushFront(3)\n  l.PushBack(4)\n  for n := l.FrontNode(); n != nil; n = n.Next() {\n    fmt.Printf(\"%v \", n.Value)\n  }\n  fmt.Printf(\"\\n===============\\n\")\n}\n\n```\n  \n- bidirectional list  \nBidirectional list supports inserting data from the head and tail, and traversing data from the head and tail.\n\n```go\npackage main\n\nimport (\n  \"fmt\"\n  \"github.com/liyue201/gostl/ds/list/bidlist\"\n)\n\nfunc main() {\n  l := bidlist.New[int]()\n  l.PushBack(1)\n  l.PushFront(2)\n  l.PushFront(3)\n  l.PushBack(4)\n  for n := l.FrontNode(); n != nil; n = n.Next() {\n    fmt.Printf(\"%v \", n.Value)\n  }\n  fmt.Printf(\"\\n\")\n\n  for n := l.BackNode(); n != nil; n = n.Prev() {\n    fmt.Printf(\"%v \", n.Value)\n  }\n}\n```\n\n### \u003ca name=\"deque\"\u003edeque\u003c/a\u003e\nDeque supports efficient data insertion from the head and tail, random access and iterator access.\n\n```go\npackage main\n\nimport (\n  \"fmt\"\n  \"github.com/liyue201/gostl/algorithm/sort\"\n  \"github.com/liyue201/gostl/ds/deque\"\n  \"github.com/liyue201/gostl/utils/comparator\"\n  \"math/rand\"\n)\n\nfunc main() {\n  q := deque.New[int]()\n  for i := 0; i \u003c 100; i++ {\n    r := rand.Int() % 100\n    q.PushBack(r)\n    q.PushFront(r)\n  }\n  fmt.Printf(\"%v\\n\", q)\n\n  sort.Sort[int](q.Begin(), q.End(), comparator.IntComparator)\n  fmt.Printf(\"%v\\n\", q)\n\n  for !q.Empty() {\n    r := rand.Int() % q.Size()\n    q.EraseAt(r)\n  }\n  fmt.Printf(\"%v\\n\", q)\n}\n\n```\n\n### \u003ca name=\"queue\"\u003equeue\u003c/a\u003e\nQueue is a first-in-first-out data structure. The bottom layer uses the deque or list as the container. By default, the deque is used. If you want to use the list, you can use the `queue.WithListContainer()` parameter when creating an object. Goroutine safety is supported.\n\n```go\npackage main\n\nimport (\n  \"fmt\"\n  \"github.com/liyue201/gostl/ds/queue\"\n)\n\nfunc main() {\n  q := queue.New[int]()\n  for i := 0; i \u003c 5; i++ {\n    q.Push(i)\n  }\n  for !q.Empty() {\n    fmt.Printf(\"%v\\n\", q.Pop())\n  }\n}\n\n```\n\n### \u003ca name=\"priority_queue\"\u003epriority_queue\u003c/a\u003e\nPriority Queue is an abstract data type that is similar to a queue, and every element has some priority value associated with it. The priority of the elements in a priority queue determines the order in which elements are served (i.e., the order in which they are removed). If in any case the elements have same priority, they are served as per their ordering in the queue.\n```go\npackage main\n\nimport (\n  \"fmt\"\n  \"github.com/liyue201/gostl/ds/priorityqueue\"\n  \"github.com/liyue201/gostl/utils/comparator\"\n)\n\nfunc main() {\n  q := priorityqueue.New[int](comparator.Reverse(comparator.IntComparator),\n    priorityqueue.WithGoroutineSafe())\n  q.Push(4)\n  q.Push(13)\n  q.Push(7)\n  q.Push(9)\n  q.Push(0)\n  q.Push(88)\n\n  for !q.Empty() {\n    fmt.Printf(\"%v\\n\", q.Pop())\n  }\n}\n\n```\n\n### \u003ca name=\"stack\"\u003estack\u003c/a\u003e\nStack is a kind of last-in-first-out data structure. The bottom layer uses the deque or list as the container. By default, the deque is used. If you want to use the list, you can use the `queue.WithListContainer()` parameter when creating an object.  Goroutine safety is supported.\n\n```go\npackage main\n\nimport (\n  \"fmt\"\n  \"github.com/liyue201/gostl/ds/stack\"\n)\n\nfunc main() {\n  s := stack.New[int]()\n  s.Push(1)\n  s.Push(2)\n  s.Push(3)\n  for !s.Empty() {\n    fmt.Printf(\"%v\\n\", s.Pop())\n  }\n}\n```\n\n### \u003ca name=\"rbtree\"\u003erbtree\u003c/a\u003e\nRed black tree is a balanced binary sort tree, which is used to insert and find data efficiently.\n\n```go\npackage main\n\nimport (\n  \"fmt\"\n  \"github.com/liyue201/gostl/ds/rbtree\"\n  \"github.com/liyue201/gostl/utils/comparator\"\n)\n\nfunc main() {\n  tree := rbtree.New[int, string](comparator.IntComparator)\n  tree.Insert(1, \"aaa\")\n  tree.Insert(5, \"bbb\")\n  tree.Insert(3, \"ccc\")\n  v, _ := tree.Find(5)\n  fmt.Printf(\"find %v returns %v\\n\", 5, v)\n\n  tree.Traversal(func(key int, value string) bool {\n    fmt.Printf(\"%v : %v\\n\", key, value)\n    return true\n  })\n  tree.Delete(tree.FindNode(3))\n}\n\n```\n\n### \u003ca name=\"map\"\u003emap\u003c/a\u003e\nThe Map bottom layer is implemented by using red black tree, and supports iterative access in key order, which is different from the go native map type (the go native map bottom layer is hash, and does not support iterative access in key order). Goroutine safety is supported.\n\n```go\npackage main\n\nimport (\n  \"fmt\"\n  \"github.com/liyue201/gostl/ds/map\"\n  \"github.com/liyue201/gostl/utils/comparator\"\n)\n\nfunc main() {\n  m := treemap.New[string, string](comparator.StringComparator, treemap.WithGoroutineSafe())\n\n  m.Insert(\"a\", \"aaa\")\n  m.Insert(\"b\", \"bbb\")\n\n  a, _ := m.Get(\"a\")\n  b, _ := m.Get(\"b\")\n  fmt.Printf(\"a = %v\\n\", a)\n  fmt.Printf(\"b = %v\\n\", b)\n\n  m.Erase(\"b\")\n}\n\n```\n\n### \u003ca name=\"set\"\u003eset\u003c/a\u003e\nThe Set bottom layer is implemented by red black tree, which supports goroutine safety. Support basic operations of set, such as union, intersection and difference. Goroutine safety is supported.\n\n```go\npackage main\n\nimport (\n  \"fmt\"\n  \"github.com/liyue201/gostl/ds/set\"\n  \"github.com/liyue201/gostl/utils/comparator\"\n)\n\nfunc main() {\n  s := set.New[int](comparator.IntComparator, set.WithGoroutineSafe())\n  s.Insert(1)\n  s.Insert(5)\n  s.Insert(3)\n  s.Insert(4)\n  s.Insert(2)\n\n  s.Erase(4)\n\n  for iter := s.Begin(); iter.IsValid(); iter.Next() {\n    fmt.Printf(\"%v\\n\", iter.Value())\n  }\n\n  fmt.Printf(\"%v\\n\", s.Contains(3))\n  fmt.Printf(\"%v\\n\", s.Contains(10))\n}\n\n```\n\n### \u003ca name=\"bitmap\"\u003ebitmap\u003c/a\u003e\nBitmap is used to quickly mark and find whether a non negative integer is in a set. It takes up less memory than map or array.\n\n```go\npackage main\n\nimport (\n  \"fmt\"\n  \"github.com/liyue201/gostl/ds/bitmap\"\n)\n\nfunc main() {\n  bm := bitmap.New(1000)\n  bm.Set(6)\n  bm.Set(10)\n\n  fmt.Printf(\"%v\\n\", bm.IsSet(5))\n  fmt.Printf(\"%v\\n\", bm.IsSet(6))\n  bm.Unset(6)\n  fmt.Printf(\"%v\\n\", bm.IsSet(6))\n}\n\n```\n### \u003ca name=\"bloom_filter\"\u003ebloom_filter\u003c/a\u003e\nBoomfilter is used to quickly determine whether the data is in the collection. The bottom layer is implemented with bitmap, which uses less memory than map. The disadvantage is that it does not support deletion and has a certain error rate. Goroutine safety is supported , supports data export and reconstruction through exported data.\n\n```go\npackage main\n\nimport (\n  \"fmt\"\n  \"github.com/liyue201/gostl/ds/bloomfilter\"\n)\n\nfunc main() {\n  filter := bloom.New(100, 4, bloom.WithGoroutineSafe())\n  filter.Add(\"hhhh\")\n  filter.Add(\"gggg\")\n\n  fmt.Printf(\"%v\\n\", filter.Contains(\"aaaa\"))\n  fmt.Printf(\"%v\\n\", filter.Contains(\"gggg\"))\n}\n\n```\n\n### \u003ca name=\"hamt\"\u003ehamt\u003c/a\u003e\nCompared with the traditional hash (open address method or linked list method hash), hamt has lower probability of hash conflict and higher space utilization. The time complexity of capacity expansion is low. Goroutine safety is supported.\n\n```go\npackage main\n\nimport (\n  \"fmt\"\n  \"github.com/liyue201/gostl/ds/hamt\"\n)\n\nfunc main() {\n  h := hamt.New[string](hamt.WithGoroutineSafe())\n  key := []byte(\"aaaaa\")\n  val := \"bbbbbbbbbbbbb\"\n\n  h.Insert(key, val)\n  v, _ := h.Get(key)\n  fmt.Printf(\"%v = %v\\n\", string(key), v)\n\n  h.Erase(key)\n  v, _ = h.Get(key)\n  fmt.Printf(\"%v = %v\\n\", string(key), v)\n}\n\n```\n\n### \u003ca name=\"ketama\"\u003eketama\u003c/a\u003e\nConsistent hash Ketama algorithm, using 64 bit hash function and map storage, has less conflict probability. Goroutine safety is supported.\n\n```go\npackage main\n\nimport (\n  \"fmt\"\n  \"github.com/liyue201/gostl/ds/ketama\"\n)\n\nfunc main() {\n  k := ketama.New()\n  k.Add(\"1.2.3.3\")\n  k.Add(\"2.4.5.6\")\n  k.Add(\"5.5.5.1\")\n\n  for i := 0; i \u003c 10; i++ {\n    node, _ := k.Get(fmt.Sprintf(\"%d\", i))\n    fmt.Printf(\"%v\\n\", node)\n  }\n  k.Remove(\"2.4.5.6\")\n}\n\n\n```\n### \u003ca name=\"skiplist\"\u003eskiplist\u003c/a\u003e\nSkiplist is a kind of data structure which can search quickly by exchanging space for time. Goroutine safety is supported.\n\n```go\npackage main\n\nimport (\n  \"fmt\"\n  \"github.com/liyue201/gostl/ds/skiplist\"\n  \"github.com/liyue201/gostl/utils/comparator\"\n)\n\nfunc main() {\n  list := skiplist.New[string, string](comparator.StringComparator, skiplist.WithMaxLevel(15))\n  list.Insert(\"aaa\", \"1111\")\n  list.Insert(\"bbb\", \"2222\")\n  v1, _ := list.Get(\"aaa\")\n  v2, _ := list.Get(\"bbb\")\n  fmt.Printf(\"aaa = %v\\n\", v1)\n  fmt.Printf(\"bbb = %v\\n\", v2)\n\n  list.Traversal(func(key, value string) bool {\n    fmt.Printf(\"key:%v value:%v\\n\", key, value)\n    return true\n  })\n\n  list.Remove(\"aaa\")\n}\n\n```\n\n### \u003ca name=\"sort\"\u003esort\u003c/a\u003e\nSort: quick sort algorithm is used internally.  \nStable: stable sorting. Merge sorting is used internally.  \nBinarysearch: determine whether an element is in the scope of iterator by binary search.  \nLowerbound: find the first data equal to the element and return the iterator by binary search.  \nUpperbound: find the first data larger than the element and return the iterator by binary search.  \n\n```go\npackage main\n\nimport (\n  \"fmt\"\n  \"github.com/liyue201/gostl/algorithm/sort\"\n  \"github.com/liyue201/gostl/ds/slice\"\n  \"github.com/liyue201/gostl/utils/comparator\"\n)\n\nfunc main() {\n  a := make([]string, 0)\n  a = append(a, \"bbbb\")\n  a = append(a, \"ccc\")\n  a = append(a, \"aaaa\")\n  a = append(a, \"bbbb\")\n  a = append(a, \"bb\")\n\n  sliceA := slice.NewSliceWrapper(a)\n\n  ////Sort in ascending order\n  sort.Sort[string](sliceA.Begin(), sliceA.End(), comparator.OrderedTypeCmp[string])\n\n  sort.Stable[string](sliceA.Begin(), sliceA.End(), comparator.StringComparator)\n  fmt.Printf(\"%v\\n\", a)\n\n  if sort.BinarySearch[string](sliceA.Begin(), sliceA.End(), \"bbbb\", comparator.StringComparator) {\n    fmt.Printf(\"BinarySearch: found bbbb\\n\")\n  }\n\n  iter := sort.LowerBound[string](sliceA.Begin(), sliceA.End(), \"bbbb\", comparator.StringComparator)\n  if iter.IsValid() {\n    fmt.Printf(\"LowerBound bbbb: %v\\n\", iter.Value())\n  }\n  iter = sort.UpperBound[string](sliceA.Begin(), sliceA.End(), \"bbbb\", comparator.StringComparator)\n  if iter.IsValid() {\n    fmt.Printf(\"UpperBound bbbb: %v\\n\", iter.Value())\n  }\n  //Sort in descending order\n  sort.Sort[string](sliceA.Begin(), sliceA.End(), comparator.Reverse(comparator.StringComparator))\n  //sort.Stable[string](sliceA.Begin(), sliceA.End(), comparator.Reverse(comparator.StringComparator))\n  fmt.Printf(\"%v\\n\", a)\n}\n\n```\n\n### \u003ca name=\"next_permutation\"\u003enext_permutation\u003c/a\u003e\nThis function modifies the data in the iterator range to the next sort combination.\n\n```go\npackage main\n\nimport (\n  \"fmt\"\n  \"github.com/liyue201/gostl/algorithm/sort\"\n  \"github.com/liyue201/gostl/ds/slice\"\n  \"github.com/liyue201/gostl/utils/comparator\"\n)\n\nfunc main() {\n  a := make([]int, 0)\n  for i := 1; i \u003c= 3; i++ {\n    a = append(a, i)\n  }\n  wa := slice.NewSliceWrapper(a)\n  fmt.Println(\"NextPermutation\")\n  for {\n    fmt.Printf(\"%v\\n\", a)\n    if !sort.NextPermutation[int](wa.Begin(), wa.End(), comparator.IntComparator) {\n      break\n    }\n  }\n  fmt.Println(\"PrePermutation\")\n  for {\n    fmt.Printf(\"%v\\n\", a)\n    if !sort.NextPermutation[int](wa.Begin(), wa.End(), comparator.Reverse(comparator.IntComparator)) {\n      break\n    }\n  }\n}\n\n```\n\n### \u003ca name=\"nth_element\"\u003enth_element\u003c/a\u003e\nPlace the nth element in the scope of the iterator in the position of N, and put the element less than or equal to it on the left, and the element greater than or equal to it on the right.\n\n```go\npackage main\n\nimport (\n  \"fmt\"\n  \"github.com/liyue201/gostl/algorithm/sort\"\n  \"github.com/liyue201/gostl/ds/deque\"\n  \"github.com/liyue201/gostl/utils/comparator\"\n)\n\nfunc main() {\n  a := deque.New[int]()\n  a.PushBack(9)\n  a.PushBack(8)\n  a.PushBack(7)\n  a.PushBack(6)\n  a.PushBack(5)\n  a.PushBack(4)\n  a.PushBack(3)\n  a.PushBack(2)\n  a.PushBack(1)\n  fmt.Printf(\"%v\\n\", a)\n  sort.NthElement[int](a.Begin(), a.End(), 3, comparator.IntComparator)\n  fmt.Printf(\"%v\\n\", a.At(3))\n  fmt.Printf(\"%v\\n\", a)\n}\n```\n\n### \u003ca name=\"algo_op\"\u003e swap/reverse \u003c/a\u003e\n- swap: swap the values of two iterators\n- reverse: Reverse values in the range of two iterators\n\n```go\npackage main\n\nimport (\n  \"fmt\"\n  \"github.com/liyue201/gostl/algorithm\"\n  \"github.com/liyue201/gostl/ds/deque\"\n)\n\nfunc main() {\n  a := deque.New[int]()\n  for i := 0; i \u003c 9; i++ {\n    a.PushBack(i)\n  }\n  fmt.Printf(\"%v\\n\", a)\n\n  algorithm.Swap[int](a.First(), a.Last())\n  fmt.Printf(\"%v\\n\", a)\n\n  algorithm.Reverse[int](a.Begin(), a.End())\n  fmt.Printf(\"%v\\n\", a)\n}\n\n```\n\n### \u003ca name=\"algo_op_const\"\u003e count/count_if/find/find_if \u003c/a\u003e\n- Count : Count the number of elements equal to the specified value in the iterator interval\n- CountIf: Count the number of elements that satisfy the function f in the iterator interval\n- Find: Find the first element equal to the specified value in the iterator interval and returns its iterator\n- FindIf：Find the first element satisfying function f in the iterator interval and return its iterator\n- MaxElement : Find the largest element and return its iterator\n- MinElement : Find the smallest element and return its iterator\n- AnyOf : Returns true if any element satisfies the function f in the iterator interval\n- AllOf: Returns true if all elements satisfy the function f in the iterator interval\n- NoneOf: Returns true if none of the elements satisfy the function f in the iterator interval\n\n```go\npackage main\n\nimport (\n  \"fmt\"\n  \"github.com/liyue201/gostl/algorithm\"\n  \"github.com/liyue201/gostl/ds/deque\"\n  \"github.com/liyue201/gostl/utils/comparator\"\n  \"github.com/liyue201/gostl/utils/iterator\"\n)\n\nfunc isEven(iter iterator.ConstIterator[int]) bool {\n  return iter.Value()%2 == 0\n}\n\nfunc greaterThan5(iter iterator.ConstIterator[int]) bool {\n  return iter.Value() \u003e 5\n}\n\nfunc main() {\n  a := deque.New[int]()\n  for i := 0; i \u003c 10; i++ {\n    a.PushBack(i)\n  }\n  for i := 0; i \u003c 5; i++ {\n    a.PushBack(i)\n  }\n  fmt.Printf(\"%v\\n\", a)\n\n  fmt.Printf(\"Count 2: %v\\n\", algorithm.Count[int](a.Begin(), a.End(), 2, comparator.IntComparator))\n\n  fmt.Printf(\"Count 2: %v\\n\", algorithm.CountIf[int](a.Begin(), a.End(), isEven))\n\n  iter := algorithm.Find[int](a.Begin(), a.End(), 2, comparator.IntComparator)\n  if !iter.Equal(a.End()) {\n    fmt.Printf(\"Fund %v\\n\", iter.Value())\n  }\n  iter = algorithm.FindIf[int](a.Begin(), a.End(), greaterThan5)\n  if !iter.Equal(a.End()) {\n    fmt.Printf(\"FindIf greaterThan5 : %v\\n\", iter.Value())\n  }\n  iter = algorithm.MaxElement[int](a.Begin(), a.End(), comparator.IntComparator)\n  if !iter.Equal(a.End()) {\n    fmt.Printf(\"largest value : %v\\n\", iter.Value())\n  }\n  iter = algorithm.MinElement[int](a.Begin(), a.End(), comparator.IntComparator)\n  if !iter.Equal(a.End()) {\n    fmt.Printf(\"largest value : %v\\n\", iter.Value())\n  }\n\n\tfmt.Printf(\"Any even: %v\\n\", algorithm.AnyOf[int](a.Begin(), a.End(), isEven))\n\tfmt.Printf(\"All even: %v\\n\", algorithm.AllOf[int](a.Begin(), a.End(), isEven))\n\tfmt.Printf(\"None even: %v\\n\", algorithm.NoneOf[int](a.Begin(), a.End(), isEven))\n}\n\n\n```\n\n## Stargazers over time\n\n[![Stargazers over time](https://starchart.cc/liyue201/gostl.svg)](https://starchart.cc/liyue201/gostl)\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fliyue201%2Fgostl","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fliyue201%2Fgostl","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fliyue201%2Fgostl/lists"}