aboutsummaryrefslogtreecommitdiffstats
path: root/contrib/libs/libfyaml/src/lib/fy-list.h
diff options
context:
space:
mode:
authorDaniil Cherednik <dan.cherednik@gmail.com>2023-05-05 11:09:01 +0300
committerDaniil Cherednik <dan.cherednik@gmail.com>2023-05-05 11:09:01 +0300
commitb5a989b16cafa8a3b3bc076f1097a0eda6f48c06 (patch)
tree4da744117a5aab37758921fa43b95a3068e5aec1 /contrib/libs/libfyaml/src/lib/fy-list.h
parentfc1cffcfa7f0497a1f97b384a24bcbf23362f3be (diff)
downloadydb-b5a989b16cafa8a3b3bc076f1097a0eda6f48c06.tar.gz
Ydb stable 23-1-2623.1.26
x-stable-origin-commit: 22184a7e157553d447f17a2dffc4ea2d32dfd74d
Diffstat (limited to 'contrib/libs/libfyaml/src/lib/fy-list.h')
-rw-r--r--contrib/libs/libfyaml/src/lib/fy-list.h79
1 files changed, 79 insertions, 0 deletions
diff --git a/contrib/libs/libfyaml/src/lib/fy-list.h b/contrib/libs/libfyaml/src/lib/fy-list.h
new file mode 100644
index 0000000000..df35cb28e0
--- /dev/null
+++ b/contrib/libs/libfyaml/src/lib/fy-list.h
@@ -0,0 +1,79 @@
+/*
+ * fy-list.h - simple doubly linked list implementation
+ *
+ * Copyright (c) 2022 Innokentii Mokin <iam@justregular.dev>
+ *
+ * SPDX-License-Identifier: MIT
+ */
+#ifndef FY_LIST_H
+#define FY_LIST_H
+
+#include <stddef.h>
+
+#define fy_container_of(ptr, type, member) \
+ ( (void)sizeof(0 ? (ptr) : &((type *)0)->member), \
+ (type *)((char*)(ptr) - offsetof(type, member)) )
+
+struct fy_list_head {
+ struct fy_list_head *prev;
+ struct fy_list_head *next;
+};
+
+static inline void fy_list_init_head(struct fy_list_head *lh)
+{
+ lh->prev = lh;
+ lh->next = lh;
+}
+
+static inline void fy_list_add_head(struct fy_list_head *ln, struct fy_list_head *lh)
+{
+ struct fy_list_head *second = lh->next;
+
+ second->prev = ln;
+ ln->next = second;
+ lh->next = ln;
+ ln->prev = lh;
+}
+
+static inline void fy_list_add_tail(struct fy_list_head *ln, struct fy_list_head *lh)
+{
+ struct fy_list_head *tail = lh->prev;
+
+ lh->prev = ln;
+ ln->next = lh;
+ tail->next = ln;
+ ln->prev = tail;
+}
+
+static inline bool fy_list_is_empty(struct fy_list_head *lh)
+{
+ return lh == lh->next;
+}
+
+static inline bool fy_list_is_singular(struct fy_list_head *lh)
+{
+ return lh != lh->next && lh == lh->next->next;
+}
+
+static inline void fy_list_del(struct fy_list_head *ln) {
+ ln->prev->next = ln->next;
+ ln->next->prev = ln->prev;
+ ln->prev = NULL;
+ ln->next = NULL;
+}
+
+static inline void fy_list_splice(struct fy_list_head *nlh, struct fy_list_head *lh) {
+ struct fy_list_head *prev = lh, *next = lh->next,
+ *head = nlh->next, *tail = nlh->prev;
+
+ if (nlh == nlh->next) {
+ return;
+ }
+
+ head->prev = prev;
+ tail->next = next;
+ prev->next = head;
+ next->prev = tail;
+}
+
+#endif