8000 GitHub - ahrtr/gocontainer: Implements some containers (stack, queue, priorityQueue, set, arrayList, linkedList, map and btree) in golang
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content

Implements some containers (stack, queue, priorityQueue, set, arrayList, linkedList, map and btree) in golang

License

Notifications You must be signed in to change notification settings

ahrtr/gocontainer

{"props":{"initialPayload":{"allShortcutsEnabled":false,"path":"/","repo":{"id":259933623,"defaultBranch":"master","name":"gocontainer","ownerLogin":"ahrtr","currentUserCanPush":false,"isFork":false,"isEmpty":false,"createdAt":"2020-04-29T13:32:15.000Z","ownerAvatar":"https://avatars.githubusercontent.com/u/30739825?v=4","public":true,"private":false,"isOrgOwned":false},"currentUser":null,"refInfo":{"name":"master","listCacheKey":"v0:1662873935.693542","canEdit":false,"refType":"branch","currentOid":"06553eb6a662ce989f29c8338894dde5717b0639"},"tree":{"items":[{"name":".github/workflows","path":".github/workflows","contentType":"directory","hasSimplifiedPath":true},{"name":"btree","path":"btree","contentType":"directory"},{"name":"collection","path":"collection","contentType":"directory"},{"name":"examples","path":"examples","contentType":"directory"},{"name":"list","path":"list","contentType":"directory"},{"name":"map/linkedmap","path":"map/linkedmap","contentType":"directory","hasSimplifiedPath":true},{"name":"queue","path":"queue","contentType":"directory"},{"name":"set","path":"set","contentType":"directory"},{"name":"stack","path":"stack","contentType":"directory"},{"name":"utils","path":"utils","contentType":"directory"},{"name":".golangci.yaml","path":".golangci.yaml","contentType":"file"},{"name":".travis.yml","path":".travis.yml","contentType":"file"},{"name":"LICENSE","path":"LICENSE","contentType":"file"},{"name":"Makefile","path":"Makefile","contentType":"file"},{"name":"README.md","path":"README.md","contentType":"file"},{"name":"README_cn.md","path":"README_cn.md","contentType":"file"},{"name":"go.mod","path":"go.mod","contentType":"file"}],"templateDirectorySuggestionUrl":null,"readme":null,"totalCount":17,"showBranchInfobar":false},"fileTree":null,"fileTreeProcessingTime":null,"foldersToFetch":[],"treeExpanded":false,"symbolsExpanded":false,"isOverview":true,"overview":{"banners":{"shouldRecommendReadme":false,"isPersonalRepo":false,"showUseActionBanner":false,"actionSlug":null,"actionId":null,"showProtectBranchBanner":false,"publishBannersInfo":{"dismissActionNoticePath":"/settings/dismiss-notice/publish_action_from_repo","releasePath":"/ahrtr/gocontainer/releases/new?marketplace=true","showPublishActionBanner":false},"interactionLimitBanner":null,"showInvitationBanner":false,"inviterName":null,"actionsMigrationBannerInfo":{"releaseTags":[],"showImmutableActionsMigrationBanner":false,"initialMigrationStatus":null}},"codeButton":{"contactPath":"/contact","isEnterprise":false,"local":{"protocolInfo":{"httpAvailable":true,"sshAvailable":null,"httpUrl":"https://github.com/ahrtr/gocontainer.git","showCloneWarning":null,"sshUrl":null,"sshCertificatesRequired":null,"sshCertificatesAvailable":null,"ghCliUrl":"gh repo clone ahrtr/gocontainer","defaultProtocol":"http","newSshKeyUrl":"/settings/ssh/new","setProtocolPath":"/users/set_protocol"},"platformInfo":{"cloneUrl":"https://desktop.github.com","showVisualStudioCloneButton":false,"visualStudioCloneUrl":"https://windows.github.com","showXcodeCloneButton":false,"xcodeCloneUrl":"xcode://clone?repo=https%3A%2F%2Fgithub.com%2Fahrtr%2Fgocontainer","zipballUrl":"/ahrtr/gocontainer/archive/refs/heads/master.zip"}},"newCodespacePath":"/codespaces/new?hide_repo_select=true\u0026repo=259933623"},"popovers":{"rename":null,"renamedParentRepo":null},"commitCount":"44","overviewFiles":[{"displayName":"README.md","repoName":"gocontainer","refName":"master","path":"README.md","preferredFileType":"readme","tabName":"README","richText":"\u003carticle class=\"markdown-body entry-content container-lg\" itemprop=\"text\"\u003e\u003cdiv class=\"markdown-heading\" dir=\"auto\"\u003e\u003ch1 tabindex=\"-1\" class=\"heading-element\" dir=\"auto\"\u003egocontainer (\u003ca href=\"/ahrtr/gocontainer/blob/master/README_cn.md\"\u003e中文版\u003c/a\u003e)\u003c/h1\u003e\u003ca id=\"user-content-gocontainer-中文版\" class=\"anchor\" aria-label=\"Permalink: gocontainer (中文版)\" href=\"#gocontainer-中文版\"\u003e\u003csvg class=\"octicon octicon-link\" viewBox=\"0 0 16 16\" version=\"1.1\" width=\"16\" height=\"16\" aria-hidden=\"true\"\u003e\u003cpath d=\"m7.775 3.275 1.25-1.25a3.5 3.5 0 1 1 4.95 4.95l-2.5 2.5a3.5 3.5 0 0 1-4.95 0 .751.751 0 0 1 .018-1.042.751.751 0 0 1 1.042-.018 1.998 1.998 0 0 0 2.83 0l2.5-2.5a2.002 2.002 0 0 0-2.83-2.83l-1.25 1.25a.751.751 0 0 1-1.042-.018.751.751 0 0 1-.018-1.042Zm-4.69 9.64a1.998 1.998 0 0 0 2.83 0l1.25-1.25a.751.751 0 0 1 1.042.018.751.751 0 0 1 .018 1.042l-1.25 1.25a3.5 3.5 0 1 1-4.95-4.95l2.5-2.5a3.5 3.5 0 0 1 4.95 0 .751.751 0 0 1-.018 1.042.751.751 0 0 1-1.042.018 1.998 1.998 0 0 0-2.83 0l-2.5 2.5a1.998 1.998 0 0 0 0 2.83Z\"\u003e\u003c/path\u003e\u003c/svg\u003e\u003c/a\u003e\u003c/div\u003e\n\u003cp dir=\"auto\"\u003egocontainer implements some containers which exist in Java, but are missing in golang. This library is \u003cstrong\u003ezero dependency\u003c/strong\u003e, which means it does NOT depend on any 3rd party packages. Currently the containers are not thread-safe.\u003c/p\u003e\n\u003cdiv class=\"markdown-heading\" dir=\"auto\"\u003e\u003ch1 tabindex=\"-1\" class=\"heading-element\" dir=\"auto\"\u003eTable of Contents\u003c/h1\u003e\u003ca id=\"user-content-table-of-contents\" class=\"anchor\" aria-label=\"Permalink: Table of Contents\" href=\"#table-of-contents\"\u003e\u003csvg class=\"octicon octicon-link\" viewBox=\"0 0 16 16\" version=\"1.1\" width=\"16\" height=\"16\" aria-hidden=\"true\"\u003e\u003cpath d=\"m7.775 3.275 1.25-1.25a3.5 3.5 0 1 1 4.95 4.95l-2.5 2.5a3.5 3.5 0 0 1-4.95 0 .751.751 0 0 1 .018-1.042.751.751 0 0 1 1.042-.018 1.998 1.998 0 0 0 2.83 0l2.5-2.5a2.002 2.002 0 0 0-2.83-2.83l-1.25 1.25a.751.751 0 0 1-1.042-.018.751.751 0 0 1-.018-1.042Zm-4.69 9.64a1.998 1.998 0 0 0 2.83 0l1.25-1.25a.751.751 0 0 1 1.042.018.751.751 0 0 1 .018 1.042l-1.25 1.25a3.5 3.5 0 1 1-4.95-4.95l2.5-2.5a3.5 3.5 0 0 1 4.95 0 .751.751 0 0 1-.018 1.042.751.751 0 0 1-1.042.018 1.998 1.998 0 0 0-2.83 0l-2.5 2.5a1.998 1.998 0 0 0 0 2.83Z\"\u003e\u003c/path\u003e\u003c/svg\u003e\u003c/a\u003e\u003c/div\u003e\n\u003cul dir=\"auto\"\u003e\n\u003cli\u003e\u003cstrong\u003e\u003ca href=\"#how-to-use-this-repo\"\u003eHow to use this repo\u003c/a\u003e\u003c/strong\u003e\u003c/li\u003e\n\u003cli\u003e\u003cstrong\u003e\u003ca href=\"#Common-Interface\"\u003eCommon Interface\u003c/a\u003e\u003c/strong\u003e\u003c/li\u003e\n\u003cli\u003e\u003cstrong\u003e\u003ca href=\"#Containers\"\u003eContainers\u003c/a\u003e\u003c/strong\u003e\n\u003cul dir=\"auto\"\u003e\n\u003cli\u003e\u003ca href=\"#stack\"\u003eStack\u003c/a\u003e\u003c/li\u003e\n\u003cli\u003e\u003ca href=\"#queue\"\u003eQueue\u003c/a\u003e\u003c/li\u003e\n\u003cli\u003e\u003ca href=\"#set\"\u003eSet\u003c/a\u003e\u003c/li\u003e\n\u003cli\u003e\u003ca href=\"#list\"\u003eList\u003c/a\u003e\u003c/li\u003e\n\u003cli\u003e\u003ca href=\"#priorityqueue\"\u003ePriorityQueue\u003c/a\u003e\u003c/li\u003e\n\u003cli\u003e\u003ca href=\"#linkedMap\"\u003eLinkedMap\u003c/a\u003e\u003c/li\u003e\n\u003cli\u003e\u003ca href=\"#bTree\"\u003eBTree\u003c/a\u003e\u003c/li\u003e\n\u003cli\u003e\u003ca href=\"#others\"\u003eOthers\u003c/a\u003e\u003c/li\u003e\n\u003c/ul\u003e\n\u003c/li\u003e\n\u003cli\u003e\u003cstrong\u003e\u003ca href=\"#Utilities\"\u003eUtilities\u003c/a\u003e\u003c/strong\u003e\n\u003cul dir=\"auto\"\u003e\n\u003cli\u003e\u003ca href=\"#Comparator\"\u003eComparator\u003c/a\u003e\u003c/li\u003e\n\u003cli\u003e\u003ca href=\"#sort\"\u003eSort\u003c/a\u003e\u003c/li\u003e\n\u003cli\u003e\u003ca href=\"#heap\"\u003eHeap\u003c/a\u003e\u003c/li\u003e\n\u003c/ul\u003e\n\u003c/li\u003e\n\u003cli\u003e\u003cstrong\u003e\u003ca href=\"#contribute-to-this-repo\"\u003eContribute to this repo\u003c/a\u003e\u003c/strong\u003e\u003c/li\u003e\n\u003cli\u003e\u003cstrong\u003e\u003ca href=\"#support\"\u003eSupport\u003c/a\u003e\u003c/strong\u003e\u003c/li\u003e\n\u003c/ul\u003e\n\u003cdiv class=\"markdown-heading\" dir=\"auto\"\u003e\u003ch1 tabindex=\"-1\" class=\"heading-element\" dir=\"auto\"\u003eHow to use this repo\u003c/h1\u003e\u003ca id=\"user-content-how-to-use-this-repo\" class=\"anchor\" aria-label=\"Permalink: How to use this repo\" href=\"#how-to-use-this-repo\"\u003e\u003csvg class=\"octicon octicon-link\" viewBox=\"0 0 16 16\" version=\"1.1\" width=\"16\" height=\"16\" aria-hidden=\"true\"\u003e\u003cpath d=\"m7.775 3.275 1.25-1.25a3.5 3.5 0 1 1 4.95 4.95l-2.5 2.5a3.5 3.5 0 0 1-4.95 0 .751.751 0 0 1 .018-1.042.751.751 0 0 1 1.042-.018 1.998 1.998 0 0 0 2.83 0l2.5-2.5a2.002 2.002 0 0 0-2.83-2.83l-1.25 1.25a.751.751 0 0 1-1.042-.018.751.751 0 0 1-.018-1.042Zm-4.69 9.64a1.998 1.998 0 0 0 2.83 0l1.25-1.25a.751.751 0 0 1 1.042.018.751.751 0 0 1 .018 1.042l-1.25 1.25a3.5 3.5 0 1 1-4.95-4.95l2.5-2.5a3.5 3.5 0 0 1 4.95 0 .751.751 0 0 1-.018 1.042.751.751 0 0 1-1.042.018 1.998 1.998 0 0 0-2.83 0l-2.5 2.5a1.998 1.998 0 0 0 0 2.83Z\"\u003e\u003c/path\u003e\u003c/svg\u003e\u003c/a\u003e\u003c/div\u003e\n\u003cp dir=\"auto\"\u003eIt's very straightforward, just imports the containers you need and then use them directly. The following is an example for ArrayList,\u003c/p\u003e\n\u003cdiv class=\"highlight highlight-source-go notranslate position-relative overflow-auto\" dir=\"auto\" data-snippet-clipboard-copy-content=\"package main\n\nimport (\n\t\u0026quot;fmt\u0026quot;\n\n\t\u0026quot;github.com/ahrtr/gocontainer/list\u0026quot;\n)\n\nfunc main() {\n\tal := list.NewArrayList()\n\tal.Add(5, 6, 7)\n\n\t// Iterate all the elements \n\tfmt.Println(\u0026quot;Iterate (method 1): \u0026quot;)\n\tfor i := 0; i \u0026lt; al.Len(); i++ {\n\t\tv, _ := al.Get(i)\n\t\tfmt.Printf(\u0026quot; Index: %d, value: %v\\n\u0026quot;, i, v)\n\t}\n}\"\u003e\u003cpre\u003e\u003cspan class=\"pl-k\"\u003epackage\u003c/span\u003e main\n\n\u003cspan class=\"pl-k\"\u003eimport\u003c/span\u003e (\n\t\u003cspan class=\"pl-s\"\u003e\"fmt\"\u003c/span\u003e\n\n\t\u003cspan class=\"pl-s\"\u003e\"github.com/ahrtr/gocontainer/list\"\u003c/span\u003e\n)\n\n\u003cspan class=\"pl-k\"\u003efunc\u003c/span\u003e \u003cspan class=\"pl-s1\"\u003emain\u003c/span\u003e() {\n\t\u003cspan class=\"pl-s1\"\u003eal\u003c/span\u003e \u003cspan class=\"pl-c1\"\u003e:=\u003c/span\u003e \u003cspan class=\"pl-s1\"\u003elist\u003c/span\u003e.\u003cspan class=\"pl-c1\"\u003eNewArrayList\u003c/span\u003e()\n\t\u003cspan class=\"pl-s1\"\u003eal\u003c/span\u003e.\u003cspan class=\"pl-c1\"\u003eAdd\u003c/span\u003e(\u003cspan class=\"pl-c1\"\u003e5\u003c/span\u003e, \u003cspan class=\"pl-c1\"\u003e6\u003c/span\u003e, \u003cspan class=\"pl-c1\"\u003e7\u003c/span\u003e)\n\n\t\u003cspan class=\"pl-c\"\u003e// Iterate all the elements \u003c/span\u003e\n\t\u003cspan class=\"pl-s1\"\u003efmt\u003c/span\u003e.\u003cspan class=\"pl-c1\"\u003ePrintln\u003c/span\u003e(\u003cspan class=\"pl-s\"\u003e\"Iterate (method 1): \"\u003c/span\u003e)\n\t\u003cspan class=\"pl-k\"\u003efor\u003c/span\u003e \u003cspan class=\"pl-s1\"\u003ei\u003c/span\u003e \u003cspan class=\"pl-c1\"\u003e:=\u003c/span\u003e \u003cspan class=\"pl-c1\"\u003e0\u003c/span\u003e; \u003cspan class=\"pl-s1\"\u003ei\u003c/span\u003e \u003cspan class=\"pl-c1\"\u003e\u0026lt;\u003c/span\u003e \u003cspan class=\"pl-s1\"\u003eal\u003c/span\u003e.\u003cspan class=\"pl-c1\"\u003eLen\u003c/span\u003e(); \u003cspan class=\"pl-s1\"\u003ei\u003c/span\u003e\u003cspan class=\"pl-c1\"\u003e++\u003c/span\u003e {\n\t\t\u003cspan class=\"pl-s1\"\u003ev\u003c/span\u003e, \u003cspan class=\"pl-s1\"\u003e_\u003c/span\u003e \u003cspan class=\"pl-c1\"\u003e:=\u003c/span\u003e \u003cspan class=\"pl-s1\"\u003eal\u003c/span\u003e.\u003cspan class=\"pl-c1\"\u003eGet\u003c/span\u003e(\u003cspan class=\"pl-s1\"\u003ei\u003c/span\u003e)\n\t\t\u003cspan class=\"pl-s1\"\u003efmt\u003c/span\u003e.\u003cspan class=\"pl-c1\"\u003ePrintf\u003c/span\u003e(\u003cspan class=\"pl-s\"\u003e\" Index: %d, value: %v\u003cspan class=\"pl-cce\"\u003e\\n\u003c/span\u003e\"\u003c/span\u003e, \u003cspan class=\"pl-s1\"\u003ei\u003c/span\u003e, \u003cspan class=\"pl-s1\"\u003ev\u003c/span\u003e)\n\t}\n}\u003c/pre\u003e\u003c/div\u003e\n\u003cp dir=\"auto\"\u003ePlease find more examples \u003cstrong\u003e\u003ca href=\"/ahrtr/gocontainer/blob/master/examples\"\u003ehere\u003c/a\u003e\u003c/strong\u003e.\u003c/p\u003e\n\u003cdiv class=\"markdown-heading\" dir=\"auto\"\u003e\u003ch1 tabindex=\"-1\" class=\"heading-element\" dir=\"auto\"\u003eCommon Interface\u003c/h1\u003e\u003ca id=\"user-content-common-interface\" class=\"anchor\" aria-label=\"Permalink: Common Interface\" href=\"#common-interface\"\u003e\u003csvg class=\"octicon octicon-link\" viewBox=\"0 0 16 16\" version=\"1.1\" width=\"16\" height=\"16\" aria-hidden=\"true\"\u003e\u003cpath d=\"m7.775 3.275 1.25-1.25a3.5 3.5 0 1 1 4.95 4.95l-2.5 2.5a3.5 3.5 0 0 1-4.95 0 .751.751 0 0 1 .018-1.042.751.751 0 0 1 1.042-.018 1.998 1.998 0 0 0 2.83 0l2.5-2.5a2.002 2.002 0 0 0-2.83-2.83l-1.25 1.25a.751.751 0 0 1-1.042-.018.751.751 0 0 1-.018-1.042Zm-4.69 9.64a1.998 1.998 0 0 0 2.83 0l1.25-1.25a.751.751 0 0 1 1.042.018.751.751 0 0 1 .018 1.042l-1.25 1.25a3.5 3.5 0 1 1-4.95-4.95l2.5-2.5a3.5 3.5 0 0 1 4.95 0 .751.751 0 0 1-.018 1.042.751.751 0 0 1-1.042.018 1.998 1.998 0 0 0-2.83 0l-2.5 2.5a1.998 1.998 0 0 0 0 2.83Z\"\u003e\u003c/path\u003e\u003c/svg\u003e\u003c/a\u003e\u003c/div\u003e\n\u003cp dir=\"auto\"\u003eAll containers in this repository implement interface \u003cstrong\u003ecollection.Interface\u003c/strong\u003e,\u003c/p\u003e\n\u003cdiv class=\"highlight highlight-source-go notranslate position-relative overflow-auto\" dir=\"auto\" data-snippet-clipboard-copy-content=\"// Interface is a type of collection, all containers should implement this interface.\ntype Interface interface {\n\t// Size returns the number of elements in the collection.\n\tSize() int\n\t// IsEmpty returns true if this container contains no elements.\n\tIsEmpty() bool\n\t// Clear removes all of the elements from this container.\n\tClear()\n}\"\u003e\u003cpre\u003e\u003cspan class=\"pl-c\"\u003e// Interface is a type of collection, all containers should implement this interface.\u003c/span\u003e\n\u003cspan class=\"pl-k\"\u003etype\u003c/span\u003e \u003cspan class=\"pl-smi\"\u003eInterface\u003c/span\u003e \u003cspan class=\"pl-k\"\u003einterface\u003c/span\u003e {\n\t\u003cspan class=\"pl-c\"\u003e// Size returns the number of elements in the collection.\u003c/span\u003e\n\t\u003cspan class=\"pl-c1\"\u003eSize\u003c/span\u003e() \u003cspan class=\"pl-smi\"\u003eint\u003c/span\u003e\n\t\u003cspan class=\"pl-c\"\u003e// IsEmpty returns true if this container contains no elements.\u003c/span\u003e\n\t\u003cspan class=\"pl-c1\"\u003eIsEmpty\u003c/span\u003e() \u003cspan class=\"pl-smi\"\u003ebool\u003c/span\u003e\n\t\u003cspan class=\"pl-c\"\u003e// Clear removes all of the elements from this container.\u003c/span\u003e\n\t\u003cspan class=\"pl-c1\"\u003eClear\u003c/span\u003e()\n}\u003c/pre\u003e\u003c/div\u003e\n\u003cdiv class=\"markdown-heading\" dir=\"auto\"\u003e\u003ch1 tabindex=\"-1\" class=\"heading-element\" dir=\"auto\"\u003eContainers\u003c/h1\u003e\u003ca id=\"user-content-containers\" class=\"anchor\" aria-label=\"Permalink: Containers\" href=\"#containers\"\u003e\u003csvg class=\"octicon octicon-link\" viewBox=\"0 0 16 16\" version=\"1.1\" width=\"16\" height=\"16\" aria-hidden=\"true\"\u003e\u003cpath d=\"m7.775 3.275 1.25-1.25a3.5 3.5 0 1 1 4.95 4.95l-2.5 2.5a3.5 3.5 0 0 1-4.95 0 .751.751 0 0 1 .018-1.042.751.751 0 0 1 1.042-.018 1.998 1.998 0 0 0 2.83 0l2.5-2.5a2.002 2.002 0 0 0-2.83-2.83l-1.25 1.25a.751.751 0 0 1-1.042-.018.751.751 0 0 1-.018-1.042Zm-4.69 9.64a1.998 1.998 0 0 0 2.83 0l1.25-1.25a.751.751 0 0 1 1.042.018.751.751 0 0 1 .018 1.042l-1.25 1.25a3.5 3.5 0 1 1-4.95-4.95l2.5-2.5a3.5 3.5 0 0 1 4.95 0 .751.751 0 0 1-.018 1.042.751.751 0 0 1-1.042.018 1.998 1.998 0 0 0-2.83 0l-2.5 2.5a1.998 1.998 0 0 0 0 2.83Z\"\u003e\u003c/path\u003e\u003c/svg\u003e\u003c/a\u003e\u003c/div\u003e\n\u003cp dir=\"auto\"\u003eCurrently this library implements the following containers:\u003c/p\u003e\n\u003cul dir=\"auto\"\u003e\n\u003cli\u003eStack\u003c/li\u003e\n\u003cli\u003eQueue\u003c/li\u003e\n\u003cli\u003eSet\u003c/li\u003e\n\u003cli\u003eList (ArrayList, LinkedList)\u003c/li\u003e\n\u003cli\u003ePriorityQueue\u003c/li\u003e\n\u003cli\u003eLinkedMap\u003c/li\u003e\n\u003cli\u003eBTree\u003c/li\u003e\n\u003c/ul\u003e\n\u003cdiv class=\"markdown-heading\" dir=\"auto\"\u003e\u003ch2 tabindex=\"-1\" class=\"heading-element\" dir=\"auto\"\u003eStack\u003c/h2\u003e\u003ca id=\"user-content-stack\" class=\"anchor\" aria-label=\"Permalink: Stack\" href=\"#stack\"\u003e\u003csvg class=\"octicon octicon-link\" viewBox=\"0 0 16 16\" version=\"1.1\" width=\"16\" height=\"16\" aria-hidden=\"true\"\u003e\u003cpath d=\"m7.775 3.275 1.25-1.25a3.5 3.5 0 1 1 4.95 4.95l-2.5 2.5a3.5 3.5 0 0 1-4.95 0 .751.751 0 0 1 .018-1.042.751.751 0 0 1 1.042-.018 1.998 1.998 0 0 0 2.83 0l2.5-2.5a2.002 2.002 0 0 0-2.83-2.83l-1.25 1.25a.751.751 0 0 1-1.042-.018.751.751 0 0 1-.018-1.042Zm-4.69 9.64a1.998 1.998 0 0 0 2.83 0l1.25-1.25a.751.751 0 0 1 1.042.018.751.751 0 0 1 .018 1.042l-1.25 1.25a3.5 3.5 0 1 1-4.95-4.95l2.5-2.5a3.5 3.5 0 0 1 4.95 0 .751.751 0 0 1-.018 1.042.751.751 0 0 1-1.042.018 1.998 1.998 0 0 0-2.83 0l-2.5 2.5a1.998 1.998 0 0 0 0 2.83Z\"\u003e\u003c/path\u003e\u003c/svg\u003e\u003c/a\u003e\u003c/div\u003e\n\u003cp dir=\"auto\"\u003eStack is a LIFO(last-in-first-out) container. It implements the following interface. Click \u003cstrong\u003e\u003ca href=\"/ahrtr/gocontainer/blob/master/examples/stack_example.go\"\u003ehere\u003c/a\u003e\u003c/strong\u003e to find examples on how to use a stack.\u003c/p\u003e\n\u003cdiv class=\"highlight highlight-source-go notranslate position-relative overflow-auto\" dir=\"auto\" data-snippet-clipboard-copy-content=\"// Interface is a stack, which is LIFO (last-in-first-out).\ntype Interface interface {\n\tcollection.Interface\n\n\t// Push pushes an element into this stack.\n\tPush(val interface{})\n\t// Pop pops the element on the top of this stack.\n\tPop() interface{}\n\t// Peek retrieves, but does not remove, the element on the top of this stack, or return nil if this stack is empty.\n\tPeek() interface{}\n}\"\u003e\u003cpre\u003e\u003cspan class=\"pl-c\"\u003e// Interface is a stack, which is LIFO (last-in-first-out).\u003c/span\u003e\n\u003cspan class=\"pl-k\"\u003etype\u003c/span\u003e \u003cspan class=\"pl-smi\"\u003eInterface\u003c/span\u003e \u003cspan class=\"pl-k\"\u003einterface\u003c/span\u003e {\n\tcollection.\u003cspan class=\"pl-smi\"\u003eInterface\u003c/span\u003e\n\n\t\u003cspan class=\"pl-c\"\u003e// Push pushes an element into this stack.\u003c/span\u003e\n\t\u003cspan class=\"pl-c1\"\u003ePush\u003c/span\u003e(\u003cspan class=\"pl-s1\"\u003eval\u003c/span\u003e \u003cspan class=\"pl-k\"\u003einterface\u003c/span\u003e{})\n\t\u003cspan class=\"pl-c\"\u003e// Pop pops the element on the top of this stack.\u003c/span\u003e\n\t\u003cspan class=\"pl-c1\"\u003ePop\u003c/span\u003e() \u003cspan class=\"pl-k\"\u003einterface\u003c/span\u003e{}\n\t\u003cspan class=\"pl-c\"\u003e// Peek retrieves, but does not remove, the element on the top of this stack, or return nil if this stack is empty.\u003c/span\u003e\n\t\u003cspan class=\"pl-c1\"\u003ePeek\u003c/span\u003e() \u003cspan class=\"pl-k\"\u003einterface\u003c/span\u003e{}\n}\u003c/pre\u003e\u003c/div\u003e\n\u003cp dir=\"auto\"\u003ePlease import the following package in order to use stack,\u003c/p\u003e\n\u003cdiv class=\"highlight highlight-source-go notranslate position-relative overflow-auto\" dir=\"auto\" data-snippet-clipboard-copy-content=\"import (\n\t\u0026quot;github.com/ahrtr/gocontainer/stack\u0026quot;\n)\"\u003e\u003cpre\u003e\u003cspan class=\"pl-k\"\u003eimport\u003c/span\u003e (\n\t\u003cspan class=\"pl-s\"\u003e\"github.com/ahrtr/gocontainer/stack\"\u003c/span\u003e\n)\u003c/pre\u003e\u003c/div\u003e\n\u003cp dir=\"auto\"\u003eCall stack.New() to create a stack,\u003c/p\u003e\n\u003cdiv class=\"highlight highlight-source-go notranslate position-relative overflow-auto\" dir=\"auto\" data-snippet-clipboard-copy-content=\"New() Interface\"\u003e\u003cpre\u003e\u003cspan class=\"pl-s1\"\u003eNew\u003c/span\u003e() \u003cspan class=\"pl-s1\"\u003eInterface\u003c/span\u003e\u003c/pre\u003e\u003c/div\u003e\n\u003cp dir=\"auto\"\u003eThe following is a simple example for stack,\u003c/p\u003e\n\u003cdiv class=\"highlight highlight-source-go notranslate position-relative overflow-auto\" dir=\"auto\" data-snippet-clipboard-copy-content=\"package main\n\nimport (\n\t\u0026quot;fmt\u0026quot;\n\n\t\u0026quot;github.com/ahrtr/gocontainer/stack\u0026quot;\n)\n\nfunc main() {\n\ts := stack.New()\n\n\tvalues := []int{5, 6, 7}\n\tfor _, v := range values {\n\t\ts.Push(v)\n\t}\n\n\tfor s.Size() \u0026gt; 0 {\n\t\tfmt.Printf(\u0026quot;s.Pop() = %v\\n\u0026quot;, s.Pop())\n\t}\n}\"\u003e\u003cpre\u003e\u003cspan class=\"pl-k\"\u003epackage\u003c/span\u003e main\n\n\u003cspan class=\"pl-k\"\u003eimport\u003c/span\u003e (\n\t\u003cspan class=\"pl-s\"\u003e\"fmt\"\u003c/span\u003e\n\n\t\u003cspan class=\"pl-s\"\u003e\"github.com/ahrtr/gocontainer/stack\"\u003c/span\u003e\n)\n\n\u003cspan class=\"pl-k\"\u003efunc\u003c/span\u003e \u003cspan class=\"pl-s1\"\u003emain\u003c/span\u003e() {\n\t\u003cspan class=\"pl-s1\"\u003es\u003c/span\u003e \u003cspan class=\"pl-c1\"\u003e:=\u003c/span\u003e \u003cspan class=\"pl-s1\"\u003estack\u003c/span\u003e.\u003cspan class=\"pl-c1\"\u003eNew\u003c/span\u003e()\n\n\t\u003cspan class=\"pl-s1\"\u003evalues\u003c/span\u003e \u003cspan class=\"pl-c1\"\u003e:=\u003c/span\u003e []\u003cspan class=\"pl-smi\"\u003eint\u003c/span\u003e{\u003cspan class=\"pl-c1\"\u003e5\u003c/span\u003e, \u003cspan class=\"pl-c1\"\u003e6\u003c/span\u003e, \u003cspan class=\"pl-c1\"\u003e7\u003c/span\u003e}\n\t\u003cspan class=\"pl-k\"\u003efor\u003c/span\u003e \u003cspan class=\"pl-s1\"\u003e_\u003c/span\u003e, \u003cspan class=\"pl-s1\"\u003ev\u003c/span\u003e \u003cspan class=\"pl-c1\"\u003e:=\u003c/span\u003e \u003cspan class=\"pl-k\"\u003erange\u003c/span\u003e \u003cspan class=\"pl-s1\"\u003evalues\u003c/span\u003e {\n\t\t\u003cspan class=\"pl-s1\"\u003es\u003c/span\u003e.\u003cspan class=\"pl-c1\"\u003ePush\u003c/span\u003e(\u003cspan class=\"pl-s1\"\u003ev\u003c/span\u003e)\n\t}\n\n\t\u003cspan class=\"pl-k\"\u003efor\u003c/span\u003e \u003cspan class=\"pl-s1\"\u003es\u003c/span\u003e.\u003cspan class=\"pl-c1\"\u003eSize\u003c/span\u003e() \u003cspan class=\"pl-c1\"\u003e\u0026gt;\u003c/span\u003e \u003cspan class=\"pl-c1\"\u003e0\u003c/span\u003e {\n\t\t\u003cspan class=\"pl-s1\"\u003efmt\u003c/span\u003e.\u003cspan class=\"pl-c1\"\u003ePrintf\u003c/span\u003e(\u003cspan class=\"pl-s\"\u003e\"s.Pop() = %v\u003cspan class=\"pl-cce\"\u003e\\n\u003c/span\u003e\"\u003c/span\u003e, \u003cspan class=\"pl-s1\"\u003es\u003c/span\u003e.\u003cspan class=\"pl-c1\"\u003ePop\u003c/span\u003e())\n\t}\n}\u003c/pre\u003e\u003c/div\u003e\n\u003cdiv class=\"markdown-heading\" dir=\"auto\"\u003e\u003ch2 tabindex=\"-1\" class=\"heading-element\" dir=\"auto\"\u003eQueue\u003c/h2\u003e\u003ca id=\"user-content-queue\" class=\"anchor\" aria-label=\"Permalink: Queue\" href=\"#queue\"\u003e\u003csvg class=\"octicon octicon-link\" viewBox=\"0 0 16 16\" version=\"1.1\" width=\"16\" height=\"16\" aria-hidden=\"true\"\u003e\u003cpath d=\"m7.775 3.275 1.25-1.25a3.5 3.5 0 1 1 4.95 4.95l-2.5 2.5a3.5 3.5 0 0 1-4.95 0 .751.751 0 0 1 .018-1.042.751.751 0 0 1 1.042-.018 1.998 1.998 0 0 0 2.83 0l2.5-2.5a2.002 2.002 0 0 0-2.83-2.83l-1.25 1.25a.751.751 0 0 1-1.042-.018.751.751 0 0 1-.018-1.042Zm-4.69 9.64a1.998 1.998 0 0 0 2.83 0l1.25-1.25a.751.751 0 0 1 1.042.018.751.751 0 0 1 .018 1.042l-1.25 1.25a3.5 3.5 0 1 1-4.95-4.95l2.5-2.5a3.5 3.5 0 0 1 4.95 0 .751.751 0 0 1-.018 1.042.751.751 0 0 1-1.042.018 1.998 1.998 0 0 0-2.83 0l-2.5 2.5a1.998 1.998 0 0 0 0 2.83Z\"\u003e\u003c/path\u003e\u003c/svg\u003e\u003c/a\u003e\u003c/div\u003e\n\u003cp dir=\"auto\"\u003eQueue is a FIFO(first-in-first-out) container. It implements the following interface. Click \u003cstrong\u003e\u003ca href=\"/ahrtr/gocontainer/blob/master/examples/queue_example.go\"\u003ehere\u003c/a\u003e\u003c/strong\u003e to find examples on how to use a queue.\u003c/p\u003e\n\u003cdiv class=\"highlight highlight-source-go notranslate position-relative overflow-auto\" dir=\"auto\" data-snippet-clipboard-copy-content=\"// Interface is a type of queue, which is FIFO(first-in-first-out).\ntype Interface interface {\n\tcollection.Interface\n\n\t// Add inserts an element into the tail of this queue.\n\tAdd(vals ...interface{})\n\t// Peek retrieves, but does not remove, the head of this queue, or return nil if this queue is empty.\n\tPeek() interface{}\n\t// Poll retrieves and removes the head of the this queue, or return nil if this queue is empty.\n\tPoll() interface{}\n}\"\u003e\u003cpre\u003e\u003cspan class=\"pl-c\"\u003e// Interface is a type of queue, which is FIFO(first-in-first-out).\u003c/span\u003e\n\u003cspan class=\"pl-k\"\u003etype\u003c/span\u003e \u003cspan class=\"pl-smi\"\u003eInterface\u003c/span\u003e \u003cspan class=\"pl-k\"\u003einterface\u003c/span\u003e {\n\tcollection.\u003cspan class=\"pl-smi\"\u003eInterface\u003c/span\u003e\n\n\t\u003cspan class=\"pl-c\"\u003e// Add inserts an element into the tail of this queue.\u003c/span\u003e\n\t\u003cspan class=\"pl-c1\"\u003eAdd\u003c/span\u003e(\u003cspan class=\"pl-s1\"\u003evals\u003c/span\u003e \u003cspan class=\"pl-c1\"\u003e...\u003c/span\u003e\u003cspan class=\"pl-k\"\u003einterface\u003c/span\u003e{})\n\t\u003cspan class=\"pl-c\"\u003e// Peek retrieves, but does not remove, the head of this queue, or return nil if this queue is empty.\u003c/span\u003e\n\t\u003cspan class=\"pl-c1\"\u003ePeek\u003c/span\u003e() \u003cspan class=\"pl-k\"\u003einterface\u003c/span\u003e{}\n\t\u003cspan class=\"pl-c\"\u003e// Poll retrieves and removes the head of the this queue, or return nil if this queue is empty.\u003c/span\u003e\n\t\u003cspan class=\"pl-c1\"\u003ePoll\u003c/span\u003e() \u003cspan class=\"pl-k\"\u003einterface\u003c/span\u003e{}\n}\u003c/pre\u003e\u003c/div\u003e\n\u003cp dir=\"auto\"\u003ePlease import the following package in order to use queue,\u003c/p\u003e\n\u003cdiv class=\"highlight highlight-source-go notranslate position-relative overflow-auto\" dir=\"auto\" data-snippet-clipboard-copy-content=\"import (\n\t\u0026quot;github.com/ahrtr/gocontainer/queue\u0026quot;\n)\"\u003e\u003cpre\u003e\u003cspan class=\"pl-k\"\u003eimport\u003c/span\u003e (\n\t\u003cspan class=\"pl-s\"\u003e\"github.com/ahrtr/gocontainer/queue\"\u003c/span\u003e\n)\u003c/pre\u003e\u003c/div\u003e\n\u003cp dir=\"auto\"\u003eCall queue.New() to create a queue,\u003c/p\u003e\n\u003cdiv class=\"highlight highlight-source-go notranslate position-relative overflow-auto\" dir=\"auto\" data-snippet-clipboard-copy-content=\"New() Interface\"\u003e\u003cpre\u003e\u003cspan class=\"pl-s1\"\u003eNew\u003c/span\u003e() \u003cspan class=\"pl-s1\"\u003eInterface\u003c/span\u003e\u003c/pre\u003e\u003c/div\u003e\n\u003cp dir=\"auto\"\u003eThe following is a simple example for queue,\u003c/p\u003e\n\u003cdiv class=\"highlight highlight-source-go notranslate position-relative overflow-auto\" dir=\"auto\" data-snippet-clipboard-copy-content=\"package main\n\nimport (\n\t\u0026quot;fmt\u0026quot;\n\n\t\u0026quot;github.com/ahrtr/gocontainer/queue\u0026quot;\n)\n\nfunc main() {\n\tq := queue.New()\n\n\tvalues := []string{\u0026quot;benjamin\u0026quot;, \u0026quot;alice\u0026quot;, \u0026quot;john\u0026quot;, \u0026quot;tom\u0026quot;, \u0026quot;bill\u0026quot;}\n\n\tfor _, v := range values {\n\t\tq.Add(v)\n\t}\n\n\tfor q.Peek() != nil {\n\t\tfmt.Printf(\u0026quot;q.Peek() = %v\\n\u0026quot;, q.Peek())\n\t\tfmt.Printf(\u0026quot;q.Poll() = %v\\n\u0026quot;, q.Poll())\n\t}\n}\"\u003e\u003cpre\u003e\u003cspan class=\"pl-k\"\u003epackage\u003c/span\u003e main\n\n\u003cspan class=\"pl-k\"\u003eimport\u003c/span\u003e (\n\t\u003cspan class=\"pl-s\"\u003e\"fmt\"\u003c/span\u003e\n\n\t\u003cspan class=\"pl-s\"\u003e\"github.com/ahrtr/gocontainer/queue\"\u003c/span\u003e\n)\n\n\u003cspan class=\"pl-k\"\u003efunc\u003c/span\u003e \u003cspan class=\"pl-s1\"\u003emain\u003c/span\u003e() {\n\t\u003cspan class=\"pl-s1\"\u003eq\u003c/span\u003e \u003cspan class=\"pl-c1\"\u003e:=\u003c/span\u003e \u003cspan class=\"pl-s1\"\u003equeue\u003c/span\u003e.\u003cspan class=\"pl-c1\"\u003eNew\u003c/span\u003e()\n\n\t\u003cspan class=\"pl-s1\"\u003evalues\u003c/span\u003e \u003cspan class=\"pl-c1\"\u003e:=\u003c/span\u003e []\u003cspan class=\"pl-smi\"\u003estring\u003c/span\u003e{\u003cspan class=\"pl-s\"\u003e\"benjamin\"\u003c/span\u003e, \u003cspan class=\"pl-s\"\u003e\"alice\"\u003c/span\u003e, \u003cspan class=\"pl-s\"\u003e\"john\"\u003c/span\u003e, \u003cspan class=\"pl-s\"\u003e\"tom\"\u003c/span\u003e, \u003cspan class=\"pl-s\"\u003e\"bill\"\u003c/span\u003e}\n\n\t\u003cspan class=\"pl-k\"\u003efor\u003c/span\u003e \u003cspan class=\"pl-s1\"\u003e_\u003c/span\u003e, \u003cspan class=\"pl-s1\"\u003ev\u003c/span\u003e \u003cspan class=\"pl-c1\"\u003e:=\u003c/span\u003e \u003cspan class=\"pl-k\"\u003erange\u003c/span\u003e \u003cspan class=\"pl-s1\"\u003evalues\u003c/span\u003e {\n\t\t\u003cspan class=\"pl-s1\"\u003eq\u003c/span\u003e.\u003cspan class=\"pl-c1\"\u003eAdd\u003c/span\u003e(\u003cspan class=\"pl-s1\"\u003ev\u003c/span\u003e)\n\t}\n\n\t\u003cspan class=\"pl-k\"\u003efor\u003c/span\u003e \u003cspan class=\"pl-s1\"\u003eq\u003c/span\u003e.\u003cspan class=\"pl-c1\"\u003ePeek\u003c/span\u003e() \u003cspan class=\"pl-c1\"\u003e!=\u003c/span\u003e \u003cspan class=\"pl-c1\"\u003enil\u003c/span\u003e {\n\t\t\u003cspan class=\"pl-s1\"\u003efmt\u003c/span\u003e.\u003cspan class=\"pl-c1\"\u003ePrintf\u003c/span\u003e(\u003cspan class=\"pl-s\"\u003e\"q.Peek() = %v\u003cspan class=\"pl-cce\"\u003e\\n\u003c/span\u003e\"\u003c/span\u003e, \u003cspan class=\"pl-s1\"\u003eq\u003c/span\u003e.\u003cspan class=\"pl-c1\"\u003ePeek\u003c/span\u003e())\n\t\t\u003cspan class=\"pl-s1\"\u003efmt\u003c/span\u003e.\u003cspan class=\"pl-c1\"\u003ePrintf\u003c/span\u003e(\u003cspan class=\"pl-s\"\u003e\"q.Poll() = %v\u003cspan class=\"pl-cce\"\u003e\\n\u003c/span\u003e\"\u003c/span\u003e, \u003cspan class=\"pl-s1\"\u003eq\u003c/span\u003e.\u003cspan class=\"pl-c1\"\u003ePoll\u003c/span\u003e())\n\t}\n}\u003c/pre\u003e\u003c/div\u003e\n\u003cdiv class=\"markdown-heading\" dir=\"auto\"\u003e\u003ch2 tabindex=\"-1\" class=\"heading-element\" dir=\"auto\"\u003eSet\u003c/h2\u003e\u003ca id=\"user-content-set\" class=\"anchor\" aria-label=\"Permalink: Set\" href=\"#set\"\u003e\u003csvg class=\"octicon octicon-link\" viewBox=\"0 0 16 16\" version=\"1.1\" width=\"16\" height=\"16\" aria-hidden=\"true\"\u003e\u003cpath d=\"m7.775 3.275 1.25-1.25a3.5 3.5 0 1 1 4.95 4.95l-2.5 2.5a3.5 3.5 0 0 1-4.95 0 .751.751 0 0 1 .018-1.042.751.751 0 0 1 1.042-.018 1.998 1.998 0 0 0 2.83 0l2.5-2.5a2.002 2.002 0 0 0-2.83-2.83l-1.25 1.25a.751.751 0 0 1-1.042-.018.751.751 0 0 1-.018-1.042Zm-4.69 9.64a1.998 1.998 0 0 0 2.83 0l1.25-1.25a.751.751 0 0 1 1.042.018.751.751 0 0 1 .018 1.042l-1.25 1.25a3.5 3.5 0 1 1-4.95-4.95l2.5-2.5a3.5 3.5 0 0 1 4.95 0 .751.751 0 0 1-.018 1.042.751.751 0 0 1-1.042.018 1.998 1.998 0 0 0-2.83 0l-2.5 2.5a1.998 1.998 0 0 0 0 2.83Z\"\u003e\u003c/path\u003e\u003c/svg\u003e\u003c/a\u003e\u003c/div\u003e\n\u003cp dir=\"auto\"\u003eA set contains no duplicate elements. The values contained in a set may be any type that is comparable, please refer to the golang \u003ca href=\"https://golang.org/ref/spec#Comparison_operators\" rel=\"nofollow\"\u003elanguage spec\u003c/a\u003e to get more detailed info on comparison operators.\u003c/p\u003e\n\u003cp dir=\"auto\"\u003eSet implements the following interface. Click \u003cstrong\u003e\u003ca href=\"/ahrtr/gocontainer/blob/master/examples/set_example.go\"\u003ehere\u003c/a\u003e\u003c/strong\u003e to find examples on how to use a set.\u003c/p\u003e\n\u003cdiv class=\"highlight highlight-source-go notranslate position-relative overflow-auto\" dir=\"auto\" data-snippet-clipboard-copy-content=\"// Interface is a type of set, which contains no duplicate elements.\ntype Interface interface {\n\tcollection.Interface\n\n\t// Add adds the specified values to this set if they are not already present.\n\t// It returns false if any value is already present.\n\tAdd(vals ...interface{}) bool\n\t// Contains returns true if this set contains the specified element.\n\tContains(val interface{}) bool\n\t// Remove removes the specif 10000 ied element from this set if it is present.\n\t// It returns false if the target value isn't present, otherwise returns true.\n\tRemove(val interface{}) bool\n\t// Iterate iterates all the elements in this set.\n\tIterate(cb IterateCallback)\n}\"\u003e\u003cpre\u003e\u003cspan class=\"pl-c\"\u003e// Interface is a type of set, which contains no duplicate elements.\u003c/span\u003e\n\u003cspan class=\"pl-k\"\u003etype\u003c/span\u003e \u003cspan class=\"pl-smi\"\u003eInterface\u003c/span\u003e \u003cspan class=\"pl-k\"\u003einterface\u003c/span\u003e {\n\tcollection.\u003cspan class=\"pl-smi\"\u003eInterface\u003c/span\u003e\n\n\t\u003cspan class=\"pl-c\"\u003e// Add adds the specified values to this set if they are not already present.\u003c/span\u003e\n\t\u003cspan class=\"pl-c\"\u003e// It returns false if any value is already present.\u003c/span\u003e\n\t\u003cspan class=\"pl-c1\"\u003eAdd\u003c/span\u003e(\u003cspan class=\"pl-s1\"\u003evals\u003c/span\u003e \u003cspan class=\"pl-c1\"\u003e...\u003c/span\u003e\u003cspan class=\"pl-k\"\u003einterface\u003c/span\u003e{}) \u003cspan class=\"pl-smi\"\u003ebool\u003c/span\u003e\n\t\u003cspan class=\"pl-c\"\u003e// Contains returns true if this set contains the specified element.\u003c/span\u003e\n\t\u003cspan class=\"pl-c1\"\u003eContains\u003c/span\u003e(\u003cspan class=\"pl-s1\"\u003eval\u003c/span\u003e \u003cspan class=\"pl-k\"\u003einterface\u003c/span\u003e{}) \u003cspan class=\"pl-smi\"\u003ebool\u003c/span\u003e\n\t\u003cspan class=\"pl-c\"\u003e// Remove removes the specified element from this set if it is present.\u003c/span\u003e\n\t\u003cspan class=\"pl-c\"\u003e// It returns false if the target value isn't present, otherwise returns true.\u003c/span\u003e\n\t\u003cspan class=\"pl-c1\"\u003eRemove\u003c/span\u003e(\u003cspan class=\"pl-s1\"\u003eval\u003c/span\u003e \u003cspan class=\"pl-k\"\u003einterface\u003c/span\u003e{}) \u003cspan class=\"pl-smi\"\u003ebool\u003c/span\u003e\n\t\u003cspan class=\"pl-c\"\u003e// Iterate iterates all the elements in this set.\u003c/span\u003e\n\t\u003cspan class=\"pl-c1\"\u003eIterate\u003c/span\u003e(\u003cspan class=\"pl-s1\"\u003ecb\u003c/span\u003e \u003cspan class=\"pl-smi\"\u003eIterateCallback\u003c/span\u003e)\n}\u003c/pre\u003e\u003c/div\u003e\n\u003cp dir=\"auto\"\u003ePlease import the following package in order to use set,\u003c/p\u003e\n\u003cdiv class=\"highlight highlight-source-go notranslate position-relative overflow-auto\" dir=\"auto\" data-snippet-clipboard-copy-content=\"import (\n\t\u0026quot;github.com/ahrtr/gocontainer/set\u0026quot;\n)\"\u003e\u003cpre\u003e\u003cspan class=\"pl-k\"\u003eimport\u003c/span\u003e (\n\t\u003cspan class=\"pl-s\"\u003e\"github.com/ahrtr/gocontainer/set\"\u003c/span\u003e\n)\u003c/pre\u003e\u003c/div\u003e\n\u003cp dir=\"auto\"\u003eCall set.New() to create a set,\u003c/p\u003e\n\u003cdiv class=\"highlight highlight-source-go notranslate position-relative overflow-auto\" dir=\"auto\" data-snippet-clipboard-copy-content=\"New() Interface\"\u003e\u003cpre\u003e\u003cspan class=\"pl-s1\"\u003eNew\u003c/span\u003e() \u003cspan class=\"pl-s1\"\u003eInterface\u003c/span\u003e\u003c/pre\u003e\u003c/div\u003e\n\u003cp dir=\"auto\"\u003eThe following is a simple example for set,\u003c/p\u003e\n\u003cdiv class=\"highlight highlight-source-go notranslate position-relative overflow-auto\" dir=\"auto\" data-snippet-clipboard-copy-content=\"package main\n\nimport (\n\t\u0026quot;fmt\u0026quot;\n\n\t\u0026quot;github.com/ahrtr/gocontainer/set\u0026quot;\n)\n\nfunc main() {\n\ts := set.New()\n\n\tvalues := []int{5, 3, 9, 7, 6}\n\tfor _, v := range values {\n\t\ts.Add(v)\n\t}\n\n\tfor _, v := range values {\n\t\tfmt.Printf(\u0026quot;s.Contains(%v) = %t\\n\u0026quot;, v, s.Contains(v))\n\t}\n\n\t// iterate all the elements, the callback function's signature:\n\t// type IterateCallback func(interface{}) bool\n\ts.Iterate(func(v interface{}) bool {\n\t\tfmt.Printf(\u0026quot;Iterate callback: %v\\n\u0026quot;, v)\n\t\treturn true\n\t})\n\n\ts.Remove(6)\n}\"\u003e\u003cpre\u003e\u003cspan class=\"pl-k\"\u003epackage\u003c/span\u003e main\n\n\u003cspan class=\"pl-k\"\u003eimport\u003c/span\u003e (\n\t\u003cspan class=\"pl-s\"\u003e\"fmt\"\u003c/span\u003e\n\n\t\u003cspan class=\"pl-s\"\u003e\"github.com/ahrtr/gocontainer/set\"\u003c/span\u003e\n)\n\n\u003cspan class=\"pl-k\"\u003efunc\u003c/span\u003e \u003cspan class=\"pl-s1\"\u003emain\u003c/span\u003e() {\n\t\u003cspan class=\"pl-s1\"\u003es\u003c/span\u003e \u003cspan class=\"pl-c1\"\u003e:=\u003c/span\u003e \u003cspan class=\"pl-s1\"\u003eset\u003c/span\u003e.\u003cspan class=\"pl-c1\"\u003eNew\u003c/span\u003e()\n\n\t\u003cspan class=\"pl-s1\"\u003evalues\u003c/span\u003e \u003cspan class=\"pl-c1\"\u003e:=\u003c/span\u003e []\u003cspan class=\"pl-smi\"\u003eint\u003c/span\u003e{\u003cspan class=\"pl-c1\"\u003e5\u003c/span\u003e, \u003cspan class=\"pl-c1\"\u003e3\u003c/span\u003e, \u003cspan class=\"pl-c1\"\u003e9\u003c/span\u003e, \u003cspan class=\"pl-c1\"\u003e7\u003c/span\u003e, \u003cspan class=\"pl-c1\"\u003e6\u003c/span\u003e}\n\t\u003cspan class=\"pl-k\"\u003efor\u003c/span\u003e \u003cspan class=\"pl-s1\"\u003e_\u003c/span\u003e, \u003cspan class=\"pl-s1\"\u003ev\u003c/span\u003e \u003cspan class=\"pl-c1\"\u003e:=\u003c/span\u003e \u003cspan class=\"pl-k\"\u003erange\u003c/span\u003e \u003cspan class=\"pl-s1\"\u003evalues\u003c/span\u003e {\n\t\t\u003cspan class=\"pl-s1\"\u003es\u003c/span\u003e.\u003cspan class=\"pl-c1\"\u003eAdd\u003c/span\u003e(\u003cspan class=\"pl-s1\"\u003ev\u003c/span\u003e)\n\t}\n\n\t\u003cspan class=\"pl-k\"\u003efor\u003c/span\u003e \u003cspan class=\"pl-s1\"\u003e_\u003c/span\u003e, \u003cspan class=\"pl-s1\"\u003ev\u003c/span\u003e \u003cspan class=\"pl-c1\"\u003e:=\u003c/span\u003e \u003cspan class=\"pl-k\"\u003erange\u003c/span\u003e \u003cspan class=\"pl-s1\"\u003evalues\u003c/span\u003e {\n\t\t\u003cspan class=\"pl-s1\"\u003efmt\u003c/span\u003e.\u003cspan class=\"pl-c1\"\u003ePrintf\u003c/span\u003e(\u003cspan class=\"pl-s\"\u003e\"s.Contains(%v) = %t\u003cspan class=\"pl-cce\"\u003e\\n\u003c/span\u003e\"\u003c/span\u003e, \u003cspan class=\"pl-s1\"\u003ev\u003c/span\u003e, \u003cspan class=\"pl-s1\"\u003es\u003c/span\u003e.\u003cspan class=\"pl-c1\"\u003eContains\u003c/span\u003e(\u003cspan class=\"pl-s1\"\u003ev\u003c/span\u003e))\n\t}\n\n\t\u003cspan class=\"pl-c\"\u003e// iterate all the elements, the callback function's signature:\u003c/span\u003e\n\t\u003cspan class=\"pl-c\"\u003e// type IterateCallback func(interface{}) bool\u003c/span\u003e\n\t\u003cspan class=\"pl-s1\"\u003es\u003c/span\u003e.\u003cspan class=\"pl-c1\"\u003eIterate\u003c/span\u003e(\u003cspan class=\"pl-k\"\u003efunc\u003c/span\u003e(\u003cspan class=\"pl-s1\"\u003ev\u003c/span\u003e \u003cspan class=\"pl-k\"\u003einterface\u003c/span\u003e{}) \u003cspan class=\"pl-smi\"\u003ebool\u003c/span\u003e {\n\t\t\u003cspan class=\"pl-s1\"\u003efmt\u003c/span\u003e.\u003cspan class=\"pl-c1\"\u003ePrintf\u003c/span\u003e(\u003cspan class=\"pl-s\"\u003e\"Iterate callback: %v\u003cspan class=\"pl-cce\"\u003e\\n\u003c/span\u003e\"\u003c/span\u003e, \u003cspan class=\"pl-s1\"\u003ev\u003c/span\u003e)\n\t\t\u003cspan class=\"pl-k\"\u003ereturn\u003c/span\u003e \u003cspan class=\"pl-c1\"\u003etrue\u003c/span\u003e\n\t})\n\n\t\u003cspan class=\"pl-s1\"\u003es\u003c/span\u003e.\u003cspan class=\"pl-c1\"\u003eRemove\u003c/span\u003e(\u003cspan class=\"pl-c1\"\u003e6\u003c/span\u003e)\n}\u003c/pre\u003e\u003c/div\u003e\n\u003cp dir=\"auto\"\u003eApplications are supposed to define a callback function (see below) when iterating a set.\u003c/p\u003e\n\u003cdiv class=\"highlight highlight-source-go notranslate position-relative overflow-auto\" dir=\"auto\" data-snippet-clipboard-copy-content=\"// IterateCallback is the signature of the callback function called by Iterate.\n// If the callback function returns false, then the iteration breaks.\ntype IterateCallback func(interface{}) bool\"\u003e\u003cpre\u003e\u003cspan class=\"pl-c\"\u003e// IterateCallback is the signature of the callback function called by Iterate.\u003c/span\u003e\n\u003cspan class=\"pl-c\"\u003e// If the callback function returns false, then the iteration breaks.\u003c/span\u003e\n\u003cspan class=\"pl-k\"\u003etype\u003c/span\u003e \u003cspan class=\"pl-smi\"\u003eIterateCallback\u003c/span\u003e \u003cspan class=\"pl-k\"\u003efunc\u003c/span\u003e(\u003cspan class=\"pl-k\"\u003einterface\u003c/span\u003e{}) \u003cspan class=\"pl-smi\"\u003ebool\u003c/span\u003e\u003c/pre\u003e\u003c/div\u003e\n\u003cp dir=\"auto\"\u003eThe following snip shows how to iterate a set. Please see the \u003cstrong\u003e\u003ca href=\"/ahrtr/gocontainer/blob/master/examples/set_example.go\"\u003eexample\u003c/a\u003e\u003c/strong\u003e to get more detailed info.\u003c/p\u003e\n\u003cdiv class=\"highlight highlight-source-go notranslate position-relative overflow-auto\" dir=\"auto\" data-snippet-clipboard-copy-content=\"// To iterate over a set (where s is an instance of set.Interface):\ns.Iterate(func(v interface{}) bool {\n\t// Do something with v\n\n\t// If you want to break the iteration, then return a false\n\treturn true\n})\"\u003e\u003cpre\u003e\u003cspan class=\"pl-c\"\u003e// To iterate over a set (where s is an instance of set.Interface):\u003c/span\u003e\n\u003cspan class=\"pl-s1\"\u003es\u003c/span\u003e.\u003cspan class=\"pl-c1\"\u003eIterate\u003c/span\u003e(\u003cspan class=\"pl-k\"\u003efunc\u003c/span\u003e(\u003cspan class=\"pl-s1\"\u003ev\u003c/span\u003e \u003cspan class=\"pl-k\"\u003einterface\u003c/span\u003e{}) \u003cspan class=\"pl-smi\"\u003ebool\u003c/span\u003e {\n\t\u003cspan class=\"pl-c\"\u003e// Do something with v\u003c/span\u003e\n\n\t\u003cspan class=\"pl-c\"\u003e// If you want to break the iteration, then return a false\u003c/span\u003e\n\t\u003cspan class=\"pl-k\"\u003ereturn\u003c/span\u003e \u003cspan class=\"pl-c1\"\u003etrue\u003c/span\u003e\n})\u003c/pre\u003e\u003c/div\u003e\n\u003cdiv class=\"markdown-heading\" dir=\"auto\"\u003e\u003ch2 tabindex=\"-1\" class=\"heading-element\" dir=\"auto\"\u003eList\u003c/h2\u003e\u003ca id=\"user-content-list\" class=\"anchor\" aria-label=\"Permalink: List\" href=\"#list\"\u003e\u003csvg class=\"octicon octicon-link\" viewBox=\"0 0 16 16\" version=\"1.1\" width=\"16\" height=\"16\" aria-hidden=\"true\"\u003e\u003cpath d=\"m7.775 3.275 1.25-1.25a3.5 3.5 0 1 1 4.95 4.95l-2.5 2.5a3.5 3.5 0 0 1-4.95 0 .751.751 0 0 1 .018-1.042.751.751 0 0 1 1.042-.018 1.998 1.998 0 0 0 2.83 0l2.5-2.5a2.002 2.002 0 0 0-2.83-2.83l-1.25 1.25a.751.751 0 0 1-1.042-.018.751.751 0 0 1-.018-1.042Zm-4.69 9.64a1.998 1.998 0 0 0 2.83 0l1.25-1.25a.751.751 0 0 1 1.042.018.751.751 0 0 1 .018 1.042l-1.25 1.25a3.5 3.5 0 1 1-4.95-4.95l2.5-2.5a3.5 3.5 0 0 1 4.95 0 .751.751 0 0 1-.018 1.042.751.751 0 0 1-1.042.018 1.998 1.998 0 0 0-2.83 0l-2.5 2.5a1.998 1.998 0 0 0 0 2.83Z\"\u003e\u003c/path\u003e\u003c/svg\u003e\u003c/a\u003e\u003c/div\u003e\n\u003cp dir=\"auto\"\u003eThis library implements two kinds of list, which are \u003cstrong\u003eArrayList\u003c/strong\u003e and \u003cstrong\u003eLinkedList\u003c/strong\u003e, both of which implement the following interface. Click \u003cstrong\u003e\u003ca href=\"/ahrtr/gocontainer/blob/master/examples/list_example.go\"\u003ehere\u003c/a\u003e\u003c/strong\u003e to find examples on how to use a list.\u003c/p\u003e\n\u003cdiv class=\"highlight highlight-source-go notranslate position-relative overflow-auto\" dir=\"auto\" data-snippet-clipboard-copy-content=\"// Interface is a type of list, both ArrayList and LinkedList implement this interface.\ntype Interface interface {\n\tcollection.Interface\n\n\t// Add appends the specified elements to the end of this list.\n\tAdd(vals ...interface{})\n\t// AddTo inserts the specified element at the specified position in this list.\n\tAddTo(index int, val interface{}) error\n\n\t// Contains returns true if this list contains the specified element.\n\tContains(val interface{}) bool\n\t// Get returns the element at the specified position in this list. The index must be in the range of [0, size).\n\tGet(index int) (interface{}, error)\n\n\t// Remove removes the element at the specified position in this list.\n\t// It returns an error if the index is out of range.\n\tRemove(index int) (interface{}, error)\n\t// RemoveByValue removes the first occurence of the specified element from this list, if it is present.\n\t// It returns false if the target value isn't present, otherwise returns true.\n\tRemoveByValue(val interface{}) bool\n\n\t// Sort sorts the element using default options below. It sorts the elements into ascending sequence according to their natural ordering.\n\t// reverse: false\n\t// comparator: nil\n\tSort()\n\t// SortWithOptions sorts the elements in the list.\n\t// Parameters:\n\t// reverse: whether sort the data in reverse ordering\n\t// c: sort the data according to the provided comparator\n\t// If reverse is true, and a comparator is also provided, then the result will be the reverse sequence as the comparator generates.\n\tSortWithOptions(reverse bool, c utils.Comparator)\n\n\t// Iterator returns an iterator over the elements in this list in proper sequence.\n\tIterator() (func() (interface{}, bool), bool)\n\t// ReverseIterator returns an iterator over the elements in this list in reverse sequence as Iterator.\n\tReverseIterator() (func() (interface{}, bool), bool)\n}\"\u003e\u003cpre\u003e\u003cspan class=\"pl-c\"\u003e// Interface is a type of list, both ArrayList and LinkedList implement this interface.\u003c/span\u003e\n\u003cspan class=\"pl-k\"\u003etype\u003c/span\u003e \u003cspan class=\"pl-smi\"\u003eInterface\u003c/span\u003e \u003cspan class=\"pl-k\"\u003einterface\u003c/span\u003e {\n\tcollection.\u003cspan class=\"pl-smi\"\u003eInterface\u003c/span\u003e\n\n\t\u003cspan class=\"pl-c\"\u003e// Add appends the specified elements to the end of this list.\u003c/span\u003e\n\t\u003cspan class=\"pl-c1\"\u003eAdd\u003c/span\u003e(\u003cspan class=\"pl-s1\"\u003evals\u003c/span\u003e \u003cspan class=\"pl-c1\"\u003e...\u003c/span\u003e\u003cspan class=\"pl-k\"\u003einterface\u003c/span\u003e{})\n\t\u003cspan class=\"pl-c\"\u003e// AddTo inserts the specified element at the specified position in this list.\u003c/span\u003e\n\t\u003cspan class=\"pl-c1\"\u003eAddTo\u003c/span\u003e(\u003cspan class=\"pl-s1\"\u003eindex\u003c/span\u003e \u003cspan class=\"pl-smi\"\u003eint\u003c/span\u003e, \u003cspan class=\"pl-s1\"\u003eval\u003c/span\u003e \u003cspan class=\"pl-k\"\u003einterface\u003c/span\u003e{}) \u003cspan class=\"pl-smi\"\u003eerror\u003c/span\u003e\n\n\t\u003cspan class=\"pl-c\"\u003e// Contains returns true if this list contains the specified element.\u003c/span\u003e\n\t\u003cspan class=\"pl-c1\"\u003eContains\u003c/span\u003e(\u003cspan class=\"pl-s1\"\u003eval\u003c/span\u003e \u003cspan class=\"pl-k\"\u003einterface\u003c/span\u003e{}) \u003cspan class=\"pl-smi\"\u003ebool\u003c/span\u003e\n\t\u003cspan class=\"pl-c\"\u003e// Get returns the element at the specified position in this list. The index must be in the range of [0, size).\u003c/span\u003e\n\t\u003cspan class=\"pl-c1\"\u003eGet\u003c/span\u003e(\u003cspan class=\"pl-s1\"\u003eindex\u003c/span\u003e \u003cspan class=\"pl-smi\"\u003eint\u003c/span\u003e) (\u003cspan class=\"pl-k\"\u003einterface\u003c/span\u003e{}, \u003cspan class=\"pl-smi\"\u003eerror\u003c/span\u003e)\n\n\t\u003cspan class=\"pl-c\"\u003e// Remove removes the element at the specified position in this list.\u003c/span\u003e\n\t\u003cspan class=\"pl-c\"\u003e// It returns an error if the index is out of range.\u003c/span\u003e\n\t\u003cspan class=\"pl-c1\"\u003eRemove\u003c/span\u003e(\u003cspan class=\"pl-s1\"\u003eindex\u003c/span\u003e \u003cspan class=\"pl-smi\"\u003eint\u003c/span\u003e) (\u003cspan class=\"pl-k\"\u003einterface\u003c/span\u003e{}, \u003cspan class=\"pl-smi\"\u003eerror\u003c/span\u003e)\n\t\u003cspan class=\"pl-c\"\u003e// RemoveByValue removes the first occurence of the specified element from this list, if it is present.\u003c/span\u003e\n\t\u003cspan class=\"pl-c\"\u003e// It returns false if the target value isn't present, otherwise returns true.\u003c/span\u003e\n\t\u003cspan class=\"pl-c1\"\u003eRemoveByValue\u003c/span\u003e(\u003cspan class=\"pl-s1\"\u003eval\u003c/span\u003e \u003cspan class=\"pl-k\"\u003einterface\u003c/span\u003e{}) \u003cspan class=\"pl-smi\"\u003ebool\u003c/span\u003e\n\n\t\u003cspan class=\"pl-c\"\u003e// Sort sorts the element using default options below. It sorts the elements into ascending sequence according to their natural ordering.\u003c/span\u003e\n\t\u003cspan class=\"pl-c\"\u003e// reverse: false\u003c/span\u003e\n\t\u003cspan class=\"pl-c\"\u003e// comparator: nil\u003c/span\u003e\n\t\u003cspan class=\"pl-c1\"\u003eSort\u003c/span\u003e()\n\t\u003cspan class=\"pl-c\"\u003e// SortWithOptions sorts the elements in the list.\u003c/span\u003e\n\t\u003cspan class=\"pl-c\"\u003e// Parameters:\u003c/span\u003e\n\t\u003cspan class=\"pl-c\"\u003e// reverse: whether sort the data in reverse ordering\u003c/span\u003e\n\t\u003cspan class=\"pl-c\"\u003e// c: sort the data according to the provided comparator\u003c/span\u003e\n\t\u003cspan class=\"pl-c\"\u003e// If reverse is true, and a comparator is also provided, then the result will be the reverse sequence as the comparator generates.\u003c/span\u003e\n\t\u003cspan class=\"pl-c1\"\u003eSortWithOptions\u003c/span\u003e(\u003cspan class=\"pl-s1\"\u003ereverse\u003c/span\u003e \u003cspan class=\"pl-smi\"\u003ebool\u003c/span\u003e, \u003cspan class=\"pl-s1\"\u003ec\u003c/span\u003e utils.\u003cspan class=\"pl-smi\"\u003eComparator\u003c/span\u003e)\n\n\t\u003cspan class=\"pl-c\"\u003e// Iterator returns an iterator over the elements in this list in proper sequence.\u003c/span\u003e\n\t\u003cspan class=\"pl-c1\"\u003eIterator\u003c/span\u003e() (\u003cspan class=\"pl-k\"\u003efunc\u003c/span\u003e() (\u003cspan class=\"pl-k\"\u003einterface\u003c/span\u003e{}, \u003cspan class=\"pl-smi\"\u003ebool\u003c/span\u003e), \u003cspan class=\"pl-smi\"\u003ebool\u003c/span\u003e)\n\t\u003cspan class=\"pl-c\"\u003e// ReverseIterator returns an iterator over the elements in this list in reverse sequence as Iterator.\u003c/span\u003e\n\t\u003cspan class=\"pl-c1\"\u003eReverseIterator\u003c/span\u003e() (\u003cspan class=\"pl-k\"\u003efunc\u003c/span\u003e() (\u003cspan class=\"pl-k\"\u003einterface\u003c/span\u003e{}, \u003cspan class=\"pl-smi\"\u003ebool\u003c/span\u003e), \u003cspan class=\"pl-smi\"\u003ebool\u003c/span\u003e)\n}\u003c/pre\u003e\u003c/div\u003e\n\u003cp dir=\"auto\"\u003ePlease import the following package in order to use list (arrayList or linkedList),\u003c/p\u003e\n\u003cdiv class=\"highlight highlight-source-go notranslate position-relative overflow-auto\" dir=\"auto\" data-snippet-clipboard-copy-content=\"import (\n\t\u0026quot;github.com/ahrtr/gocontainer/list\u0026quot;\n)\"\u003e\u003cpre\u003e\u003cspan class=\"pl-k\"\u003eimport\u003c/span\u003e (\n\t\u003cspan class=\"pl-s\"\u003e\"github.com/ahrtr/gocontainer/list\"\u003c/span\u003e\n)\u003c/pre\u003e\u003c/div\u003e\n\u003cp dir=\"auto\"\u003eCall list.NewArrayList() and list.NewLinkedList() to create a ArrayList and a LinkedList respectively,\u003c/p\u003e\n\u003cdiv class=\"highlight highlight-source-go notranslate position-relative overflow-auto\" dir=\"auto\" data-snippet-clipboard-copy-content=\"NewArrayList() Interface\nNewLinkedList() Interface\"\u003e\u003cpre\u003e\u003cspan class=\"pl-s1\"\u003eNewArrayList\u003c/span\u003e() \u003cspan class=\"pl-s1\"\u003eInterface\u003c/span\u003e\n\u003cspan class=\"pl-s1\"\u003eNewLinkedList\u003c/span\u003e() \u003cspan class=\"pl-s1\"\u003eInterface\u003c/span\u003e\u003c/pre\u003e\u003c/div\u003e\n\u003cp dir=\"auto\"\u003eThe following is a simple example for arrayList,\u003c/p\u003e\n\u003cdiv class=\"highlight highlight-source-go notranslate position-relative overflow-auto\" dir=\"auto\" data-snippet-clipboard-copy-content=\"package main\n\nimport (\n\t\u0026quot;fmt\u0026quot;\n\n\t\u0026quot;github.com/ahrtr/gocontainer/list\u0026quot;\n)\n\nfunc main() {\n\tal := list.NewArrayList()\n\tvalues := []int{5, 7, 12, 9}\n\tfor _, v := range values {\n\t\tal.Add(v)\n\t}\n\n\tal.AddTo(2, 18)\n\tv3, _ := al.Remove(3)\n\tfmt.Printf(\u0026quot;al.Remove(3) = %v\\n\u0026quot;, v3)\n\n\t// Iterate all the elements \n\tfmt.Println(\u0026quot;Iterate: \u0026quot;)\n\tfor i := 0; i \u0026lt; al.Size(); i++ {\n\t\tv, _ := al.Get(i)\n\t\tfmt.Printf(\u0026quot; Index: %d, value: %v\\n\u0026quot;, i, v)\n\t}\n}\"\u003e\u003cpre\u003e\u003cspan class=\"pl-k\"\u003epackage\u003c/span\u003e main\n\n\u003cspan class=\"pl-k\"\u003eimport\u003c/span\u003e (\n\t\u003cspan class=\"pl-s\"\u003e\"fmt\"\u003c/span\u003e\n\n\t\u003cspan class=\"pl-s\"\u003e\"github.com/ahrtr/gocontainer/list\"\u003c/span\u003e\n)\n\n\u003cspan class=\"pl-k\"\u003efunc\u003c/span\u003e \u003cspan class=\"pl-s1\"\u003emain\u003c/span\u003e() {\n\t\u003cspan class=\"pl-s1\"\u003eal\u003c/span\u003e \u003cspan class=\"pl-c1\"\u003e:=\u003c/span\u003e \u003cspan class=\"pl-s1\"\u003elist\u003c/span\u003e.\u003cspan class=\"pl-c1\"\u003eNewArrayList\u003c/span\u003e()\n\t\u003cspan class=\"pl-s1\"\u003evalues\u003c/span\u003e \u003cspan class=\"pl-c1\"\u003e:=\u003c/span\u003e []\u003cspan class=\"pl-smi\"\u003eint\u003c/span\u003e{\u003cspan class=\"pl-c1\"\u003e5\u003c/span\u003e, \u003cspan class=\"pl-c1\"\u003e7\u003c/span\u003e, \u003cspan class=\"pl-c1\"\u003e12\u003c/span\u003e, \u003cspan class=\"pl-c1\"\u003e9\u003c/span\u003e}\n\t\u003cspan class=\"pl-k\"\u003efor\u003c/span\u003e \u003cspan class=\"pl-s1\"\u003e_\u003c/span\u003e, \u003cspan class=\"pl-s1\"\u003ev\u003c/span\u003e \u003cspan class=\"pl-c1\"\u003e:=\u003c/span\u003e \u003cspan class=\"pl-k\"\u003erange\u003c/span\u003e \u003cspan class=\"pl-s1\"\u003evalues\u003c/span\u003e {\n\t\t\u003cspan class=\"pl-s1\"\u003eal\u003c/span\u003e.\u003cspan class=\"pl-c1\"\u003eAdd\u003c/span\u003e(\u003cspan class=\"pl-s1\"\u003ev\u003c/span\u003e)\n\t}\n\n\t\u003cspan class=\"pl-s1\"\u003eal\u003c/span\u003e.\u003cspan class=\"pl-c1\"\u003eAddTo\u003c/span\u003e(\u003cspan class=\"pl-c1\"\u003e2\u003c/span\u003e, \u003cspan class=\"pl-c1\"\u003e18\u003c/span\u003e)\n\t\u003cspan class=\"pl-s1\"\u003ev3\u003c/span\u003e, \u003cspan class=\"pl-s1\"\u003e_\u003c/span\u003e \u003cspan class=\"pl-c1\"\u003e:=\u003c/span\u003e \u003cspan class=\"pl-s1\"\u003eal\u003c/span\u003e.\u003cspan class=\"pl-c1\"\u003eRemove\u003c/span\u003e(\u003cspan class=\"pl-c1\"\u003e3\u003c/span\u003e)\n\t\u003cspan class=\"pl-s1\"\u003efmt\u003c/span\u003e.\u003cspan class=\"pl-c1\"\u003ePrintf\u003c/span\u003e(\u003cspan class=\"pl-s\"\u003e\"al.Remove(3) = %v\u003cspan class=\"pl-cce\"\u003e\\n\u003c/span\u003e\"\u003c/span\u003e, \u003cspan class=\"pl-s1\"\u003ev3\u003c/span\u003e)\n\n\t\u003cspan class=\"pl-c\"\u003e// Iterate all the elements \u003c/span\u003e\n\t\u003cspan class=\"pl-s1\"\u003efmt\u003c/span\u003e.\u003cspan class=\"pl-c1\"\u003ePrintln\u003c/span\u003e(\u003cspan class=\"pl-s\"\u003e\"Iterate: \"\u003c/span\u003e)\n\t\u003cspan class=\"pl-k\"\u003efor\u003c/span\u003e \u003cspan class=\"pl-s1\"\u003ei\u003c/span\u003e \u003cspan class=\"pl-c1\"\u003e:=\u003c/span\u003e \u003cspan class=\"pl-c1\"\u003e0\u003c/span\u003e; \u003cspan class=\"pl-s1\"\u003ei\u003c/span\u003e \u003cspan class=\"pl-c1\"\u003e\u0026lt;\u003c/span\u003e \u003cspan class=\"pl-s1\"\u003eal\u003c/span\u003e.\u003cspan class=\"pl-c1\"\u003eSize\u003c/span\u003e(); \u003cspan class=\"pl-s1\"\u003ei\u003c/span\u003e\u003cspan class=\"pl-c1\"\u003e++\u003c/span\u003e {\n\t\t\u003cspan class=\"pl-s1\"\u003ev\u003c/span\u003e, \u003cspan class=\"pl-s1\"\u003e_\u003c/span\u003e \u003cspan class=\"pl-c1\"\u003e:=\u003c/span\u003e \u003cspan class=\"pl-s1\"\u003eal\u003c/span\u003e.\u003cspan class=\"pl-c1\"\u003eGet\u003c/span\u003e(\u003cspan class=\"pl-s1\"\u003ei\u003c/span\u003e)\n\t\t\u003cspan class=\"pl-s1\"\u003efmt\u003c/span\u003e.\u003cspan class=\"pl-c1\"\u003ePrintf\u003c/span\u003e(\u003cspan class=\"pl-s\"\u003e\" Index: %d, value: %v\u003cspan class=\"pl-cce\"\u003e\\n\u003c/span\u003e\"\u003c/span\u003e, \u003cspan class=\"pl-s1\"\u003ei\u003c/span\u003e, \u003cspan class=\"pl-s1\"\u003ev\u003c/span\u003e)\n\t}\n}\u003c/pre\u003e\u003c/div\u003e\n\u003cp dir=\"auto\"\u003eThe following is a simple example for linkedList,\u003c/p\u003e\n\u003cdiv class=\"highlight highlight-source-go notranslate position-relative overflow-auto\" dir=\"auto\" data-snippet-clipboard-copy-content=\"package main\n\nimport (\n\t\u0026quot;fmt\u0026quot;\n\n\t\u0026quot;github.com/ahrtr/gocontainer/list\u0026quot;\n)\nfunc main() {\n\tll := list.NewLinkedList()\n\tvalues := []int{5, 7, 12, 9}\n\tfor _, v := range values {\n\t\tll.Add(v)\n\t}\n\n\tll.AddTo(2, 18)\n\tv3, _ := ll.Remove(3)\n\tfmt.Printf(\u0026quot;ll.Remove(3) = %v\\n\u0026quot;, v3)\n\n\n\t// Iterate all the elements\n\tfmt.Println(\u0026quot;Iterate: \u0026quot;)\n\tit, hasNext := ll.Iterator()\n\tvar v interface{}\n\tfor hasNext {\n\t\tv, hasNext = it()\n\t\tfmt.Printf(\u0026quot; Value: %v\\n\u0026quot;, v)\n\t}\n}\"\u003e\u003cpre\u003e\u003cspan class=\"pl-k\"\u003epackage\u003c/span\u003e main\n\n\u003cspan class=\"pl-k\"\u003eimport\u003c/span\u003e (\n\t\u003cspan class=\"pl-s\"\u003e\"fmt\"\u003c/span\u003e\n\n\t\u003cspan class=\"pl-s\"\u003e\"github.com/ahrtr/gocontainer/list\"\u003c/span\u003e\n)\n\u003cspan class=\"pl-k\"\u003efunc\u003c/span\u003e \u003cspan class=\"pl-s1\"\u003emain\u003c/span\u003e() {\n\t\u003cspan class=\"pl-s1\"\u003ell\u003c/span\u003e \u003cspan class=\"pl-c1\"\u003e:=\u003c/span\u003e \u003cspan class=\"pl-s1\"\u003elist\u003c/span\u003e.\u003cspan class=\"pl-c1\"\u003eNewLinkedList\u003c/span\u003e()\n\t\u003cspan class=\"pl-s1\"\u003evalues\u003c/span\u003e \u003cspan class=\"pl-c1\"\u003e:=\u003c/span\u003e []\u003cspan class=\"pl-smi\"\u003eint\u003c/span\u003e{\u003cspan class=\"pl-c1\"\u003e5\u003c/span\u003e, \u003cspan class=\"pl-c1\"\u003e7\u003c/span\u003e, \u003cspan class=\"pl-c1\"\u003e12\u003c/span\u003e, \u003cspan class=\"pl-c1\"\u003e9\u003c/span\u003e}\n\t\u003cspan class=\"pl-k\"\u003efor\u003c/span\u003e \u003cspan class=\"pl-s1\"\u003e_\u003c/span\u003e, \u003cspan class=\"pl-s1\"\u003ev\u003c/span\u003e \u003cspan class=\"pl-c1\"\u003e:=\u003c/span\u003e \u003cspan class=\"pl-k\"\u003erange\u003c/span\u003e \u003cspan class=\"pl-s1\"\u003evalues\u003c/span\u003e {\n\t\t\u003cspan class=\"pl-s1\"\u003ell\u003c/span\u003e.\u003cspan class=\"pl-c1\"\u003eAdd\u003c/span\u003e(\u003cspan class=\"pl-s1\"\u003ev\u003c/span\u003e)\n\t}\n\n\t\u003cspan class=\"pl-s1\"\u003ell\u003c/span\u003e.\u003cspan class=\"pl-c1\"\u003eAddTo\u003c/span\u003e(\u003cspan class=\"pl-c1\"\u003e2\u003c/span\u003e, \u003cspan class=\"pl-c1\"\u003e18\u003c/span\u003e)\n\t\u003cspan class=\"pl-s1\"\u003ev3\u003c/span\u003e, \u003cspan class=\"pl-s1\"\u003e_\u003c/span\u003e \u003cspan class=\"pl-c1\"\u003e:=\u003c/span\u003e \u003cspan class=\"pl-s1\"\u003ell\u003c/span\u003e.\u003cspan class=\"pl-c1\"\u003eRemove\u003c/span\u003e(\u003cspan class=\"pl-c1\"\u003e3\u003c/span\u003e)\n\t\u003cspan class=\"pl-s1\"\u003efmt\u003c/span\u003e.\u003cspan class=\"pl-c1\"\u003ePrintf\u003c/span\u003e(\u003cspan class=\"pl-s\"\u003e\"ll.Remove(3) = %v\u003cspan class=\"pl-cce\"\u003e\\n\u003c/span\u003e\"\u003c/span\u003e, \u003cspan class=\"pl-s1\"\u003ev3\u003c/span\u003e)\n\n\n\t\u003cspan class=\"pl-c\"\u003e// Iterate all the elements\u003c/span\u003e\n\t\u003cspan class=\"pl-s1\"\u003efmt\u003c/span\u003e.\u003cspan class=\"pl-c1\"\u003ePrintln\u003c/span\u003e(\u003cspan class=\"pl-s\"\u003e\"Iterate: \"\u003c/span\u003e)\n\t\u003cspan class=\"pl-s1\"\u003eit\u003c/span\u003e, \u003cspan class=\"pl-s1\"\u003ehasNext\u003c/span\u003e \u003cspan class=\"pl-c1\"\u003e:=\u003c/span\u003e \u003cspan class=\"pl-s1\"\u003ell\u003c/span\u003e.\u003cspan class=\"pl-c1\"\u003eIterator\u003c/span\u003e()\n\t\u003cspan class=\"pl-k\"\u003evar\u003c/span\u003e \u003cspan class=\"pl-s1\"\u003ev\u003c/span\u003e \u003cspan class=\"pl-k\"\u003einterface\u003c/span\u003e{}\n\t\u003cspan class=\"pl-k\"\u003efor\u003c/span\u003e \u003cspan class=\"pl-s1\"\u003ehasNext\u003c/span\u003e {\n\t\t\u003cspan class=\"pl-s1\"\u003ev\u003c/span\u003e, \u003cspan class=\"pl-s1\"\u003ehasNext\u003c/span\u003e \u003cspan class=\"pl-c1\"\u003e=\u003c/span\u003e \u003cspan class=\"pl-s1\"\u003eit\u003c/span\u003e()\n\t\t\u003cspan class=\"pl-s1\"\u003efmt\u003c/span\u003e.\u003cspan class=\"pl-c1\"\u003ePrintf\u003c/span\u003e(\u003cspan class=\"pl-s\"\u003e\" Value: %v\u003cspan class=\"pl-cce\"\u003e\\n\u003c/span\u003e\"\u003c/span\u003e, \u003cspan class=\"pl-s1\"\u003ev\u003c/span\u003e)\n\t}\n}\u003c/pre\u003e\u003c/div\u003e\n\u003cp dir=\"auto\"\u003eA list can be sorted using one of the following two methods. The first method Sort() sorts the list into ascending sequence according to the natural ordering of its elements by default; actually it just calls the second method SortWithOptions(false, nil) using the default parameters. SortWithOptions sorts the list according to the provided parameters. Please get more detailed info in \u003cstrong\u003e\u003ca href=\"#comparator\"\u003eComparator\u003c/a\u003e\u003c/strong\u003e\u003c/p\u003e\n\u003cdiv class=\"highlight highlight-source-go notranslate position-relative overflow-auto\" dir=\"auto\" data-snippet-clipboard-copy-content=\"Sort()\nSortWithOptions(reverse bool, c utils.Comparator)\"\u003e\u003cpre\u003e\u003cspan class=\"pl-s1\"\u003eSort\u003c/span\u003e()\n\u003cspan class=\"pl-s1\"\u003eSortWithOptions\u003c/span\u003e(\u003cspan class=\"pl-s1\"\u003ereverse\u003c/span\u003e \u003cspan class=\"pl-s1\"\u003ebool\u003c/span\u003e, \u003cspan class=\"pl-s1\"\u003ec\u003c/span\u003e \u003cspan class=\"pl-s1\"\u003eutils\u003c/span\u003e.\u003cspan class=\"pl-c1\"\u003eComparator\u003c/span\u003e)\u003c/pre\u003e\u003c/div\u003e\n\u003cp dir=\"auto\"\u003eThere are multiple ways to iterate a list. The following snips show how to iterate a list (arrayList or linkedList),\u003c/p\u003e\n\u003cdiv class=\"highlight highlight-source-go notranslate position-relative overflow-auto\" dir=\"auto\" data-snippet-clipboard-copy-content=\"// To iterate over a list (where l is an instance of list.Interface):\nit, hasNext := l.Iterator()\nvar v interface{}\nfor hasNext {\n\tv, hasNext = it()\n\t// do something with v\n}\"\u003e\u003cpre\u003e\u003cspan class=\"pl-c\"\u003e// To iterate over a list (where l is an instance of list.Interface):\u003c/span\u003e\n\u003cspan class=\"pl-s1\"\u003eit\u003c/span\u003e, \u003cspan class=\"pl-s1\"\u003ehasNext\u003c/span\u003e \u003cspan class=\"pl-c1\"\u003e:=\u003c/span\u003e \u003cspan class=\"pl-s1\"\u003el\u003c/span\u003e.\u003cspan class=\"pl-c1\"\u003eIterator\u003c/span\u003e()\n\u003cspan class=\"pl-k\"\u003evar\u003c/span\u003e \u003cspan class=\"pl-s1\"\u003ev\u003c/span\u003e \u003cspan class=\"pl-k\"\u003einterface\u003c/span\u003e{}\n\u003cspan class=\"pl-k\"\u003efor\u003c/span\u003e \u003cspan class=\"pl-s1\"\u003ehasNext\u003c/span\u003e {\n\t\u003cspan class=\"pl-s1\"\u003ev\u003c/span\u003e, \u003cspan class=\"pl-s1\"\u003ehasNext\u003c/span\u003e \u003cspan class=\"pl-c1\"\u003e=\u003c/span\u003e \u003cspan class=\"pl-s1\"\u003eit\u003c/span\u003e()\n\t\u003cspan class=\"pl-c\"\u003e// do something with v\u003c/span\u003e\n}\u003c/pre\u003e\u003c/div\u003e\n\u003cdiv class=\"highlight highlight-source-go notranslate position-relative overflow-auto\" dir=\"auto\" data-snippet-clipboard-copy-content=\"// To iterate over a list (where l is an instance of list.Interface):\n// This approach isn't efficient for linkedList.\nfor i:=0; i\u0026lt;l.Len(); i++ {\n\tv, _ := l.Get(i)\n\t// Do something with v\n}\"\u003e\u003cpre\u003e\u003cspan class=\"pl-c\"\u003e// To iterate over a list (where l is an instance of list.Interface):\u003c/span\u003e\n\u003cspan class=\"pl-c\"\u003e// This approach isn't efficient for linkedList.\u003c/span\u003e\n\u003cspan class=\"pl-k\"\u003efor\u003c/span\u003e \u003cspan class=\"pl-s1\"\u003ei\u003c/span\u003e\u003cspan class=\"pl-c1\"\u003e:=\u003c/span\u003e\u003cspan class=\"pl-c1\"\u003e0\u003c/span\u003e; \u003cspan class=\"pl-s1\"\u003ei\u003c/span\u003e\u003cspan class=\"pl-c1\"\u003e\u0026lt;\u003c/span\u003e\u003cspan class=\"pl-s1\"\u003el\u003c/span\u003e.\u003cspan class=\"pl-c1\"\u003eLen\u003c/span\u003e(); \u003cspan class=\"pl-s1\"\u003ei\u003c/span\u003e\u003cspan class=\"pl-c1\"\u003e++\u003c/span\u003e {\n\t\u003cspan class=\"pl-s1\"\u003ev\u003c/span\u003e, \u003cspan class=\"pl-s1\"\u003e_\u003c/span\u003e \u003cspan class=\"pl-c1\"\u003e:=\u003c/span\u003e \u003cspan class=\"pl-s1\"\u003el\u003c/span\u003e.\u003cspan class=\"pl-c1\"\u003eGet\u003c/span\u003e(\u003cspan class=\"pl-s1\"\u003ei\u003c/span\u003e)\n\t\u003cspan class=\"pl-c\"\u003e// Do something with v\u003c/span\u003e\n}\u003c/pre\u003e\u003c/div\u003e\n\u003cdiv class=\"highlight highlight-source-go notranslate position-relative overflow-auto\" dir=\"auto\" data-snippet-clipboard-copy-content=\"// To iterate over a list in reverse order (where l is an instance of list.Interface):\nit, hasPrev := l.ReverseIterator()\nvar v interface{}\nfor hasPrev {\n\tv, hasPrev = it()\n\t// do something with v\n}\"\u003e\u003cpre\u003e\u003cspan class=\"pl-c\"\u003e// To iterate over a list in reverse order (where l is an instance of list.Interface):\u003c/span\u003e\n\u003cspan class=\"pl-s1\"\u003eit\u003c/span\u003e, \u003cspan class=\"pl-s1\"\u003ehasPrev\u003c/span\u003e \u003cspan class=\"pl-c1\"\u003e:=\u003c/span\u003e \u003cspan class=\"pl-s1\"\u003el\u003c/span\u003e.\u003cspan class=\"pl-c1\"\u003eReverseIterator\u003c/span\u003e()\n\u003cspan class=\"pl-k\"\u003evar\u003c/span\u003e \u003cspan class=\"pl-s1\"\u003ev\u003c/span\u003e \u003cspan class=\"pl-k\"\u003einterface\u003c/span\u003e{}\n\u003cspan class=\"pl-k\"\u003efor\u003c/span\u003e \u003cspan class=\"pl-s1\"\u003ehasPrev\u003c/span\u003e {\n\t\u003cspan class=\"pl-s1\"\u003ev\u003c/span\u003e, \u003cspan class=\"pl-s1\"\u003ehasPrev\u003c/span\u003e \u003cspan class=\"pl-c1\"\u003e=\u003c/span\u003e \u003cspan class=\"pl-s1\"\u003eit\u003c/span\u003e()\n\t\u003cspan class=\"pl-c\"\u003e// do something with v\u003c/span\u003e\n}\u003c/pre\u003e\u003c/div\u003e\n\u003cdiv class=\"highlight highlight-source-go notranslate position-relative overflow-auto\" dir=\"auto\" data-snippet-clipboard-copy-content=\"// To iterate over a list in reverse order (where l is an instance of list.Interface):\n// This approach isn't efficient for linkedList.\nfor i:=l.Len()-1; i\u0026gt;=0; i-- {\n\tv, _ := l.Get(i)\n\t// Do something with v\n}\"\u003e\u003cpre\u003e\u003cspan class=\"pl-c\"\u003e// To iterate over a list in reverse order (where l is an instance of list.Interface):\u003c/span\u003e\n\u003cspan class=\"pl-c\"\u003e// This approach isn't efficient for linkedList.\u003c/span\u003e\n\u003cspan class=\"pl-k\"\u003efor\u003c/span\u003e \u003cspan class=\"pl-s1\"\u003ei\u003c/span\u003e\u003cspan class=\"pl-c1\"\u003e:=\u003c/span\u003e\u003cspan class=\"pl-s1\"\u003el\u003c/span\u003e.\u003cspan class=\"pl-c1\"\u003eLen\u003c/span\u003e()\u003cspan class=\"pl-c1\"\u003e-\u003c/span\u003e\u003cspan class=\"pl-c1\"\u003e1\u003c/span\u003e; \u003cspan class=\"pl-s1\"\u003ei\u003c/span\u003e\u003cspan class=\"pl-c1\"\u003e\u0026gt;=\u003c/span\u003e\u003cspan class=\"pl-c1\"\u003e0\u003c/span\u003e; \u003cspan class=\"pl-s1\"\u003ei\u003c/span\u003e\u003cspan class=\"pl-c1\"\u003e--\u003c/span\u003e {\n\t\u003cspan class=\"pl-s1\"\u003ev\u003c/span\u003e, \u003cspan class=\"pl-s1\"\u003e_\u003c/span\u003e \u003cspan class=\"pl-c1\"\u003e:=\u003c/span\u003e \u003cspan class=\"pl-s1\"\u003el\u003c/span\u003e.\u003cspan class=\"pl-c1\"\u003eGet\u003c/span\u003e(\u003cspan class=\"pl-s1\"\u003ei\u003c/span\u003e)\n\t\u003cspan class=\"pl-c\"\u003e// Do something with v\u003c/span\u003e\n}\u003c/pre\u003e\u003c/div\u003e\n\u003cdiv class=\"markdown-heading\" dir=\"auto\"\u003e\u003ch2 tabindex=\"-1\" class=\"heading-element\" dir=\"auto\"\u003ePriorityQueue\u003c/h2\u003e\u003ca id=\"user-content-priorityqueue\" class=\"anchor\" aria-label=\"Permalink: PriorityQueue\" href=\"#priorityqueue\"\u003e\u003csvg class=\"octicon octicon-link\" viewBox=\"0 0 16 16\" version=\"1.1\" width=\"16\" height=\"16\" aria-hidden=\"true\"\u003e\u003cpath d=\"m7.775 3.275 1.25-1.25a3.5 3.5 0 1 1 4.95 4.95l-2.5 2.5a3.5 3.5 0 0 1-4.95 0 .751.751 0 0 1 .018-1.042.751.751 0 0 1 1.042-.018 1.998 1.998 0 0 0 2.83 0l2.5-2.5a2.002 2.002 0 0 0-2.83-2.83l-1.25 1.25a.751.751 0 0 1-1.042-.018.751.751 0 0 1-.018-1.042Zm-4.69 9.64a1.998 1.998 0 0 0 2.83 0l1.25-1.25a.751.751 0 0 1 1.042.018.751.751 0 0 1 .018 1.042l-1.25 1.25a3.5 3.5 0 1 1-4.95-4.95l2.5-2.5a3.5 3.5 0 0 1 4.95 0 .751.751 0 0 1-.018 1.042.751.751 0 0 1-1.042.018 1.998 1.998 0 0 0-2.83 0l-2.5 2.5a1.998 1.998 0 0 0 0 2.83Z\"\u003e\u003c/path\u003e\u003c/svg\u003e\u003c/a\u003e\u003c/div\u003e\n\u003cp dir=\"auto\"\u003ePriorityQueue is an unbounded priority queue based on a priority heap. It implements the following interface. Click \u003cstrong\u003e\u003ca href=\"/ahrtr/gocontainer/blob/master/examples/priorityqueue_example.go\"\u003ehere\u003c/a\u003e\u003c/strong\u003e to find examples on how to use a priority queue.\u003c/p\u003e\n\u003cdiv class=\"highlight highlight-source-go notranslate position-relative overflow-auto\" dir=\"auto\" data-snippet-clipboard-copy-content=\"// Interface is a type of priority queue, and priorityQueue implement this interface.\ntype Interface interface {\n\tqueue.Interface\n\n\t// WithComparator sets a utils.Comparator instance for the queue.\n\t// It's used to imposes a total ordering on the elements in the queue.\n\tWithComparator(c utils.Comparator) Interface\n\t// WithMinHeap configures whether or not using min-heap.\n\t// If not configured, then it's min-heap by default.\n\tWithMinHeap(isMinHeap bool) Interface\n\n\t// Contains returns true if this queue contains the specified element.\n\tContains(val interface{}) bool\n\t// Remove a single instance of the specified element from this queue, if it is present.\n\t// It returns false if the target value isn't present, otherwise returns true.\n\tRemove(val interface{}) bool\n}\"\u003e\u003cpre\u003e\u003cspan class=\"pl-c\"\u003e// Interface is a type of priority queue, and priorityQueue implement this interface.\u003c/span\u003e\n\u003cspan class=\"pl-k\"\u003etype\u003c/span\u003e \u003cspan class=\"pl-smi\"\u003eInterface\u003c/span\u003e \u003cspan class=\"pl-k\"\u003einterface\u003c/span\u003e {\n\tqueue.\u003cspan class=\"pl-smi\"\u003eInterface\u003c/span\u003e\n\n\t\u003cspan class=\"pl-c\"\u003e// WithComparator sets a utils.Comparator instance for the queue.\u003c/span\u003e\n\t\u003cspan class=\"pl-c\"\u003e// It's used to imposes a total ordering on the elements in the queue.\u003c/span\u003e\n\t\u003cspan class=\"pl-c1\"\u003eWithComparator\u003c/span\u003e(\u003cspan class=\"pl-s1\"\u003ec\u003c/span\u003e utils.\u003cspan class=\"pl-smi\"\u003eComparator\u003c/span\u003e) \u003cspan class=\"pl-smi\"\u003eInterface\u003c/span\u003e\n\t\u003cspan class=\"pl-c\"\u003e// WithMinHeap configures whether or not using min-heap.\u003c/span\u003e\n\t\u003cspan class=\"pl-c\"\u003e// If not configured, then it's min-heap by default.\u003c/span\u003e\n\t\u003cspan class=\"pl-c1\"\u003eWithMinHeap\u003c/span\u003e(\u003cspan class=\"pl-s1\"\u003eisMinHeap\u003c/span\u003e \u003cspan class=\"pl-smi\"\u003ebool\u003c/span\u003e) \u003cspan class=\"pl-smi\"\u003eInterface\u003c/span\u003e\n\n\t\u003cspan class=\"pl-c\"\u003e// Contains returns true if this queue contains the specified element.\u003c/span\u003e\n\t\u003cspan class=\"pl-c1\"\u003eContains\u003c/span\u003e(\u003cspan class=\"pl-s1\"\u003eval\u003c/span\u003e \u003cspan class=\"pl-k\"\u003einterface\u003c/span\u003e{}) \u003cspan class=\"pl-smi\"\u003ebool\u003c/span\u003e\n\t\u003cspan class=\"pl-c\"\u003e// Remove a single instance of the specified element from this queue, if it is present.\u003c/span\u003e\n\t\u003cspan class=\"pl-c\"\u003e// It returns false if the target value isn't present, otherwise returns true.\u003c/span\u003e\n\t\u003cspan class=\"pl-c1\"\u003eRemove\u003c/span\u003e(\u003cspan class=\"pl-s1\"\u003eval\u003c/span\u003e \u003cspan class=\"pl-k\"\u003einterface\u003c/span\u003e{}) \u003cspan class=\"pl-smi\"\u003ebool\u003c/span\u003e\n}\u003c/pre\u003e\u003c/div\u003e\n\u003cp dir=\"auto\"\u003ePlease import the following package in order to use priorityQueue,\u003c/p\u003e\n\u003cdiv class=\"highlight highlight-source-go notranslate position-relative overflow-auto\" dir=\"auto\" data-snippet-clipboard-copy-content=\"import (\n\t\u0026quot;github.com/ahrtr/gocontainer/queue/priorityqueue\u0026quot;\n)\"\u003e\u003cpre\u003e\u003cspan class=\"pl-k\"\u003eimport\u003c/span\u003e (\n\t\u003cspan class=\"pl-s\"\u003e\"github.com/ahrtr/gocontainer/queue/priorityqueue\"\u003c/span\u003e\n)\u003c/pre\u003e\u003c/div\u003e\n\u003cp dir=\"auto\"\u003eCall priorityqueue.New() to create a PriorityQueue,\u003c/p\u003e\n\u003cdiv class=\"highlight highlight-source-go notranslate position-relative overflow-auto\" dir=\"auto\" data-snippet-clipboard-copy-content=\"New() Interface \"\u003e\u003cpre\u003e\u003cspan class=\"pl-s1\"\u003eNew\u003c/span\u003e() \u003cspan class=\"pl-s1\"\u003eInterface\u003c/span\u003e \u003c/pre\u003e\u003c/div\u003e\n\u003cp dir=\"auto\"\u003eThe following is a simple example for priorityQueue,\u003c/p\u003e\n\u003cdiv class=\"highlight highlight-source-go notranslate position-relative overflow-auto\" dir=\"auto\" data-snippet-clipboard-copy-content=\"package main\n\nimport (\n\t\u0026quot;fmt\u0026quot;\n\n\t\u0026quot;github.com/ahrtr/gocontainer/queue/priorityqueue\u0026quot;\n)\n\nfunc main() {\n\tpq := priorityqueue.New()\n\n\tvalues := []string{\u0026quot;benjamin\u0026quot;, \u0026quot;alice\u0026quot;, \u0026quot;john\u0026quot;, \u0026quot;tom\u0026quot;, \u0026quot;bill\u0026quot;}\n\n\tfor _, v := range values {\n\t\tpq.Add(v)\n\t}\n\n\tfor _, v := range values {\n\t\tfmt.Printf(\u0026quot;pq.Contains(%v) = %t\\n\u0026quot;, v, pq.Contains(v))\n\t}\n\n\tfmt.Printf(\u0026quot;pq.Remove(john) = %t\\n\u0026quot;, pq.Remove(\u0026quot;john\u0026quot;))\n\n\tfor pq.Peek() != nil {\n\t\tfmt.Printf(\u0026quot;pq.Peek() = %v\\n\u0026quot;, pq.Peek())\n\t\tfmt.Printf(\u0026quot;pq.Poll() = %v\\n\u0026quot;, pq.Poll())\n\t}\n}\"\u003e\u003cpre\u003e\u003cspan class=\"pl-k\"\u003epackage\u003c/span\u003e main\n\n\u003cspan class=\"pl-k\"\u003eimport\u003c/span\u003e (\n\t\u003cspan class=\"pl-s\"\u003e\"fmt\"\u003c/span\u003e\n\n\t\u003cspan class=\"pl-s\"\u003e\"github.com/ahrtr/gocontainer/queue/priorityqueue\"\u003c/span\u003e\n)\n\n\u003cspan class=\"pl-k\"\u003efunc\u003c/span\u003e \u003cspan class=\"pl-s1\"\u003emain\u003c/span\u003e() {\n\t\u003cspan class=\"pl-s1\"\u003epq\u003c/span\u003e \u003cspan class=\"pl-c1\"\u003e:=\u003c/span\u003e \u003cspan class=\"pl-s1\"\u003epriorityqueue\u003c/span\u003e.\u003cspan class=\"pl-c1\"\u003eNew\u003c/span\u003e()\n\n\t\u003cspan class=\"pl-s1\"\u003evalues\u003c/span\u003e \u003cspan class=\"pl-c1\"\u003e:=\u003c/span\u003e []\u003cspan class=\"pl-smi\"\u003estring\u003c/span\u003e{\u003cspan class=\"pl-s\"\u003e\"benjamin\"\u003c/span\u003e, \u003cspan class=\"pl-s\"\u003e\"alice\"\u003c/span\u003e, \u003cspan class=\"pl-s\"\u003e\"john\"\u003c/span\u003e, \u003cspan class=\"pl-s\"\u003e\"tom\"\u003c/span\u003e, \u003cspan class=\"pl-s\"\u003e\"bill\"\u003c/span\u003e}\n\n\t\u003cspan class=\"pl-k\"\u003efor\u003c/span\u003e \u003cspan class=\"pl-s1\"\u003e_\u003c/span\u003e, \u003cspan class=\"pl-s1\"\u003ev\u003c/span\u003e \u003cspan class=\"pl-c1\"\u003e:=\u003c/span\u003e \u003cspan class=\"pl-k\"\u003erange\u003c/span\u003e \u003cspan class=\"pl-s1\"\u003evalues\u003c/span\u003e {\n\t\t\u003cspan class=\"pl-s1\"\u003epq\u003c/span\u003e.\u003cspan class=\"pl-c1\"\u003eAdd\u003c/span\u003e(\u003cspan class=\"pl-s1\"\u003ev\u003c/span\u003e)\n\t}\n\n\t\u003cspan class=\"pl-k\"\u003efor\u003c/span\u003e \u003cspan class=\"pl-s1\"\u003e_\u003c/span\u003e, \u003cspan class=\"pl-s1\"\u003ev\u003c/span\u003e \u003cspan class=\"pl-c1\"\u003e:=\u003c/span\u003e \u003cspan class=\"pl-k\"\u003erange\u003c/span\u003e \u003cspan class=\"pl-s1\"\u003evalues\u003c/span\u003e {\n\t\t\u003cspan class=\"pl-s1\"\u003efmt\u003c/span\u003e.\u003cspan class=\"pl-c1\"\u003ePrintf\u003c/span\u003e(\u003cspan class=\"pl-s\"\u003e\"pq.Contains(%v) = %t\u003cspan class=\"pl-cce\"\u003e\\n\u003c/span\u003e\"\u003c/span\u003e, \u003cspan class=\"pl-s1\"\u003ev\u003c/span\u003e, \u003cspan class=\"pl-s1\"\u003epq\u003c/span\u003e.\u003cspan class=\"pl-c1\"\u003eContains\u003c/span\u003e(\u003cspan class=\"pl-s1\"\u003ev\u003c/span\u003e))\n\t}\n\n\t\u003cspan class=\"pl-s1\"\u003efmt\u003c/span\u003e.\u003cspan class=\"pl-c1\"\u003ePrintf\u003c/span\u003e(\u003cspan class=\"pl-s\"\u003e\"pq.Remove(john) = %t\u003cspan class=\"pl-cce\"\u003e\\n\u003c/span\u003e\"\u003c/span\u003e, \u003cspan class=\"pl-s1\"\u003epq\u003c/span\u003e.\u003cspan class=\"pl-c1\"\u003eRemove\u003c/span\u003e(\u003cspan class=\"pl-s\"\u003e\"john\"\u003c/span\u003e))\n\n\t\u003cspan class=\"pl-k\"\u003efor\u003c/span\u003e \u003cspan class=\"pl-s1\"\u003epq\u003c/span\u003e.\u003cspan class=\"pl-c1\"\u003ePeek\u003c/span\u003e() \u003cspan class=\"pl-c1\"\u003e!=\u003c/span\u003e \u003cspan class=\"pl-c1\"\u003enil\u003c/span\u003e {\n\t\t\u003cspan class=\"pl-s1\"\u003efmt\u003c/span\u003e.\u003cspan class=\"pl-c1\"\u003ePrintf\u003c/span\u003e(\u003cspan class=\"pl-s\"\u003e\"pq.Peek() = %v\u003cspan class=\"pl-cce\"\u003e\\n\u003c/span\u003e\"\u003c/span\u003e, \u003cspan class=\"pl-s1\"\u003epq\u003c/span\u003e.\u003cspan class=\"pl-c1\"\u003ePeek\u003c/span\u003e())\n\t\t\u003cspan class=\"pl-s1\"\u003efmt\u003c/span\u003e.\u003cspan class=\"pl-c1\"\u003ePrintf\u003c/span\u003e(\u003cspan class=\"pl-s\"\u003e\"pq.Poll() = %v\u003cspan class=\"pl-cce\"\u003e\\n\u003c/span\u003e\"\u003c/span\u003e, \u003cspan class=\"pl-s1\"\u003epq\u003c/span\u003e.\u003cspan class=\"pl-c1\"\u003ePoll\u003c/span\u003e())\n\t}\n}\u003c/pre\u003e\u003c/div\u003e\n\u003cp dir=\"auto\"\u003eA utils.Comparator instance can be provided for a priorityQueue by method WithComparator, please get more detailed info in \u003cstrong\u003e\u003ca href=\"#comparator\"\u003eComparator\u003c/a\u003e\u003c/strong\u003e.\u003c/p\u003e\n\u003cdiv class=\"highlight highlight-source-go notranslate position-relative overflow-auto\" dir=\"auto\" data-snippet-clipboard-copy-content=\"WithComparator(c utils.Comparator) Interface\"\u003e\u003cpre\u003e\u003cspan class=\"pl-s1\"\u003eWithComparator\u003c/span\u003e(\u003cspan class=\"pl-s1\"\u003ec\u003c/span\u003e \u003cspan class=\"pl-s1\"\u003eutils\u003c/span\u003e.\u003cspan class=\"pl-c1\"\u003eComparator\u003c/span\u003e) \u003cspan class=\"pl-s1\"\u003eInterface\u003c/span\u003e\u003c/pre\u003e\u003c/div\u003e\n\u003cp dir=\"auto\"\u003eA priorityQueue can be configured to use min-heap or max-heap using method WithMinHeap. If the parameter is true, then it's a min-heap, which is the default option as well; otherwise, it's a max-heap.\u003c/p\u003e\n\u003cdiv class=\"highlight highlight-source-go notranslate position-relative overflow-auto\" dir=\"auto\" data-snippet-clipboard-copy-content=\"WithMinHeap(isMinHeap bool) Interface\"\u003e\u003cpre\u003e\u003cspan class=\"pl-s1\"\u003eWithMinHeap\u003c/span\u003e(\u003cspan class=\"pl-s1\"\u003eisMinHeap\u003c/span\u003e \u003cspan class=\"pl-s1\"\u003ebool\u003c/span\u003e) \u003cspan class=\"pl-s1\"\u003eInterface\u003c/span\u003e\u003c/pre\u003e\u003c/div\u003e\n\u003cdiv class=\"markdown-heading\" dir=\"auto\"\u003e\u003ch2 tabindex=\"-1\" class=\"heading-element\" dir=\"auto\"\u003eLinkedMap\u003c/h2\u003e\u003ca id=\"user-content-linkedmap\" class=\"anchor\" aria-label=\"Permalink: LinkedMap\" href=\"#linkedmap\"\u003e\u003csvg class=\"octicon octicon-link\" viewBox=\"0 0 16 16\" version=\"1.1\" width=\"16\" height=\"16\" aria-hidden=\"true\"\u003e\u003cpath d=\"m7.775 3.275 1.25-1.25a3.5 3.5 0 1 1 4.95 4.95l-2.5 2.5a3.5 3.5 0 0 1-4.95 0 .751.751 0 0 1 .018-1.042.751.751 0 0 1 1.042-.018 1.998 1.998 0 0 0 2.83 0l2.5-2.5a2.002 2.002 0 0 0-2.83-2.83l-1.25 1.25a.751.751 0 0 1-1.042-.018.751.751 0 0 1-.018-1.042Zm-4.69 9.64a1.998 1.998 0 0 0 2.83 0l1.25-1.25a.751.751 0 0 1 1.042.018.751.751 0 0 1 .018 1.042l-1.25 1.25a3.5 3.5 0 1 1-4.95-4.95l2.5-2.5a3.5 3.5 0 0 1 4.95 0 .751.751 0 0 1-.018 1.042.751.751 0 0 1-1.042.018 1.998 1.998 0 0 0-2.83 0l-2.5 2.5a1.998 1.998 0 0 0 0 2.83Z\"\u003e\u003c/path\u003e\u003c/svg\u003e\u003c/a\u003e\u003c/div\u003e\n\u003cp dir=\"auto\"\u003eLinkedMap is based on a map and a doubly linked list. The iteration ordering is normally the order in which keys were inserted into the map, or the order in which the keys were accessed if the accessOrder flag is set. It implements the following interface. Click \u003cstrong\u003e\u003ca href=\"/ahrtr/gocontainer/blob/master/examples/linkedmap_example.go\"\u003ehere\u003c/a\u003e\u003c/strong\u003e to find examples on how to use a linked map.\u003c/p\u003e\n\u003cdiv class=\"highlight highlight-source-go notranslate position-relative overflow-auto\" dir=\"auto\" data-snippet-clipboard-copy-content=\"// Interface is a type of linked map, and linkedMap implements this interface.\ntype Interface interface {\n\tcollection.Interface\n\n\t// Put associates the specified value with the specified key in this map. If the map previously contained a mapping for the key,\n\t// the old value is replaced by the specified value.\n\t// It returns the previous value associated with the specified key, or nil if there was no mapping for the key.\n\t// A nil return can also indicate that the map previously associated nil with the specified key.\n\tPut(k, v interface{}) interface{}\n\n\t// WithAccessOrder configures the iteration ordering for this linked map,\n\t// true for access-order, and false for insertion-order.\n\tWithAccessOrder(accessOrder bool) Interface\n\n\t// Get returns the value to which the specified key is mapped, or nil if this map contains no mapping for the key.\n\tGet(k interface{}) interface{}\n\t// GetOrDefault returns the value to which the specified key is mapped, or the defaultValue if this map contains no mapping for the key.\n\tGetOrDefault(k, defaultValue interface{}) interface{}\n\t// GetFirstElement gets the first element from this map, which is the head of the list.\n\t// It returns the (key, value, true) if the map isn't empty, or (nil, nil, false) if the map is empty.\n\tGetFirstElement() (interface{}, interface{}, bool)\n\t// GetLastElement gets the last element from this map, which is the tail of the list.\n\t// It returns the (key, value, true) if the map isn't empty, or (nil, nil, false) if the map is empty.\n\tGetLastElement() (interface{}, interface{}, bool)\n\n\t// ContainsKey returns true if this map contains a mapping for the specified key.\n\tContainsKey(k interface{}) bool\n\t// ContainsValue returns true if this map maps one or more keys to the specified value.\n\tContainsValue(v interface{}) bool\n\n\t// Remove removes the mapping for a key from this map if it is present.\n\t// It returns the value to which this map previously associated the key, and true,\n\t// or nil and false if the map contained no mapping for the key.\n\tRemove(k interface{}) (interface{}, bool)\n\t// RemoveFirstElement removes the first element from this map, which is the head of the list.\n\t// It returns the (key, value, true) if the map isn't empty, or (nil, nil, false) if the map is empty.\n\tRemoveFirstElement() (interface{}, interface{}, bool)\n\t// RemoveLastElement removes the last element from this map, which is the tail of the list.\n\t// It returns the (key, value, true) if the map isn't empty, or (nil, nil, false) if the map is empty.\n\tRemoveLastElement() (interface{}, interface{}, bool)\n\n\t// Iterator returns an iterator over the elements in this map in proper sequence.\n\tIterator() (func() (interface{}, interface{}, bool), bool)\n\t// ReverseIterator returns an iterator over the elements in this map in reverse sequence as Iterator.\n\tReverseIterator() (func() (interface{}, interface{}, bool), bool)\n}\"\u003e\u003cpre\u003e\u003cspan class=\"pl-c\"\u003e// Interface is a type of linked map, and linkedMap implements this interface.\u003c/span\u003e\n\u003cspan class=\"pl-k\"\u003etype\u003c/span\u003e \u003cspan class=\"pl-smi\"\u003eInterface\u003c/span\u003e \u003cspan class=\"pl-k\"\u003einterface\u003c/span\u003e {\n\tcollection.\u003cspan class=\"pl-smi\"\u003eInterface\u003c/span\u003e\n\n\t\u003cspan class=\"pl-c\"\u003e// Put associates the specified value with the specified key in this map. If the map previously contained a mapping for the key,\u003c/span\u003e\n\t\u003cspan class=\"pl-c\"\u003e// the old value is replaced by the specified value.\u003c/span\u003e\n\t\u003cspan class=\"pl-c\"\u003e// It returns the previous value associated with the specified key, or nil if there was no mapping for the key.\u003c/span\u003e\n\t\u003cspan class=\"pl-c\"\u003e// A nil return can also indicate that the map previously associated nil with the specified key.\u003c/span\u003e\n\t\u003cspan class=\"pl-c1\"\u003ePut\u003c/span\u003e(\u003cspan class=\"pl-s1\"\u003ek\u003c/span\u003e, \u003cspan class=\"pl-s1\"\u003ev\u003c/span\u003e \u003cspan class=\"pl-k\"\u003einterface\u003c/span\u003e{}) \u003cspan class=\"pl-k\"\u003einterface\u003c/span\u003e{}\n\n\t\u003cspan class=\"pl-c\"\u003e// WithAccessOrder configures the iteration ordering for this linked map,\u003c/span\u003e\n\t\u003cspan class=\"pl-c\"\u003e// true for access-order, and false for insertion-order.\u003c/span\u003e\n\t\u003cspan class=\"pl-c1\"\u003eWithAccessOrder\u003c/span\u003e(\u003cspan class=\"pl-s1\"\u003eaccessOrder\u003c/span\u003e \u003cspan class=\"pl-smi\"\u003ebool\u003c/span\u003e) \u003cspan class=\"pl-smi\"\u003eInterface\u003c/span\u003e\n\n\t\u003cspan class=\"pl-c\"\u003e// Get returns the value to which the specified key is mapped, or nil if this map contains no mapping for the key.\u003c/span\u003e\n\t\u003cspan class=\"pl-c1\"\u003eGet\u003c/span\u003e(\u003cspan class=\"pl-s1\"\u003ek\u003c/span\u003e \u003cspan class=\"pl-k\"\u003einterface\u003c/span\u003e{}) \u003cspan class=\"pl-k\"\u003einterface\u003c/span\u003e{}\n\t\u003cspan class=\"pl-c\"\u003e// GetOrDefault returns the value to which the specified key is mapped, or the defaultValue if this map contains no mapping for the key.\u003c/span\u003e\n\t\u003cspan class=\"pl-c1\"\u003eGetOrDefault\u003c/span\u003e(\u003cspan class=\"pl-s1\"\u003ek\u003c/span\u003e, \u003cspan class=\"pl-s1\"\u003edefaultValue\u003c/span\u003e \u003cspan class=\"pl-k\"\u003einterface\u003c/span\u003e{}) \u003cspan class=\"pl-k\"\u003einterface\u003c/span\u003e{}\n\t\u003cspan class=\"pl-c\"\u003e// GetFirstElement gets the first element from this map, which is the head of the list.\u003c/span\u003e\n\t\u003cspan class=\"pl-c\"\u003e// It returns the (key, value, true) if the map isn't empty, or (nil, nil, false) if the map is empty.\u003c/span\u003e\n\t\u003cspan class=\"pl-c1\"\u003eGetFirstElement\u003c/span\u003e() (\u003cspan class=\"pl-k\"\u003einterface\u003c/span\u003e{}, \u003cspan class=\"pl-k\"\u003einterface\u003c/span\u003e{}, \u003cspan class=\"pl-smi\"\u003ebool\u003c/span\u003e)\n\t\u003cspan class=\"pl-c\"\u003e// GetLastElement gets the last element from this map, which is the tail of the list.\u003c/span\u003e\n\t\u003cspan class=\"pl-c\"\u003e// It returns the (key, value, true) if the map isn't empty, or (nil, nil, false) if the map is empty.\u003c/span\u003e\n\t\u003cspan class=\"pl-c1\"\u003eGetLastElement\u003c/span\u003e() (\u003cspan class=\"pl-k\"\u003einterface\u003c/span\u003e{}, \u003cspan class=\"pl-k\"\u003einterface\u003c/span\u003e{}, \u003cspan class=\"pl-smi\"\u003ebool\u003c/span\u003e)\n\n\t\u003cspan class=\"pl-c\"\u003e// ContainsKey returns true if this map contains a mapping for the specified key.\u003c/span\u003e\n\t\u003cspan class=\"pl-c1\"\u003eContainsKey\u003c/span\u003e(\u003cspan class=\"pl-s1\"\u003ek\u003c/span\u003e \u003cspan class=\"pl-k\"\u003einterface\u003c/span\u003e{}) \u003cspan class=\"pl-smi\"\u003ebool\u003c/span\u003e\n\t\u003cspan class=\"pl-c\"\u003e// ContainsValue returns true if this map maps one or more keys to the specified value.\u003c/span\u003e\n\t\u003cspan class=\"pl-c1\"\u003eContainsValue\u003c/span\u003e(\u003cspan class=\"pl-s1\"\u003ev\u003c/span\u003e \u003cspan class=\"pl-k\"\u003einterface\u003c/span\u003e{}) \u003cspan class=\"pl-smi\"\u003ebool\u003c/span\u003e\n\n\t\u003cspan class=\"pl-c\"\u003e// Remove removes the mapping for a key from this map if it is present.\u003c/span\u003e\n\t\u003cspan class=\"pl-c\"\u003e// It returns the value to which this map previously associated the key, and true,\u003c/span\u003e\n\t\u003cspan class=\"pl-c\"\u003e// or nil and false if the map contained no mapping for the key.\u003c/span\u003e\n\t\u003cspan class=\"pl-c1\"\u003eRemove\u003c/span\u003e(\u003cspan class=\"pl-s1\"\u003ek\u003c/span\u003e \u003cspan class=\"pl-k\"\u003einterface\u003c/span\u003e{}) (\u003cspan class=\"pl-k\"\u003einterface\u003c/span\u003e{}, \u003cspan class=\"pl-smi\"\u003ebool\u003c/span\u003e)\n\t\u003cspan class=\"pl-c\"\u003e// RemoveFirstElement removes the first element from this map, which is the head of the list.\u003c/span\u003e\n\t\u003cspan class=\"pl-c\"\u003e// It returns the (key, value, true) if the map isn't empty, or (nil, nil, false) if the map is empty.\u003c/span\u003e\n\t\u003cspan class=\"pl-c1\"\u003eRemoveFirstElement\u003c/span\u003e() (\u003cspan class=\"pl-k\"\u003einterface\u003c/span\u003e{}, \u003cspan class=\"pl-k\"\u003einterface\u003c/span\u003e{}, \u003cspan class=\"pl-smi\"\u003ebool\u003c/span\u003e)\n\t\u003cspan class=\"pl-c\"\u003e// RemoveLastElement removes the last element from this map, which is the tail of the list.\u003c/span\u003e\n\t\u003cspan class=\"pl-c\"\u003e// It returns the (key, value, true) if the map isn't empty, or (nil, nil, false) if the map is empty.\u003c/span\u003e\n\t\u003cspan class=\"pl-c1\"\u003eRemoveLastElement\u003c/span\u003e() (\u003cspan class=\"pl-k\"\u003einterface\u003c/span\u003e{}, \u003cspan class=\"pl-k\"\u003einterface\u003c/span\u003e{}, \u003cspan class=\"pl-smi\"\u003ebool\u003c/span\u003e)\n\n\t\u003cspan class=\"pl-c\"\u003e// Iterator returns an iterator over the elements in this map in proper sequence.\u003c/span\u003e\n\t\u003cspan class=\"pl-c1\"\u003eIterator\u003c/span\u003e() (\u003cspan class=\"pl-k\"\u003efunc\u003c/span\u003e() (\u003cspan class=\"pl-k\"\u003einterface\u003c/span\u003e{}, \u003cspan class=\"pl-k\"\u003einterface\u003c/span\u003e{}, \u003cspan class=\"pl-smi\"\u003ebool\u003c/span\u003e), \u003cspan class=\"pl-smi\"\u003ebool\u003c/span\u003e)\n\t\u003cspan class=\"pl-c\"\u003e// ReverseIterator returns an iterator over the elements in this map in reverse sequence as Iterator.\u003c/span\u003e\n\t\u003cspan class=\"pl-c1\"\u003eReverseIterator\u003c/span\u003e() (\u003cspan class=\"pl-k\"\u003efunc\u003c/span\u003e() (\u003cspan class=\"pl-k\"\u003einterface\u003c/span\u003e{}, \u003cspan class=\"pl-k\"\u003einterface\u003c/span\u003e{}, \u003cspan class=\"pl-smi\"\u003ebool\u003c/span\u003e), \u003cspan class=\"pl-smi\"\u003ebool\u003c/span\u003e)\n}\u003c/pre\u003e\u003c/div\u003e\n\u003cp dir=\"auto\"\u003ePlease import the following package in order to use linkedMap,\u003c/p\u003e\n\u003cdiv class=\"highlight highlight-source-go notranslate position-relative overflow-auto\" dir=\"auto\" data-snippet-clipboard-copy-content=\"import (\n\t\u0026quot;github.com/ahrtr/gocontainer/map/linkedmap\u0026quot;\n)\"\u003e\u003cpre\u003e\u003cspan class=\"pl-k\"\u003eimport\u003c/span\u003e (\n\t\u003cspan class=\"pl-s\"\u003e\"github.com/ahrtr/gocontainer/map/linkedmap\"\u003c/span\u003e\n)\u003c/pre\u003e\u003c/div\u003e\n\u003cp dir=\"auto\"\u003eCall linkedmap.New() to create a linked map,\u003c/p\u003e\n\u003cdiv class=\"highlight highlight-source-go notranslate position-relative overflow-auto\" dir=\"auto\" data-snippet-clipboard-copy-content=\"New() Interface\"\u003e\u003cpre\u003e\u003cspan class=\"pl-s1\"\u003eNew\u003c/span\u003e() \u003cspan class=\"pl-s1\"\u003eInterface\u003c/span\u003e\u003c/pre\u003e\u003c/div\u003e\n\u003cp dir=\"auto\"\u003eThe following is a simple example for linkedMap,\u003c/p\u003e\n\u003cdiv class=\"highlight highlight-source-go notranslate position-relative overflow-auto\" dir=\"auto\" data-snippet-clipboard-copy-content=\"package main\n\nimport (\n\t\u0026quot;fmt\u0026quot;\n\n\t\u0026quot;github.com/ahrtr/gocontainer/map/linkedmap\u0026quot;\n)\n\nfunc main() {\n\tlm := linkedmap.New()\n\n\tkeys := []interface{}{24, 43, 18, 23, 35}\n\tvalues := []interface{}{\u0026quot;benjamin\u0026quot;, \u0026quot;alice\u0026quot;, \u0026quot;john\u0026quot;, \u0026quot;tom\u0026quot;, \u0026quot;bill\u0026quot;}\n\tfor i := 0; i \u0026lt; len(keys); i++ {\n\t\tlm.Put(keys[i], values[i])\n\t}\n\n\tfor _, k := range keys {\n\t\tfmt.Printf(\u0026quot;Get(%v) = %v\\n\u0026quot;, k, lm.Get(k))\n\t}\n\n\tv, _ := lm.Remove(18)\n\tfmt.Printf(\u0026quot;The value associated with 18 is %v\\n\u0026quot;, v)\n\n\tk, v, _ := lm.RemoveFirstElement()\n\tfmt.Printf(\u0026quot;The first element removed is (%v, %v)\\n\u0026quot;, k, v)\n\n\tk, v, _ = lm.RemoveLastElement()\n\tfmt.Printf(\u0026quot;The last element removed is (%v, %v)\\n\u0026quot;, k, v)\n}\"\u003e\u003cpre\u003e\u003cspan class=\"pl-k\"\u003epackage\u003c/span\u003e main\n\n\u003cspan class=\"pl-k\"\u003eimport\u003c/span\u003e (\n\t\u003cspan class=\"pl-s\"\u003e\"fmt\"\u003c/span\u003e\n\n\t\u003cspan class=\"pl-s\"\u003e\"github.com/ahrtr/gocontainer/map/linkedmap\"\u003c/span\u003e\n)\n\n\u003cspan class=\"pl-k\"\u003efunc\u003c/span\u003e \u003cspan class=\"pl-s1\"\u003emain\u003c/span\u003e() {\n\t\u003cspan class=\"pl-s1\"\u003elm\u003c/span\u003e \u003cspan class=\"pl-c1\"\u003e:=\u003c/span\u003e \u003cspan class=\"pl-s1\"\u003elinkedmap\u003c/span\u003e.\u003cspan class=\"pl-c1\"\u003eNew\u003c/span\u003e()\n\n\t\u003cspan class=\"pl-s1\"\u003ekeys\u003c/span\u003e \u003cspan class=\"pl-c1\"\u003e:=\u003c/span\u003e []\u003cspan class=\"pl-k\"\u003einterface\u003c/span\u003e{}{\u003cspan class=\"pl-c1\"\u003e24\u003c/span\u003e, \u003cspan class=\"pl-c1\"\u003e43\u003c/span\u003e, \u003cspan class=\"pl-c1\"\u003e18\u003c/span\u003e, \u003cspan class=\"pl-c1\"\u003e23\u003c/span\u003e, \u003cspan class=\"pl-c1\"\u003e35\u003c/span\u003e}\n\t\u003cspan class=\"pl-s1\"\u003evalues\u003c/span\u003e \u003cspan class=\"pl-c1\"\u003e:=\u003c/span\u003e []\u003cspan class=\"pl-k\"\u003einterface\u003c/span\u003e{}{\u003cspan class=\"pl-s\"\u003e\"benjamin\"\u003c/span\u003e, \u003cspan class=\"pl-s\"\u003e\"alice\"\u003c/span\u003e, \u003cspan class=\"pl-s\"\u003e\"john\"\u003c/span\u003e, \u003cspan class=\"pl-s\"\u003e\"tom\"\u003c/span\u003e, \u003cspan class=\"pl-s\"\u003e\"bill\"\u003c/span\u003e}\n\t\u003cspan class=\"pl-k\"\u003efor\u003c/span\u003e \u003cspan class=\"pl-s1\"\u003ei\u003c/span\u003e \u003cspan class=\"pl-c1\"\u003e:=\u003c/span\u003e \u003cspan class=\"pl-c1\"\u003e0\u003c/span\u003e; \u003cspan class=\"pl-s1\"\u003ei\u003c/span\u003e \u003cspan class=\"pl-c1\"\u003e\u0026lt;\u003c/span\u003e \u003cspan class=\"pl-s1\"\u003elen\u003c/span\u003e(\u003cspan class=\"pl-s1\"\u003ekeys\u003c/span\u003e); \u003cspan class=\"pl-s1\"\u003ei\u003c/span\u003e\u003cspan class=\"pl-c1\"\u003e++\u003c/span\u003e {\n\t\t\u003cspan class=\"pl-s1\"\u003elm\u003c/span\u003e.\u003cspan class=\"pl-c1\"\u003ePut\u003c/span\u003e(\u003cspan class=\"pl-s1\"\u003ekeys\u003c/span\u003e[\u003cspan class=\"pl-s1\"\u003ei\u003c/span\u003e], \u003cspan class=\"pl-s1\"\u003evalues\u003c/span\u003e[\u003cspan class=\"pl-s1\"\u003ei\u003c/span\u003e])\n\t}\n\n\t\u003cspan class=\"pl-k\"\u003efor\u003c/span\u003e \u003cspan class=\"pl-s1\"\u003e_\u003c/span\u003e, \u003cspan class=\"pl-s1\"\u003ek\u003c/span\u003e \u003cspan class=\"pl-c1\"\u003e:=\u003c/span\u003e \u003cspan class=\"pl-k\"\u003erange\u003c/span\u003e \u003cspan class=\"pl-s1\"\u003ekeys\u003c/span\u003e {\n\t\t\u003cspan class=\"pl-s1\"\u003efmt\u003c/span\u003e.\u003cspan class=\"pl-c1\"\u003ePrintf\u003c/span\u003e(\u003cspan class=\"pl-s\"\u003e\"Get(%v) = %v\u003cspan class=\"pl-cce\"\u003e\\n\u003c/span\u003e\"\u003c/span\u003e, \u003cspan class=\"pl-s1\"\u003ek\u003c/span\u003e, \u003cspan class=\"pl-s1\"\u003elm\u003c/span\u003e.\u003cspan class=\"pl-c1\"\u003eGet\u003c/span\u003e(\u003cspan class=\"pl-s1\"\u003ek\u003c/span\u003e))\n\t}\n\n\t\u003cspan class=\"pl-s1\"\u003ev\u003c/span\u003e, \u003cspan class=\"pl-s1\"\u003e_\u003 10000 c/span\u003e \u003cspan class=\"pl-c1\"\u003e:=\u003c/span\u003e \u003cspan class=\"pl-s1\"\u003elm\u003c/span\u003e.\u003cspan class=\"pl-c1\"\u003eRemove\u003c/span\u003e(\u003cspan class=\"pl-c1\"\u003e18\u003c/span\u003e)\n\t\u003cspan class=\"pl-s1\"\u003efmt\u003c/span\u003e.\u003cspan class=\"pl-c1\"\u003ePrintf\u003c/span\u003e(\u003cspan class=\"pl-s\"\u003e\"The value associated with 18 is %v\u003cspan class=\"pl-cce\"\u003e\\n\u003c/span\u003e\"\u003c/span\u003e, \u003cspan class=\"pl-s1\"\u003ev\u003c/span\u003e)\n\n\t\u003cspan class=\"pl-s1\"\u003ek\u003c/span\u003e, \u003cspan class=\"pl-s1\"\u003ev\u003c/span\u003e, \u003cspan class=\"pl-s1\"\u003e_\u003c/span\u003e \u003cspan class=\"pl-c1\"\u003e:=\u003c/span\u003e \u003cspan class=\"pl-s1\"\u003elm\u003c/span\u003e.\u003cspan class=\"pl-c1\"\u003eRemoveFirstElement\u003c/span\u003e()\n\t\u003cspan class=\"pl-s1\"\u003efmt\u003c/span\u003e.\u003cspan class=\"pl-c1\"\u003ePrintf\u003c/span\u003e(\u003cspan class=\"pl-s\"\u003e\"The first element removed is (%v, %v)\u003cspan class=\"pl-cce\"\u003e\\n\u003c/span\u003e\"\u003c/span\u003e, \u003cspan class=\"pl-s1\"\u003ek\u003c/span\u003e, \u003cspan class=\"pl-s1\"\u003ev\u003c/span\u003e)\n\n\t\u003cspan class=\"pl-s1\"\u003ek\u003c/span\u003e, \u003cspan class=\"pl-s1\"\u003ev\u003c/span\u003e, \u003cspan class=\"pl-s1\"\u003e_\u003c/span\u003e \u003cspan class=\"pl-c1\"\u003e=\u003c/span\u003e \u003cspan class=\"pl-s1\"\u003elm\u003c/span\u003e.\u003cspan class=\"pl-c1\"\u003eRemoveLastElement\u003c/span\u003e()\n\t\u003cspan class=\"pl-s1\"\u003efmt\u003c/span\u003e.\u003cspan class=\"pl-c1\"\u003ePrintf\u003c/span\u003e(\u003cspan class=\"pl-s\"\u003e\"The last element removed is (%v, %v)\u003cspan class=\"pl-cce\"\u003e\\n\u003c/span\u003e\"\u003c/span\u003e, \u003cspan class=\"pl-s1\"\u003ek\u003c/span\u003e, \u003cspan class=\"pl-s1\"\u003ev\u003c/span\u003e)\n}\u003c/pre\u003e\u003c/div\u003e\n\u003cp dir=\"auto\"\u003eIf the order in which the keys were accessed is expected for the iteration ordering, then the accessOrder flag should be set,\u003c/p\u003e\n\u003cdiv class=\"highlight highlight-source-go notranslate position-relative overflow-auto\" dir=\"auto\" data-snippet-clipboard-copy-content=\"// WithAccessOrder configures the iteration ordering for this linked map,\n// true for access-order, and false for insertion-order.\nWithAccessOrder(accessOrder bool) Interface\"\u003e\u003cpre\u003e\u003cspan class=\"pl-c\"\u003e// WithAccessOrder configures the iteration ordering for this linked map,\u003c/span\u003e\n\u003cspan class=\"pl-c\"\u003e// true for access-order, and false for insertion-order.\u003c/span\u003e\n\u003cspan class=\"pl-s1\"\u003eWithAccessOrder\u003c/span\u003e(\u003cspan class=\"pl-s1\"\u003eaccessOrder\u003c/span\u003e \u003cspan class=\"pl-s1\"\u003ebool\u003c/span\u003e) \u003cspan class=\"pl-s1\"\u003eInterface\u003c/span\u003e\u003c/pre\u003e\u003c/div\u003e\n\u003cp dir=\"auto\"\u003eThe following snips show how to interate a linkedMap,\u003c/p\u003e\n\u003cdiv class=\"highlight highlight-source-go notranslate position-relative overflow-auto\" dir=\"auto\" data-snippet-clipboard-copy-content=\"// To iterate over an linkedMap (where lm is an instance of linkedmap.Interface):\nit, hasNext := lm.Iterator()\nvar k, v interface{}\nfor hasNext {\n\tk, v, hasNext = it()\n\t// do something with k \u0026amp; v\n}\"\u003e\u003cpre\u003e\u003cspan class=\"pl-c\"\u003e// To iterate over an linkedMap (where lm is an instance of linkedmap.Interface):\u003c/span\u003e\n\u003cspan class=\"pl-s1\"\u003eit\u003c/span\u003e, \u003cspan class=\"pl-s1\"\u003ehasNext\u003c/span\u003e \u003cspan class=\"pl-c1\"\u003e:=\u003c/span\u003e \u003cspan class=\"pl-s1\"\u003elm\u003c/span\u003e.\u003cspan class=\"pl-c1\"\u003eIterator\u003c/span\u003e()\n\u003cspan class=\"pl-k\"\u003evar\u003c/span\u003e \u003cspan class=\"pl-s1\"\u003ek\u003c/span\u003e, \u003cspan class=\"pl-s1\"\u003ev\u003c/span\u003e \u003cspan class=\"pl-k\"\u003einterface\u003c/span\u003e{}\n\u003cspan class=\"pl-k\"\u003efor\u003c/span\u003e \u003cspan class=\"pl-s1\"\u003ehasNext\u003c/span\u003e {\n\t\u003cspan class=\"pl-s1\"\u003ek\u003c/span\u003e, \u003cspan class=\"pl-s1\"\u003ev\u003c/span\u003e, \u003cspan class=\"pl-s1\"\u003ehasNext\u003c/span\u003e \u003cspan class=\"pl-c1\"\u003e=\u003c/span\u003e \u003cspan class=\"pl-s1\"\u003eit\u003c/span\u003e()\n\t\u003cspan class=\"pl-c\"\u003e// do something with k \u0026amp; v\u003c/span\u003e\n}\u003c/pre\u003e\u003c/div\u003e\n\u003cdiv class=\"highlight highlight-source-go notranslate position-relative overflow-auto\" dir=\"auto\" data-snippet-clipboard-copy-content=\"// To iterate over an linkedMap in reverse order (where lm is an instance of linkedmap.Interface):\nit, hasPrev := lm.ReverseIterator()\nvar k, v interface{}\nfor hasPrev {\n\tk, v, hasPrev = it()\n\t// do something with k \u0026amp; v\n}\"\u003e\u003cpre\u003e\u003cspan class=\"pl-c\"\u003e// To iterate over an linkedMap in reverse order (where lm is an instance of linkedmap.Interface):\u003c/span\u003e\n\u003cspan class=\"pl-s1\"\u003eit\u003c/span\u003e, \u003cspan class=\"pl-s1\"\u003ehasPrev\u003c/span\u003e \u003cspan class=\"pl-c1\"\u003e:=\u003c/span\u003e \u003cspan class=\"pl-s1\"\u003elm\u003c/span\u003e.\u003cspan class=\"pl-c1\"\u003eReverseIterator\u003c/span\u003e()\n\u003cspan class=\"pl-k\"\u003evar\u003c/span\u003e \u003cspan class=\"pl-s1\"\u003ek\u003c/span\u003e, \u003cspan class=\"pl-s1\"\u003ev\u003c/span\u003e \u003cspan class=\"pl-k\"\u003einterface\u003c/span\u003e{}\n\u003cspan class=\"pl-k\"\u003efor\u003c/span\u003e \u003cspan class=\"pl-s1\"\u003ehasPrev\u003c/span\u003e {\n\t\u003cspan class=\"pl-s1\"\u003ek\u003c/span\u003e, \u003cspan class=\"pl-s1\"\u003ev\u003c/span\u003e, \u003cspan class=\"pl-s1\"\u003ehasPrev\u003c/span\u003e \u003cspan class=\"pl-c1\"\u003e=\u003c/span\u003e \u003cspan class=\"pl-s1\"\u003eit\u003c/span\u003e()\n\t\u003cspan class=\"pl-c\"\u003e// do something with k \u0026amp; v\u003c/span\u003e\n}\u003c/pre\u003e\u003c/div\u003e\n\u003cdiv class=\"markdown-heading\" dir=\"auto\"\u003e\u003ch2 tabindex=\"-1\" class=\"heading-element\" dir=\"auto\"\u003e\u003cstrong\u003eBTree\u003c/strong\u003e\u003c/h2\u003e\u003ca id=\"user-content-btree\" class=\"anchor\" aria-label=\"Permalink: BTree\" href=\"#btree\"\u003e\u003csvg class=\"octicon octicon-link\" viewBox=\"0 0 16 16\" version=\"1.1\" width=\"16\" height=\"16\" aria-hidden=\"true\"\u003e\u003cpath d=\"m7.775 3.275 1.25-1.25a3.5 3.5 0 1 1 4.95 4.95l-2.5 2.5a3.5 3.5 0 0 1-4.95 0 .751.751 0 0 1 .018-1.042.751.751 0 0 1 1.042-.018 1.998 1.998 0 0 0 2.83 0l2.5-2.5a2.002 2.002 0 0 0-2.83-2.83l-1.25 1.25a.751.751 0 0 1-1.042-.018.751.751 0 0 1-.018-1.042Zm-4.69 9.64a1.998 1.998 0 0 0 2.83 0l1.25-1.25a.751.751 0 0 1 1.042.018.751.751 0 0 1 .018 1.042l-1.25 1.25a3.5 3.5 0 1 1-4.95-4.95l2.5-2.5a3.5 3.5 0 0 1 4.95 0 .751.751 0 0 1-.018 1.042.751.751 0 0 1-1.042.018 1.998 1.998 0 0 0-2.83 0l-2.5 2.5a1.998 1.998 0 0 0 0 2.83Z\"\u003e\u003c/path\u003e\u003c/svg\u003e\u003c/a\u003e\u003c/div\u003e\n\u003cp dir=\"auto\"\u003eBTree is a B-Tree implementation. It was originally copied from github.com/google/btree, but it is refactored to adapt to the interface convention in this repository. Some improvements are also applied on top of the original design \u0026amp; implementation, so that it's more user-friendly.\nIt implements the following interface. Click \u003cstrong\u003e\u003ca href=\"/ahrtr/gocontainer/blob/master/examples/btree_example.go\"\u003ehere\u003c/a\u003e\u003c/strong\u003e to find examples on how to use a BTree.\u003c/p\u003e\n\u003cdiv class=\"highlight highlight-source-go notranslate position-relative overflow-auto\" dir=\"auto\" data-snippet-clipboard-copy-content=\"// Interface is a type of btree, and bTree implements this interface\ntype Interface interface {\n\tcollection.Interface\n\n\t// WithComparator sets an utils.Comparator instance for the btree.\n\t// It's used to impose a total ordering on the elements in the btree.\n\tWithComparator(c utils.Comparator) Interface\n\n\t// Clone clones the btree, lazily. The internal tree structure is marked read-only and\n\t// shared between the old and new btree. Writes to both the old and the new btree use copy-on-write logic.\n\tClone() Interface\n\t// ReplaceOrInsert adds the given item to the tree. If an item in the tree\n\t// already equals the given one, it is removed from the tree and returned.\n\t// Otherwise, nil is returned.\n\tReplaceOrInsert(item interface{}) interface{}\n\t// Delete removes an item equal to the passed in item from the tree, returning\n\t// it. If no such item exists, returns nil.\n\tDelete(item interface{}) interface{}\n\t// DeleteMin removes the smallest item in the tree and returns it.\n\t// If no such item exists, returns nil.\n\tDeleteMin() interface{}\n\t// DeleteMax removes the largest item in the tree and returns it.\n\t// If no such item exists, returns nil.\n\tDeleteMax() interface{}\n\n\t// AscendRange calls the iterator for every value in the tree within the range\n\t// [greaterOrEqual, lessThan), until iterator returns false.\n\tAscendRange(greaterOrEqual, lessThan interface{}, iterator ItemIterator)\n\t// AscendLessThan calls the iterator for every value in the tree within the range\n\t// [first, pivot), until iterator returns false.\n\tAscendLessThan(pivot interface{}, iterator ItemIterator)\n\t// AscendGreaterOrEqual calls the iterator for every value in the tree within\n\t// the range [pivot, last], until iterator returns false.\n\tAscendGreaterOrEqual(pivot interface{}, iterator ItemIterator)\n\t// Ascend calls the iterator for every value in the tree within the range\n\t// [first, last], until iterator returns false.\n\tAscend(iterator ItemIterator)\n\n\t// DescendRange calls the iterator for every value in the tree within the range\n\t// [lessOrEqual, greaterThan), until iterator returns false.\n\tDescendRange(lessOrEqual, greaterThan interface{}, iterator ItemIterator)\n\t// DescendLessOrEqual calls the iterator for every value in the tree within the range\n\t// [pivot, first], until iterator returns false.\n\tDescendLessOrEqual(pivot interface{}, iterator ItemIterator)\n\t// DescendGreaterThan calls the iterator for every value in the tree within\n\t// the range [last, pivot), until iterator returns false.\n\tDescendGreaterThan(pivot interface{}, iterator ItemIterator)\n\t// Descend calls the iterator for every value in the tree within the range\n\t// [last, first], until iterator returns false.\n\tDescend(iterator ItemIterator)\n\n\t// Get looks for the key item in the tree, returning it. It returns nil if\n\t// unable to find that item.\n\tGet(key interface{}) interface{}\n\t// Min returns the smallest item in the tree, or nil if the tree is empty.\n\tMin() interface{}\n\t// Max returns the largest item in the tree, or nil if the tree is empty.\n\tMax() interface{}\n\t// Has returns true if the given key is in the tree.\n\tHas(key interface{}) bool\n}\"\u003e\u003cpre\u003e\u003cspan class=\"pl-c\"\u003e// Interface is a type of btree, and bTree implements this interface\u003c/span\u003e\n\u003cspan class=\"pl-k\"\u003etype\u003c/span\u003e \u003cspan class=\"pl-smi\"\u003eInterface\u003c/span\u003e \u003cspan class=\"pl-k\"\u003einterface\u003c/span\u003e {\n\tcollection.\u003cspan class=\"pl-smi\"\u003eInterface\u003c/span\u003e\n\n\t\u003cspan class=\"pl-c\"\u003e// WithComparator sets an utils.Comparator instance for the btree.\u003c/span\u003e\n\t\u003cspan class=\"pl-c\"\u003e// It's used to impose a total ordering on the elements in the btree.\u003c/span\u003e\n\t\u003cspan class=\"pl-c1\"\u003eWithComparator\u003c/span\u003e(\u003cspan class=\"pl-s1\"\u003ec\u003c/span\u003e utils.\u003cspan class=\"pl-smi\"\u003eComparator\u003c/span\u003e) \u003cspan class=\"pl-smi\"\u003eInterface\u003c/span\u003e\n\n\t\u003cspan class=\"pl-c\"\u003e// Clone clones the btree, lazily. The internal tree structure is marked read-only and\u003c/span\u003e\n\t\u003cspan class=\"pl-c\"\u003e// shared between the old and new btree. Writes to both the old and the new btree use copy-on-write logic.\u003c/span\u003e\n\t\u003cspan class=\"pl-c1\"\u003eClone\u003c/span\u003e() \u003cspan class=\"pl-smi\"\u003eInterface\u003c/span\u003e\n\t\u003cspan class=\"pl-c\"\u003e// ReplaceOrInsert adds the given item to the tree. If an item in the tree\u003c/span\u003e\n\t\u003cspan class=\"pl-c\"\u003e// already equals the given one, it is removed from the tree and returned.\u003c/span\u003e\n\t\u003cspan class=\"pl-c\"\u003e// Otherwise, nil is returned.\u003c/span\u003e\n\t\u003cspan class=\"pl-c1\"\u003eReplaceOrInsert\u003c/span\u003e(\u003cspan class=\"pl-s1\"\u003eitem\u003c/span\u003e \u003cspan class=\"pl-k\"\u003einterface\u003c/span\u003e{}) \u003cspan class=\"pl-k\"\u003einterface\u003c/span\u003e{}\n\t\u003cspan class=\"pl-c\"\u003e// Delete removes an item equal to the passed in item from the tree, returning\u003c/span\u003e\n\t\u003cspan class=\"pl-c\"\u003e// it. If no such item exists, returns nil.\u003c/span\u003e\n\t\u003cspan class=\"pl-c1\"\u003eDelete\u003c/span\u003e(\u003cspan class=\"pl-s1\"\u003eitem\u003c/span\u003e \u003cspan class=\"pl-k\"\u003einterface\u003c/span\u003e{}) \u003cspan class=\"pl-k\"\u003einterface\u003c/span\u003e{}\n\t\u003cspan class=\"pl-c\"\u003e// DeleteMin removes the smallest item in the tree and returns it.\u003c/span\u003e\n\t\u003cspan class=\"pl-c\"\u003e// If no such item exists, returns nil.\u003c/span\u003e\n\t\u003cspan class=\"pl-c1\"\u003eDeleteMin\u003c/span\u003e() \u003cspan class=\"pl-k\"\u003einterface\u003c/span\u003e{}\n\t\u003cspan class=\"pl-c\"\u003e// DeleteMax removes the largest item in the tree and returns it.\u003c/span\u003e\n\t\u003cspan class=\"pl-c\"\u003e// If no such item exists, returns nil.\u003c/span\u003e\n\t\u003cspan class=\"pl-c1\"\u003eDeleteMax\u003c/span\u003e() \u003cspan class=\"pl-k\"\u003einterface\u003c/span\u003e{}\n\n\t\u003cspan class=\"pl-c\"\u003e// AscendRange calls the iterator for every value in the tree within the range\u003c/span\u003e\n\t\u003cspan class=\"pl-c\"\u003e// [greaterOrEqual, lessThan), until iterator returns false.\u003c/span\u003e\n\t\u003cspan class=\"pl-c1\"\u003eAscendRange\u003c/span\u003e(\u003cspan class=\"pl-s1\"\u003egreaterOrEqual\u003c/span\u003e, \u003cspan class=\"pl-s1\"\u003elessThan\u003c/span\u003e \u003cspan class=\"pl-k\"\u003einterface\u003c/span\u003e{}, \u003cspan class=\"pl-s1\"\u003eiterator\u003c/span\u003e \u003cspan class=\"pl-smi\"\u003eItemIterator\u003c/span\u003e)\n\t\u003cspan class=\"pl-c\"\u003e// AscendLessThan calls the iterator for every value in the tree within the range\u003c/span\u003e\n\t\u003cspan class=\"pl-c\"\u003e// [first, pivot), until iterator returns false.\u003c/span\u003e\n\t\u003cspan class=\"pl-c1\"\u003eAscendLessThan\u003c/span\u003e(\u003cspan class=\"pl-s1\"\u003epivot\u003c/span\u003e \u003cspan class=\"pl-k\"\u003einterface\u003c/span\u003e{}, \u003cspan class=\"pl-s1\"\u003eiterator\u003c/span\u003e \u003cspan class=\"pl-smi\"\u003eItemIterator\u003c/span\u003e)\n\t\u003cspan class=\"pl-c\"\u003e// AscendGreaterOrEqual calls the iterator for every value in the tree within\u003c/span\u003e\n\t\u003cspan class=\"pl-c\"\u003e// the range [pivot, last], until iterator returns false.\u003c/span\u003e\n\t\u003cspan class=\"pl-c1\"\u003eAscendGreaterOrEqual\u003c/span\u003e(\u003cspan class=\"pl-s1\"\u003epivot\u003c/span\u003e \u003cspan class=\"pl-k\"\u003einterface\u003c/span\u003e{}, \u003cspan class=\"pl-s1\"\u003eiterator\u003c/span\u003e \u003cspan class=\"pl-smi\"\u003eItemIterator\u003c/span\u003e)\n\t\u003cspan class=\"pl-c\"\u003e// Ascend calls the iterator for every value in the tree within the range\u003c/span\u003e\n\t\u003cspan class=\"pl-c\"\u003e// [first, last], until iterator returns false.\u003c/span\u003e\n\t\u003cspan class=\"pl-c1\"\u003eAscend\u003c/span\u003e(\u003cspan class=\"pl-s1\"\u003eiterator\u003c/span\u003e \u003cspan class=\"pl-smi\"\u003eItemIterator\u003c/span\u003e)\n\n\t\u003cspan class=\"pl-c\"\u003e// DescendRange calls the iterator for every value in the tree within the range\u003c/span\u003e\n\t\u003cspan class=\"pl-c\"\u003e// [lessOrEqual, greaterThan), until iterator returns false.\u003c/span\u003e\n\t\u003cspan class=\"pl-c1\"\u003eDescendRange\u003c/span\u003e(\u003cspan class=\"pl-s1\"\u003elessOrEqual\u003c/span\u003e, \u003cspan class=\"pl-s1\"\u003egreaterThan\u003c/span\u003e \u003cspan class=\"pl-k\"\u003einterface\u003c/span\u003e{}, \u003cspan class=\"pl-s1\"\u003eiterator\u003c/span\u003e \u003cspan class=\"pl-smi\"\u003eItemIterator\u003c/span\u003e)\n\t\u003cspan class=\"pl-c\"\u003e// DescendLessOrEqual calls the iterator for every value in the tree within the range\u003c/span\u003e\n\t\u003cspan class=\"pl-c\"\u003e// [pivot, first], until iterator returns false.\u003c/span\u003e\n\t\u003cspan class=\"pl-c1\"\u003eDescendLessOrEqual\u003c/span\u003e(\u003cspan class=\"pl-s1\"\u003epivot\u003c/span\u003e \u003cspan class=\"pl-k\"\u003einterface\u003c/span\u003e{}, \u003cspan class=\"pl-s1\"\u003eiterator\u003c/span\u003e \u003cspan class=\"pl-smi\"\u003eItemIterator\u003c/span\u003e)\n\t\u003cspan class=\"pl-c\"\u003e// DescendGreaterThan calls the iterator for every value in the tree within\u003c/span\u003e\n\t\u003cspan class=\"pl-c\"\u003e// the range [last, pivot), until iterator returns false.\u003c/span\u003e\n\t\u003cspan class=\"pl-c1\"\u003eDescendGreaterThan\u003c/span\u003e(\u003cspan class=\"pl-s1\"\u003epivot\u003c/span\u003e \u003cspan class=\"pl-k\"\u003einterface\u003c/span\u003e{}, \u003cspan class=\"pl-s1\"\u003eiterator\u003c/span\u003e \u003cspan class=\"pl-smi\"\u003eItemIterator\u003c/span\u003e)\n\t\u003cspan class=\"pl-c\"\u003e// Descend calls the iterator for every value in the tree within the range\u003c/span\u003e\n\t\u003cspan class=\"pl-c\"\u003e// [last, first], until iterator returns false.\u003c/span\u003e\n\t\u003cspan class=\"pl-c1\"\u003eDescend\u003c/span\u003e(\u003cspan class=\"pl-s1\"\u003eiterator\u003c/span\u003e \u003cspan class=\"pl-smi\"\u003eItemIterator\u003c/span\u003e)\n\n\t\u003cspan class=\"pl-c\"\u003e// Get looks for the key item in the tree, returning it. It returns nil if\u003c/span\u003e\n\t\u003cspan class=\"pl-c\"\u003e// unable to find that item.\u003c/span\u003e\n\t\u003cspan class=\"pl-c1\"\u003eGet\u003c/span\u003e(\u003cspan class=\"pl-s1\"\u003ekey\u003c/span\u003e \u003cspan class=\"pl-k\"\u003einterface\u003c/span\u003e{}) \u003cspan class=\"pl-k\"\u003einterface\u003c/span\u003e{}\n\t\u003cspan class=\"pl-c\"\u003e// Min returns the smallest item in the tree, or nil if the tree is empty.\u003c/span\u003e\n\t\u003cspan class=\"pl-c1\"\u003eMin\u003c/span\u003e() \u003cspan class=\"pl-k\"\u003einterface\u003c/span\u003e{}\n\t\u003cspan class=\"pl-c\"\u003e// Max returns the largest item in the tree, or nil if the tree is empty.\u003c/span\u003e\n\t\u003cspan class=\"pl-c1\"\u003eMax\u003c/span\u003e() \u003cspan class=\"pl-k\"\u003einterface\u003c/span\u003e{}\n\t\u003cspan class=\"pl-c\"\u003e// Has returns true if the given key is in the tree.\u003c/span\u003e\n\t\u003cspan class=\"pl-c1\"\u003eHas\u003c/span\u003e(\u003cspan class=\"pl-s1\"\u003ekey\u003c/span\u003e \u003cspan class=\"pl-k\"\u003einterface\u003c/span\u003e{}) \u003cspan class=\"pl-smi\"\u003ebool\u003c/span\u003e\n}\u003c/pre\u003e\u003c/div\u003e\n\u003cp dir=\"auto\"\u003ePlease import the following package in order to use btree,\u003c/p\u003e\n\u003cdiv class=\"highlight highlight-source-go notranslate position-relative overflow-auto\" dir=\"auto\" data-snippet-clipboard-copy-content=\"import (\n\t\u0026quot;github.com/ahrtr/gocontainer/btree\u0026quot;\n)\"\u003e\u003cpre\u003e\u003cspan class=\"pl-k\"\u003eimport\u003c/span\u003e (\n\t\u003cspan class=\"pl-s\"\u003e\"github.com/ahrtr/gocontainer/btree\"\u003c/span\u003e\n)\u003c/pre\u003e\u003c/div\u003e\n\u003cp dir=\"auto\"\u003eCall btree.New() to create a BTree,\u003c/p\u003e\n\u003cdiv class=\"highlight highlight-source-go notranslate position-relative overflow-auto\" dir=\"auto\" data-snippet-clipboard-copy-content=\"New() Interface \"\u003e\u003cpre\u003e\u003cspan class=\"pl-s1\"\u003eNew\u003c/span\u003e() \u003cspan class=\"pl-s1\"\u003eInterface\u003c/span\u003e \u003c/pre\u003e\u003c/div\u003e\n\u003cp dir=\"auto\"\u003eThe following is a simple example for btree,\u003c/p\u003e\n\u003cdiv class=\"highlight highlight-source-go notranslate position-relative overflow-auto\" dir=\"auto\" data-snippet-clipboard-copy-content=\"package main\n\nimport (\n\t\u0026quot;fmt\u0026quot;\n\n\t\u0026quot;github.com/ahrtr/gocontainer/btree\u0026quot;\n)\n\nfunc main() {\n items := []int {5, 9, 2, 4, 11, 6}\n tr := btree.New(2)\n\n fmt.Printf(\u0026quot;tr.Size(): %d\\n\u0026quot;, tr.Size()) // should be 0 in the beginning\n\n // Insert values\n fmt.Printf(\u0026quot;Inserting %d items: %v\\n\u0026quot;, len(items), items)\n for _, item := range items {\n \ttr.ReplaceOrInsert(item)\n }\n\n // Search values\n fmt.Printf(\u0026quot; tr.Size(): %d\\n\u0026quot;, tr.Size()) // should be len(items): 6 now\n fmt.Printf(\u0026quot; tr.Min(): %v\\n\u0026quot;, tr.Min()) // should be 2\n fmt.Printf(\u0026quot; tr.Max(): %v\\n\u0026quot;, tr.Max()) // should be 11\n fmt.Printf(\u0026quot; tr.Has(6): %t\\n\u0026quot;, tr.Has(6)) // true\n fmt.Printf(\u0026quot; tr.Get(6): %v\\n\u0026quot;, tr.Get(6)) // 6\n fmt.Printf(\u0026quot; tr.Has(7): %t\\n\u0026quot;, tr.Has(7)) // false\n fmt.Printf(\u0026quot; tr.Get(7): %v\\n\u0026quot;, tr.Get(7)) // nil\n\n // Delete values\n fmt.Println(\u0026quot;Deleting items:\u0026quot;)\n fmt.Printf(\u0026quot; tr.DeleteMin(): %v\\n\u0026quot;, tr.DeleteMin()) // 2 is deleted and returned\n fmt.Printf(\u0026quot; tr.Min(): %v\\n\u0026quot;, tr.Min()) // should be 4 now\n fmt.Printf(\u0026quot; tr.DeleteMax(): %v\\n\u0026quot;, tr.DeleteMax()) // 11 is deleted and returned\n fmt.Printf(\u0026quot; tr.Max(): %v\\n\u0026quot;, tr.Max()) // should be 9 now\n fmt.Printf(\u0026quot; tr.Delete(6): %v\\n\u0026quot;, tr.Delete(6)) // 6 is deleted and returned\n fmt.Printf(\u0026quot; tr.Delete(7): %v\\n\u0026quot;, tr.Delete(7)) // 7 doesn't exist, so nil is returned\n\n fmt.Printf(\u0026quot;tr.Size(): %d\\n\u0026quot;, tr.Size()) // should be 3 now because 3 items have already been removed\n}\"\u003e\u003cpre\u003e\u003cspan class=\"pl-k\"\u003epackage\u003c/span\u003e main\n\n\u003cspan class=\"pl-k\"\u003eimport\u003c/span\u003e (\n\t\u003cspan class=\"pl-s\"\u003e\"fmt\"\u003c/span\u003e\n\n\t\u003cspan class=\"pl-s\"\u003e\"github.com/ahrtr/gocontainer/btree\"\u003c/span\u003e\n)\n\n\u003cspan class=\"pl-k\"\u003efunc\u003c/span\u003e \u003cspan class=\"pl-s1\"\u003emain\u003c/span\u003e() {\n \u003cspan class=\"pl-s1\"\u003eitems\u003c/span\u003e \u003cspan class=\"pl-c1\"\u003e:=\u003c/span\u003e []\u003cspan class=\"pl-smi\"\u003eint\u003c/span\u003e {\u003cspan class=\"pl-c1\"\u003e5\u003c/span\u003e, \u003cspan class=\"pl-c1\"\u003e9\u003c/span\u003e, \u003cspan class=\"pl-c1\"\u003e2\u003c/span\u003e, \u003cspan class=\"pl-c1\"\u003e4\u003c/span\u003e, \u003cspan class=\"pl-c1\"\u003e11\u003c/span\u003e, \u003cspan class=\"pl-c1\"\u003e6\u003c/span\u003e}\n \u003cspan class=\"pl-s1\"\u003etr\u003c/span\u003e \u003cspan class=\"pl-c1\"\u003e:=\u003c/span\u003e \u003cspan class=\"pl-s1\"\u003ebtree\u003c/span\u003e.\u003cspan class=\"pl-c1\"\u003eNew\u003c/span\u003e(\u003cspan class=\"pl-c1\"\u003e2\u003c/span\u003e)\n\n \u003cspan class=\"pl-s1\"\u003efmt\u003c/span\u003e.\u003cspan class=\"pl-c1\"\u003ePrintf\u003c/span\u003e(\u003cspan class=\"pl-s\"\u003e\"tr.Size(): %d\u003cspan class=\"pl-cce\"\u003e\\n\u003c/span\u003e\"\u003c/span\u003e, \u003cspan class=\"pl-s1\"\u003etr\u003c/span\u003e.\u003cspan class=\"pl-c1\"\u003eSize\u003c/span\u003e()) \u003cspan class=\"pl-c\"\u003e// should be 0 in the beginning\u003c/span\u003e\n\n \u003cspan class=\"pl-c\"\u003e// Insert values\u003c/span\u003e\n \u003cspan class=\"pl-s1\"\u003efmt\u003c/span\u003e.\u003cspan class=\"pl-c1\"\u003ePrintf\u003c/span\u003e(\u003cspan class=\"pl-s\"\u003e\"Inserting %d items: %v\u003cspan class=\"pl-cce\"\u003e\\n\u003c/span\u003e\"\u003c/span\u003e, \u003cspan class=\"pl-s1\"\u003elen\u003c/span\u003e(\u003cspan class=\"pl-s1\"\u003eitems\u003c/span\u003e), \u003cspan class=\"pl-s1\"\u003eitems\u003c/span\u003e)\n \u003cspan class=\"pl-k\"\u003efor\u003c/span\u003e \u003cspan class=\"pl-s1\"\u003e_\u003c/span\u003e, \u003cspan class=\"pl-s1\"\u003eitem\u003c/span\u003e \u003cspan class=\"pl-c1\"\u003e:=\u003c/span\u003e \u003cspan class=\"pl-k\"\u003erange\u003c/span\u003e \u003cspan class=\"pl-s1\"\u003eitems\u003c/span\u003e {\n \t\u003cspan class=\"pl-s1\"\u003etr\u003c/span\u003e.\u003cspan class=\"pl-c1\"\u003eReplaceOrInsert\u003c/span\u003e(\u003cspan class=\"pl-s1\"\u003eitem\u003c/span\u003e)\n }\n\n \u003cspan class=\"pl-c\"\u003e// Search values\u003c/span\u003e\n \u003cspan class=\"pl-s1\"\u003efmt\u003c/span\u003e.\u003cspan class=\"pl-c1\"\u003ePrintf\u003c/span\u003e(\u003cspan class=\"pl-s\"\u003e\" tr.Size(): %d\u003cspan class=\"pl-cce\"\u003e\\n\u003c/span\u003e\"\u003c/span\u003e, \u003cspan class=\"pl-s1\"\u003etr\u003c/span\u003e.\u003cspan class=\"pl-c1\"\u003eSize\u003c/span\u003e()) \u003cspan class=\"pl-c\"\u003e// should be len(items): 6 now\u003c/span\u003e\n \u003cspan class=\"pl-s1\"\u003efmt\u003c/span\u003e.\u003cspan class=\"pl-c1\"\u003ePrintf\u003c/span\u003e(\u003cspan class=\"pl-s\"\u003e\" tr.Min(): %v\u003cspan class=\"pl-cce\"\u003e\\n\u003c/span\u003e\"\u003c/span\u003e, \u003cspan class=\"pl-s1\"\u003etr\u003c/span\u003e.\u003cspan class=\"pl-c1\"\u003eMin\u003c/span\u003e()) \u003cspan class=\"pl-c\"\u003e// should be 2\u003c/span\u003e\n \u003cspan class=\"pl-s1\"\u003efmt\u003c/span\u003e.\u003cspan class=\"pl-c1\"\u003ePrintf\u003c/span\u003e(\u003cspan class=\"pl-s\"\u003e\" tr.Max(): %v\u003cspan class=\"pl-cce\"\u003e\\n\u003c/span\u003e\"\u003c/span\u003e, \u003cspan class=\"pl-s1\"\u003etr\u003c/span\u003e.\u003cspan class=\"pl-c1\"\u003eMax\u003c/span\u003e()) \u003cspan class=\"pl-c\"\u003e// should be 11\u003c/span\u003e\n \u003cspan class=\"pl-s1\"\u003efmt\u003c/span\u003e.\u003cspan class=\"pl-c1\"\u003ePrintf\u003c/span\u003e(\u003cspan class=\"pl-s\"\u003e\" tr.Has(6): %t\u003cspan class=\"pl-cce\"\u003e\\n\u003c/span\u003e\"\u003c/span\u003e, \u003cspan class=\"pl-s1\"\u003etr\u003c/span\u003e.\u003cspan class=\"pl-c1\"\u003eHas\u003c/span\u003e(\u003cspan class=\"pl-c1\"\u003e6\u003c/span\u003e)) \u003cspan class=\"pl-c\"\u003e// true\u003c/span\u003e\n \u003cspan class=\"pl-s1\"\u003efmt\u003c/span\u003e.\u003cspan class=\"pl-c1\"\u003ePrintf\u003c/span\u003e(\u003cspan class=\"pl-s\"\u003e\" tr.Get(6): %v\u003cspan class=\"pl-cce\"\u003e\\n\u003c/span\u003e\"\u003c/span\u003e, \u003cspan class=\"pl-s1\"\u003etr\u003c/span\u003e.\u003cspan class=\"pl-c1\"\u003eGet\u003c/span\u003e(\u003cspan class=\"pl-c1\"\u003e6\u003c/span\u003e)) \u003cspan class=\"pl-c\"\u003e// 6\u003c/span\u003e\n \u003cspan class=\"pl-s1\"\u003efmt\u003c/span\u003e.\u003cspan class=\"pl-c1\"\u003ePrintf\u003c/span\u003e(\u003cspan class=\"pl-s\"\u003e\" tr.Has(7): %t\u003cspan class=\"pl-cce\"\u003e\\n\u003c/span\u003e\"\u003c/span\u003e, \u003cspan class=\"pl-s1\"\u003etr\u003c/span\u003e.\u003cspan class=\"pl-c1\"\u003eHas\u003c/span\u003e(\u003cspan class=\"pl-c1\"\u003e7\u003c/span\u003e)) \u003cspan class=\"pl-c\"\u003e// false\u003c/span\u003e\n \u003cspan class=\"pl-s1\"\u003efmt\u003c/span\u003e.\u003cspan class=\"pl-c1\"\u003ePrintf\u003c/span\u003e(\u003cspan class=\"pl-s\"\u003e\" tr.Get(7): %v\u003cspan class=\"pl-cce\"\u003e\\n\u003c/span\u003e\"\u003c/span\u003e, \u003cspan class=\"pl-s1\"\u003etr\u003c/span\u003e.\u003cspan class=\"pl-c1\"\u003eGet\u003c/span\u003e(\u003cspan class=\"pl-c1\"\u003e7\u003c/span\u003e)) \u003cspan class=\"pl-c\"\u003e// nil\u003c/span\u003e\n\n \u003cspan class=\"pl-c\"\u003e// Delete values\u003c/span\u003e\n \u003cspan class=\"pl-s1\"\u003efmt\u003c/span\u003e.\u003cspan class=\"pl-c1\"\u003ePrintln\u003c/span\u003e(\u003cspan class=\"pl-s\"\u003e\"Deleting items:\"\u003c/span\u003e)\n \u003cspan class=\"pl-s1\"\u003efmt\u003c/span\u003e.\u003cspan class=\"pl-c1\"\u003ePrintf\u003c/span\u003e(\u003cspan class=\"pl-s\"\u003e\" tr.DeleteMin(): %v\u003cspan class=\"pl-cce\"\u003e\\n\u003c/span\u003e\"\u003c/span\u003e, \u003cspan class=\"pl-s1\"\u003etr\u003c/span\u003e.\u003cspan class=\"pl-c1\"\u003eDeleteMin\u003c/span\u003e()) \u003cspan class=\"pl-c\"\u003e// 2 is deleted and returned\u003c/span\u003e\n \u003cspan class=\"pl-s1\"\u003efmt\u003c/span\u003e.\u003cspan class=\"pl-c1\"\u003ePrintf\u003c/span\u003e(\u003cspan class=\"pl-s\"\u003e\" tr.Min(): %v\u003cspan class=\"pl-cce\"\u003e\\n\u003c/span\u003e\"\u003c/span\u003e, \u003cspan class=\"pl-s1\"\u003etr\u003c/span\u003e.\u003cspan class=\"pl-c1\"\u003eMin\u003c/span\u003e()) \u003cspan class=\"pl-c\"\u003e// should be 4 now\u003c/span\u003e\n \u003cspan class=\"pl-s1\"\u003efmt\u003c/span\u003e.\u003cspan class=\"pl-c1\"\u003ePrintf\u003c/span\u003e(\u003cspan class=\"pl-s\"\u003e\" tr.DeleteMax(): %v\u003cspan class=\"pl-cce\"\u003e\\n\u003c/span\u003e\"\u003c/span\u003e, \u003cspan class=\"pl-s1\"\u003etr\u003c/span\u003e.\u003cspan class=\"pl-c1\"\u003eDeleteMax\u003c/span\u003e()) \u003cspan class=\"pl-c\"\u003e// 11 is deleted and returned\u003c/span\u003e\n \u003cspan class=\"pl-s1\"\u003efmt\u003c/span\u003e.\u003cspan class=\"pl-c1\"\u003ePrintf\u003c/span\u003e(\u003cspan class=\"pl-s\"\u003e\" tr.Max(): %v\u003cspan class=\"pl-cce\"\u003e\\n\u003c/span\u003e\"\u003c/span\u003e, \u003cspan class=\"pl-s1\"\u003etr\u003c/span\u003e.\u003cspan class=\"pl-c1\"\u003eMax\u003c/span\u003e()) \u003cspan class=\"pl-c\"\u003e// should be 9 now\u003c/span\u003e\n \u003cspan class=\"pl-s1\"\u003efmt\u003c/span\u003e.\u003cspan class=\"pl-c1\"\u003ePrintf\u003c/span\u003e(\u003cspan class=\"pl-s\"\u003e\" tr.Delete(6): %v\u003cspan class=\"pl-cce\"\u003e\\n\u003c/span\u003e\"\u003c/span\u003e, \u003cspan class=\"pl-s1\"\u003etr\u003c/span\u003e.\u003cspan class=\"pl-c1\"\u003eDelete\u003c/span\u003e(\u003cspan class=\"pl-c1\"\u003e6\u003c/span\u003e)) \u003cspan class=\"pl-c\"\u003e// 6 is deleted and returned\u003c/span\u003e\n \u003cspan class=\"pl-s1\"\u003efmt\u003c/span\u003e.\u003cspan class=\"pl-c1\"\u003ePrintf\u003c/span\u003e(\u003cspan class=\"pl-s\"\u003e\" tr.Delete(7): %v\u003cspan class=\"pl-cce\"\u003e\\n\u003c/span\u003e\"\u003c/span\u003e, \u003cspan class=\"pl-s1\"\u003etr\u003c/span\u003e.\u003cspan class=\"pl-c1\"\u003eDelete\u003c/span\u003e(\u003cspan class=\"pl-c1\"\u003e7\u003c/span\u003e)) \u003cspan class=\"pl-c\"\u003e// 7 doesn't exist, so nil is returned\u003c/span\u003e\n\n \u003cspan class=\"pl-s1\"\u003efmt\u003c/span\u003e.\u003cspan class=\"pl-c1\"\u003ePrintf\u003c/span\u003e(\u003cspan class=\"pl-s\"\u003e\"tr.Size(): %d\u003cspan class=\"pl-cce\"\u003e\\n\u003c/span\u003e\"\u003c/span\u003e, \u003cspan class=\"pl-s1\"\u003etr\u003c/span\u003e.\u003cspan class=\"pl-c1\"\u003eSize\u003c/span\u003e()) \u003cspan class=\"pl-c\"\u003e// should be 3 now because 3 items have already been removed\u003c/span\u003e\n}\u003c/pre\u003e\u003c/div\u003e\n\u003cp dir=\"auto\"\u003eA utils.Comparator instance can be provided for a btree by method WithComparator, please get more detailed info in \u003cstrong\u003e\u003ca href=\"#comparator\"\u003eComparator\u003c/a\u003e\u003c/strong\u003e.\u003c/p\u003e\n\u003cdiv class=\"highlight highlight-source-go notranslate position-relative overflow-auto\" dir=\"auto\" data-snippet-clipboard-copy-content=\"WithComparator(c utils.Comparator) Interface\"\u003e\u003cpre\u003e\u003cspan class=\"pl-s1\"\u003eWithComparator\u003c/span\u003e(\u003cspan class=\"pl-s1\"\u003ec\u003c/span\u003e \u003cspan class=\"pl-s1\"\u003eutils\u003c/span\u003e.\u003cspan class=\"pl-c1\"\u003eComparator\u003c/span\u003e) \u003cspan class=\"pl-s1\"\u003eInterface\u003c/span\u003e\u003c/pre\u003e\u003c/div\u003e\n\u003cdiv class=\"markdown-heading\" dir=\"auto\"\u003e\u003ch2 tabindex=\"-1\" class=\"heading-element\" dir=\"auto\"\u003eOthers\u003c/h2\u003e\u003ca id=\"user-content-others\" class=\"anchor\" aria-label=\"Permalink: Others\" href=\"#others\"\u003e\u003csvg class=\"octicon octicon-link\" viewBox=\"0 0 16 16\" version=\"1.1\" width=\"16\" height=\"16\" aria-hidden=\"true\"\u003e\u003cpath d=\"m7.775 3.275 1.25-1.25a3.5 3.5 0 1 1 4.95 4.95l-2.5 2.5a3.5 3.5 0 0 1-4.95 0 .751.751 0 0 1 .018-1.042.751.751 0 0 1 1.042-.018 1.998 1.998 0 0 0 2.83 0l2.5-2.5a2.002 2.002 0 0 0-2.83-2.83l-1.25 1.25a.751.751 0 0 1-1.042-.018.751.751 0 0 1-.018-1.042Zm-4.69 9.64a1.998 1.998 0 0 0 2.83 0l1.25-1.25a.751.751 0 0 1 1.042.018.751.751 0 0 1 .018 1.042l-1.25 1.25a3.5 3.5 0 1 1-4.95-4.95l2.5-2.5a3.5 3.5 0 0 1 4.95 0 .751.751 0 0 1-.018 1.042.751.751 0 0 1-1.042.018 1.998 1.998 0 0 0-2.83 0l-2.5 2.5a1.998 1.998 0 0 0 0 2.83Z\"\u003e\u003c/path\u003e\u003c/svg\u003e\u003c/a\u003e\u003c/div\u003e\n\u003cp dir=\"auto\"\u003eMore containers will be added soon. Please also kindly let me know if you need any other kinds of containers. Feel free to raise issues.\u003c/p\u003e\n\u003cdiv class=\"markdown-heading\" dir=\"auto\"\u003e\u003ch1 tabindex=\"-1\" class=\"heading-element\" dir=\"auto\"\u003eUtilities\u003c/h1\u003e\u003ca id=\"user-content-utilities\" class=\"anchor\" aria-label=\"Permalink: Utilities\" href=\"#utilities\"\u003e\u003csvg class=\"octicon octicon-link\" viewBox=\"0 0 16 16\" version=\"1.1\" width=\"16\" height=\"16\" aria-hidden=\"true\"\u003e\u003cpath d=\"m7.775 3.275 1.25-1.25a3.5 3.5 0 1 1 4.95 4.95l-2.5 2.5a3.5 3.5 0 0 1-4.95 0 .751.751 0 0 1 .018-1.042.751.751 0 0 1 1.042-.018 1.998 1.998 0 0 0 2.83 0l2.5-2.5a2.002 2.002 0 0 0-2.83-2.83l-1.25 1.25a.751.751 0 0 1-1.042-.018.751.751 0 0 1-.018-1.042Zm-4.69 9.64a1.998 1.998 0 0 0 2.83 0l1.25-1.25a.751.751 0 0 1 1.042.018.751.751 0 0 1 .018 1.042l-1.25 1.25a3.5 3.5 0 1 1-4.95-4.95l2.5-2.5a3.5 3.5 0 0 1 4.95 0 .751.751 0 0 1-.018 1.042.751.751 0 0 1-1.042.018 1.998 1.998 0 0 0-2.83 0l-2.5 2.5a1.998 1.998 0 0 0 0 2.83Z\"\u003e\u003c/path\u003e\u003c/svg\u003e\u003c/a\u003e\u003c/div\u003e\n\u003cdiv class=\"markdown-heading\" dir=\"auto\"\u003e\u003ch2 tabindex=\"-1\" class=\"heading-element\" dir=\"auto\"\u003eComparator\u003c/h2\u003e\u003ca id=\"user-content-comparator\" class=\"anchor\" aria-label=\"Permalink: Comparator\" href=\"#comparator\"\u003e\u003csvg class=\"octicon octicon-link\" viewBox=\"0 0 16 16\" version=\"1.1\" width=\"16\" height=\"16\" aria-hidden=\"true\"\u003e\u003cpath d=\"m7.775 3.275 1.25-1.25a3.5 3.5 0 1 1 4.95 4.95l-2.5 2.5a3.5 3.5 0 0 1-4.95 0 .751.751 0 0 1 .018-1.042.751.751 0 0 1 1.042-.018 1.998 1.998 0 0 0 2.83 0l2.5-2.5a2.002 2.002 0 0 0-2.83-2.83l-1.25 1.25a.751.751 0 0 1-1.042-.018.751.751 0 0 1-.018-1.042Zm-4.69 9.64a1.998 1.998 0 0 0 2.83 0l1.25-1.25a.751.751 0 0 1 1.042.018.751.751 0 0 1 .018 1.042l-1.25 1.25a3.5 3.5 0 1 1-4.95-4.95l2.5-2.5a3.5 3.5 0 0 1 4.95 0 .751.751 0 0 1-.018 1.042.751.751 0 0 1-1.042.018 1.998 1.998 0 0 0-2.83 0l-2.5 2.5a1.998 1.998 0 0 0 0 2.83Z\"\u003e\u003c/path\u003e\u003c/svg\u003e\u003c/a\u003e\u003c/div\u003e\n\u003cp dir=\"auto\"\u003eThe comparator utility contains a function \"Compare\" and an interface \"Comparator\",\u003c/p\u003e\n\u003cdiv class=\"highlight highlight-source-go notranslate position-relative overflow-auto\" dir=\"auto\" data-snippet-clipboard-copy-content=\"// Compare compares two arguments using the given Comparator. If the Comparator isn't provided, then the two values are compared according to their natural ordering.\n// They must be the same type, otherwise returns an error in the second return value.\n// It returns a negative integer, zero, or a positive integer as the first argument is less than, equal to, or greater than the second.\nfunc Compare(v1 interface{}, v2 interface{}, cmp Comparator) (int, error)\n\n// Comparator imposes a total ordering on some collection of objects, and it allows precise control over the sort order.\ntype Comparator interface {\n\t// Compare compares its two arguments for order.\n\t// It returns a negative integer, zero, or a positive integer as the first argument is less than, equal to, or greater than the second.\n\tCompare(v1 interface{}, v2 interface{}) (int, error)\n}\"\u003e\u003cpre\u003e\u003cspan class=\"pl-c\"\u003e// Compare compares two arguments using the given Comparator. If the Comparator isn't provided, then the two values are compared according to their natural ordering.\u003c/span\u003e\n\u003cspan class=\"pl-c\"\u003e// They must be the same type, otherwise returns an error in the second return value.\u003c/span\u003e\n\u003cspan class=\"pl-c\"\u003e// It returns a negative integer, zero, or a positive integer as the first argument is less than, equal to, or greater than the second.\u003c/span\u003e\n\u003cspan class=\"pl-k\"\u003efunc\u003c/span\u003e \u003cspan class=\"pl-s1\"\u003eCompare\u003c/span\u003e(\u003cspan class=\"pl-s1\"\u003ev1\u003c/span\u003e \u003cspan class=\"pl-k\"\u003einterface\u003c/span\u003e{}, \u003cspan class=\"pl-s1\"\u003ev2\u003c/span\u003e \u003cspan class=\"pl-k\"\u003einterface\u003c/span\u003e{}, \u003cspan class=\"pl-s1\"\u003ecmp\u003c/span\u003e \u003cspan class=\"pl-smi\"\u003eComparator\u003c/span\u003e) (\u003cspan class=\"pl-smi\"\u003eint\u003c/span\u003e, \u003cspan class=\"pl-smi\"\u003eerror\u003c/span\u003e)\n\n\u003cspan class=\"pl-c\"\u003e// Comparator imposes a total ordering on some collection of objects, and it allows precise control over the sort order.\u003c/span\u003e\n\u003cspan class=\"pl-k\"\u003etype\u003c/span\u003e \u003cspan class=\"pl-smi\"\u003eComparator\u003c/span\u003e \u003cspan class=\"pl-k\"\u003einterface\u003c/span\u003e {\n\t\u003cspan class=\"pl-c\"\u003e// Compare compares its two arguments for order.\u003c/span\u003e\n\t\u003cspan class=\"pl-c\"\u003e// It returns a negative integer, zero, or a positive integer as the first argument is less than, equal to, or greater than the second.\u003c/span\u003e\n\t\u003cspan class=\"pl-c1\"\u003eCompare\u003c/span\u003e(\u003cspan class=\"pl-s1\"\u003ev1\u003c/span\u003e \u003cspan class=\"pl-k\"\u003einterface\u003c/span\u003e{}, \u003cspan class=\"pl-s1\"\u003ev2\u003c/span\u003e \u003cspan class=\"pl-k\"\u003einterface\u003c/span\u003e{}) (\u003cspan class=\"pl-smi\"\u003eint\u003c/span\u003e, \u003cspan class=\"pl-smi\"\u003eerror\u003c/span\u003e)\n}\u003c/pre\u003e\u003c/div\u003e\n\u003cp dir=\"auto\"\u003eThe function \"Compare\" is used to compare two values using the given Comparator of the third parameter. If the Comparator is nil, then they are compared according to their natural ordering of golang build-in data types listed below. The two arguments must be the same data type, otherwise an error in the second return value will be returned. It returns a negative integer, zero, or a positive integer as the first argument is less than, equal to, or greater than the second one. Note that for \u003cstrong\u003ebool\u003c/strong\u003e, a false is regarded as less than a true.\u003c/p\u003e\n\u003cul dir=\"auto\"\u003e\n\u003cli\u003ebool\u003c/li\u003e\n\u003cli\u003eint\u003c/li\u003e\n\u003cli\u003eint8\u003c/li\u003e\n\u003cli\u003eint16\u003c/li\u003e\n\u003cli\u003eint32\u003c/li\u003e\n\u003cli\u003eint64\u003c/li\u003e\n\u003cli\u003euint\u003c/li\u003e\n\u003cli\u003euint8\u003c/li\u003e\n\u003cli\u003euint16\u003c/li\u003e\n\u003cli\u003euint32\u003c/li\u003e\n\u003cli\u003euint64\u003c/li\u003e\n\u003cli\u003efloat32\u003c/li\u003e\n\u003cli\u003efloat64\u003c/li\u003e\n\u003cli\u003estring\u003c/li\u003e\n\u003cli\u003ebyte\u003c/li\u003e\n\u003cli\u003erune\u003c/li\u003e\n\u003cli\u003etime.Time\u003c/li\u003e\n\u003c/ul\u003e\n\u003cp dir=\"auto\"\u003eApplications can also provide a utils.Comparators instance to customize the comparing. The following example demonstrates how to compare two students by age.\u003c/p\u003e\n\u003cdiv class=\"highlight highlight-source-go notranslate position-relative overflow-auto\" dir=\"auto\" data-snippet-clipboard-copy-content=\"type student struct {\n\tname string\n\tage int\n}\n\ntype MyComparator struct{}\n\nfunc (c *MyComparator) Compare(v1, v2 interface{}) (int, error) {\n\te1, e2 := v1.(*student), v2.(*student)\n\tif e1.age \u0026lt; e2.age {\n\t\treturn -1, nil\n\t}\n\tif e1.age \u0026gt; e2.age {\n\t\treturn 1, nil\n\t}\n\treturn 0, nil\n}\"\u003e\u003cpre\u003e\u003cspan class=\"pl-k\"\u003etype\u003c/span\u003e \u003cspan class=\"pl-smi\"\u003estudent\u003c/span\u003e \u003cspan class=\"pl-k\"\u003estruct\u003c/span\u003e {\n\t\u003cspan class=\"pl-c1\"\u003ename\u003c/span\u003e \u003cspan class=\"pl-smi\"\u003estring\u003c/span\u003e\n\t\u003cspan class=\"pl-c1\"\u003eage\u003c/span\u003e \u003cspan class=\"pl-smi\"\u003eint\u003c/span\u003e\n}\n\n\u003cspan class=\"pl-k\"\u003etype\u003c/span\u003e \u003cspan class=\"pl-smi\"\u003eMyComparator\u003c/span\u003e \u003cspan class=\"pl-k\"\u003estruct\u003c/span\u003e{}\n\n\u003cspan class=\"pl-k\"\u003efunc\u003c/span\u003e (\u003cspan class=\"pl-s1\"\u003ec\u003c/span\u003e \u003cspan class=\"pl-c1\"\u003e*\u003c/span\u003e\u003cspan class=\"pl-smi\"\u003eMyComparator\u003c/span\u003e) \u003cspan class=\"pl-c1\"\u003eCompare\u003c/span\u003e(\u003cspan class=\"pl-s1\"\u003ev1\u003c/span\u003e, \u003cspan class=\"pl-s1\"\u003ev2\u003c/span\u003e \u003cspan class=\"pl-k\"\u003einterface\u003c/span\u003e{}) (\u003cspan class=\"pl-smi\"\u003eint\u003c/span\u003e, \u003cspan class=\"pl-smi\"\u003eerror\u003c/span\u003e) {\n\t\u003cspan class=\"pl-s1\"\u003ee1\u003c/span\u003e, \u003cspan class=\"pl-s1\"\u003ee2\u003c/span\u003e \u003cspan class=\"pl-c1\"\u003e:=\u003c/span\u003e \u003cspan class=\"pl-s1\"\u003ev1\u003c/span\u003e.(\u003cspan class=\"pl-c1\"\u003e*\u003c/span\u003e\u003cspan class=\"pl-smi\"\u003estudent\u003c/span\u003e), \u003cspan class=\"pl-s1\"\u003ev2\u003c/span\u003e.(\u003cspan class=\"pl-c1\"\u003e*\u003c/span\u003e\u003cspan class=\"pl-smi\"\u003estudent\u003c/span\u003e)\n\t\u003cspan class=\"pl-k\"\u003eif\u003c/span\u003e \u003cspan class=\"pl-s1\"\u003ee1\u003c/span\u003e.\u003cspan class=\"pl-c1\"\u003eage\u003c/span\u003e \u003cspan class=\"pl-c1\"\u003e\u0026lt;\u003c/span\u003e \u003cspan class=\"pl-s1\"\u003ee2\u003c/span\u003e.\u003cspan class=\"pl-c1\"\u003eage\u003c/span\u003e {\n\t\t\u003cspan class=\"pl-k\"\u003ereturn\u003c/span\u003e \u003cspan class=\"pl-c1\"\u003e-\u003c/span\u003e\u003cspan class=\"pl-c1\"\u003e1\u003c/span\u003e, \u003cspan class=\"pl-c1\"\u003enil\u003c/span\u003e\n\t}\n\t\u003cspan class=\"pl-k\"\u003eif\u003c/span\u003e \u003cspan class=\"pl-s1\"\u003ee1\u003c/span\u003e.\u003cspan class=\"pl-c1\"\u003eage\u003c/span\u003e \u003cspan class=\"pl-c1\"\u003e\u0026gt;\u003c/span\u003e \u003cspan class=\"pl-s1\"\u003ee2\u003c/span\u003e.\u003cspan class=\"pl-c1\"\u003eage\u003c/span\u003e {\n\t\t\u003cspan class=\"pl-k\"\u003ereturn\u003c/span\u003e \u003cspan class=\"pl-c1\"\u003e1\u003c/span\u003e, \u003cspan class=\"pl-c1\"\u003enil\u003c/span\u003e\n\t}\n\t\u003cspan class=\"pl-k\"\u003ereturn\u003c/span\u003e \u003cspan class=\"pl-c1\"\u003e0\u003c/span\u003e, \u003cspan class=\"pl-c1\"\u003enil\u003c/span\u003e\n}\u003c/pre\u003e\u003c/div\u003e\n\u003cdiv class=\"markdown-heading\" dir=\"auto\"\u003e\u003ch2 tabindex=\"-1\" class=\"heading-element\" dir=\"auto\"\u003eSort\u003c/h2\u003e\u003ca id=\"user-content-sort\" class=\"anchor\" aria-label=\"Permalink: Sort\" href=\"#sort\"\u003e\u003csvg class=\"octicon octicon-link\" viewBox=\"0 0 16 16\" version=\"1.1\" width=\"16\" height=\"16\" aria-hidden=\"true\"\u003e\u003cpath d=\"m7.775 3.275 1.25-1.25a3.5 3.5 0 1 1 4.95 4.95l-2.5 2.5a3.5 3.5 0 0 1-4.95 0 .751.751 0 0 1 .018-1.042.751.751 0 0 1 1.042-.018 1.998 1.998 0 0 0 2.83 0l2.5-2.5a2.002 2.002 0 0 0-2.83-2.83l-1.25 1.25a.751.751 0 0 1-1.042-.018.751.751 0 0 1-.018-1.042Zm-4.69 9.64a1.998 1.998 0 0 0 2.83 0l1.25-1.25a.751.751 0 0 1 1.042.018.751.751 0 0 1 .018 1.042l-1.25 1.25a3.5 3.5 0 1 1-4.95-4.95l2.5-2.5a3.5 3.5 0 0 1 4.95 0 .751.751 0 0 1-.018 1.042.751.751 0 0 1-1.042.018 1.998 1.998 0 0 0-2.83 0l-2.5 2.5a1.998 1.998 0 0 0 0 2.83Z\"\u003e\u003c/path\u003e\u003c/svg\u003e\u003c/a\u003e\u003c/div\u003e\n\u003cp dir=\"auto\"\u003eThe sort utility provides the following two functions to sort the values in the provided slice.\u003c/p\u003e\n\u003cdiv class=\"highlight highlight-source-go notranslate position-relative overflow-auto\" dir=\"auto\" data-snippet-clipboard-copy-content=\"// Sort sorts values into ascending sequence according to their natural ordering, or according to the provided comparator.\nfunc Sort(values []interface{}, c Comparator)\n\n// ReverseSort sorts the values into opposite sequence to Sort\nfunc ReverseSort(values []interface{}, c Comparator)\"\u003e\u003cpre\u003e\u003cspan class=\"pl-c\"\u003e// Sort sorts values into ascending sequence according to their natural ordering, or according to the provided comparator.\u003c/span\u003e\n\u003cspan class=\"pl-k\"\u003efunc\u003c/span\u003e \u003cspan class=\"pl-s1\"\u003eSort\u003c/span\u003e(\u003cspan class=\"pl-s1\"\u003evalues\u003c/span\u003e []\u003cspan class=\"pl-k\"\u003einterface\u003c/span\u003e{}, \u003cspan class=\"pl-s1\"\u003ec\u003c/span\u003e \u003cspan class=\"pl-smi\"\u003eComparator\u003c/span\u003e)\n\n\u003cspan class=\"pl-c\"\u003e// ReverseSort sorts the values into opposite sequence to Sort\u003c/span\u003e\n\u003cspan class=\"pl-k\"\u003efunc\u003c/span\u003e \u003cspan class=\"pl-s1\"\u003eReverseSort\u003c/span\u003e(\u003cspan class=\"pl-s1\"\u003evalues\u003c/span\u003e []\u003cspan class=\"pl-k\"\u003einterface\u003c/span\u003e{}, \u003cspan class=\"pl-s1\"\u003ec\u003c/span\u003e \u003cspan class=\"pl-smi\"\u003eComparator\u003c/span\u003e)\u003c/pre\u003e\u003c/div\u003e\n\u003cp dir=\"auto\"\u003eBoth of the above functions sort values in-place. The first function \"Sort\" sorts the values into ascending sequence according to their natural ordering, or according to the provided comparator. The second function \"ReverseSort\" sorts the values into opposite sequence to the first function \"Sort\".\u003c/p\u003e\n\u003cdiv class=\"markdown-heading\" dir=\"auto\"\u003e\u003ch2 tabindex=\"-1\" class=\"heading-element\" dir=\"auto\"\u003eHeap\u003c/h2\u003e\u003ca id=\"user-content-heap\" class=\"anchor\" aria-label=\"Permalink: Heap\" href=\"#heap\"\u003e\u003csvg class=\"octicon octicon-link\" viewBox=\"0 0 16 16\" version=\"1.1\" width=\"16\" height=\"16\" aria-hidden=\"true\"\u003e\u003cpath d=\"m7.775 3.275 1.25-1.25a3.5 3.5 0 1 1 4.95 4.95l-2.5 2.5a3.5 3.5 0 0 1-4.95 0 .751.751 0 0 1 .018-1.042.751.751 0 0 1 1.042-.018 1.998 1.998 0 0 0 2.83 0l2.5-2.5a2.002 2.002 0 0 0-2.83-2.83l-1.25 1.25a.751.751 0 0 1-1.042-.018.751.751 0 0 1-.018-1.042Zm-4.69 9.64a1.998 1.998 0 0 0 2.83 0l1.25-1.25a.751.751 0 0 1 1.042.018.751.751 0 0 1 .018 1.042l-1.25 1.25a3.5 3.5 0 1 1-4.95-4.95l2.5-2.5a3.5 3.5 0 0 1 4.95 0 .751.751 0 0 1-.018 1.042.751.751 0 0 1-1.042.018 1.998 1.998 0 0 0-2.83 0l-2.5 2.5a1.998 1.998 0 0 0 0 2.83Z\"\u003e\u003c/path\u003e\u003c/svg\u003e\u003c/a\u003e\u003c/div\u003e\n\u003cp dir=\"auto\"\u003eThe heap utility provides the following functions. It's useful for containers like priorityQueue. Please read the comment for each function to get more detailed info.\u003c/p\u003e\n\u003cdiv class=\"highlight highlight-source-go notranslate position-relative overflow-auto\" dir=\"auto\" data-snippet-clipboard-copy-content=\"// HeapInit establishes the heap from scratch. The operation is in-place.\n// Parameters:\n// values: the data source of the heap\n// isMinHeap: true for min-hap, false for max-heap\n// c: an utils.Comparator instance\nfunc HeapInit(values []interface{}, isMinHeap bool, c Comparator) \n\n// HeapPostPush moves the new element up until it gets to the right place. The operation is in-place.\n// Push workflow (this functions takes care of the second step):\n// 1. add a new element to the end of the slice;\n// 2*. call this method to move the new element up until it gets to the right place.\n// Parameters:\n// values: the data source of the heap\n// isMinHeap: true for min-hap, false for max-heap\n// c: an utils.Comparator instance\nfunc HeapPostPush(values []interface{}, isMinHeap bool, c Comparator) \n\n// HeapPrePop move the top element down until it gets to the right place. The operation is in-place.\n// Pop workflow (this function takes care of step 1 and 2):\n// 1*. swap the first and the last element;\n// 2*. move the first/top element down until it gets to the right place;\n// 3. remove the last element, and return the removed element to users.\n// Parameters:\n// values: the data source of the heap\n// isMinHeap: true for min-hap, false for max-heap\n// c: an utils.Comparator instance\nfunc HeapPrePop(values []interface{}, isMinHeap bool, c Comparator)\n\n// HeapPreRemove move the element with the specified index down or up until it gets to the right place. The operation is in-place.\n// Remove workflow(this function takes care of step 1 and 2):\n// 1*. swap the element with the specifed index and the last element;\n// 2*. move the element with the specified index down or up until it gets to the right place;\n// 3. remove the last element, and return the removed element to users.\n// Parameters:\n// values: the data source of the heap\n// index: the element at the specified index will be removed after calling this function\n// isMinHeap: true for min-hap, false for max-heap\n// c: an utils.Comparator instance\nfunc HeapPreRemove(values []interface{}, index int, isMinHeap bool, c Comparator) \n\n// HeapPostUpdate re-establishes the heap ordering after the element at the specified index has changed its value. The operation is in-place.\n// Update workflow (this function takes care of the second step):\n// 1. update the element's value at the specified index;\n// 2*. call this function to move the updated element down or up until it gets to the right place.\n// Parameters:\n// values: the data source of the heap\n// index: the element at the specified index should have already been updated before calling this function\n// isMinHeap: true for min-hap, false for max-heap\n// c: an utils.Comparator instance\nfunc HeapPostUpdate(values []interface{}, index int, isMinHeap bool, c Comparator)\"\u003e\u003cpre\u003e\u003cspan class=\"pl-c\"\u003e// HeapInit establishes the heap from scratch. The operation is in-place.\u003c/span\u003e\n\u003cspan class=\"pl-c\"\u003e// Parameters:\u003c/span\u003e\n\u003cspan class=\"pl-c\"\u003e// values: the data source of the heap\u003c/span\u003e\n\u003cspan class=\"pl-c\"\u003e// isMinHeap: true for min-hap, false for max-heap\u003c/span\u003e\n\u003cspan class=\"pl-c\"\u003e// c: an utils.Comparator instance\u003c/span\u003e\n\u003cspan class=\"pl-k\"\u003efunc\u003c/span\u003e \u003cspan class=\"pl-s1\"\u003eHeapInit\u003c/span\u003e(\u003cspan class=\"pl-s1\"\u003evalues\u003c/span\u003e []\u003cspan class=\"pl-k\"\u003einterface\u003c/span\u003e{}, \u003cspan class=\"pl-s1\"\u003eisMinHeap\u003c/span\u003e \u003cspan class=\"pl-smi\"\u003ebool\u003c/span\u003e, \u003cspan class=\"pl-s1\"\u003ec\u003c/span\u003e \u003cspan class=\"pl-smi\"\u003eComparator\u003c/span\u003e) \n\n\u003cspan class=\"pl-c\"\u003e// HeapPostPush moves the new element up until it gets to the right place. The operation is in-place.\u003c/span\u003e\n\u003cspan class=\"pl-c\"\u003e// Push workflow (this functions takes care of the second step):\u003c/span\u003e\n\u003cspan class=\"pl-c\"\u003e// 1. add a new element to the end of the slice;\u003c/span\u003e\n\u003cspan class=\"pl-c\"\u003e// 2*. call this method to move the new element up until it gets to the right place.\u003c/span\u003e\n\u003cspan class=\"pl-c\"\u003e// Parameters:\u003c/span\u003e\n\u003cspan class=\"pl-c\"\u003e// values: the data source of the heap\u003c/span\u003e\n\u003cspan class=\"pl-c\"\u003e// isMinHeap: true for min-hap, false for max-heap\u003c/span\u003e\n\u003cspan class=\"pl-c\"\u003e// c: an utils.Comparator instance\u003c/span\u003e\n\u003cspan class=\"pl-k\"\u003efunc\u003c/span\u003e \u003cspan class=\"pl-s1\"\u003eHeapPostPush\u003c/span\u003e(\u003cspan class=\"pl-s1\"\u003evalues\u003c/span\u003e []\u003cspan class=\"pl-k\"\u003einterface\u003c/span\u003e{}, \u003cspan class=\"pl-s1\"\u003eisMinHeap\u003c/span\u003e \u003cspan class=\"pl-smi\"\u003ebool\u003c/span\u003e, \u003cspan class=\"pl-s1\"\u003ec\u003c/span\u003e \u003cspan class=\"pl-smi\"\u003eComparator\u003c/span\u003e) \n\n\u003cspan class=\"pl-c\"\u003e// HeapPrePop move the top element down until it gets to the right place. The operation is in-place.\u003c/span\u003e\n\u003cspan class=\"pl-c\"\u003e// Pop workflow (this function takes care of step 1 and 2):\u003c/span\u003e\n\u003cspan class=\"pl-c\"\u003e// 1*. swap the first and the last element;\u003c/span\u003e\n\u003cspan class=\"pl-c\"\u003e// 2*. move the first/top element down until it gets to the right place;\u003c/span\u003e\n\u003cspan class=\"pl-c\"\u003e// 3. remove the last element, and return the removed element to users.\u003c/span\u003e\n\u003cspan class=\"pl-c\"\u003e// Parameters:\u003c/span\u003e\n\u003cspan class=\"pl-c\"\u003e// values: the data source of the heap\u003c/span\u003e\n\u003cspan class=\"pl-c\"\u003e// isMinHeap: true for min-hap, false for max-heap\u003c/span\u003e\n\u003cspan class=\"pl-c\"\u003e// c: an utils.Comparator instance\u003c/span\u003e\n\u003cspan class=\"pl-k\"\u003efunc\u003c/span\u003e \u003cspan class=\"pl-s1\"\u003eHeapPrePop\u003c/span\u003e(\u003cspan class=\"pl-s1\"\u003evalues\u003c/span\u003e []\u003cspan class=\"pl-k\"\u003einterface\u003c/span\u003e{}, \u003cspan class=\"pl-s1\"\u003eisMinHeap\u003c/span\u003e \u003cspan class=\"pl-smi\"\u003ebool\u003c/span\u003e, \u003cspan class=\"pl-s1\"\u003ec\u003c/span\u003e \u003cspan class=\"pl-smi\"\u003eComparator\u003c/span\u003e)\n\n\u003cspan class=\"pl-c\"\u003e// HeapPreRemove move the element with the specified index down or up until it gets to the right place. The operation is in-place.\u003c/span\u003e\n\u003cspan class=\"pl-c\"\u003e// Remove workflow(this function takes care of step 1 and 2):\u003c/span\u003e\n\u003cspan class=\"pl-c\"\u003e// 1*. swap the element with the specifed index and the last element;\u003c/span\u003e\n\u003cspan class=\"pl-c\"\u003e// 2*. move the element with the specified index down or up until it gets to the right place;\u003c/span\u003e\n\u003cspan class=\"pl-c\"\u003e// 3. remove the last element, and return the removed element to users.\u003c/span\u003e\n\u003cspan class=\"pl-c\"\u003e// Parameters:\u003c/span\u003e\n\u003cspan class=\"pl-c\"\u003e// values: the data source of the heap\u003c/span\u003e\n\u003cspan class=\"pl-c\"\u003e// index: the element at the specified index will be removed after calling this function\u003c/span\u003e\n\u003cspan class=\"pl-c\"\u003e// isMinHeap: true for min-hap, false for max-heap\u003c/span\u003e\n\u003cspan class=\"pl-c\"\u003e// c: an utils.Comparator instance\u003c/span\u003e\n\u003cspan class=\"pl-k\"\u003efunc\u003c/span\u003e \u003cspan class=\"pl-s1\"\u003eHeapPreRemove\u003c/span\u003e(\u003cspan class=\"pl-s1\"\u003evalues\u003c/span\u003e []\u003cspan class=\"pl-k\"\u003einterface\u003c/span\u003e{}, \u003cspan class=\"pl-s1\"\u003eindex\u003c/span\u003e \u003cspan class=\"pl-smi\"\u003eint\u003c/span\u003e, \u003cspan class=\"pl-s1\"\u003eisMinHeap\u003c/span\u003e \u003cspan class=\"pl-smi\"\u003ebool\u003c/span\u003e, \u003cspan class=\"pl-s1\"\u003ec\u003c/span\u003e \u003cspan class=\"pl-smi\"\u003eComparator\u003c/span\u003e) \n\n\u003cspan class=\"pl-c\"\u003e// HeapPostUpdate re-establishes the heap ordering after the element at the specified index has changed its value. The operation is in-place.\u003c/span\u003e\n\u003cspan class=\"pl-c\"\u003e// Update workflow (this function takes care of the second step):\u003c/span\u003e\n\u003cspan class=\"pl-c\"\u003e// 1. update the element's value at the specified index;\u003c/span\u003e\n\u003cspan class=\"pl-c\"\u003e// 2*. call this function to move the updated element down or up until it gets to the right place.\u003c/span\u003e\n\u003cspan class=\"pl-c\"\u003e// Parameters:\u003c/span\u003e\n\u003cspan class=\"pl-c\"\u003e// values: the data source of the heap\u003c/span\u003e\n\u003cspan class=\"pl-c\"\u003e// index: the element at the specified index should have already been updated before calling this function\u003c/span\u003e\n\u003cspan class=\"pl-c\"\u003e// isMinHeap: true for min-hap, false for max-heap\u003c/span\u003e\n\u003cspan class=\"pl-c\"\u003e// c: an utils.Comparator instance\u003c/span\u003e\n\u003cspan class=\"pl-k\"\u003efunc\u003c/span\u003e \u003cspan class=\"pl-s1\"\u003eHeapPostUpdate\u003c/span\u003e(\u003cspan class=\"pl-s1\"\u003evalues\u003c/span\u003e []\u003cspan class=\"pl-k\"\u003einterface\u003c/span\u003e{}, \u003cspan class=\"pl-s1\"\u003eindex\u003c/span\u003e \u003cspan class=\"pl-smi\"\u003eint\u003c/span\u003e, \u003cspan class=\"pl-s1\"\u003eisMinHeap\u003c/span\u003e \u003cspan class=\"pl-smi\"\u003ebool\u003c/span\u003e, \u003cspan class=\"pl-s1\"\u003ec\u003c/span\u003e \u003cspan class=\"pl-smi\"\u003eComparator\u003c/span\u003e)\u003c/pre\u003e\u003c/div\u003e\n\u003cdiv class=\"markdown-heading\" dir=\"auto\"\u003e\u003ch1 tabindex=\"-1\" class=\"heading-element\" dir=\"auto\"\u003eContribute to this repo\u003c/h1\u003e\u003ca id=\"user-content-contribute-to-this-repo\" class=\"anchor\" aria-label=\"Permalink: Contribute to this repo\" href=\"#contribute-to-this-repo\"\u003e\u003csvg class=\"octicon octicon-link\" viewBox=\"0 0 16 16\" version=\"1.1\" width=\"16\" height=\"16\" aria-hidden=\"true\"\u003e\u003cpath d=\"m7.775 3.275 1.25-1.25a3.5 3.5 0 1 1 4.95 4.95l-2.5 2.5a3.5 3.5 0 0 1-4.95 0 .751.751 0 0 1 .018-1.042.751.751 0 0 1 1.042-.018 1.998 1.998 0 0 0 2.83 0l2.5-2.5a2.002 2.002 0 0 0-2.83-2.83l-1.25 1.25a.751.751 0 0 1-1.042-.018.751.751 0 0 1-.018-1.042Zm-4.69 9.64a1.998 1.998 0 0 0 2.83 0l1.25-1.25a.751.751 0 0 1 1.042.018.751.751 0 0 1 .018 1.042l-1.25 1.25a3.5 3.5 0 1 1-4.95-4.95l2.5-2.5a3.5 3.5 0 0 1 4.95 0 .751.751 0 0 1-.018 1.042.751.751 0 0 1-1.042.018 1.998 1.998 0 0 0-2.83 0l-2.5 2.5a1.998 1.998 0 0 0 0 2.83Z\"\u003e\u003c/path\u003e\u003c/svg\u003e\u003c/a\u003e\u003c/div\u003e\n\u003cp dir=\"auto\"\u003eAnyone is welcome to contribute to this repo. Please raise an issue firstly, then fork this repo and submit a pull request.\u003c/p\u003e\n\u003cp dir=\"auto\"\u003eCurrently this repo is under heavily development, any helps are appreciated!\u003c/p\u003e\n\u003cdiv class=\"markdown-heading\" dir=\"auto\"\u003e\u003ch1 tabindex=\"-1\" class=\"heading-element\" dir=\"auto\"\u003eSupport\u003c/h1\u003e\u003ca id=\"user-content-support\" class=\"anchor\" aria-label=\"Permalink: Support\" href=\"#support\"\u003e\u003csvg class=\"octicon octicon-link\" viewBox=\"0 0 16 16\" version=\"1.1\" width=\"16\" height=\"16\" aria-hidden=\"true\"\u003e\u003cpath d=\"m7.775 3.275 1.25-1.25a3.5 3.5 0 1 1 4.95 4.95l-2.5 2.5a3.5 3.5 0 0 1-4.95 0 .751.751 0 0 1 .018-1.042.751.751 0 0 1 1.042-.018 1.998 1.998 0 0 0 2.83 0l2.5-2.5a2.002 2.002 0 0 0-2.83-2.83l-1.25 1.25a.751.751 0 0 1-1.042-.018.751.751 0 0 1-.018-1.042Zm-4.69 9.64a1.998 1.998 0 0 0 2.83 0l1.25-1.25a.751.751 0 0 1 1.042.018.751.751 0 0 1 .018 1.042l-1.25 1.25a3.5 3.5 0 1 1-4.95-4.95l2.5-2.5a3.5 3.5 0 0 1 4.95 0 .751.751 0 0 1-.018 1.042.751.751 0 0 1-1.042.018 1.998 1.998 0 0 0-2.83 0l-2.5 2.5a1.998 1.998 0 0 0 0 2.83Z\"\u003e\u003c/path\u003e\u003c/svg\u003e\u003c/a\u003e\u003c/div\u003e\n\u003cp dir=\"auto\"\u003eIf you need any support, please raise issues.\u003c/p\u003e\n\u003cp dir=\"auto\"\u003eIf you have any suggestions or proposals, please also raise issues. Thanks!\u003c/p\u003e\n\u003c/article\u003e","loaded":true,"timedOut":false,"errorMessage":null,"headerInfo":{"toc":[{"level":1,"text":"gocontainer (中文版)","anchor":"gocontainer-中文版","htmlText":"gocontainer (中文版)"},{"level":1,"text":"Table of Contents","anchor":"table-of-contents","htmlText":"Table of Contents"},{"level":1,"text":"How to use this repo","anchor":"how-to-use-this-repo","htmlText":"How to use this repo"},{"level":1,"text":"Common Interface","anchor":"common-interface","htmlText":"Common Interface"},{"level":1,"text":"Containers","anchor":"containers","htmlText":"Containers"},{"level":2,"text":"Stack","anchor":"stack","htmlText":"Stack"},{"level":2,"text":"Queue","anchor":"queue","htmlText":"Queue"},{"level":2,"text":"Set","anchor":"set","htmlText":"Set"},{"level":2,"text":"List","anchor":"list","htmlText":"List"},{"level":2,"text":"PriorityQueue","anchor":"priorityqueue","htmlText":"PriorityQueue"},{"level":2,"text":"LinkedMap","anchor":"linkedmap","htmlText":"LinkedMap"},{"level":2,"text":"BTree","anchor":"btree","htmlText":"BTree"},{"level":2,"text":"Others","anchor":"others","htmlText":"Others"},{"level":1,"text":"Utilities","anchor":"utilities","htmlText":"Utilities"},{"level":2,"text":"Comparator","anchor":"comparator","htmlText":"Comparator"},{"level":2,"text":"Sort","anchor":"sort","htmlText":"Sort"},{"level":2,"text":"Heap","anchor":"heap","htmlText":"Heap"},{"level":1,"text":"Contribute to this repo","anchor":"contribute-to-this-repo","htmlText":"Contribute to this repo"},{"level":1,"text":"Support","anchor":"support","htmlText":"Support"}],"siteNavLoginPath":"/login?return_to=https%3A%2F%2Fgithub.com%2Fahrtr%2Fgocontainer"}},{"displayName":"LICENSE","repoName":"gocontainer","refName":"master","path":"LICENSE","preferredFileType":"license","tabName":"MIT","richText":null,"loaded":false,"timedOut":false,"errorMessage":null,"headerInfo":{"toc":null,"siteNavLoginPath":"/login?return_to=https%3A%2F%2Fgithub.com%2Fahrtr%2Fgocontainer"}}],"overviewFilesProcessingTime":0}},"appPayload":{"helpUrl":"https://docs.github.com","findFileWorkerPath":"/assets-cdn/worker/find-file-worker-7d7eb7c71814.js","findInFileWorkerPath":"/assets-cdn/worker/find-in-file-worker-708ec8ade250.js","githubDevUrl":null,"enabled_features":{"copilot_workspace":null,"code_nav_ui_events":false,"react_blob_overlay":false,"accessible_code_button":true,"github_models_repo_integration":false}}}}

Repository files navigation

gocontainer (中文版)

gocontainer implements some containers which exist in Java, but are missing in golang. This library is zero dependency, which means it does NOT depend on any 3rd party packages. Currently the containers are not thread-safe.

Table of Contents

How to use this repo

It's very straightforward, just imports the containers you need and then use them directly. The following is an example for ArrayList,

package main

import (
	"fmt"

	"github.com/ahrtr/gocontainer/list"
)

func main() {
	al := list.NewArrayList()
	al.Add(5, 6, 7)

	// Iterate all the elements 
	fmt.Println("Iterate (method 1): ")
	for i := 0; i < al.Len(); i++ {
		v, _ := al.Get(i)
		fmt.Printf("    Index: %d, value: %v\n", i, v)
	}
}

Please find more examples here.

Common Interface

All containers in this repository implement interface collection.Interface,

// Interface is a type of collection, all containers should implement this interface.
type Interface interface {
	// Size returns the number of elements in the collection.
	Size() int
	// IsEmpty returns true if this container contains no elements.
	IsEmpty() bool
	// Clear removes all of the elements from this container.
	Clear()
}

Containers

Currently this library implements the following containers:

  • Stack
  • Queue
  • Set
  • List (ArrayList, LinkedList)
  • PriorityQueue
  • LinkedMap
  • BTree

Stack

Stack is a LIFO(last-in-first-out) container. It implements the following interface. Click here to find examples on how to use a stack.

// Interface is a stack, which is LIFO (last-in-first-out).
type Interface interface {
	collection.Interface

	// Push pushes an element into this stack.
	Push(val interface{})
	// Pop pops the element on the top of this stack.
	Pop() interface{}
	// Peek retrieves, but does not remove, the element on the top of this stack, or return nil if this stack is empty.
	Peek() interface{}
}

Please import the following package in order to use stack,

import (
	"github.com/ahrtr/gocontainer/stack"
)

Call stack.New() to create a stack,

New() Interface

The following is a simple example for stack,

package main

import (
	"fmt"

	"github.com/ahrtr/gocontainer/stack"
)

func main() {
	s := stack.New()

	values := []int{5, 6, 7}
	for _, v := range values {
		s.Push(v)
	}

	for s.Size() > 0 {
		fmt.Printf("s.Pop() = %v\n", s.Pop())
	}
}

Queue

Queue is a FIFO(first-in-first-out) container. It implements the following interface. Click here to find examples on how to use a queue.

// Interface is a type of queue, which is FIFO(first-in-first-out).
type Interface interface {
	collection.Interface

	// Add inserts an element into the tail of this queue.
	Add(vals ...interface{})
	// Peek retrieves, but does not remove, the head of this queue, or return nil if this queue is empty.
	Peek() interface{}
	// Poll retrieves and removes the head of the this queue, or return nil if this queue is empty.
	Poll() interface{}
}

Please import the following package in order to use queue,

import (
	"github.com/ahrtr/gocontainer/queue"
)

Call queue.New() to create a queue,

New() Interface

The following is a simple example for queue,

package main

import (
	"fmt"

	"github.com/ahrtr/gocontainer/queue"
)

func main() {
	q := queue.New()

	values := []string{"benjamin", "alice", "john", "tom", "bill"}

	for _, v := range values {
		q.Add(v)
	}

	for q.Peek() != nil {
		fmt.Printf("q.Peek() = %v\n", q.Peek())
		fmt.Printf("q.Poll() = %v\n", q.Poll())
	}
}

Set

A set contains no duplicate elements. The values contained in a set may be any type that is comparable, please refer to the golang language spec to get more detailed info on comparison operators.

Set implements the following interface. Click here to find examples on how to use a set.

// Interface is a type of set, which contains no duplicate elements.
type Interface interface {
	collection.Interface

	// Add adds the specified values to this set if they are not already present.
	// It returns false if any value is already present.
	Add(vals ...interface{}) bool
	// Contains returns true if this set contains the specified element.
	Contains(val interface{}) bool
	// Remove removes the specified element from this set if it is present.
	// It returns false if the target value isn't present, otherwise returns true.
	Remove(val interface{}) bool
	// Iterate iterates all the elements in this set.
	Iterate(cb IterateCallback)
}

Please import the following package in order to use set,

import (
	"github.com/ahrtr/gocontainer/set"
)

Call set.New() to create a set,

New() Interface

The following is a simple example for set,

package main

import (
	"fmt"

	"github.com/ahrtr/gocontainer/set"
)

func main() {
	s := set.New()

	values := []int{5, 3, 9, 7, 6}
	for _, v := range values {
		s.Add(v)
	}

	for _, v := range values {
		fmt.Printf("s.Contains(%v) = %t\n", v, s.Contains(v))
	}

	// iterate all the elements, the callback function's signature:
	//   type IterateCallback func(interface{}) bool
	s.Iterate(func(v interface{}) bool {
		fmt.Printf("Iterate callback: %v\n", v)
		return true
	})

	s.Remove(6)
}

Applications are supposed to define a callback function (see below) when iterating a set.

// IterateCallback is the signature of the callback function called by Iterate.
// If the callback function returns false, then the iteration breaks.
type IterateCallback func(interface{}) bool

The following snip shows how to iterate a set. Please see the example to get more detailed info.

// To iterate over a set (where s is an instance of set.Interface):
s.Iterate(func(v interface{}) bool {
	// Do something with v

	// If you want to break the iteration, then return a false
	return true
})

List

This library implements two kinds of list, which are ArrayList and LinkedList, both of which implement the following interface. Click here to find examples on how to use a list.

// Interface is a type of list, both ArrayList and LinkedList implement this interface.
type Interface interface {
	collection.Interface

	// Add appends the specified elements to the end of this list.
	Add(vals ...interface{})
	// AddTo inserts the specified element at the specified position in this list.
	AddTo(index int, val interface{}) error

	// Contains returns true if this list contains the specified element.
	Contains(val interface{}) bool
	// Get returns the element at the specified position in this list. The index must be in the range of [0, size).
	Get(index int) (interface{}, error)

	// Remove removes the element at the specified position in this list.
	// It returns an error if the index is out of range.
	Remove(index int) (interface{}, error)
	// RemoveByValue removes the first occurence of the specified element from this list, if it is present.
	// It returns false if the target value isn't present, otherwise returns true.
	RemoveByValue(val interface{}) bool

	// Sort sorts the element using default options below. It sorts the elements into ascending sequence according to their natural ordering.
	//     reverse: false
	//     comparator: nil
	Sort()
	// SortWithOptions sorts the elements in the list.
	// Parameters:
	//     reverse: whether sort the data in reverse ordering
	//     c:       sort the data according to the provided comparator
	// If reverse is true, and a comparator is also provided, then the result will be the reverse sequence as the comparator generates.
	SortWithOptions(reverse bool, c utils.Comparator)

	// Iterator returns an iterator over the elements in this list in proper sequence.
	Iterator() (func() (interface{}, bool), bool)
	// ReverseIterator returns an iterator over the elements in this list in reverse sequence as Iterator.
	ReverseIterator() (func() (interface{}, bool), bool)
}

Please import the following package in order to use list (arrayList or linkedList),

import (
	"github.com/ahrtr/gocontainer/list"
)

Call list.NewArrayList() and list.NewLinkedList() to create a ArrayList and a LinkedList respectively,

NewArrayList() Interface
NewLinkedList() Interface

The following is a simple example for arrayList,

package main

import (
	"fmt"

	"github.com/ahrtr/gocontainer/list"
)

func main() {
	al := list.NewArrayList()
	values := []int{5, 7, 12, 9}
	for _, v := range values {
		al.Add(v)
	}

	al.AddTo(2, 18)
	v3, _ := al.Remove(3)
	fmt.Printf("al.Remove(3) = %v\n", v3)

	// Iterate all the elements 
	fmt.Println("Iterate: ")
	for i := 0; i < al.Size(); i++ {
		v, _ := al.Get(i)
		fmt.Printf("    Index: %d, value: %v\n", i, v)
	}
}

The following is a simple example for linkedList,

package main

import (
	"fmt"

	"github.com/ahrtr/gocontainer/list"
)
func main() {
	ll := list.NewLinkedList()
	values := []int{5, 7, 12, 9}
	for _, v := range values {
		ll.Add(v)
	}

	ll.AddTo(2, 18)
	v3, _ := ll.Remove(3)
	fmt.Printf("ll.Remove(3) = %v\n", v3)


	// Iterate all the elements
	fmt.Println("Iterate: ")
	it, hasNext := ll.Iterator()
	var v interface{}
	for hasNext {
		v, hasNext = it()
		fmt.Printf("    Value: %v\n", v)
	}
}

A list can be sorted using one of the following two methods. The first method Sort() sorts the list into ascending sequence according to the natural ordering of its elements by default; actually it just calls the second method SortWithOptions(false, nil) using the default parameters. SortWithOptions sorts the list according to the provided parameters. Please get more detailed info in Comparator

Sort()
SortWithOptions(reverse bool, c utils.Comparator)

There are multiple ways to iterate a list. The following snips show how to iterate a list (arrayList or linkedList),

// To iterate over a list (where l is an instance of list.Interface):
it, hasNext := l.Iterator()
var v interface{}
for hasNext {
	v, hasNext = it()
	// do something with v
}
// To iterate over a list (where l is an instance of list.Interface):
// This approach isn't efficient for linkedList.
for i:=0; i<l.Len(); i++ {
	v, _ := l.Get(i)
	// Do something with v
}
// To iterate over a list in reverse order (where l is an instance of list.Interface):
it, hasPrev := l.ReverseIterator()
var v interface{}
for hasPrev {
	v, hasPrev = it()
	// do something with v
}
// To iterate over a list in reverse order (where l is an instance of list.Interface):
// This approach isn't efficient for linkedList.
for i:=l.Len()-1; i>=0; i-- {
	v, _ := l.Get(i)
	// Do something with v
}

PriorityQueue

PriorityQueue is an unbounded priority queue based on a priority heap. It implements the following interface. Click here to find examples on how to use a priority queue.

// Interface is a type of priority queue, and priorityQueue implement this interface.
type Interface interface {
	queue.Interface

	// WithComparator sets a utils.Comparator instance for the queue.
	// It's used to imposes a total ordering on the elements in the queue.
	WithComparator(c utils.Comparator) Interface
	// WithMinHeap configures whether or not using min-heap.
	// If not configured, then it's min-heap by default.
	WithMinHeap(isMinHeap bool) Interface

	// Contains returns true if this queue contains the specified element.
	Contains(val interface{}) bool
	// Remove a single instance of the specified element from this queue, if it is present.
	// It returns false if the target value isn't present, otherwise returns true.
	Remove(val interface{}) bool
}

Please import the following package in order to use priorityQueue,

import (
	"github.com/ahrtr/gocontainer/queue/priorityqueue"
)

Call priorityqueue.New() to create a PriorityQueue,

New() Interface 

The following is a simple example for priorityQueue,

package main

import (
	"fmt"

	"github.com/ahrtr/gocontainer/queue/priorityqueue"
)

func main() {
	pq := priorityqueue.New()

	values := []string{"benjamin", "alice", "john", "tom", "bill"}

	for _, v := range values {
		pq.Add(v)
	}

	for _, v := range values {
		fmt.Printf("pq.Contains(%v) = %t\n", v, pq.Contains(v))
	}

	fmt.Printf("pq.Remove(john) = %t\n", pq.Remove("john"))

	for pq.Peek() != nil {
		fmt.Printf("pq.Peek() = %v\n", pq.Peek())
		fmt.Printf("pq.Poll() = %v\n", pq.Poll())
	}
}

A utils.Comparator instance can be provided for a priorityQueue by method WithComparator, please get more detailed info in Comparator.

WithComparator(c utils.Comparator) Interface

A priorityQueue can be configured to use min-heap or max-heap using method WithMinHeap. If the parameter is true, then it's a min-heap, which is the default option as well; otherwise, it's a max-heap.

WithMinHeap(isMinHeap bool) Interface

LinkedMap

LinkedMap is based on a map and a doubly linked list. The iteration ordering is normally the order in which keys were inserted into the map, or the order in which the keys were accessed if the accessOrder flag is set. It implements the following interface. Click here to find examples on how to use a linked map.

// Interface is a type of linked map, and linkedMap implements this interface.
type Interface interface {
	collection.Interface

	// Put associates the specified value with the specified key in this map. If the map previously contained a mapping for the key,
	// the old value is replaced by the specified value.
	// It returns the previous value associated with the specified key, or nil if there was no mapping for the key.
	// A nil return can also indicate that the map previously associated nil with the specified key.
	Put(k, v interface{}) interface{}

	// WithAccessOrder configures the iteration ordering for this linked map,
	// true for access-order, and false for insertion-order.
	WithAccessOrder(accessOrder bool) Interface

	// Get returns the value to which the specified key is mapped, or nil if this map contains no mapping for the key.
	Get(k interface{}) interface{}
	// GetOrDefault returns the value to which the specified key is mapped, or the defaultValue if this map contains no mapping for the key.
	GetOrDefault(k, defaultValue interface{}) interface{}
	// GetFirstElement gets the first element from this map, which is the head of the list.
	// It returns the (key, value, true) if the map isn't empty, or (nil, nil, false) if the map is empty.
	GetFirstElement() (interface{}, interface{}, bool)
	// GetLastElement gets the last element from this map, which is the tail of the list.
	// It returns the (key, value, true) if the map isn't empty, or (nil, nil, false) if the map is empty.
	GetLastElement() (interface{}, interface{}, bool)

	// ContainsKey returns true if this map contains a mapping for the specified key.
	ContainsKey(k interface{}) bool
	// ContainsValue returns true if this map maps one or more keys to the specified value.
	ContainsValue(v interface{}) bool

	// Remove removes the mapping for a key from this map if it is present.
	// It returns the value to which this map previously associated the key, and true,
	// or nil and false if the map contained no mapping for the key.
	Remove(k interface{}) (interface{}, bool)
	// RemoveFirstElement removes the first element from this map, which is the head of the list.
	// It returns the (key, value, true) if the map isn't empty, or (nil, nil, false) if the map is empty.
	RemoveFirstElement() (interface{}, interface{}, bool)
	// RemoveLastElement removes the last element from this map, which is the tail of the list.
	// It returns the (key, value, true) if the map isn't empty, or (nil, nil, false) if the map is empty.
	RemoveLastElement() (interface{}, interface{}, bool)

	// Iterator returns an iterator over the elements in this map in proper sequence.
	Iterator() (func() (interface{}, interface{}, bool), bool)
	// ReverseIterator returns an iterator over the elements in this map in reverse sequence as Iterator.
	ReverseIterator() (func() (interface{}, interface{}, bool), bool)
}

Please import the following package in order to use linkedMap,

import (
	"github.com/ahrtr/gocontainer/map/linkedmap"
)

Call linkedmap.New() to create a linked map,

New() Interface

The following is a simple example for linkedMap,

package main

import (
	"fmt"

	"github.com/ahrtr/gocontainer/map/linkedmap"
)

func main() {
	lm := linkedmap.New()

	keys := []interface{}{24, 43, 18, 23, 35}
	values := []interface{}{"benjamin", "alice", "john", "tom", "bill"}
	for i := 0; i < len(keys); i++ {
		lm.Put(keys[i], values[i])
	}

	for _, k := range keys {
		fmt.Printf("Get(%v) = %v\n", k, lm.Get(k))
	}

	v, _ := lm.Remove(18)
	fmt.Printf("The value associated with 18 is %v\n", v)

	k, v, _ := lm.RemoveFirstElement()
	fmt.Printf("The first element removed is (%v, %v)\n", k, v)

	k, v, _ = lm.RemoveLastElement()
	fmt.Printf("The last element removed is (%v, %v)\n", k, v)
}

If the order in which the keys were accessed is expected for the iteration ordering, then the accessOrder flag should be set,

// WithAccessOrder configures the iteration ordering for this linked map,
// true for access-order, and false for insertion-order.
WithAccessOrder(accessOrder bool) Interface

The following snips show how to interate a linkedMap,

// To iterate over an linkedMap (where lm is an instance of linkedmap.Interface):
it, hasNext := lm.Iterator()
var k, v interface{}
for hasNext {
	k, v, hasNext = it()
	// do something with k & v
}
// To iterate over an linkedMap in reverse order (where lm is an instance of linkedmap.Interface):
it, hasPrev := lm.ReverseIterator()
var k, v interface{}
for hasPrev {
	k, v, hasPrev = it()
	// do something with k & v
}

BTree

BTree is a B-Tree implementation. It was originally copied from github.com/google/btree, but it is refactored to adapt to the interface convention in this repository. Some improvements are also applied on top of the original design & implementation, so that it's more user-friendly. It implements the following interface. Click here to find examples on how to use a BTree.

// Interface is a type of btree, and bTree implements this interface
type Interface interface {
	collection.Interface

	// WithComparator sets an utils.Comparator instance for the btree.
	// It's used to impose a total ordering on the elements in the btree.
	WithComparator(c utils.Comparator) Interface

	// Clone clones the btree, lazily. The internal tree structure is marked read-only and
	// shared between the old and new btree. Writes to both the old and the new btree use copy-on-write logic.
	Clone() Interface
	// ReplaceOrInsert adds the given item to the tree.  If an item in the tree
	// already equals the given one, it is removed from the tree and returned.
	// Otherwise, nil is returned.
	ReplaceOrInsert(item interface{}) interface{}
	// Delete removes an item equal to the passed in item from the tree, returning
	// it.  If no such item exists, returns nil.
	Delete(item interface{}) interface{}
	// DeleteMin removes the smallest item in the tree and returns it.
	// If no such item exists, returns nil.
	DeleteMin() interface{}
	// DeleteMax removes the largest item in the tree and returns it.
	// If no such item exists, returns nil.
	DeleteMax() interface{}

	// AscendRange calls the iterator for every value in the tree within the range
	// [greaterOrEqual, lessThan), until iterator returns false.
	AscendRange(greaterOrEqual, lessThan interface{}, iterator ItemIterator)
	// AscendLessThan calls the iterator for every value in the tree within the range
	// [first, pivot), until iterator returns false.
	AscendLessThan(pivot interface{}, iterator ItemIterator)
	// AscendGreaterOrEqual calls the iterator for every value in the tree within
	// the range [pivot, last], until iterator returns false.
	AscendGreaterOrEqual(pivot interface{}, iterator ItemIterator)
	// Ascend calls the iterator for every value in the tree within the range
	// [first, last], until iterator returns false.
	Ascend(iterator ItemIterator)

	// DescendRange calls the iterator for every value in the tree within the range
	// [lessOrEqual, greaterThan), until iterator returns false.
	DescendRange(lessOrEqual, greaterThan interface{}, iterator ItemIterator)
	// DescendLessOrEqual calls the iterator for every value in the tree within the range
	// [pivot, first], until iterator returns false.
	DescendLessOrEqual(pivot interface{}, iterator ItemIterator)
	// DescendGreaterThan calls the iterator for every value in the tree within
	// the range [last, pivot), until iterator returns false.
	DescendGreaterThan(pivot interface{}, iterator ItemIterator)
	// Descend calls the iterator for every value in the tree within the range
	// [last, first], until iterator returns false.
	Descend(iterator ItemIterator)

	// Get looks for the key item in the tree, returning it.  It returns nil if
	// unable to find that item.
	Get(key interface{}) interface{}
	// Min returns the smallest item in the tree, or nil if the tree is empty.
	Min() interface{}
	// Max returns the largest item in the tree, or nil if the tree is empty.
	Max() interface{}
	// Has returns true if the given key is in the tree.
	Has(key interface{}) bool
}

Please import the following package in order to use btree,

import (
	"github.com/ahrtr/gocontainer/btree"
)

Call btree.New() to create a BTree,

New() Interface 

The following is a simple example for btree,

package main

import (
	"fmt"

	"github.com/ahrtr/gocontainer/btree"
)

func main() {
    items := []int {5, 9, 2, 4, 11, 6}
    tr := btree.New(2)

    fmt.Printf("tr.Size(): %d\n", tr.Size()) // should be 0 in the beginning

    // Insert values
    fmt.Printf("Inserting %d items: %v\n", len(items), items)
    for _, item := range items {
    	tr.ReplaceOrInsert(item)
    }

    // Search values
    fmt.Printf("    tr.Size(): %d\n", tr.Size()) // should be len(items): 6 now
    fmt.Printf("    tr.Min(): %v\n", tr.Min()) // should be 2
    fmt.Printf("    tr.Max(): %v\n", tr.Max()) // should be 11
    fmt.Printf("    tr.Has(6): %t\n", tr.Has(6))  // true
    fmt.Printf("    tr.Get(6): %v\n", tr.Get(6))  // 6
    fmt.Printf("    tr.Has(7): %t\n", tr.Has(7))  // false
    fmt.Printf("    tr.Get(7): %v\n", tr.Get(7))  // nil

    // Delete values
    fmt.Println("Deleting items:")
    fmt.Printf("    tr.DeleteMin(): %v\n", tr.DeleteMin()) // 2 is deleted and returned
    fmt.Printf("    tr.Min(): %v\n", tr.Min()) // should be 4 now
    fmt.Printf("    tr.DeleteMax(): %v\n", tr.DeleteMax()) // 11 is deleted and returned
    fmt.Printf("    tr.Max(): %v\n", tr.Max()) // should be 9 now
    fmt.Printf("    tr.Delete(6): %v\n", tr.Delete(6)) // 6 is deleted and returned
    fmt.Printf("    tr.Delete(7): %v\n", tr.Delete(7)) // 7 doesn't exist, so nil is returned

    fmt.Printf("tr.Size(): %d\n", tr.Size()) // should be 3 now because 3 items have already been removed
}

A utils.Comparator instance can be provided for a btree by method WithComparator, please get more detailed info in Comparator.

WithComparator(c utils.Comparator) Interface

Others

More containers will be added soon. Please also kindly let me know if you need any other kinds of containers. Feel free to raise issues.

Utilities

Comparator

The comparator utility contains a function "Compare" and an interface "Comparator",

// Compare compares two arguments using the given Comparator. If the Comparator isn't provided, then the two values are compared according to their natural ordering.
// They must be the same type, otherwise returns an error in the second return value.
// It returns a negative integer, zero, or a positive integer as the first argument is less than, equal to, or greater than the second.
func Compare(v1 interface{}, v2 interface{}, cmp Comparator) (int, error)

// Comparator imposes a total ordering on some collection of objects, and it allows precise control over the sort order.
type Comparator interface {
	// Compare compares its two arguments for order.
	// It returns a negative integer, zero, or a positive integer as the first argument is less than, equal to, or greater than the second.
	Compare(v1 interface{}, v2 interface{}) (int, error)
}

The function "Compare" is used to compare two values using the given Comparator of the third parameter. If the Comparator is nil, then they are compared according to their natural ordering of golang build-in data types listed below. The two arguments must be the same data type, otherwise an error in the second return value will be returned. It returns a negative integer, zero, or a positive integer as the first argument is less than, equal to, or greater than the second one. Note that for bool, a false is regarded as less than a true.

  • bool
  • int
  • int8
  • int16
  • int32
  • int64
  • uint
  • uint8
  • uint16
  • uint32
  • uint64
  • float32
  • float64
  • string
  • byte
  • rune
  • time.Time

Applications can also provide a utils.Comparators instance to customize the comparing. The following example demonstrates how to compare two students by age.

type student struct {
	name string
	age int
}

type MyComparator struct{}

func (c *MyComparator) Compare(v1, v2 interface{}) (int, error) {
	e1, e2 := v1.(*student), v2.(*student)
	if e1.age < e2.age {
		return -1, nil
	}
	if e1.age > e2.age {
		return 1, nil
	}
	return 0, nil
}

Sort

The sort utility provides the following two functions to sort the values in the provided slice.

// Sort sorts values into ascending sequence according to their natural ordering, or according to the provided comparator.
func Sort(values []interface{}, c Comparator)

// ReverseSort sorts the values into opposite sequence to Sort
func ReverseSort(values []interface{}, c Comparator)

Both of the above functions sort values in-place. The first function "Sort" sorts the values into ascending sequence according to their natural ordering, or according to the provided comparator. The second function "ReverseSort" sorts the values into opposite sequence to the first function "Sort".

Heap

The heap utility provides the following functions. It's useful for containers like priorityQueue. Please read the comment for each function to get more detailed info.

// HeapInit establishes the heap from scratch. The operation is in-place.
// Parameters:
//     values:    the data source of the heap
//     isMinHeap: true for min-hap, false for max-heap
//     c:         an utils.Comparator instance
func HeapInit(values []interface{}, isMinHeap bool, c Comparator) 

// HeapPostPush moves the new element up until it gets to the right place. The operation is in-place.
// Push workflow (this functions takes care of the second step):
//     1.  add a new element to the end of the slice;
//     2*. call this method to move the new element up until it gets to the right place.
// Parameters:
//     values:    the data source of the heap
//     isMinHeap: true for min-hap, false for max-heap
//     c:         an utils.Comparator instance
func HeapPostPush(values []interface{}, isMinHeap bool, c Comparator) 

// HeapPrePop move the top element down until it gets to the right place. The operation is in-place.
// Pop workflow (this function takes care of step 1 and 2):
//    1*. swap the first and the last element;
//    2*. move the first/top element down until it gets to the right place;
//    3.  remove the last element, and return the removed element to users.
// Parameters:
//     values:    the data source of the heap
//     isMinHeap: true for min-hap, false for max-heap
//     c:         an utils.Comparator instance
func HeapPrePop(values []interface{}, isMinHeap bool, c Comparator)

// HeapPreRemove move the element with the specified index down or up until it gets to the right place. The operation is in-place.
// Remove workflow(this function takes care of step 1 and 2):
//    1*. swap the element with the specifed index and the last element;
//    2*. move the element with the specified index down or up until it gets to the right place;
//    3.  remove the last element, and return the removed element to users.
// Parameters:
//     values:    the data source of the heap
//     index:     the element at the specified index will be removed after calling this function
//     isMinHeap: true for min-hap, false for max-heap
//     c:         an utils.Comparator instance
func HeapPreRemove(values []interface{}, index int, isMinHeap bool, c Comparator) 

// HeapPostUpdate re-establishes the heap ordering after the element at the specified index has changed its value. The operation is in-place.
// Update workflow (this function takes care of the second step):
//    1.  update the element's value at the specified index;
//    2*. call this function to move the updated element down or up until it gets to the right place.
// Parameters:
//     values:    the data source of the heap
//     index:     the element at the specified index should have already been updated before calling this function
//     isMinHeap: true for min-hap, false for max-heap
//     c:         an utils.Comparator instance
func HeapPostUpdate(values []interface{}, index int, isMinHeap bool, c Comparator)

Contribute to this repo

Anyone is welcome to contribute to this repo. Please raise an issue firstly, then fork this repo and submit a pull request.

Currently this repo is under heavily development, any helps are appreciated!

Support

If you need any support, please raise issues.

If you have any suggestions or proposals, please also raise issues. Thanks!

0