{"id":27696750,"url":"https://github.com/hayageek/threadsafe","last_synced_at":"2025-04-25T15:12:52.853Z","repository":{"id":246184798,"uuid":"820345775","full_name":"hayageek/threadsafe","owner":"hayageek","description":" A Go package providing thread-safe implementations of array, slice, map, stack and queue","archived":false,"fork":false,"pushed_at":"2024-08-21T11:35:20.000Z","size":56,"stargazers_count":20,"open_issues_count":1,"forks_count":2,"subscribers_count":1,"default_branch":"main","last_synced_at":"2024-11-02T09:53:16.446Z","etag":null,"topics":["array","slice","thread-safe","thread-safe-map","thread-safe-queue","threadsafe","threadsafety"],"latest_commit_sha":null,"homepage":"https://pkg.go.dev/github.com/hayageek/threadsafe","language":"Go","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/hayageek.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2024-06-26T09:35:12.000Z","updated_at":"2024-08-06T13:14:25.000Z","dependencies_parsed_at":"2024-06-28T16:30:31.582Z","dependency_job_id":null,"html_url":"https://github.com/hayageek/threadsafe","commit_stats":null,"previous_names":["hayageek/threadsafe"],"tags_count":5,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/hayageek%2Fthreadsafe","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/hayageek%2Fthreadsafe/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/hayageek%2Fthreadsafe/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/hayageek%2Fthreadsafe/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/hayageek","download_url":"https://codeload.github.com/hayageek/threadsafe/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":250840584,"owners_count":21495910,"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":["array","slice","thread-safe","thread-safe-map","thread-safe-queue","threadsafe","threadsafety"],"created_at":"2025-04-25T15:12:52.240Z","updated_at":"2025-04-25T15:12:52.831Z","avatar_url":"https://github.com/hayageek.png","language":"Go","readme":"\n# Threadsafe\n\nA Go package providing thread-safe implementations of arrays, slices, maps, stack \u0026 queue using generics and type constraints.\n\n## Installation\n\nTo install the package, use the following command:\n\n```sh\ngo get github.com/hayageek/threadsafe\n```\n\n## Usage\n\nThis package provides thread-safe implementations for arrays, slices, maps, stack \u0026 queue. Below are usage examples and API lists for each of these data types.\n\n### Thread-Safe Array\n\nA thread-safe array with a fixed size.\n\n#### APIs\n\n- `NewArray(size int) *Array[T]` - Creates a new thread-safe array with the given size.\n- `(*Array[T]) Get(index int) (T, bool)` - Retrieves the value at the given index.\n- `(*Array[T]) Set(index int, value T) bool` - Sets the value at the given index.\n- `(*Array[T]) Append(value T)` - Appends a value to the array.\n- `(*Array[T]) Remove(index int) bool` - Removes the element at the given index.\n- `(*Array[T]) Contains(value T) bool` - Checks if the array contains the specified value.\n- `(*Array[T]) Clear()` - Clears all elements from the array.\n- `(*Array[T]) Insert(index int, value T) bool` - Inserts a value at the specified index.\n- `(*Array[T]) Copy() *Array[T]` - Returns a copy of the array.\n- `(*Array[T]) Values() []T` - Returns a slice of all elements in the array.\n- `(*Array[T]) Length() int` - Returns the length of the array.\n\n#### Example\n\n```go\npackage main\n\nimport (\n    \"fmt\"\n    \"github.com/hayageek/threadsafe\"\n)\n\nfunc main() {\n    // Create a new thread-safe array with size 5\n    arr := threadsafe.NewArray[int](5)\n\n    // Set values in the array\n    for i := 0; i \u003c arr.Length(); i++ {\n        arr.Set(i, i*10)\n    }\n\n    // Get values from the array\n    for i := 0; i \u003c arr.Length(); i++ {\n        value, _ := arr.Get(i)\n        fmt.Println(value)\n    }\n}\n```\n\n### Thread-Safe Slice\n\nA dynamically-sized, thread-safe slice.\n\n#### APIs\n\n- `NewSlice() *Slice[T]` - Creates a new thread-safe slice.\n- `(*Slice[T]) Append(value T)` - Appends a value to the slice.\n- `(*Slice[T]) Get(index int) (T, bool)` - Retrieves the value at the given index.\n- `(*Slice[T]) Set(index int, value T) bool` - Sets the value at the given index.\n- `(*Slice[T]) Remove(index int) bool` - Removes the element at the given index.\n- `(*Slice[T]) Contains(value T) bool` - Checks if the slice contains the specified value.\n- `(*Slice[T]) Clear()` - Clears all elements from the slice.\n- `(*Slice[T]) Insert(index int, value T) bool` - Inserts a value at the specified index.\n- `(*Slice[T]) Copy() *Slice[T]` - Returns a copy of the slice.\n- `(*Slice[T]) Values() []T` - Returns a slice of all values present in the slice.\n- `(*Slice[T]) Length() int` - Returns the length of the slice.\n\n#### Example\n\n```go\npackage main\n\nimport (\n    \"fmt\"\n    \"github.com/hayageek/threadsafe\"\n)\n\nfunc main() {\n    // Create a new thread-safe slice\n    slice := threadsafe.NewSlice[int]()\n\n    // Append values to the slice\n    for i := 0; i \u003c 5; i++ {\n        slice.Append(i * 10)\n    }\n\n    // Get values from the slice\n    for i := 0; i \u003c slice.Length(); i++ {\n        value, _ := slice.Get(i)\n        fmt.Println(value)\n    }\n\n    // Set values in the slice\n    for i := 0; i \u003c slice.Length(); i++ {\n        slice.Set(i, i*20)\n    }\n\n    // Get updated values from the slice\n    for i := 0; i \u003c slice.Length(); i++ {\n        value, _ := slice.Get(i)\n        fmt.Println(value)\n    }\n}\n```\n\n### Thread-Safe Map\n\nA thread-safe map for storing key-value pairs.\n\n#### APIs\n\n- `NewMap() *Map[K, V]` - Creates a new thread-safe map.\n- `(*Map[K, V]) Get(key K) (V, bool)` - Retrieves the value associated with the key.\n- `(*Map[K, V]) Set(key K, value V)` - Sets the value for the given key.\n- `(*Map[K, V]) Delete(key K)` - Deletes the value associated with the key.\n- `(*Map[K, V]) Contains(key K) bool` - Checks if the map contains the specified key.\n- `(*Map[K, V]) Clear()` - Clears all key-value pairs from the map.\n- `(*Map[K, V]) Copy() *Map[K, V]` - Returns a copy of the map.\n- `(*Map[K, V]) Length() int` - Returns the number of key-value pairs in the map.\n- `(*Map[K, V]) Keys() []K` - Returns a slice of all keys present in the map.\n- `(*Map[K, V]) Values() []V` - Returns a slice of all values present in the map.\n\n#### Example\n\n```go\npackage main\n\nimport (\n    \"fmt\"\n    \"github.com/hayageek/threadsafe\"\n)\n\nfunc main() {\n    // Create a new thread-safe map\n    m := threadsafe.NewMap[string, int]()\n\n    // Set values in the map\n    m.Set(\"one\", 1)\n    m.Set(\"two\", 2)\n    m.Set(\"three\", 3)\n\n    // Get values from the map\n    value, _ := m.Get(\"one\")\n    fmt.Println(value)\n\n    value, _ = m.Get(\"two\")\n    fmt.Println(value)\n\n    // Delete a key from the map\n    m.Delete(\"two\")\n\n    // Check if a key exists\n    _, ok := m.Get(\"two\")\n    if !ok {\n        fmt.Println(\"Key 'two' not found\")\n    }\n\n    // Get the length of the map\n    length := m.Length()\n    fmt.Println(\"Length:\", length)\n\n    // Get all keys from the map\n    keys := m.Keys()\n    fmt.Println(\"Keys:\", keys)\n\n    // Get all values from the map\n    values := m.Values()\n    fmt.Println(\"Values:\", values)\n}\n```\n\n\n### Thread-Safe Stack\n\nA thread-safe stack for safely adding and removing items.\n\n#### APIs\n\n- `NewStack() *Stack` - Creates a new thread-safe stack.\n- `(*Stack) Push(value interface{})` - Adds an element to the stack.\n- `(*Stack) Pop() (interface{}, bool)` - Removes and returns an element from the stack. Returns `false` if the stack is empty.\n- `(*Stack) Peek() (interface{}, bool)` - Returns the element at the top of the stack without removing it.\n- `(*Stack) IsEmpty() bool` - Checks if the stack is empty.\n- `(*Stack) Clear()` - Clears all elements from the stack.\n- `(*Stack) Values() []interface{}` - Returns a slice of all elements in the stack.\n- `(*Stack) Len() int` - Returns the number of elements in the stack.\n\n#### Example\n\n```go\npackage main\n\nimport (\n    \"fmt\"\n    \"github.com/hayageek/threadsafe\"\n)\n\nfunc main() {\n    stack := threadsafe.NewStack()\n\n    stack.Push(10)\n    stack.Push(20)\n    stack.Push(30)\n\n    fmt.Println(\"Stack length:\", stack.Len())\n\n    value, ok := stack.Pop()\n    if ok {\n        fmt.Println(\"Popped value:\", value)\n    } else {\n        fmt.Println(\"Stack is empty\")\n    }\n\n    fmt.Println(\"Stack length after pop:\", stack.Len())\n}\n```\n\n\n### Thread-Safe Queue\n\nA thread-safe queue for safely adding and removing items.\n\n#### APIs\n\n- `NewQueue() *Queue` - Creates a new thread-safe queue.\n- `(*Queue) Enqueue(value interface{})` - Adds an element to the queue.\n- `(*Queue) Dequeue() (interface{}, bool)` - Removes and returns an element from the queue. Returns `false` if the queue is empty.\n- `(*Queue) Peek() (interface{}, bool)` - Returns the element at the front of the queue without removing it.\n- `(*Queue) IsEmpty() bool` - Checks if the queue is empty.\n- `(*Queue) Clear()` - Clears all elements from the queue.\n- `(*Queue) Values() []interface{}` - Returns a slice of all elements in the queue.\n- `(*Queue) Len() int` - Returns the number of elements in the queue.\n\n#### Queue Example\n\n```go\npackage main\n\nimport (\n    \"fmt\"\n    \"github.com/hayageek/threadsafe\"\n)\n\nfunc main() {\n    queue := threadsafe.NewQueue()\n\n    queue.Enqueue(10)\n    queue.Enqueue(20)\n    queue.Enqueue(30)\n\n    fmt.Println(\"Queue length:\", queue.Len())\n\n    value, ok := queue.Dequeue()\n    if ok {\n        fmt.Println(\"Dequeued value:\", value)\n    } else {\n        fmt.Println(\"Queue is empty\")\n    }\n\n    fmt.Println(\"Queue length after dequeue:\", queue.Len())\n}\n```\n","funding_links":[],"categories":[],"sub_categories":[],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fhayageek%2Fthreadsafe","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fhayageek%2Fthreadsafe","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fhayageek%2Fthreadsafe/lists"}