{"id":15034961,"url":"https://github.com/liwnn/zset","last_synced_at":"2025-04-10T00:21:08.470Z","repository":{"id":57746757,"uuid":"520847802","full_name":"liwnn/zset","owner":"liwnn","description":"This Go package provides an implementation of sorted set in redis","archived":false,"fork":false,"pushed_at":"2023-09-01T06:05:03.000Z","size":35,"stargazers_count":4,"open_issues_count":0,"forks_count":0,"subscribers_count":1,"default_branch":"main","last_synced_at":"2025-03-24T02:05:02.995Z","etag":null,"topics":["go","ranking","redis","skiplist","sorted-set","zset"],"latest_commit_sha":null,"homepage":"","language":"Go","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/liwnn.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":null,"code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2022-08-03T11:10:38.000Z","updated_at":"2023-09-20T14:07:45.000Z","dependencies_parsed_at":"2024-06-20T05:53:45.612Z","dependency_job_id":"2af2bd6d-7110-43d2-9585-faddc437908e","html_url":"https://github.com/liwnn/zset","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/liwnn%2Fzset","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/liwnn%2Fzset/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/liwnn%2Fzset/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/liwnn%2Fzset/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/liwnn","download_url":"https://codeload.github.com/liwnn/zset/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":248132081,"owners_count":21052972,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["go","ranking","redis","skiplist","sorted-set","zset"],"created_at":"2024-09-24T20:27:01.312Z","updated_at":"2025-04-10T00:21:08.430Z","avatar_url":"https://github.com/liwnn.png","language":"Go","funding_links":[],"categories":[],"sub_categories":[],"readme":"# ZSet\nThis Go package provides an implementation of sorted set in redis.\n\n## Usage (go \u003c 1.18)\nAll you have to do is to implement a comparison `function Less(Item) bool` and a `function Key() string` for your Item which will be store in the zset, here are some examples.\n``` go\npackage main\n\nimport (\n\t\"fmt\"\n\n\t\"github.com/liwnn/zset\"\n)\n\ntype User struct {\n\tName  string\n\tScore int\n}\n\nfunc (u User) Key() string {\n\treturn u.Name\n}\n\nfunc (u User) Less(than zset.Item) bool {\n\tif u.Score == than.(User).Score {\n\t\treturn u.Name \u003c than.(User).Name\n\t}\n\treturn u.Score \u003c than.(User).Score\n}\n\nfunc main() {\n\tzs := zset.New()\n\n\t// Add\n\tzs.Add(\"Hurst\", User{Name: \"Hurst\", Score: 88})\n\tzs.Add(\"Peek\", User{Name: \"Peek\", Score: 100})\n\tzs.Add(\"Beaty\", User{Name: \"Beaty\", Score: 66})\n\n\t// Rank\n\trank := zs.Rank(\"Hurst\", true)\n\tfmt.Printf(\"Hurst's rank is %v\\n\", rank) // expected 2\n\n\t// Range\n\tfmt.Println()\n\tfmt.Println(\"Range[0,3]:\")\n\tzs.Range(0, 3, true, func(v zset.Item, rank int) bool {\n\t\tfmt.Printf(\"%v's rank is %v\\n\", v.(User).Key(), rank)\n\t\treturn true\n\t})\n\n\t// Range with Iterator\n\tfmt.Println()\n\tfmt.Println(\"Range[0,3] with Iterator:\")\n\tfor it := zs.RangeIterator(0, 3, true); it.Valid(); it.Next() {\n\t\tfmt.Printf(\"Ite: %v's rank is %v\\n\", it.Item().(User).Key(), it.Rank())\n\t}\n\n\t// Range by score [88, 100]\n\tfmt.Println()\n\tfmt.Println(\"RangeByScore[88,100]:\")\n\tzs.RangeByScore(func(i zset.Item) bool {\n\t\treturn i.(User).Score \u003e= 88\n\t}, func(i zset.Item) bool {\n\t\treturn i.(User).Score \u003c= 100\n\t}, true, func(i zset.Item, rank int) bool {\n\t\tfmt.Printf(\"%v's score[%v] rank is %v\\n\", i.(User).Key(), i.(User).Score, rank)\n\t\treturn true\n\t})\n\n\t// Remove\n\tzs.Remove(\"Peek\")\n\n\t// Rank\n\tfmt.Println()\n\tfmt.Println(\"After remove Peek:\")\n\trank = zs.Rank(\"Hurst\", true)\n\tfmt.Printf(\"Hurst's rank is %v\\n\", rank) // expected 1\n}\n```\nOutput:\n```\nHurst's rank is 2\n\nRange[0,3]:\nPeek's rank is 1\nHurst's rank is 2\nBeaty's rank is 3\n\nRange[0,3] with Iterator:\nIte: Peek's rank is 1\nIte: Hurst's rank is 2\nIte: Beaty's rank is 3\n\nRangeByScore[88,100]:\nPeek's score[100] rank is 1\nHurst's score[88] rank is 2\n\nAfter remove Peek:\nHurst's rank is 1\n```\n## Usage (go \u003e= 1.18)\n``` go\npackage main\n\nimport (\n\t\"fmt\"\n\n\t\"github.com/liwnn/zset\"\n)\n\ntype User struct {\n\tName  string\n\tScore int\n}\n\nfunc (u User) Key() string {\n\treturn u.Name\n}\n\nfunc (u User) Less(than User) bool {\n\tif u.Score == than.Score {\n\t\treturn u.Name \u003c than.Name\n\t}\n\treturn u.Score \u003c than.Score\n}\n\nfunc main() {\n\tzs := zset.New[string, User](func(a, b User) bool {\n\t\treturn a.Less(b)\n\t})\n\n\t// Add\n\tzs.Add(\"Hurst\", User{Name: \"Hurst\", Score: 88})\n\tzs.Add(\"Peek\", User{Name: \"Peek\", Score: 100})\n\tzs.Add(\"Beaty\", User{Name: \"Beaty\", Score: 66})\n\n\t// Rank\n\trank := zs.Rank(\"Hurst\", true)\n\tfmt.Printf(\"Hurst's rank is %v\\n\", rank) // expected 2\n\n\t// Range\n\tfmt.Println()\n\tfmt.Println(\"Range[0,3]:\")\n\tzs.Range(0, 3, true, func(v User, rank int) bool {\n\t\tfmt.Printf(\"%v's rank is %v\\n\", v.Key(), rank)\n\t\treturn true\n\t})\n\n\t// Range with Iterator\n\tfmt.Println()\n\tfmt.Println(\"Range[0,3] with Iterator:\")\n\tfor it := zs.RangeIterator(0, 3, true); it.Valid(); it.Next() {\n\t\tfmt.Printf(\"Ite: %v's rank is %v\\n\", it.Item().Key(), it.Rank())\n\t}\n\n\t// Range by score [88, 100]\n\tfmt.Println()\n\tfmt.Println(\"RangeByScore[88,100]:\")\n\tzs.RangeByScore(func(i User) bool {\n\t\treturn i.Score \u003e= 88\n\t}, func(i User) bool {\n\t\treturn i.Score \u003c= 100\n\t}, true, func(i User, rank int) bool {\n\t\tfmt.Printf(\"%v's score[%v] rank is %v\\n\", i.Key(), i.Score, rank)\n\t\treturn true\n\t})\n\n\t// Remove\n\tzs.Remove(\"Peek\")\n\n\t// Rank\n\tfmt.Println()\n\tfmt.Println(\"After remove Peek:\")\n\trank = zs.Rank(\"Hurst\", true)\n\tfmt.Printf(\"Hurst's rank is %v\\n\", rank) // expected 1\n}\n```","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fliwnn%2Fzset","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fliwnn%2Fzset","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fliwnn%2Fzset/lists"}