mirror of
https://github.com/RubyMetric/chsrc
synced 2025-09-09 02:26:43 +08:00
实现 xy_seq_each/len/first/last()
This commit is contained in:
parent
77a3b458bd
commit
57d81b2edd
46
lib/xy.h
46
lib/xy.h
@ -1094,7 +1094,7 @@ XySeqItem_t;
|
||||
|
||||
|
||||
XySeq_t*
|
||||
xy_new_seq (void)
|
||||
xy_seq_new (void)
|
||||
{
|
||||
XySeq_t *seq = xy_malloc0 (sizeof (XySeq_t));
|
||||
if (!seq) return NULL;
|
||||
@ -1106,6 +1106,35 @@ xy_new_seq (void)
|
||||
return seq;
|
||||
}
|
||||
|
||||
/**
|
||||
* @flavor Python: len()
|
||||
*/
|
||||
uint32_t
|
||||
xy_seq_len (XySeq_t *seq)
|
||||
{
|
||||
return seq->length;
|
||||
}
|
||||
|
||||
/**
|
||||
* @flavor Ruby: Enumerable#first
|
||||
*/
|
||||
void *
|
||||
xy_seq_first (XySeq_t *seq)
|
||||
{
|
||||
if (!seq) return NULL;
|
||||
return seq->first_item ? seq->first_item->data : NULL;
|
||||
}
|
||||
|
||||
/**
|
||||
* @flavor Ruby: Enumerable#last
|
||||
*/
|
||||
void *
|
||||
xy_seq_last (XySeq_t *seq)
|
||||
{
|
||||
if (!seq) return NULL;
|
||||
return seq->last_item ? seq->last_item->data : NULL;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @flavor Perl: push
|
||||
@ -1164,4 +1193,19 @@ xy_seq_pop (XySeq_t *seq)
|
||||
return data;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @flavor Ruby: Array#each
|
||||
*/
|
||||
void
|
||||
xy_seq_each (XySeq_t *seq, void (*func)(void *))
|
||||
{
|
||||
if (!seq || !func) return;
|
||||
|
||||
for (XySeqItem_t *it = seq->first_item; it; it = it->next)
|
||||
{
|
||||
func (it->data);
|
||||
}
|
||||
}
|
||||
|
||||
#endif
|
||||
|
Loading…
x
Reference in New Issue
Block a user