{"id":27862503,"url":"https://github.com/henryzhao2020/alist-c","last_synced_at":"2025-05-04T20:29:43.785Z","repository":{"id":289207061,"uuid":"970478227","full_name":"HenryZhao2020/alist-c","owner":"HenryZhao2020","description":"alist is a generic, feature-rich, and memory-safe dynamic array implementation for C.","archived":false,"fork":false,"pushed_at":"2025-04-28T20:28:54.000Z","size":85,"stargazers_count":1,"open_issues_count":0,"forks_count":0,"subscribers_count":1,"default_branch":"main","last_synced_at":"2025-04-28T21:38:32.114Z","etag":null,"topics":["adt","c","generic-types","memory-management"],"latest_commit_sha":null,"homepage":"","language":"C","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/HenryZhao2020.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,"zenodo":null}},"created_at":"2025-04-22T04:41:54.000Z","updated_at":"2025-04-28T20:28:57.000Z","dependencies_parsed_at":"2025-04-28T21:39:18.456Z","dependency_job_id":"ff1a8f19-621f-43df-8b31-4c3b12e6f9f7","html_url":"https://github.com/HenryZhao2020/alist-c","commit_stats":null,"previous_names":["henryzhao2020/adt","henryzhao2020/alist-c"],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/HenryZhao2020%2Falist-c","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/HenryZhao2020%2Falist-c/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/HenryZhao2020%2Falist-c/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/HenryZhao2020%2Falist-c/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/HenryZhao2020","download_url":"https://codeload.github.com/HenryZhao2020/alist-c/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":252394971,"owners_count":21740937,"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":["adt","c","generic-types","memory-management"],"created_at":"2025-05-04T20:29:42.696Z","updated_at":"2025-05-04T20:29:43.777Z","avatar_url":"https://github.com/HenryZhao2020.png","language":"C","funding_links":[],"categories":[],"sub_categories":[],"readme":"## Overview\n\nAn alist stores items in a dynamically resizable array, with all items deeply copied into heap memory.\n\nEach alist is associated with a specific **datatype**, defining a common type for all stored items. \nType-specific behaviors (duplication, comparison, printing, and deallocation) are handled through the **datatype interface**.\n\n---\n\n## Memory Model\n\n- All inserted items are deeply copied into separately allocated heap memory.\n- Stack-allocated or heap-allocated objects are both safe to insert; the alist duplicates them internally.\n- Stored items are automatically freed when individually removed or when the alist is destroyed.\n- Clients are responsible for freeing the original heap-allocated objects after insertion to avoid memory leaks.\n\n---\n\n## Example Usage (examples/demo_alist_int.c)\n\n```c\n// Demonstration of alist usage.\n\n#include \u003cstdlib.h\u003e\n#include \u003cstdio.h\u003e\n#include \u003cassert.h\u003e\n#include \"alist.h\"\n\n// Append stack-allocated, temporary values to al\nstatic void alist_append_stack(alist *al) {\n  assert(al);\n\n  int a = 5;\n  const int b = 10;\n  alist_append(al, \u0026a);   // a is deep-copied into al\n  alist_add(al, \u0026b);      // 'alist_add' is an alias for 'alist_append'\n  // a and b can now safely exit the stack frame\n\n  // 'WRAP_INT' adds a stack-allocated integer without a variable\n  alist_append(al, WRAP_INT(20));\n}\n\n// Append heap-allocated objects to al\nstatic void alist_append_heap(alist *al) {\n  assert(al);\n\n  int *ptr = malloc(sizeof(*ptr));\n  *ptr = 30;\n  alist_append(al, ptr);  // ptr is deep-copied into al\n  free(ptr);              // Client must free original pointer\n}\n\n// Check if num is even\nstatic bool is_even(const void *ptr) {\n  assert(ptr);\n  const int *int_ptr = ptr;\n  return (*int_ptr % 2 == 0);\n}\n\n// Multiply the value of num by 3\nstatic void triple(void *ptr) {\n  assert(ptr);\n  int *int_ptr = ptr;\n  *int_ptr *= 3;\n}\n\nint main(void) {\n  // Create an integer alist ['int_type' is included in datatype.h]\n  alist *al = alist_create(int_type());\n  \n  // Append integers to the back of al\n  alist_append_stack(al);\n  alist_append_heap(al);\n  \n  // Print al to the console\n  alist_print(al);        // Console: [5, 10, 20, 30]\n  // Get the size of al\n  assert(alist_size(al) == 4);\n\n  // Inserting integers before index positions\n  alist_insert(al, 2, WRAP_INT(5));       // al: [5, 10, 5, 20, 30]\n  alist_insert(al, 3, WRAP_INT(25));      // al: [5, 10, 5, 25, 20, 30]\n  alist_insert_front(al, WRAP_INT(35));   // al: [35, 5, 10, 5, 25, 20, 30]\n\n  // Duplicate al\n  alist *al_copy = alist_dup(al);\n  alist_print(al_copy);   // Console: [35, 5, 10, 5, 25, 20, 30]\n  // Comparing al and al_copy for equality\n  assert(alist_equals(al, al_copy) == true);\n\n  // Remove the item at index position 2\n  alist_pop(al, 2);                   // al: [35, 5, 5, 25, 20, 30]\n  // Remove the first item with the value 25\n  alist_remove(al, WRAP_INT(25));     // al: [35, 5, 5, 20, 30]\n  // Remove all items with the value 5\n  alist_remove_all(al, WRAP_INT(5));  // al: [35, 20, 30]\n  // Remove all even integers from al\n  alist_remove_if(al, is_even);       // al: [35]\n  alist_print(al);        // Console: [35]\n  \n  // Remove all elements from al, but al is kept\n  alist_clear(al);\n  alist_print(al);        // Console: []\n\n  // Add previous items from the copy\n  alist_append_all(al, al_copy);  \n  alist_print(al);        // Console: [35, 5, 10, 5, 25, 20, 30]\n\n  // Check if integers are in al\n  assert(alist_contains(al, WRAP_INT(10)));\n  assert(!alist_contains(al, WRAP_INT(99)));\n  // First occurrence of 5 is at index position 1\n  assert(alist_index(al, WRAP_INT(5)) == 1);\n  // Last occurrence of 5 is at index position 3\n  assert(alist_index_last(al, WRAP_INT(5)) == 3);\n  // 'ALIST_INDEX_NOT_FOUND': 99 is not in al\n  assert(alist_index(al, WRAP_INT(99)) == ALIST_INDEX_NOT_FOUND);\n  // Count the number of occurrences\n  assert(alist_count(al, WRAP_INT(5)) == 2);\n  assert(alist_count(al, WRAP_INT(99)) == 0);\n\n  // Access and mutate items at specific index positions\n  // Get the item at index position 2\n  const int *item = alist_get(al, 2);\n  assert(*item == 10);\n  // Modify the item at index position 0\n  int *item_mutable = alist_get_mutable(al, 0);\n  *item_mutable = 40;\n  // Set the item at index position 5\n  alist_set(al, 5, WRAP_INT(15));\n  alist_print(al);        // Console: [40, 5, 10, 5, 25, 15, 30]\n\n  // Quick sort algorithm\n  alist_qsort(al);         \n  alist_print(al);        // Console: [5, 5, 10, 15, 25, 30, 40]\n  // Binary search on the sorted list\n  assert(alist_bsearch(al, WRAP_INT(15)) != ALIST_INDEX_NOT_FOUND);\n  assert(alist_bsearch(al, WRAP_INT(99)) == ALIST_INDEX_NOT_FOUND);\n  // Reverse the list\n  alist_reverse(al);\n  alist_print(al);        // Console: [40, 30, 25, 15, 10, 5, 5]\n  // Multiply all items by 3\n  alist_map(al, triple);\n  alist_print(al);        // Console: [120, 90, 75, 45, 30, 15, 15]\n\n  // Convert the alist to int array\n  void **arr = alist_to_array(al);\n  printf(\"[\");\n  for (size_t i = 0; i \u003c alist_size(al); ++i) {\n    if (i != 0) printf(\", \");\n    int *ptr = arr[i];\n    printf(\"%d\", *ptr);\n    free(ptr);\n  }\n  printf(\"]\\n\");          // Console: [120, 90, 75, 45, 30, 15, 15]\n  free(arr);\n\n  // IMPORTANT: free al and al_copy to prevent memory leak\n  alist_destroy(al);\n  alist_destroy(al_copy);\n  alist_destroy(NULL);    // Also safe\n}\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fhenryzhao2020%2Falist-c","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fhenryzhao2020%2Falist-c","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fhenryzhao2020%2Falist-c/lists"}