2012-08-18 13:07:14 +02:00
|
|
|
/*
|
2012-11-06 22:03:07 +01:00
|
|
|
* c_list.c - Circular doubly-linked lists
|
2012-08-18 13:07:14 +02:00
|
|
|
*
|
|
|
|
* Copyright (C) 2012 Tobias Boege <tobias@gambas-buch.de>
|
|
|
|
*
|
|
|
|
* This program is free software; you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
* the Free Software Foundation; either version 2, or (at your option)
|
|
|
|
* any later version.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
* along with this program; if not, write to the Free Software
|
|
|
|
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
|
|
|
|
* MA 02110-1301, USA.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#define __C_LIST_C
|
|
|
|
|
2012-11-06 22:03:07 +01:00
|
|
|
#include <assert.h>
|
|
|
|
|
2012-08-18 23:46:25 +02:00
|
|
|
#include "gambas.h"
|
2012-11-06 22:03:07 +01:00
|
|
|
#include "gb_common.h"
|
2012-08-30 03:51:01 +02:00
|
|
|
#include "list.h"
|
2012-08-18 13:07:14 +02:00
|
|
|
#include "c_list.h"
|
|
|
|
|
2012-08-30 03:51:01 +02:00
|
|
|
#define CHUNK_SIZE 16
|
2012-08-18 13:07:14 +02:00
|
|
|
|
2012-08-30 03:51:01 +02:00
|
|
|
/*
|
|
|
|
* List implementation properties:
|
2012-11-06 22:03:07 +01:00
|
|
|
* + Increase cache locality by saving CHUNK_SIZE values inside a single
|
2012-08-30 03:51:01 +02:00
|
|
|
* chunk.
|
2012-11-06 22:03:07 +01:00
|
|
|
* + A special algorithm to re-arrange the values in a chunk guarantees some
|
|
|
|
* properties we can use to speed up the critical paths, such as
|
|
|
|
* traversals.
|
|
|
|
* - Fragmentation in the middle chunks in a list because of the (O)
|
|
|
|
* postulate of the rearrangement algorithm.
|
2012-08-30 03:51:01 +02:00
|
|
|
*/
|
2012-08-18 13:07:14 +02:00
|
|
|
|
2012-08-30 03:51:01 +02:00
|
|
|
typedef struct {
|
|
|
|
LIST list;
|
2012-11-06 22:03:07 +01:00
|
|
|
GB_VARIANT_VALUE var[CHUNK_SIZE];
|
|
|
|
int first; /* First valid element in var */
|
|
|
|
int last; /* Last valid element in var */
|
2012-08-30 03:51:01 +02:00
|
|
|
} CHUNK;
|
2012-11-06 22:03:07 +01:00
|
|
|
#define get_chunk(node) LIST_data(node, CHUNK, list)
|
2012-08-30 03:51:01 +02:00
|
|
|
|
|
|
|
typedef struct {
|
2012-11-06 22:03:07 +01:00
|
|
|
CHUNK *ck;
|
|
|
|
int fidx; /* Relative to ->ck->first */
|
|
|
|
int lidx; /* Relative to ->ck->last */
|
|
|
|
} VAL;
|
2012-08-30 03:51:01 +02:00
|
|
|
|
|
|
|
typedef struct {
|
|
|
|
GB_BASE ob;
|
|
|
|
LIST list; /* Beginning of linked CHUNKs */
|
2012-11-06 22:03:07 +01:00
|
|
|
VAL current; /* Current element */
|
2012-08-30 03:51:01 +02:00
|
|
|
int count; /* Do not iterate over all elements to get this */
|
|
|
|
} CLIST;
|
|
|
|
|
|
|
|
static void CHUNK_init(CHUNK *ck)
|
2012-08-18 13:07:14 +02:00
|
|
|
{
|
2012-08-30 03:51:01 +02:00
|
|
|
int i;
|
2012-08-18 13:07:14 +02:00
|
|
|
|
2012-08-30 03:51:01 +02:00
|
|
|
LIST_init(&ck->list);
|
2012-11-06 22:03:07 +01:00
|
|
|
for (i = 0; i < CHUNK_SIZE; i++)
|
|
|
|
ck->var[i].type = GB_T_NULL;
|
|
|
|
ck->first = -1;
|
|
|
|
ck->last = -1;
|
2012-08-18 13:07:14 +02:00
|
|
|
}
|
|
|
|
|
2012-08-30 03:51:01 +02:00
|
|
|
static CHUNK *CHUNK_new(void)
|
2012-08-18 13:07:14 +02:00
|
|
|
{
|
2012-08-30 03:51:01 +02:00
|
|
|
CHUNK *new;
|
2012-08-18 13:07:14 +02:00
|
|
|
|
2012-08-30 03:51:01 +02:00
|
|
|
GB.Alloc((void **) &new, sizeof(*new));
|
|
|
|
CHUNK_init(new);
|
2012-08-18 13:07:14 +02:00
|
|
|
return new;
|
|
|
|
}
|
|
|
|
|
2012-11-06 22:03:07 +01:00
|
|
|
static inline int CHUNK_count(CHUNK *ck)
|
|
|
|
{
|
|
|
|
return ck->last - ck->first + 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
static inline int CHUNK_is_first(CLIST *list, CHUNK *ck)
|
|
|
|
{
|
|
|
|
return list->list.next == &ck->list;
|
|
|
|
}
|
|
|
|
|
|
|
|
static inline int CHUNK_is_last(CLIST *list, CHUNK *ck)
|
|
|
|
{
|
|
|
|
return list->list.prev == &ck->list;
|
|
|
|
}
|
|
|
|
|
|
|
|
static inline void set_fidx(int fidx, VAL *val)
|
|
|
|
{
|
|
|
|
val->fidx = fidx;
|
|
|
|
val->lidx = val->ck->first + fidx - val->ck->last;
|
|
|
|
}
|
|
|
|
|
|
|
|
static inline void set_lidx(int lidx, VAL *val)
|
|
|
|
{
|
|
|
|
val->lidx = lidx;
|
|
|
|
val->fidx = val->ck->last + lidx - val->ck->first;
|
|
|
|
}
|
|
|
|
|
2012-08-30 03:51:01 +02:00
|
|
|
static void CHUNK_free_all(CHUNK *ck)
|
2012-08-18 13:07:14 +02:00
|
|
|
{
|
2012-11-06 22:03:07 +01:00
|
|
|
int i;
|
|
|
|
|
|
|
|
if (ck->first < 0 || ck->last < 0)
|
|
|
|
return;
|
|
|
|
for (i = ck->first; i <= ck->last; i++)
|
|
|
|
if (ck->var[i].type != GB_T_NULL)
|
|
|
|
GB.StoreVariant(NULL, &ck->var[i]);
|
2012-11-30 21:34:11 +01:00
|
|
|
ck->first = ck->last = -1;
|
2012-08-18 13:07:14 +02:00
|
|
|
}
|
|
|
|
|
2012-08-30 03:51:01 +02:00
|
|
|
static void CHUNK_destroy(CHUNK *ck)
|
2012-08-18 13:07:14 +02:00
|
|
|
{
|
2012-11-06 22:03:07 +01:00
|
|
|
/* The chunk *must* be unlinked */
|
2012-08-30 03:51:01 +02:00
|
|
|
CHUNK_free_all(ck);
|
|
|
|
GB.Free((void **) &ck);
|
2012-08-18 13:07:14 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
#define THIS ((CLIST *) _object)
|
|
|
|
|
2012-08-30 03:51:01 +02:00
|
|
|
BEGIN_METHOD_VOID(List_new)
|
2012-08-18 13:07:14 +02:00
|
|
|
|
2012-08-30 03:51:01 +02:00
|
|
|
LIST_init(&THIS->list);
|
2012-11-06 22:03:07 +01:00
|
|
|
THIS->current.ck = NULL;
|
2012-08-30 03:51:01 +02:00
|
|
|
THIS->count = 0;
|
2012-08-18 13:07:14 +02:00
|
|
|
|
|
|
|
END_METHOD
|
|
|
|
|
|
|
|
BEGIN_METHOD_VOID(List_free)
|
|
|
|
|
2012-08-30 03:51:01 +02:00
|
|
|
LIST *node, *next;
|
|
|
|
CHUNK *ck;
|
|
|
|
|
|
|
|
list_for_each_safe(node, &THIS->list, next) {
|
|
|
|
LIST_unlink(node);
|
|
|
|
ck = get_chunk(node);
|
|
|
|
CHUNK_destroy(ck);
|
|
|
|
}
|
2012-11-30 21:34:11 +01:00
|
|
|
THIS->current.ck = NULL;
|
|
|
|
THIS->count = 0;
|
2012-08-18 13:07:14 +02:00
|
|
|
|
|
|
|
END_METHOD
|
|
|
|
|
2012-11-06 22:03:07 +01:00
|
|
|
static inline GB_VARIANT_VALUE *VAL_value(VAL *val)
|
|
|
|
{
|
|
|
|
int i = val->fidx + val->ck->first;
|
|
|
|
int j = val->lidx + val->ck->last;
|
|
|
|
|
|
|
|
assert(val->fidx >= 0);
|
|
|
|
assert(val->lidx <= 0);
|
|
|
|
assert(i == j);
|
|
|
|
if (val->fidx == -1 || i > val->ck->last)
|
|
|
|
return NULL;
|
|
|
|
return &val->ck->var[i];
|
|
|
|
}
|
|
|
|
|
|
|
|
static inline int VAL_is_valid(VAL *val)
|
|
|
|
{
|
|
|
|
int i = val->fidx + val->ck->first;
|
|
|
|
int j = val->lidx + val->ck->last;
|
|
|
|
|
|
|
|
if (i != j || !val->ck || val->fidx == -1 || i > val->ck->last)
|
|
|
|
return 0;
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
static inline int VAL_is_equal(VAL *v1, VAL *v2)
|
|
|
|
{
|
|
|
|
return v1->ck == v2->ck && v1->fidx == v2->fidx;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void CLIST_first(CLIST *list, VAL *buf)
|
|
|
|
{
|
|
|
|
if (!list->count) {
|
|
|
|
buf->ck = NULL;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
buf->ck = get_chunk(list->list.next);
|
|
|
|
set_fidx(0, buf);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void CLIST_last(CLIST *list, VAL *buf)
|
|
|
|
{
|
|
|
|
if (!list->count) {
|
|
|
|
buf->ck = NULL;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
buf->ck = get_chunk(list->list.prev);
|
|
|
|
set_lidx(0, buf);
|
|
|
|
}
|
2012-08-30 03:51:01 +02:00
|
|
|
|
|
|
|
/*
|
2012-11-06 22:03:07 +01:00
|
|
|
* Modify @val so that it points to the next/prev valid value. @first is
|
|
|
|
* used to detect the end of an enumeration, i.e. where no other elements
|
|
|
|
* should be looked for. In this case, NULL is written to @val->ck.
|
2012-08-30 03:51:01 +02:00
|
|
|
*/
|
|
|
|
|
2012-11-06 22:03:07 +01:00
|
|
|
static void CHUNK_next(CLIST *list, VAL *val)
|
|
|
|
{
|
|
|
|
LIST *node;
|
|
|
|
|
|
|
|
/* Try to just update the index. This approach comes from the
|
|
|
|
* rearrange algorithm. */
|
|
|
|
if (val->lidx) {
|
|
|
|
set_fidx(val->fidx + 1, val);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Go to next chunk */
|
|
|
|
if ((node = val->ck->list.next) == &list->list)
|
|
|
|
node = node->next;
|
|
|
|
val->ck = get_chunk(node);
|
|
|
|
set_fidx(0, val);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void CHUNK_next_enum(CLIST *list, VAL *first, VAL *val)
|
2012-08-18 13:07:14 +02:00
|
|
|
{
|
2012-11-06 22:03:07 +01:00
|
|
|
CHUNK *ck = val->ck;
|
2012-08-30 03:51:01 +02:00
|
|
|
LIST *node;
|
2012-08-18 13:07:14 +02:00
|
|
|
|
2012-11-06 22:03:07 +01:00
|
|
|
assert(first != val);
|
|
|
|
if (val->lidx) {
|
|
|
|
set_fidx(val->fidx + 1, val);
|
|
|
|
if (VAL_is_equal(first, val))
|
|
|
|
goto no_next;
|
2012-08-30 03:51:01 +02:00
|
|
|
return;
|
|
|
|
}
|
2012-11-06 22:03:07 +01:00
|
|
|
if ((node = ck->list.next) == &list->list)
|
2012-08-30 03:51:01 +02:00
|
|
|
node = node->next;
|
2012-11-06 22:03:07 +01:00
|
|
|
ck = get_chunk(node);
|
|
|
|
if (ck == first->ck && first->fidx == 0)
|
|
|
|
goto no_next;
|
|
|
|
val->ck = ck;
|
|
|
|
set_fidx(0, val);
|
|
|
|
return;
|
|
|
|
|
|
|
|
no_next:
|
|
|
|
val->ck = NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void CHUNK_next_safe(CLIST *list, VAL *val)
|
|
|
|
{
|
|
|
|
LIST *node;
|
|
|
|
|
2012-11-30 21:34:11 +01:00
|
|
|
if (val->lidx) {
|
|
|
|
set_lidx(val->lidx + 1, val);
|
2012-11-06 22:03:07 +01:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
/* Also prevent from getting over the end! */
|
|
|
|
if ((node = val->ck->list.next) == &list->list) {
|
|
|
|
CLIST_last(list, val);
|
|
|
|
} else {
|
|
|
|
val->ck = get_chunk(node);
|
|
|
|
set_fidx(0, val);
|
|
|
|
}
|
2012-08-30 03:51:01 +02:00
|
|
|
}
|
2012-08-18 13:07:14 +02:00
|
|
|
|
2012-11-30 21:34:11 +01:00
|
|
|
static void CHUNK_prev_safe(CLIST *list, VAL *val)
|
|
|
|
{
|
|
|
|
LIST *node;
|
|
|
|
|
|
|
|
if (val->fidx) {
|
|
|
|
set_fidx(val->fidx - 1, val);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
if ((node = val->ck->list.prev) == &list->list) {
|
|
|
|
CLIST_first(list, val);
|
|
|
|
} else {
|
|
|
|
val->ck = get_chunk(node);
|
|
|
|
set_lidx(0, val);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-11-06 22:03:07 +01:00
|
|
|
static void CHUNK_prev(CLIST *list, VAL *val)
|
2012-08-30 03:51:01 +02:00
|
|
|
{
|
|
|
|
LIST *node;
|
|
|
|
|
2012-11-06 22:03:07 +01:00
|
|
|
if (val->fidx) {
|
|
|
|
set_fidx(val->fidx - 1, val);
|
2012-08-30 03:51:01 +02:00
|
|
|
return;
|
2012-08-18 13:07:14 +02:00
|
|
|
}
|
2012-11-06 22:03:07 +01:00
|
|
|
if ((node = val->ck->list.prev) == &list->list)
|
2012-08-30 03:51:01 +02:00
|
|
|
node = node->prev;
|
2012-11-06 22:03:07 +01:00
|
|
|
val->ck = get_chunk(node);
|
|
|
|
set_lidx(0, val);
|
2012-08-18 13:07:14 +02:00
|
|
|
}
|
|
|
|
|
2012-11-06 22:03:07 +01:00
|
|
|
static void CHUNK_prev_enum(CLIST *list, VAL *first, VAL *val)
|
|
|
|
{
|
|
|
|
CHUNK *ck = val->ck;
|
|
|
|
LIST *node;
|
|
|
|
|
|
|
|
assert(first != val);
|
|
|
|
if (val->fidx) {
|
|
|
|
set_fidx(val->fidx - 1, val);
|
|
|
|
if (VAL_is_equal(first, val))
|
|
|
|
goto no_prev;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
if ((node = ck->list.prev) == &list->list)
|
|
|
|
node = node->prev;
|
|
|
|
ck = get_chunk(node);
|
|
|
|
if (ck == first->ck && first->lidx == 0)
|
|
|
|
goto no_prev;
|
|
|
|
val->ck = ck;
|
|
|
|
set_lidx(0, val);
|
|
|
|
return;
|
|
|
|
no_prev:
|
|
|
|
val->ck = NULL;
|
|
|
|
}
|
2012-08-18 13:07:14 +02:00
|
|
|
|
|
|
|
/*
|
2012-11-06 22:03:07 +01:00
|
|
|
* Random access function.
|
|
|
|
* Negative @idx makes go backwards. The @val is filled. Out of bounds is
|
|
|
|
* signalled by val->ck == NULL.
|
2012-08-18 13:07:14 +02:00
|
|
|
*/
|
|
|
|
|
2012-11-06 22:03:07 +01:00
|
|
|
#ifndef sgn
|
|
|
|
# define sgn(x) ((x) < 0 ? -1 : ((x) ? 1 : 0))
|
|
|
|
#endif
|
|
|
|
|
|
|
|
static void CLIST_get(CLIST *list, int idx, VAL *val)
|
|
|
|
{
|
|
|
|
LIST *node;
|
|
|
|
CHUNK *ck;
|
|
|
|
int i;
|
|
|
|
int dir = sgn(idx);
|
|
|
|
int off, count;
|
|
|
|
|
|
|
|
/* We do _not_ allow indices to wrap around the end, like we could:
|
|
|
|
* idx %= list->count. Don't use a loop just to detect that case. */
|
|
|
|
i = dir * idx - (dir < 0 ? 1 : 0);
|
|
|
|
if (i >= list->count)
|
|
|
|
goto out_of_bounds;
|
|
|
|
|
|
|
|
/* Don't traverse forwards/backwards when the absolute value of
|
|
|
|
* index is above the half of the number of elements */
|
|
|
|
if (i > (list->count - 1) / 2) {
|
|
|
|
dir = -dir;
|
|
|
|
/*idx = -(idx + 1);*/
|
|
|
|
i = list->count - (i + 1);
|
|
|
|
}
|
|
|
|
|
|
|
|
node = &list->list;
|
|
|
|
/* Compute the offset to node->next or node->prev depending on the
|
|
|
|
* direction we traverse. This saves an if (dir >= 0) branch
|
2012-11-30 21:34:11 +01:00
|
|
|
* prediction catastrophe in loop. Still, very awful for caches. */
|
2012-11-06 22:03:07 +01:00
|
|
|
off = (dir >= 0 ? offsetof(LIST, next) : offsetof(LIST, prev));
|
|
|
|
do {
|
|
|
|
node = *((LIST **) ((long) node + off));
|
|
|
|
assert(node != &list->list);
|
|
|
|
ck = get_chunk(node);
|
|
|
|
count = CHUNK_count(ck);
|
|
|
|
if (i < count) {
|
|
|
|
val->ck = ck;
|
|
|
|
if (dir < 0)
|
|
|
|
set_lidx(-i, val);
|
|
|
|
else
|
|
|
|
set_fidx(i, val);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
i -= count;
|
|
|
|
} while (node != &list->list);
|
|
|
|
__builtin_trap(); /* XXX */
|
|
|
|
|
|
|
|
out_of_bounds:
|
|
|
|
/* Not enough elements. */
|
|
|
|
val->ck = NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* XXX: sizeof(VAL) may never exceed 3*sizeof(intptr_t)! */
|
|
|
|
struct enum_state {
|
|
|
|
CHUNK *first;
|
|
|
|
VAL next;
|
|
|
|
};
|
|
|
|
|
2012-08-18 13:07:14 +02:00
|
|
|
BEGIN_METHOD_VOID(List_next)
|
|
|
|
|
2012-08-30 03:51:01 +02:00
|
|
|
struct enum_state *state = GB.GetEnum();
|
2012-11-06 22:03:07 +01:00
|
|
|
GB_VARIANT_VALUE *val;
|
2012-11-30 21:34:11 +01:00
|
|
|
VAL start; /* XXX: Would like to cache that in the enum_state... */
|
2012-08-18 13:07:14 +02:00
|
|
|
|
2012-11-06 22:03:07 +01:00
|
|
|
if (!*((int *) state)) { /* Beginning */
|
|
|
|
CLIST_first(THIS, &state->next);
|
|
|
|
state->first = state->next.ck;
|
|
|
|
}
|
|
|
|
/* No elements left? */
|
|
|
|
if (!state->next.ck)
|
|
|
|
goto stop_enum;
|
|
|
|
|
|
|
|
val = VAL_value(&state->next);
|
|
|
|
start.ck = state->first;
|
|
|
|
set_fidx(0, &start);
|
|
|
|
CHUNK_next_enum(THIS, &start, &state->next);
|
|
|
|
GB.ReturnVariant(val);
|
2012-08-18 13:07:14 +02:00
|
|
|
return;
|
|
|
|
|
|
|
|
stop_enum:
|
|
|
|
GB.StopEnum();
|
|
|
|
|
|
|
|
END_METHOD
|
|
|
|
|
2012-11-06 22:03:07 +01:00
|
|
|
/*
|
|
|
|
* The same as List_next but backwards.
|
|
|
|
*/
|
2012-08-18 13:07:14 +02:00
|
|
|
|
2012-11-06 22:03:07 +01:00
|
|
|
BEGIN_METHOD_VOID(ListBackwards_next)
|
|
|
|
|
|
|
|
struct enum_state *state = GB.GetEnum();
|
|
|
|
GB_VARIANT_VALUE *val;
|
|
|
|
VAL start;
|
|
|
|
|
|
|
|
if (!*((int *) state)) { /* Beginning */
|
|
|
|
CLIST_last(THIS, &state->next);
|
|
|
|
state->first = state->next.ck;
|
2012-08-18 13:07:14 +02:00
|
|
|
}
|
2012-11-06 22:03:07 +01:00
|
|
|
/* No elements left? */
|
|
|
|
if (!state->next.ck)
|
|
|
|
goto stop_enum;
|
|
|
|
|
|
|
|
val = VAL_value(&state->next);
|
|
|
|
start.ck = state->first;
|
|
|
|
set_lidx(0, &start);
|
|
|
|
CHUNK_prev_enum(THIS, &start, &state->next);
|
|
|
|
GB.ReturnVariant(val);
|
|
|
|
return;
|
|
|
|
|
|
|
|
stop_enum:
|
|
|
|
GB.StopEnum();
|
|
|
|
|
|
|
|
END_METHOD
|
2012-08-18 13:07:14 +02:00
|
|
|
|
2012-08-30 03:51:01 +02:00
|
|
|
BEGIN_METHOD(List_get, GB_INTEGER index)
|
2012-08-18 13:07:14 +02:00
|
|
|
|
2012-11-06 22:03:07 +01:00
|
|
|
int index = VARG(index);
|
|
|
|
VAL val;
|
2012-08-18 13:07:14 +02:00
|
|
|
|
2012-11-06 22:03:07 +01:00
|
|
|
CLIST_get(THIS, index, &val);
|
|
|
|
if (!val.ck) {
|
2012-08-30 03:51:01 +02:00
|
|
|
GB.Error(GB_ERR_BOUND);
|
|
|
|
return;
|
|
|
|
}
|
2012-11-06 22:03:07 +01:00
|
|
|
GB.ReturnVariant(VAL_value(&val));
|
2012-08-18 13:07:14 +02:00
|
|
|
|
|
|
|
END_METHOD
|
|
|
|
|
2012-08-30 03:51:01 +02:00
|
|
|
BEGIN_METHOD(List_put, GB_VARIANT var; GB_INTEGER index)
|
2012-08-18 13:07:14 +02:00
|
|
|
|
2012-11-06 22:03:07 +01:00
|
|
|
int index = VARG(index);
|
|
|
|
VAL val;
|
2012-08-18 13:07:14 +02:00
|
|
|
|
2012-11-06 22:03:07 +01:00
|
|
|
CLIST_get(THIS, index, &val);
|
|
|
|
if (!val.ck) {
|
2012-08-30 03:51:01 +02:00
|
|
|
GB.Error(GB_ERR_BOUND);
|
|
|
|
return;
|
|
|
|
}
|
2012-11-06 22:03:07 +01:00
|
|
|
GB.StoreVariant(ARG(var), VAL_value(&val));
|
2012-08-18 13:07:14 +02:00
|
|
|
|
|
|
|
END_METHOD
|
|
|
|
|
|
|
|
/*
|
2012-11-06 22:03:07 +01:00
|
|
|
* The machinery to modify the List structure, i.e. Append/Prepend/Take
|
|
|
|
* shall satisfy the following points:
|
|
|
|
*
|
|
|
|
* Notification prevents persistent VALs (Current and the enumerators)
|
|
|
|
* from getting ("dangerously", as opposed to "intentionally") invalid:
|
2012-11-30 21:34:11 +01:00
|
|
|
* (V) Value. If an element other than that a VAL refers to is removed, or
|
|
|
|
* an element is added, the VAL shall be modified according to the
|
2012-11-06 22:03:07 +01:00
|
|
|
* operation, i.e. it shall stay pointing to the particular value it has
|
2012-11-30 21:34:11 +01:00
|
|
|
* pointed to before. VALs are value-bound.
|
2012-11-06 22:03:07 +01:00
|
|
|
* (B) Beginning. If the element a VAL refers to is removed, it shall remain
|
|
|
|
* relative to the beginning of the list (as long as it doesn't get
|
2012-11-30 21:34:11 +01:00
|
|
|
* empty), i.e. it moves on to the next value. This will guarantee that
|
|
|
|
* we get all the intended values in an enumeration when removing.
|
2012-11-06 22:03:07 +01:00
|
|
|
* If the list gets empty, the VALs are invalidated.
|
|
|
|
*
|
|
|
|
* The rearrange algorithm shall assure the following:
|
|
|
|
* (C) Coherency. All values in a chunk must be contiguous.
|
|
|
|
* (A) Alignment. The first chunk has all elements aligned to its end; the
|
|
|
|
* last chunk has all elements aligned to its beginning. For the special
|
|
|
|
* case of only one chunk in the List (the 'sole chunk'), it is not
|
|
|
|
* specially aligned but its initial element is at CHUNK_SIZE/2-1.
|
|
|
|
* (L) Least Copy. Rearrangement for any non-aligned chunk shall strive for
|
|
|
|
* the least copy operations.
|
|
|
|
* (O) Order. Values get never reordered within a chunk or rearranged into
|
|
|
|
* other chunks. They always keep their relative position to each other.
|
|
|
|
* Note that if (O) would not apply, on the one hand, we could make more
|
|
|
|
* intelligent algorithms, but on the other hand, the notification algorithm
|
|
|
|
* must be improved so it remains in the first place.
|
2012-08-18 13:07:14 +02:00
|
|
|
*/
|
|
|
|
|
2012-11-06 22:03:07 +01:00
|
|
|
/* XXX: Give only variables! */
|
|
|
|
#define for_all_persistent_vals(list, vp, es, ebuf) \
|
|
|
|
for (ebuf = GB.BeginEnum(list), vp = &list->current, es = NULL; \
|
|
|
|
vp ? 1 : (GB.EndEnum(ebuf), 0); \
|
|
|
|
!GB.NextEnum() ? (es = (struct enum_state*) GB.GetEnum(),\
|
|
|
|
vp = &es->next) : (vp = NULL))
|
|
|
|
|
2012-08-30 03:51:01 +02:00
|
|
|
static void CLIST_append(CLIST *list, GB_VARIANT *val)
|
2012-08-18 13:07:14 +02:00
|
|
|
{
|
2012-11-06 22:03:07 +01:00
|
|
|
CHUNK *ck;
|
|
|
|
VAL *cur;
|
|
|
|
struct enum_state *es;
|
|
|
|
void *save_enum;
|
|
|
|
|
|
|
|
ck = get_chunk(list->list.next);
|
|
|
|
/* (A) */
|
|
|
|
if (UNLIKELY(!list->count)) {
|
|
|
|
ck = CHUNK_new();
|
|
|
|
ck->first = ck->last = CHUNK_SIZE / 2 - 1;
|
|
|
|
LIST_append(&list->list, &ck->list);
|
|
|
|
} else if (UNLIKELY(ck->first == 0)) {
|
|
|
|
ck = CHUNK_new();
|
|
|
|
ck->first = ck->last = CHUNK_SIZE - 1;
|
|
|
|
LIST_append(&list->list, &ck->list);
|
|
|
|
} else {
|
|
|
|
ck->first--;
|
|
|
|
}
|
|
|
|
/* (C), (O) */
|
|
|
|
GB.StoreVariant(val, &ck->var[ck->first]);
|
|
|
|
list->count++;
|
|
|
|
|
2012-11-30 21:34:11 +01:00
|
|
|
/* (V) */
|
2012-11-06 22:03:07 +01:00
|
|
|
for_all_persistent_vals(list, cur, es, save_enum) {
|
|
|
|
if (cur->ck != ck)
|
|
|
|
continue;
|
|
|
|
/* On Append, the ->lidx + ->ck->last is still correct */
|
|
|
|
set_lidx(cur->lidx, cur);
|
|
|
|
}
|
|
|
|
}
|
2012-08-30 03:51:01 +02:00
|
|
|
|
2012-11-06 22:03:07 +01:00
|
|
|
static void CLIST_prepend(CLIST *list, GB_VARIANT *val)
|
|
|
|
{
|
|
|
|
CHUNK *ck;
|
|
|
|
VAL *cur;
|
|
|
|
struct enum_state *es;
|
|
|
|
void *save_enum;
|
|
|
|
|
|
|
|
ck = get_chunk(list->list.prev);
|
|
|
|
/* (A) */
|
|
|
|
if (UNLIKELY(!list->count)) {
|
|
|
|
ck = CHUNK_new();
|
|
|
|
ck->first = ck->last = CHUNK_SIZE / 2 - 1;
|
|
|
|
LIST_prepend(&list->list, &ck->list);
|
|
|
|
} else if (UNLIKELY(ck->last == CHUNK_SIZE - 1)) {
|
|
|
|
ck = CHUNK_new();
|
|
|
|
ck->first = ck->last = 0;
|
|
|
|
LIST_prepend(&list->list, &ck->list);
|
|
|
|
} else {
|
|
|
|
ck->last++;
|
|
|
|
}
|
|
|
|
/* (C), (O) */
|
|
|
|
GB.StoreVariant(val, &ck->var[ck->last]);
|
2012-08-30 03:51:01 +02:00
|
|
|
list->count++;
|
2012-11-06 22:03:07 +01:00
|
|
|
|
2012-11-30 21:34:11 +01:00
|
|
|
/* (V) */
|
2012-11-06 22:03:07 +01:00
|
|
|
for_all_persistent_vals(list, cur, es, save_enum) {
|
|
|
|
if (cur->ck != ck)
|
|
|
|
continue;
|
|
|
|
/* Conversely to Append, here ->fidx + ->ck->first is still
|
|
|
|
* correct */
|
|
|
|
set_fidx(cur->fidx, cur);
|
|
|
|
}
|
2012-08-18 13:07:14 +02:00
|
|
|
}
|
|
|
|
|
2012-11-06 22:03:07 +01:00
|
|
|
static void CLIST_take(CLIST *list, VAL *val, GB_VARIANT_VALUE *buf)
|
|
|
|
{
|
|
|
|
GB_VARIANT_VALUE *v;
|
|
|
|
CHUNK *ck = val->ck;
|
2012-11-30 21:34:11 +01:00
|
|
|
VAL *cur, back;
|
2012-11-06 22:03:07 +01:00
|
|
|
struct enum_state *es;
|
|
|
|
void *save_enum;
|
2012-11-30 21:34:11 +01:00
|
|
|
int gets_empty, is_last;
|
2012-11-06 22:03:07 +01:00
|
|
|
int i, src, dst, phantom;
|
|
|
|
int n, m;
|
|
|
|
size_t size;
|
|
|
|
|
|
|
|
i = val->fidx + ck->first;
|
|
|
|
v = &ck->var[i];
|
|
|
|
/* Save that value */
|
|
|
|
memcpy(buf, v, sizeof(*buf));
|
|
|
|
/* No need to not (O) */
|
|
|
|
|
|
|
|
gets_empty = (CHUNK_count(ck) == 1);
|
|
|
|
if (gets_empty) {
|
|
|
|
phantom = i;
|
2012-11-30 21:34:11 +01:00
|
|
|
src = dst = 0;
|
2012-11-06 22:03:07 +01:00
|
|
|
goto no_move;
|
|
|
|
}
|
2012-08-18 13:07:14 +02:00
|
|
|
|
2012-11-06 22:03:07 +01:00
|
|
|
n = i - ck->first;
|
|
|
|
m = ck->last - i;
|
2012-11-30 21:34:11 +01:00
|
|
|
is_last = CHUNK_is_last(list, ck);
|
2012-11-06 22:03:07 +01:00
|
|
|
/* (A) */
|
|
|
|
if (CHUNK_is_first(list, ck)) {
|
2012-11-30 21:34:11 +01:00
|
|
|
if (is_last) /* Sole */
|
2012-11-06 22:03:07 +01:00
|
|
|
goto normal;
|
|
|
|
goto first; /* Algorithms match */
|
2012-11-30 21:34:11 +01:00
|
|
|
} else if (is_last) {
|
2012-11-06 22:03:07 +01:00
|
|
|
goto last;
|
2012-11-30 21:34:11 +01:00
|
|
|
}
|
|
|
|
normal:
|
|
|
|
/* (L) */
|
|
|
|
if (n <= m) {
|
|
|
|
first: /* Move block before i upwards */
|
|
|
|
src = ck->first;
|
|
|
|
dst = src + 1;
|
|
|
|
ck->first++;
|
|
|
|
phantom = src;
|
2012-11-06 22:03:07 +01:00
|
|
|
} else {
|
2012-11-30 21:34:11 +01:00
|
|
|
last: /* Move block after i downwards */
|
|
|
|
src = i + 1;
|
|
|
|
dst = i;
|
|
|
|
n = m;
|
|
|
|
ck->last--;
|
|
|
|
phantom = i + m;
|
2012-11-06 22:03:07 +01:00
|
|
|
}
|
|
|
|
/* (C) */
|
|
|
|
size = n * sizeof(ck->var[0]);
|
|
|
|
memmove(&ck->var[dst], &ck->var[src], size);
|
2012-08-18 13:07:14 +02:00
|
|
|
|
2012-11-06 22:03:07 +01:00
|
|
|
no_move:
|
|
|
|
/* Don't forget to remove the phantom value */
|
|
|
|
ck->var[phantom].type = GB_T_NULL;
|
2012-08-18 13:07:14 +02:00
|
|
|
|
2012-11-06 22:03:07 +01:00
|
|
|
list->count--;
|
2012-08-18 13:07:14 +02:00
|
|
|
|
2012-11-30 21:34:11 +01:00
|
|
|
/* Don't accidentally erase information */
|
|
|
|
memcpy(&back, val, sizeof(back));
|
2012-11-06 22:03:07 +01:00
|
|
|
for_all_persistent_vals(list, cur, es, save_enum) {
|
2012-11-30 21:34:11 +01:00
|
|
|
/* Benoit, this is about the enumerator case. If you print the address of
|
|
|
|
* the cur->ck, you will find that this cur->ck == NULL only appears for
|
|
|
|
* already de-allocated enumerators (is the memory area just reused or is
|
|
|
|
* the enumerator stray?) */
|
|
|
|
#if 0
|
2012-11-06 22:03:07 +01:00
|
|
|
if (!cur->ck)
|
2012-11-07 23:51:04 +01:00
|
|
|
fprintf(stderr, "cur->ck == NULL => spurious enumerator?\n");
|
2012-11-06 22:03:07 +01:00
|
|
|
#endif
|
2012-11-30 21:34:11 +01:00
|
|
|
if (cur->ck != back.ck)
|
2012-11-06 22:03:07 +01:00
|
|
|
continue;
|
2012-11-30 21:34:11 +01:00
|
|
|
/* (B) */
|
|
|
|
if (cur->fidx == back.fidx) {
|
2012-11-06 22:03:07 +01:00
|
|
|
if (!list->count)
|
|
|
|
cur->ck = NULL;
|
|
|
|
else
|
|
|
|
CHUNK_next_safe(list, cur);
|
2012-11-30 21:34:11 +01:00
|
|
|
/* (V) */
|
|
|
|
} else if (src < dst) {
|
|
|
|
CHUNK_prev_safe(list, cur);
|
2012-11-06 22:03:07 +01:00
|
|
|
} else {
|
2012-11-30 21:34:11 +01:00
|
|
|
CHUNK_next_safe(list, cur);
|
2012-11-06 22:03:07 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
if (gets_empty) {
|
|
|
|
LIST_unlink(&ck->list);
|
|
|
|
CHUNK_destroy(ck);
|
|
|
|
}
|
2012-08-30 03:51:01 +02:00
|
|
|
}
|
2012-08-18 13:07:14 +02:00
|
|
|
|
2012-11-06 22:03:07 +01:00
|
|
|
BEGIN_METHOD(List_Append, GB_VARIANT value)
|
2012-08-18 13:07:14 +02:00
|
|
|
|
2012-11-06 22:03:07 +01:00
|
|
|
CLIST_append(THIS, ARG(value));
|
2012-08-18 13:07:14 +02:00
|
|
|
|
2012-08-30 03:51:01 +02:00
|
|
|
END_METHOD
|
2012-08-18 13:07:14 +02:00
|
|
|
|
2012-11-06 22:03:07 +01:00
|
|
|
BEGIN_METHOD(List_Prepend, GB_VARIANT value)
|
2012-08-18 13:07:14 +02:00
|
|
|
|
2012-11-06 22:03:07 +01:00
|
|
|
CLIST_prepend(THIS, ARG(value));
|
2012-08-18 13:07:14 +02:00
|
|
|
|
2012-11-06 22:03:07 +01:00
|
|
|
END_METHOD
|
2012-08-18 13:07:14 +02:00
|
|
|
|
2012-11-06 22:03:07 +01:00
|
|
|
#define CHECK_CURRENT() (THIS->current.ck)
|
|
|
|
#define CHECK_RAISE_CURRENT() \
|
|
|
|
if (!CHECK_CURRENT()) { \
|
2012-08-30 03:51:01 +02:00
|
|
|
GB.Error("No current element"); \
|
|
|
|
return; \
|
2012-08-18 13:07:14 +02:00
|
|
|
}
|
|
|
|
|
2012-08-30 03:51:01 +02:00
|
|
|
BEGIN_METHOD(List_Take, GB_INTEGER index)
|
2012-08-18 13:07:14 +02:00
|
|
|
|
2012-11-06 22:03:07 +01:00
|
|
|
VAL val;
|
|
|
|
GB_VARIANT_VALUE buf;
|
2012-08-30 03:51:01 +02:00
|
|
|
|
2012-11-06 22:03:07 +01:00
|
|
|
if (!THIS->count) {
|
|
|
|
GB.Error(GB_ERR_BOUND);
|
|
|
|
return;
|
|
|
|
}
|
2012-08-30 03:51:01 +02:00
|
|
|
if (MISSING(index)) {
|
2012-11-06 22:03:07 +01:00
|
|
|
CHECK_RAISE_CURRENT();
|
|
|
|
memcpy(&val, &THIS->current, sizeof(val));
|
|
|
|
CLIST_take(THIS, &val, &buf);
|
|
|
|
memcpy(&THIS->current, &val, sizeof(val));
|
2012-08-30 03:51:01 +02:00
|
|
|
} else {
|
2012-11-06 22:03:07 +01:00
|
|
|
CLIST_get(THIS, VARG(index), &val);
|
|
|
|
if (!val.ck) {
|
2012-08-18 13:07:14 +02:00
|
|
|
GB.Error(GB_ERR_BOUND);
|
|
|
|
return;
|
|
|
|
}
|
2012-11-06 22:03:07 +01:00
|
|
|
CLIST_take(THIS, &val, &buf);
|
2012-08-18 13:07:14 +02:00
|
|
|
}
|
2012-11-06 22:03:07 +01:00
|
|
|
GB.ReturnVariant(&buf);
|
2012-08-30 03:51:01 +02:00
|
|
|
GB.ReturnBorrow();
|
2012-11-06 22:03:07 +01:00
|
|
|
GB.StoreVariant(NULL, &buf);
|
2012-08-30 03:51:01 +02:00
|
|
|
GB.ReturnRelease();
|
2012-08-18 13:07:14 +02:00
|
|
|
|
|
|
|
END_METHOD
|
|
|
|
|
2012-11-06 22:03:07 +01:00
|
|
|
#define IMPLEMENT_Move(which, magic) \
|
2012-08-30 03:51:01 +02:00
|
|
|
BEGIN_METHOD_VOID(List_Move ## which) \
|
|
|
|
\
|
2012-11-06 22:03:07 +01:00
|
|
|
if (!THIS->count) { \
|
|
|
|
GB.Error("No elements"); \
|
|
|
|
return; \
|
2012-08-30 03:51:01 +02:00
|
|
|
} \
|
2012-11-06 22:03:07 +01:00
|
|
|
magic; \
|
2012-08-30 03:51:01 +02:00
|
|
|
\
|
2012-08-18 13:07:14 +02:00
|
|
|
END_METHOD
|
|
|
|
|
2012-11-06 22:03:07 +01:00
|
|
|
IMPLEMENT_Move(Next, if (!CHECK_CURRENT())CLIST_first(THIS, &THIS->current);
|
|
|
|
CHUNK_next(THIS, &THIS->current))
|
|
|
|
IMPLEMENT_Move(Prev, if (!CHECK_CURRENT()) CLIST_last(THIS, &THIS->current);
|
|
|
|
CHUNK_prev(THIS, &THIS->current))
|
|
|
|
IMPLEMENT_Move(First, CLIST_first(THIS, &THIS->current))
|
|
|
|
IMPLEMENT_Move(Last, CLIST_last(THIS, &THIS->current))
|
2012-08-30 03:51:01 +02:00
|
|
|
|
2012-11-06 22:03:07 +01:00
|
|
|
/*
|
|
|
|
* Modify @val to point to the next/prev value equal to @comp. If nothing
|
|
|
|
* found - @val is allowed to cycle and point to itself again! - NULL is
|
|
|
|
* written to @val->ck.
|
|
|
|
*/
|
2012-08-18 13:07:14 +02:00
|
|
|
|
2012-11-06 22:03:07 +01:00
|
|
|
static void CLIST_find_forward(CLIST *list, VAL *val, GB_VARIANT *comp)
|
|
|
|
{
|
|
|
|
CHUNK *last = NULL;
|
|
|
|
int cached_diff;
|
|
|
|
VAL start;
|
|
|
|
|
|
|
|
memcpy(&start, val, sizeof(start));
|
|
|
|
|
|
|
|
cached_diff = 1;
|
|
|
|
do {
|
|
|
|
/* We do actually enumerate but do the checking ourselves */
|
|
|
|
CHUNK_next(list, val);
|
|
|
|
/* Note that comparing here allows @val to point to itself
|
|
|
|
* again. This is intentional for cyclic lists */
|
|
|
|
if (!GB.CompVariant(VAL_value(val), &comp->value))
|
|
|
|
return;
|
|
|
|
if (val->ck != last)
|
|
|
|
last = val->ck;
|
|
|
|
if (last == start.ck && val->fidx == start.fidx)
|
|
|
|
cached_diff = 0;
|
|
|
|
} while (cached_diff);
|
|
|
|
/* Invalidate */
|
|
|
|
val->ck = NULL; /* This is most likely &list->current */
|
2012-08-30 03:51:01 +02:00
|
|
|
}
|
2012-08-18 13:07:14 +02:00
|
|
|
|
2012-11-06 22:03:07 +01:00
|
|
|
static void CLIST_find_backward(CLIST *list, VAL *val, GB_VARIANT *comp)
|
2012-08-30 03:51:01 +02:00
|
|
|
{
|
2012-11-06 22:03:07 +01:00
|
|
|
CHUNK *last = NULL;
|
|
|
|
int cached_diff;
|
|
|
|
VAL start;
|
2012-08-18 13:07:14 +02:00
|
|
|
|
2012-11-06 22:03:07 +01:00
|
|
|
memcpy(&start, val, sizeof(start));
|
2012-08-18 13:07:14 +02:00
|
|
|
|
2012-11-06 22:03:07 +01:00
|
|
|
cached_diff = 1;
|
|
|
|
do {
|
|
|
|
CHUNK_prev(list, val);
|
|
|
|
if (!GB.CompVariant(VAL_value(val), &comp->value))
|
|
|
|
return;
|
|
|
|
if (val->ck != last)
|
|
|
|
last = val->ck;
|
|
|
|
if (last == start.ck && val->fidx == start.fidx)
|
|
|
|
cached_diff = 0;
|
|
|
|
} while (cached_diff);
|
|
|
|
val->ck = NULL;
|
2012-08-30 03:51:01 +02:00
|
|
|
}
|
2012-08-18 13:07:14 +02:00
|
|
|
|
2012-11-30 21:34:11 +01:00
|
|
|
#define CHECK_RET_CURRENT() \
|
|
|
|
if (!CHECK_CURRENT()) { \
|
2012-11-06 22:03:07 +01:00
|
|
|
GB.ReturnNull(); \
|
|
|
|
return; \
|
|
|
|
}
|
|
|
|
|
2012-08-30 03:51:01 +02:00
|
|
|
#define IMPLEMENT_Find(which, which2, magic) \
|
|
|
|
\
|
|
|
|
BEGIN_METHOD(List_Find ## which, GB_VARIANT value) \
|
|
|
|
\
|
2012-11-06 22:03:07 +01:00
|
|
|
if (!THIS->count) { \
|
|
|
|
GB.Error("No elements"); \
|
|
|
|
return; \
|
|
|
|
} \
|
2012-08-30 03:51:01 +02:00
|
|
|
magic; \
|
2012-11-06 22:03:07 +01:00
|
|
|
CLIST_find_ ## which2 (THIS, &THIS->current, ARG(value)); \
|
2012-08-30 03:51:01 +02:00
|
|
|
\
|
|
|
|
END_METHOD
|
2012-08-18 13:07:14 +02:00
|
|
|
|
2012-08-30 03:51:01 +02:00
|
|
|
IMPLEMENT_Find(Next, forward,
|
2012-11-30 21:34:11 +01:00
|
|
|
if (!CHECK_CURRENT()) CLIST_first(THIS, &THIS->current))
|
2012-08-30 03:51:01 +02:00
|
|
|
IMPLEMENT_Find(Prev, backward,
|
2012-11-30 21:34:11 +01:00
|
|
|
if (!CHECK_CURRENT()) CLIST_last(THIS, &THIS->current))
|
2012-11-06 22:03:07 +01:00
|
|
|
IMPLEMENT_Find(First, forward, CLIST_first(THIS, &THIS->current))
|
|
|
|
IMPLEMENT_Find(Last, backward, CLIST_last(THIS, &THIS->current))
|
2012-08-18 13:07:14 +02:00
|
|
|
|
2012-08-30 03:51:01 +02:00
|
|
|
BEGIN_PROPERTY(List_Current)
|
2012-08-18 13:07:14 +02:00
|
|
|
|
2012-11-06 22:03:07 +01:00
|
|
|
GB_VARIANT_VALUE *val;
|
|
|
|
|
2012-11-30 21:34:11 +01:00
|
|
|
CHECK_RET_CURRENT();
|
2012-11-06 22:03:07 +01:00
|
|
|
val = VAL_value(&THIS->current);
|
2012-08-30 03:51:01 +02:00
|
|
|
if (READ_PROPERTY) {
|
2012-11-06 22:03:07 +01:00
|
|
|
GB.ReturnVariant(val);
|
2012-08-30 03:51:01 +02:00
|
|
|
return;
|
|
|
|
}
|
2012-11-06 22:03:07 +01:00
|
|
|
GB.StoreVariant(PROP(GB_VARIANT), val);
|
2012-08-18 13:07:14 +02:00
|
|
|
|
|
|
|
END_PROPERTY
|
|
|
|
|
2012-08-30 03:51:01 +02:00
|
|
|
BEGIN_PROPERTY(List_Count)
|
2012-08-18 13:07:14 +02:00
|
|
|
|
2012-08-30 03:51:01 +02:00
|
|
|
GB.ReturnInteger(THIS->count);
|
2012-08-18 13:07:14 +02:00
|
|
|
|
|
|
|
END_PROPERTY
|
|
|
|
|
|
|
|
GB_DESC CListDesc[] = {
|
|
|
|
GB_DECLARE("List", sizeof(CLIST)),
|
|
|
|
|
2012-08-30 03:51:01 +02:00
|
|
|
GB_METHOD("_new", NULL, List_new, NULL),
|
2012-08-18 13:07:14 +02:00
|
|
|
GB_METHOD("_free", NULL, List_free, NULL),
|
2012-08-30 03:51:01 +02:00
|
|
|
GB_METHOD("_next", "v", List_next, NULL),
|
|
|
|
GB_METHOD("_get", "v", List_get, "(Index)i"),
|
|
|
|
GB_METHOD("_put", NULL, List_put, "(Value)v(Index)i"),
|
2012-08-18 13:07:14 +02:00
|
|
|
|
2012-11-30 21:34:11 +01:00
|
|
|
GB_METHOD("Clear", NULL, List_free, NULL),
|
|
|
|
|
2012-08-30 03:51:01 +02:00
|
|
|
GB_METHOD("Append", NULL, List_Append, "(Value)v"),
|
|
|
|
GB_METHOD("Prepend", NULL, List_Prepend, "(Value)v"),
|
|
|
|
GB_METHOD("Take", "v", List_Take, "[(Index)i]"),
|
2012-08-18 13:07:14 +02:00
|
|
|
|
2012-08-30 03:51:01 +02:00
|
|
|
GB_METHOD("MoveNext", NULL, List_MoveNext, NULL),
|
|
|
|
GB_METHOD("MovePrev", NULL, List_MovePrev, NULL),
|
|
|
|
GB_METHOD("MovePrevious", NULL, List_MovePrev, NULL),
|
|
|
|
GB_METHOD("MoveFirst", NULL, List_MoveFirst, NULL),
|
|
|
|
GB_METHOD("MoveLast", NULL, List_MoveLast, NULL),
|
2012-08-18 13:07:14 +02:00
|
|
|
|
2012-11-06 22:03:07 +01:00
|
|
|
GB_METHOD("FindNext", NULL, List_FindNext, "(Value)v"),
|
|
|
|
GB_METHOD("FindPrev", NULL, List_FindPrev, "(Value)v"),
|
|
|
|
GB_METHOD("FindPrevious", NULL, List_FindPrev, "(Value)v"),
|
|
|
|
GB_METHOD("FindFirst", NULL, List_FindFirst, "(Value)v"),
|
|
|
|
GB_METHOD("FindLast", NULL, List_FindLast, "(Value)v"),
|
2012-08-18 13:07:14 +02:00
|
|
|
|
2012-11-30 21:34:11 +01:00
|
|
|
/*
|
|
|
|
* XXX: Shouldn't this be rather more exposed as the virtual
|
|
|
|
* container it actually is? But with an absolute index, of
|
|
|
|
* course. But can this be done efficiently?
|
|
|
|
*/
|
2012-08-30 03:51:01 +02:00
|
|
|
GB_PROPERTY("Current", "v", List_Current),
|
|
|
|
GB_PROPERTY_READ("Count", "i", List_Count),
|
2012-11-06 22:03:07 +01:00
|
|
|
GB_PROPERTY_SELF("Backwards", ".List.Backwards"),
|
|
|
|
|
|
|
|
GB_END_DECLARE
|
|
|
|
};
|
|
|
|
|
|
|
|
GB_DESC CListBackwardsDesc[] = {
|
|
|
|
GB_DECLARE(".List.Backwards", 0),
|
|
|
|
GB_VIRTUAL_CLASS(),
|
|
|
|
|
|
|
|
GB_METHOD("_next", "v", ListBackwards_next, NULL),
|
2012-08-18 13:07:14 +02:00
|
|
|
|
|
|
|
GB_END_DECLARE
|
|
|
|
};
|