From e2c85250271b44d83fd66f3e973c7830f0da2098 Mon Sep 17 00:00:00 2001 From: Aoran Zeng Date: Wed, 20 Aug 2025 13:01:46 +0800 Subject: [PATCH] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=20`xy=5Fseq=5Fpush()`?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- lib/xy.h | 71 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 71 insertions(+) diff --git a/lib/xy.h b/lib/xy.h index e973a3d..4a1972f 100644 --- a/lib/xy.h +++ b/lib/xy.h @@ -1068,4 +1068,75 @@ xy_parent_dir (const char *path) return dir; } + + +/****************************************************** + * Container + ******************************************************/ + +typedef struct XySeq_t +{ + XySeqItem_t *first_item; + xySeqItem_t *last_item; + + uint32_t length; +} +XySeq_t; + +typedef struct XySeqItem_t +{ + XySeqItem_t *next; + + void *data; +} +XySeqItem_t; + + +XySeq_t* +xy_new_seq (void) +{ + XySeq_t *seq = xy_malloc0 (sizeof (XySeq_t)); + if (!seq) return NULL; + + seq->first_item = NULL; + seq->last_item = NULL; + seq->length = 0; + + return seq; +} + + +/** + * @flavor Perl: push + */ +XySeq_t* +xy_seq_push (XySeq_t *seq, void *data) +{ + if (!seq) return NULL; + + XySeqItem_t *it = xy_malloc0 (sizeof (XySeqItem_t)); + if (!it) return NULL; + + it->data = data; + it->next = NULL; + + if (seq->last) + { + // 旧的最后项成为倒数第二项 + seq->last_item->next = it; + } + // it 成为新的最后一项 + seq->last_item = it; + + // 若 seq 为空,成为第一项 + if (!seq->first_item) + { + seq->first_item = it; + } + + seq->length++; + + return seq; +} + #endif