Browse Source

[sdk] Implement mini lists, which are singly-linked

coderain 1 month ago
parent
commit
f873ac437d
1 changed files with 41 additions and 0 deletions
  1. 41 0
      sdk/list.h

+ 41 - 0
sdk/list.h

@@ -28,11 +28,19 @@
 #define list_put_after  list_prepend
 #define list_put_before list_append
 
+#define mini_list_put_after  mini_list_prepend
+#define mini_list_put_before mini_list_append
+
 typedef struct _list_entry_t
 {
     struct _list_entry_t *next, *prev;
 } list_entry_t;
 
+typedef struct _mini_list_entry_t
+{
+    struct _mini_list_entry_t *next;
+} mini_list_entry_t;
+
 static inline void list_prepend(list_entry_t *list, list_entry_t *entry)
 {
     entry->next = list->next;
@@ -66,4 +74,37 @@ static inline void list_init_array(list_entry_t *list_array, size_t size)
     for (i = 0; i < size; i++) list_init(&list_array[i]);
 }
 
+static inline void mini_list_prepend(mini_list_entry_t *list, mini_list_entry_t *entry)
+{
+    entry->next = list->next;
+    list->next = entry;
+}
+
+static inline void mini_list_append(mini_list_entry_t *list, mini_list_entry_t *entry)
+{
+    mini_list_entry_t *final = list->next;
+    while (final->next != list) final = final->next;
+
+    final->next = entry;
+    entry->next = list;
+}
+
+static inline void mini_list_remove(mini_list_entry_t *entry)
+{
+    mini_list_entry_t *prev = entry->next;
+    while (prev->next != entry) prev = prev->next;
+    prev->next = entry->next;
+}
+
+static inline void mini_list_init(mini_list_entry_t *list)
+{
+    list->next = list;
+}
+
+static inline void mini_list_init_array(mini_list_entry_t *list_array, size_t size)
+{
+    size_t i;
+    for (i = 0; i < size; i++) mini_list_init(&list_array[i]);
+}
+
 #endif