mirror of
https://gitlab.gnome.org/GNOME/glib.git
synced 2025-01-11 15:06:14 +01:00
Added stack, queue ADTs and related tests.
Tue Mar 9 14:37:32 1999 Jeff Garzik <jgarzik@pobox.com> * Makefile.am, glib.h, gstack.c, gqueue.c, tests/Makefile.am, tests/queue-test.c, tests/stack-test.c: Added stack, queue ADTs and related tests. * glib.h, glist.c: New g_list_delete() function.
This commit is contained in:
parent
0ecd369530
commit
fd7ba69e32
11
ChangeLog
11
ChangeLog
@ -1,4 +1,13 @@
|
||||
Sat Mar 6 11:03:08 1999 ape@lrdpf.spacetec.no (Asbjorn Pettersen)
|
||||
Tue Mar 9 14:37:32 1999 Jeff Garzik <jgarzik@pobox.com>
|
||||
|
||||
* Makefile.am, glib.h, gstack.c, gqueue.c,
|
||||
tests/Makefile.am, tests/queue-test.c, tests/stack-test.c:
|
||||
Added stack, queue ADTs and related tests.
|
||||
|
||||
* glib.h, glist.c:
|
||||
New g_list_delete() function.
|
||||
|
||||
Sat Mar 6 11:03:08 1999 Asbjorn Pettersen <ape@lrdpf.spacetec.no>
|
||||
|
||||
* gutils.c (g_get_any_init): add OS/2 changes.
|
||||
change '\\' in HOME to '/'.
|
||||
|
@ -1,4 +1,13 @@
|
||||
Sat Mar 6 11:03:08 1999 ape@lrdpf.spacetec.no (Asbjorn Pettersen)
|
||||
Tue Mar 9 14:37:32 1999 Jeff Garzik <jgarzik@pobox.com>
|
||||
|
||||
* Makefile.am, glib.h, gstack.c, gqueue.c,
|
||||
tests/Makefile.am, tests/queue-test.c, tests/stack-test.c:
|
||||
Added stack, queue ADTs and related tests.
|
||||
|
||||
* glib.h, glist.c:
|
||||
New g_list_delete() function.
|
||||
|
||||
Sat Mar 6 11:03:08 1999 Asbjorn Pettersen <ape@lrdpf.spacetec.no>
|
||||
|
||||
* gutils.c (g_get_any_init): add OS/2 changes.
|
||||
change '\\' in HOME to '/'.
|
||||
|
@ -1,4 +1,13 @@
|
||||
Sat Mar 6 11:03:08 1999 ape@lrdpf.spacetec.no (Asbjorn Pettersen)
|
||||
Tue Mar 9 14:37:32 1999 Jeff Garzik <jgarzik@pobox.com>
|
||||
|
||||
* Makefile.am, glib.h, gstack.c, gqueue.c,
|
||||
tests/Makefile.am, tests/queue-test.c, tests/stack-test.c:
|
||||
Added stack, queue ADTs and related tests.
|
||||
|
||||
* glib.h, glist.c:
|
||||
New g_list_delete() function.
|
||||
|
||||
Sat Mar 6 11:03:08 1999 Asbjorn Pettersen <ape@lrdpf.spacetec.no>
|
||||
|
||||
* gutils.c (g_get_any_init): add OS/2 changes.
|
||||
change '\\' in HOME to '/'.
|
||||
|
@ -1,4 +1,13 @@
|
||||
Sat Mar 6 11:03:08 1999 ape@lrdpf.spacetec.no (Asbjorn Pettersen)
|
||||
Tue Mar 9 14:37:32 1999 Jeff Garzik <jgarzik@pobox.com>
|
||||
|
||||
* Makefile.am, glib.h, gstack.c, gqueue.c,
|
||||
tests/Makefile.am, tests/queue-test.c, tests/stack-test.c:
|
||||
Added stack, queue ADTs and related tests.
|
||||
|
||||
* glib.h, glist.c:
|
||||
New g_list_delete() function.
|
||||
|
||||
Sat Mar 6 11:03:08 1999 Asbjorn Pettersen <ape@lrdpf.spacetec.no>
|
||||
|
||||
* gutils.c (g_get_any_init): add OS/2 changes.
|
||||
change '\\' in HOME to '/'.
|
||||
|
@ -1,4 +1,13 @@
|
||||
Sat Mar 6 11:03:08 1999 ape@lrdpf.spacetec.no (Asbjorn Pettersen)
|
||||
Tue Mar 9 14:37:32 1999 Jeff Garzik <jgarzik@pobox.com>
|
||||
|
||||
* Makefile.am, glib.h, gstack.c, gqueue.c,
|
||||
tests/Makefile.am, tests/queue-test.c, tests/stack-test.c:
|
||||
Added stack, queue ADTs and related tests.
|
||||
|
||||
* glib.h, glist.c:
|
||||
New g_list_delete() function.
|
||||
|
||||
Sat Mar 6 11:03:08 1999 Asbjorn Pettersen <ape@lrdpf.spacetec.no>
|
||||
|
||||
* gutils.c (g_get_any_init): add OS/2 changes.
|
||||
change '\\' in HOME to '/'.
|
||||
|
@ -1,4 +1,13 @@
|
||||
Sat Mar 6 11:03:08 1999 ape@lrdpf.spacetec.no (Asbjorn Pettersen)
|
||||
Tue Mar 9 14:37:32 1999 Jeff Garzik <jgarzik@pobox.com>
|
||||
|
||||
* Makefile.am, glib.h, gstack.c, gqueue.c,
|
||||
tests/Makefile.am, tests/queue-test.c, tests/stack-test.c:
|
||||
Added stack, queue ADTs and related tests.
|
||||
|
||||
* glib.h, glist.c:
|
||||
New g_list_delete() function.
|
||||
|
||||
Sat Mar 6 11:03:08 1999 Asbjorn Pettersen <ape@lrdpf.spacetec.no>
|
||||
|
||||
* gutils.c (g_get_any_init): add OS/2 changes.
|
||||
change '\\' in HOME to '/'.
|
||||
|
@ -1,4 +1,13 @@
|
||||
Sat Mar 6 11:03:08 1999 ape@lrdpf.spacetec.no (Asbjorn Pettersen)
|
||||
Tue Mar 9 14:37:32 1999 Jeff Garzik <jgarzik@pobox.com>
|
||||
|
||||
* Makefile.am, glib.h, gstack.c, gqueue.c,
|
||||
tests/Makefile.am, tests/queue-test.c, tests/stack-test.c:
|
||||
Added stack, queue ADTs and related tests.
|
||||
|
||||
* glib.h, glist.c:
|
||||
New g_list_delete() function.
|
||||
|
||||
Sat Mar 6 11:03:08 1999 Asbjorn Pettersen <ape@lrdpf.spacetec.no>
|
||||
|
||||
* gutils.c (g_get_any_init): add OS/2 changes.
|
||||
change '\\' in HOME to '/'.
|
||||
|
@ -1,4 +1,13 @@
|
||||
Sat Mar 6 11:03:08 1999 ape@lrdpf.spacetec.no (Asbjorn Pettersen)
|
||||
Tue Mar 9 14:37:32 1999 Jeff Garzik <jgarzik@pobox.com>
|
||||
|
||||
* Makefile.am, glib.h, gstack.c, gqueue.c,
|
||||
tests/Makefile.am, tests/queue-test.c, tests/stack-test.c:
|
||||
Added stack, queue ADTs and related tests.
|
||||
|
||||
* glib.h, glist.c:
|
||||
New g_list_delete() function.
|
||||
|
||||
Sat Mar 6 11:03:08 1999 Asbjorn Pettersen <ape@lrdpf.spacetec.no>
|
||||
|
||||
* gutils.c (g_get_any_init): add OS/2 changes.
|
||||
change '\\' in HOME to '/'.
|
||||
|
@ -44,9 +44,11 @@ libglib_la_SOURCES = \
|
||||
gmutex.c \
|
||||
gnode.c \
|
||||
gprimes.c \
|
||||
gqueue.c \
|
||||
grel.c \
|
||||
gscanner.c \
|
||||
gslist.c \
|
||||
gstack.c \
|
||||
gstrfuncs.c \
|
||||
gstring.c \
|
||||
gtimer.c \
|
||||
@ -99,4 +101,4 @@ snapshot:
|
||||
$(MAKE) dist distdir=$(PACKAGE)`date +"%y%m%d"`
|
||||
|
||||
dist-hook:
|
||||
cp glib.spec $(distdir)
|
||||
cp glib.spec $(distdir)
|
||||
|
76
glib.h
76
glib.h
@ -685,10 +685,12 @@ typedef struct _GList GList;
|
||||
typedef struct _GMemChunk GMemChunk;
|
||||
typedef struct _GNode GNode;
|
||||
typedef struct _GPtrArray GPtrArray;
|
||||
typedef struct _GQueue GQueue;
|
||||
typedef struct _GRelation GRelation;
|
||||
typedef struct _GScanner GScanner;
|
||||
typedef struct _GScannerConfig GScannerConfig;
|
||||
typedef struct _GSList GSList;
|
||||
typedef struct _GStack GStack;
|
||||
typedef struct _GString GString;
|
||||
typedef struct _GStringChunk GStringChunk;
|
||||
typedef struct _GTimer GTimer;
|
||||
@ -805,6 +807,18 @@ struct _GSList
|
||||
GSList *next;
|
||||
};
|
||||
|
||||
struct _GStack
|
||||
{
|
||||
GList *list;
|
||||
};
|
||||
|
||||
struct _GQueue
|
||||
{
|
||||
GList *list;
|
||||
GList *list_end;
|
||||
guint list_size;
|
||||
};
|
||||
|
||||
struct _GString
|
||||
{
|
||||
gchar *str;
|
||||
@ -860,6 +874,8 @@ GList* g_list_insert_sorted (GList *list,
|
||||
GCompareFunc func);
|
||||
GList* g_list_concat (GList *list1,
|
||||
GList *list2);
|
||||
GList* g_list_delete (GList *list,
|
||||
GList *link);
|
||||
GList* g_list_remove (GList *list,
|
||||
gpointer data);
|
||||
GList* g_list_remove_link (GList *list,
|
||||
@ -939,6 +955,66 @@ gpointer g_slist_nth_data (GSList *list,
|
||||
#define g_slist_next(slist) ((slist) ? (((GSList *)(slist))->next) : NULL)
|
||||
|
||||
|
||||
/* Stacks
|
||||
*/
|
||||
|
||||
GStack * g_stack_new (void);
|
||||
void g_stack_free (GStack *stack);
|
||||
gpointer g_stack_pop (GStack *stack);
|
||||
|
||||
#define g_stack_empty(stack) \
|
||||
((((GStack *)(stack)) && ((GStack *)(stack))->list) ? FALSE : TRUE)
|
||||
|
||||
#define g_stack_peek(stack) \
|
||||
((((GStack *)(stack)) && ((GStack *)(stack))->list) ? \
|
||||
((GStack *)(stack))->list->data : NULL)
|
||||
|
||||
#define g_stack_index(stack,ptr) \
|
||||
((((GStack *)(stack)) && ((GStack *)(stack))->list) ? \
|
||||
g_list_index (((GStack *)(stack))->list, (ptr)) : -1)
|
||||
|
||||
#define g_stack_push(stack,data) G_STMT_START { \
|
||||
if ((GStack *)(stack)) \
|
||||
((GStack *)(stack))->list = \
|
||||
g_list_prepend (((GStack *)(stack))->list, (data)); \
|
||||
} G_STMT_END
|
||||
|
||||
|
||||
|
||||
/* Queues
|
||||
*/
|
||||
|
||||
GQueue * g_queue_new (void);
|
||||
void g_queue_free (GQueue *q);
|
||||
guint g_queue_get_size (GQueue *q);
|
||||
void g_queue_push_front (GQueue *q, gpointer data);
|
||||
void g_queue_push_back (GQueue *q, gpointer data);
|
||||
gpointer g_queue_pop_front (GQueue *q);
|
||||
gpointer g_queue_pop_back (GQueue *q);
|
||||
|
||||
#define g_queue_empty(queue) \
|
||||
((((GQueue *)(queue)) && ((GQueue *)(queue))->list) ? FALSE : TRUE)
|
||||
|
||||
#define g_queue_peek_front(queue) \
|
||||
((((GQueue *)(queue)) && ((GQueue *)(queue))->list) ? \
|
||||
((GQueue *)(queue))->list->data : NULL)
|
||||
|
||||
#define g_queue_peek_back(queue) \
|
||||
((((GQueue *)(queue)) && ((GQueue *)(queue))->list_end) ? \
|
||||
((GQueue *)(queue))->list_end->data : NULL)
|
||||
|
||||
#define g_queue_index(queue,ptr) \
|
||||
((((GQueue *)(queue)) && ((GQueue *)(queue))->list) ? \
|
||||
g_list_index (((GQueue *)(queue))->list, (ptr)) : -1)
|
||||
|
||||
#define g_queue_push g_queue_push_back
|
||||
#define g_queue_pop g_queue_pop_front
|
||||
#define g_queue_peek g_queue_peek_front
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
/* Hash tables
|
||||
*/
|
||||
GHashTable* g_hash_table_new (GHashFunc hash_func,
|
||||
|
@ -44,9 +44,11 @@ libglib_la_SOURCES = \
|
||||
gmutex.c \
|
||||
gnode.c \
|
||||
gprimes.c \
|
||||
gqueue.c \
|
||||
grel.c \
|
||||
gscanner.c \
|
||||
gslist.c \
|
||||
gstack.c \
|
||||
gstrfuncs.c \
|
||||
gstring.c \
|
||||
gtimer.c \
|
||||
@ -99,4 +101,4 @@ snapshot:
|
||||
$(MAKE) dist distdir=$(PACKAGE)`date +"%y%m%d"`
|
||||
|
||||
dist-hook:
|
||||
cp glib.spec $(distdir)
|
||||
cp glib.spec $(distdir)
|
||||
|
76
glib/glib.h
76
glib/glib.h
@ -685,10 +685,12 @@ typedef struct _GList GList;
|
||||
typedef struct _GMemChunk GMemChunk;
|
||||
typedef struct _GNode GNode;
|
||||
typedef struct _GPtrArray GPtrArray;
|
||||
typedef struct _GQueue GQueue;
|
||||
typedef struct _GRelation GRelation;
|
||||
typedef struct _GScanner GScanner;
|
||||
typedef struct _GScannerConfig GScannerConfig;
|
||||
typedef struct _GSList GSList;
|
||||
typedef struct _GStack GStack;
|
||||
typedef struct _GString GString;
|
||||
typedef struct _GStringChunk GStringChunk;
|
||||
typedef struct _GTimer GTimer;
|
||||
@ -805,6 +807,18 @@ struct _GSList
|
||||
GSList *next;
|
||||
};
|
||||
|
||||
struct _GStack
|
||||
{
|
||||
GList *list;
|
||||
};
|
||||
|
||||
struct _GQueue
|
||||
{
|
||||
GList *list;
|
||||
GList *list_end;
|
||||
guint list_size;
|
||||
};
|
||||
|
||||
struct _GString
|
||||
{
|
||||
gchar *str;
|
||||
@ -860,6 +874,8 @@ GList* g_list_insert_sorted (GList *list,
|
||||
GCompareFunc func);
|
||||
GList* g_list_concat (GList *list1,
|
||||
GList *list2);
|
||||
GList* g_list_delete (GList *list,
|
||||
GList *link);
|
||||
GList* g_list_remove (GList *list,
|
||||
gpointer data);
|
||||
GList* g_list_remove_link (GList *list,
|
||||
@ -939,6 +955,66 @@ gpointer g_slist_nth_data (GSList *list,
|
||||
#define g_slist_next(slist) ((slist) ? (((GSList *)(slist))->next) : NULL)
|
||||
|
||||
|
||||
/* Stacks
|
||||
*/
|
||||
|
||||
GStack * g_stack_new (void);
|
||||
void g_stack_free (GStack *stack);
|
||||
gpointer g_stack_pop (GStack *stack);
|
||||
|
||||
#define g_stack_empty(stack) \
|
||||
((((GStack *)(stack)) && ((GStack *)(stack))->list) ? FALSE : TRUE)
|
||||
|
||||
#define g_stack_peek(stack) \
|
||||
((((GStack *)(stack)) && ((GStack *)(stack))->list) ? \
|
||||
((GStack *)(stack))->list->data : NULL)
|
||||
|
||||
#define g_stack_index(stack,ptr) \
|
||||
((((GStack *)(stack)) && ((GStack *)(stack))->list) ? \
|
||||
g_list_index (((GStack *)(stack))->list, (ptr)) : -1)
|
||||
|
||||
#define g_stack_push(stack,data) G_STMT_START { \
|
||||
if ((GStack *)(stack)) \
|
||||
((GStack *)(stack))->list = \
|
||||
g_list_prepend (((GStack *)(stack))->list, (data)); \
|
||||
} G_STMT_END
|
||||
|
||||
|
||||
|
||||
/* Queues
|
||||
*/
|
||||
|
||||
GQueue * g_queue_new (void);
|
||||
void g_queue_free (GQueue *q);
|
||||
guint g_queue_get_size (GQueue *q);
|
||||
void g_queue_push_front (GQueue *q, gpointer data);
|
||||
void g_queue_push_back (GQueue *q, gpointer data);
|
||||
gpointer g_queue_pop_front (GQueue *q);
|
||||
gpointer g_queue_pop_back (GQueue *q);
|
||||
|
||||
#define g_queue_empty(queue) \
|
||||
((((GQueue *)(queue)) && ((GQueue *)(queue))->list) ? FALSE : TRUE)
|
||||
|
||||
#define g_queue_peek_front(queue) \
|
||||
((((GQueue *)(queue)) && ((GQueue *)(queue))->list) ? \
|
||||
((GQueue *)(queue))->list->data : NULL)
|
||||
|
||||
#define g_queue_peek_back(queue) \
|
||||
((((GQueue *)(queue)) && ((GQueue *)(queue))->list_end) ? \
|
||||
((GQueue *)(queue))->list_end->data : NULL)
|
||||
|
||||
#define g_queue_index(queue,ptr) \
|
||||
((((GQueue *)(queue)) && ((GQueue *)(queue))->list) ? \
|
||||
g_list_index (((GQueue *)(queue))->list, (ptr)) : -1)
|
||||
|
||||
#define g_queue_push g_queue_push_back
|
||||
#define g_queue_pop g_queue_pop_front
|
||||
#define g_queue_peek g_queue_peek_front
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
/* Hash tables
|
||||
*/
|
||||
GHashTable* g_hash_table_new (GHashFunc hash_func,
|
||||
|
11
glib/glist.c
11
glib/glist.c
@ -314,6 +314,17 @@ g_list_remove_link (GList *list,
|
||||
return list;
|
||||
}
|
||||
|
||||
|
||||
GList*
|
||||
g_list_delete (GList *list, GList *link)
|
||||
{
|
||||
list = g_list_remove_link (list, link);
|
||||
g_list_free_1 (link);
|
||||
|
||||
return list;
|
||||
}
|
||||
|
||||
|
||||
GList*
|
||||
g_list_copy (GList *list)
|
||||
{
|
||||
|
144
glib/gqueue.c
Normal file
144
glib/gqueue.c
Normal file
@ -0,0 +1,144 @@
|
||||
/*
|
||||
* GQueue, opaque ADT with interface:
|
||||
|
||||
q = g_queue_new ();
|
||||
count = g_queue_size (q);
|
||||
|
||||
q_queue_push_front (q, data);
|
||||
q_queue_push_back (q, data);
|
||||
data = q_queue_pop_front (q);
|
||||
data = q_queue_pop_back (q);
|
||||
#define q_queue_push q_queue_push_back
|
||||
#define q_queue_pop q_queue_pop_front
|
||||
|
||||
list = g_queue_get_list (q);
|
||||
#define g_queue_get_front g_queue_get_list
|
||||
list_end = g_queue_get_back (q);
|
||||
*/
|
||||
|
||||
|
||||
#ifdef HAVE_CONFIG_H
|
||||
# include <config.h>
|
||||
#endif
|
||||
|
||||
#include <glib.h>
|
||||
|
||||
|
||||
|
||||
|
||||
GQueue *
|
||||
g_queue_new (void)
|
||||
{
|
||||
GQueue *q = g_new (GQueue, 1);
|
||||
|
||||
q->list = q->list_end = NULL;
|
||||
q->list_size = 0;
|
||||
|
||||
return q;
|
||||
}
|
||||
|
||||
|
||||
void
|
||||
g_queue_free (GQueue *q)
|
||||
{
|
||||
if (q) {
|
||||
if (q->list)
|
||||
g_list_free (q->list);
|
||||
g_free (q);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
guint
|
||||
g_queue_get_size (GQueue *q)
|
||||
{
|
||||
return (q == NULL) ? 0 : q->list_size;
|
||||
}
|
||||
|
||||
|
||||
void
|
||||
g_queue_push_front (GQueue *q, gpointer data)
|
||||
{
|
||||
if (q) {
|
||||
q->list = g_list_prepend (q->list, data);
|
||||
|
||||
if (q->list_end == NULL)
|
||||
q->list_end = q->list;
|
||||
|
||||
q->list_size++;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
void
|
||||
g_queue_push_back (GQueue *q, gpointer data)
|
||||
{
|
||||
if (q) {
|
||||
q->list_end = g_list_append (q->list_end, data);
|
||||
|
||||
if (! q->list)
|
||||
q->list = q->list_end;
|
||||
else
|
||||
q->list_end = q->list_end->next;
|
||||
|
||||
q->list_size++;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
gpointer
|
||||
g_queue_pop_front (GQueue *q)
|
||||
{
|
||||
gpointer data = NULL;
|
||||
|
||||
if ((q) && (q->list)) {
|
||||
GList *node;
|
||||
|
||||
node = q->list;
|
||||
data = node->data;
|
||||
|
||||
if (! node->next) {
|
||||
q->list = q->list_end = NULL;
|
||||
q->list_size = 0;
|
||||
}
|
||||
else {
|
||||
q->list = node->next;
|
||||
q->list->prev = NULL;
|
||||
q->list_size--;
|
||||
}
|
||||
|
||||
g_list_free_1 (node);
|
||||
}
|
||||
|
||||
return data;
|
||||
}
|
||||
|
||||
|
||||
gpointer
|
||||
g_queue_pop_back (GQueue *q)
|
||||
{
|
||||
gpointer data = NULL;
|
||||
|
||||
if ((q) && (q->list)) {
|
||||
GList *node;
|
||||
|
||||
node = q->list_end;
|
||||
data = node->data;
|
||||
|
||||
if (! node->prev) {
|
||||
q->list = q->list_end = NULL;
|
||||
q->list_size = 0;
|
||||
}
|
||||
else {
|
||||
q->list_end = node->prev;
|
||||
q->list_end->next = NULL;
|
||||
q->list_size--;
|
||||
}
|
||||
|
||||
g_list_free_1 (node);
|
||||
}
|
||||
|
||||
return data;
|
||||
}
|
||||
|
||||
|
11
glist.c
11
glist.c
@ -314,6 +314,17 @@ g_list_remove_link (GList *list,
|
||||
return list;
|
||||
}
|
||||
|
||||
|
||||
GList*
|
||||
g_list_delete (GList *list, GList *link)
|
||||
{
|
||||
list = g_list_remove_link (list, link);
|
||||
g_list_free_1 (link);
|
||||
|
||||
return list;
|
||||
}
|
||||
|
||||
|
||||
GList*
|
||||
g_list_copy (GList *list)
|
||||
{
|
||||
|
144
gqueue.c
Normal file
144
gqueue.c
Normal file
@ -0,0 +1,144 @@
|
||||
/*
|
||||
* GQueue, opaque ADT with interface:
|
||||
|
||||
q = g_queue_new ();
|
||||
count = g_queue_size (q);
|
||||
|
||||
q_queue_push_front (q, data);
|
||||
q_queue_push_back (q, data);
|
||||
data = q_queue_pop_front (q);
|
||||
data = q_queue_pop_back (q);
|
||||
#define q_queue_push q_queue_push_back
|
||||
#define q_queue_pop q_queue_pop_front
|
||||
|
||||
list = g_queue_get_list (q);
|
||||
#define g_queue_get_front g_queue_get_list
|
||||
list_end = g_queue_get_back (q);
|
||||
*/
|
||||
|
||||
|
||||
#ifdef HAVE_CONFIG_H
|
||||
# include <config.h>
|
||||
#endif
|
||||
|
||||
#include <glib.h>
|
||||
|
||||
|
||||
|
||||
|
||||
GQueue *
|
||||
g_queue_new (void)
|
||||
{
|
||||
GQueue *q = g_new (GQueue, 1);
|
||||
|
||||
q->list = q->list_end = NULL;
|
||||
q->list_size = 0;
|
||||
|
||||
return q;
|
||||
}
|
||||
|
||||
|
||||
void
|
||||
g_queue_free (GQueue *q)
|
||||
{
|
||||
if (q) {
|
||||
if (q->list)
|
||||
g_list_free (q->list);
|
||||
g_free (q);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
guint
|
||||
g_queue_get_size (GQueue *q)
|
||||
{
|
||||
return (q == NULL) ? 0 : q->list_size;
|
||||
}
|
||||
|
||||
|
||||
void
|
||||
g_queue_push_front (GQueue *q, gpointer data)
|
||||
{
|
||||
if (q) {
|
||||
q->list = g_list_prepend (q->list, data);
|
||||
|
||||
if (q->list_end == NULL)
|
||||
q->list_end = q->list;
|
||||
|
||||
q->list_size++;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
void
|
||||
g_queue_push_back (GQueue *q, gpointer data)
|
||||
{
|
||||
if (q) {
|
||||
q->list_end = g_list_append (q->list_end, data);
|
||||
|
||||
if (! q->list)
|
||||
q->list = q->list_end;
|
||||
else
|
||||
q->list_end = q->list_end->next;
|
||||
|
||||
q->list_size++;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
gpointer
|
||||
g_queue_pop_front (GQueue *q)
|
||||
{
|
||||
gpointer data = NULL;
|
||||
|
||||
if ((q) && (q->list)) {
|
||||
GList *node;
|
||||
|
||||
node = q->list;
|
||||
data = node->data;
|
||||
|
||||
if (! node->next) {
|
||||
q->list = q->list_end = NULL;
|
||||
q->list_size = 0;
|
||||
}
|
||||
else {
|
||||
q->list = node->next;
|
||||
q->list->prev = NULL;
|
||||
q->list_size--;
|
||||
}
|
||||
|
||||
g_list_free_1 (node);
|
||||
}
|
||||
|
||||
return data;
|
||||
}
|
||||
|
||||
|
||||
gpointer
|
||||
g_queue_pop_back (GQueue *q)
|
||||
{
|
||||
gpointer data = NULL;
|
||||
|
||||
if ((q) && (q->list)) {
|
||||
GList *node;
|
||||
|
||||
node = q->list_end;
|
||||
data = node->data;
|
||||
|
||||
if (! node->prev) {
|
||||
q->list = q->list_end = NULL;
|
||||
q->list_size = 0;
|
||||
}
|
||||
else {
|
||||
q->list_end = node->prev;
|
||||
q->list_end->next = NULL;
|
||||
q->list_size--;
|
||||
}
|
||||
|
||||
g_list_free_1 (node);
|
||||
}
|
||||
|
||||
return data;
|
||||
}
|
||||
|
||||
|
63
gstack.c
Normal file
63
gstack.c
Normal file
@ -0,0 +1,63 @@
|
||||
#ifdef HAVE_CONFIG_H
|
||||
# include <config.h>
|
||||
#endif
|
||||
#include <glib.h>
|
||||
|
||||
/*
|
||||
* GStack, opaque ADT with interface:
|
||||
|
||||
stack = g_stack_new ();
|
||||
count = g_stack_size (stack);
|
||||
|
||||
g_stack_push (stack, data);
|
||||
data = g_stack_pop (stack);
|
||||
|
||||
slist = g_stack_get_list (stack);
|
||||
*/
|
||||
|
||||
GStack *
|
||||
g_stack_new (void)
|
||||
{
|
||||
GStack *s;
|
||||
|
||||
s = g_new (GStack, 1);
|
||||
if (!s)
|
||||
return NULL;
|
||||
|
||||
s->list = NULL;
|
||||
|
||||
return s;
|
||||
}
|
||||
|
||||
|
||||
void
|
||||
g_stack_free (GStack *stack)
|
||||
{
|
||||
if (stack) {
|
||||
if (stack->list)
|
||||
g_list_free (stack->list);
|
||||
|
||||
g_free (stack);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
gpointer
|
||||
g_stack_pop (GStack *stack)
|
||||
{
|
||||
gpointer data = NULL;
|
||||
|
||||
if ((stack) && (stack->list)) {
|
||||
GList *node = stack->list;
|
||||
|
||||
stack->list = stack->list->next;
|
||||
|
||||
data = node->data;
|
||||
|
||||
g_list_free_1 (node);
|
||||
}
|
||||
|
||||
return data;
|
||||
}
|
||||
|
||||
|
@ -33,3 +33,5 @@ tree-test
|
||||
dirname-test
|
||||
type-test
|
||||
strfunc-test
|
||||
queue-test
|
||||
stack-test
|
||||
|
@ -7,8 +7,10 @@ TESTS = \
|
||||
hash-test \
|
||||
list-test \
|
||||
node-test \
|
||||
queue-test \
|
||||
relation-test \
|
||||
slist-test \
|
||||
stack-test \
|
||||
string-test \
|
||||
strfunc-test \
|
||||
tree-test \
|
||||
@ -21,8 +23,10 @@ dirname_test_LDADD = $(top_builddir)/libglib.la
|
||||
hash_test_LDADD = $(top_builddir)/libglib.la
|
||||
list_test_LDADD = $(top_builddir)/libglib.la
|
||||
node_test_LDADD = $(top_builddir)/libglib.la
|
||||
queue_test_LDADD = $(top_builddir)/libglib.la
|
||||
relation_test_LDADD = $(top_builddir)/libglib.la
|
||||
slist_test_LDADD = $(top_builddir)/libglib.la
|
||||
stack_test_LDADD = $(top_builddir)/libglib.la
|
||||
string_test_LDADD = $(top_builddir)/libglib.la
|
||||
strfunc_test_LDADD = $(top_builddir)/libglib.la
|
||||
tree_test_LDADD = $(top_builddir)/libglib.la
|
||||
|
83
tests/queue-test.c
Normal file
83
tests/queue-test.c
Normal file
@ -0,0 +1,83 @@
|
||||
#ifdef HAVE_CONFIG_H
|
||||
# include <config.h>
|
||||
#endif
|
||||
#include <glib.h>
|
||||
|
||||
int main()
|
||||
{
|
||||
GQueue *q;
|
||||
|
||||
q = g_queue_new ();
|
||||
|
||||
g_assert (g_queue_empty (q) == TRUE);
|
||||
|
||||
g_queue_push (q, GINT_TO_POINTER (1));
|
||||
g_assert (g_list_length (q->list) == 1);
|
||||
g_queue_push (q, GINT_TO_POINTER (2));
|
||||
g_assert (g_list_length (q->list) == 2);
|
||||
g_queue_push (q, GINT_TO_POINTER (3));
|
||||
g_assert (g_list_length (q->list) == 3);
|
||||
g_queue_push (q, GINT_TO_POINTER (4));
|
||||
g_assert (g_list_length (q->list) == 4);
|
||||
g_queue_push (q, GINT_TO_POINTER (5));
|
||||
g_assert (g_list_length (q->list) == 5);
|
||||
|
||||
g_assert (g_queue_empty (q) == FALSE);
|
||||
|
||||
g_assert (g_queue_index (q, GINT_TO_POINTER (2)) == 1);
|
||||
g_assert (g_queue_index (q, GINT_TO_POINTER (142)) == -1);
|
||||
|
||||
g_assert (g_queue_peek (q) == GINT_TO_POINTER (1));
|
||||
g_assert (g_queue_peek_front (q) == GINT_TO_POINTER (1));
|
||||
g_assert (g_queue_peek_back (q) == GINT_TO_POINTER (5));
|
||||
|
||||
g_assert (g_queue_pop (q) == GINT_TO_POINTER (1));
|
||||
g_assert (g_list_length (q->list) == 4);
|
||||
g_assert (g_queue_pop (q) == GINT_TO_POINTER (2));
|
||||
g_assert (g_list_length (q->list) == 3);
|
||||
g_assert (g_queue_pop (q) == GINT_TO_POINTER (3));
|
||||
g_assert (g_list_length (q->list) == 2);
|
||||
g_assert (g_queue_pop (q) == GINT_TO_POINTER (4));
|
||||
g_assert (g_list_length (q->list) == 1);
|
||||
g_assert (g_queue_pop (q) == GINT_TO_POINTER (5));
|
||||
g_assert (g_list_length (q->list) == 0);
|
||||
g_assert (g_queue_pop (q) == NULL);
|
||||
g_assert (g_list_length (q->list) == 0);
|
||||
g_assert (g_queue_pop (q) == NULL);
|
||||
g_assert (g_list_length (q->list) == 0);
|
||||
|
||||
g_assert (g_queue_empty (q) == TRUE);
|
||||
|
||||
/************************/
|
||||
|
||||
g_queue_push_front (q, GINT_TO_POINTER (1));
|
||||
g_assert (g_list_length (q->list) == 1);
|
||||
g_queue_push_front (q, GINT_TO_POINTER (2));
|
||||
g_assert (g_list_length (q->list) == 2);
|
||||
g_queue_push_front (q, GINT_TO_POINTER (3));
|
||||
g_assert (g_list_length (q->list) == 3);
|
||||
g_queue_push_front (q, GINT_TO_POINTER (4));
|
||||
g_assert (g_list_length (q->list) == 4);
|
||||
g_queue_push_front (q, GINT_TO_POINTER (5));
|
||||
g_assert (g_list_length (q->list) == 5);
|
||||
|
||||
g_assert (g_queue_pop_front (q) == GINT_TO_POINTER (5));
|
||||
g_assert (g_list_length (q->list) == 4);
|
||||
g_assert (g_queue_pop_front (q) == GINT_TO_POINTER (4));
|
||||
g_assert (g_list_length (q->list) == 3);
|
||||
g_assert (g_queue_pop_front (q) == GINT_TO_POINTER (3));
|
||||
g_assert (g_list_length (q->list) == 2);
|
||||
g_assert (g_queue_pop_front (q) == GINT_TO_POINTER (2));
|
||||
g_assert (g_list_length (q->list) == 1);
|
||||
g_assert (g_queue_pop_front (q) == GINT_TO_POINTER (1));
|
||||
g_assert (g_list_length (q->list) == 0);
|
||||
g_assert (g_queue_pop_front (q) == NULL);
|
||||
g_assert (g_list_length (q->list) == 0);
|
||||
g_assert (g_queue_pop_front (q) == NULL);
|
||||
g_assert (g_list_length (q->list) == 0);
|
||||
|
||||
g_queue_free (q);
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
51
tests/stack-test.c
Normal file
51
tests/stack-test.c
Normal file
@ -0,0 +1,51 @@
|
||||
#ifdef HAVE_CONFIG_H
|
||||
# include <config.h>
|
||||
#endif
|
||||
|
||||
#include <glib.h>
|
||||
|
||||
int main()
|
||||
{
|
||||
GStack *s;
|
||||
|
||||
s = g_stack_new ();
|
||||
|
||||
g_assert (g_stack_empty (s) == TRUE);
|
||||
|
||||
g_stack_push (s, GINT_TO_POINTER (1));
|
||||
g_assert (g_list_length (s->list) == 1);
|
||||
g_stack_push (s, GINT_TO_POINTER (2));
|
||||
g_assert (g_list_length (s->list) == 2);
|
||||
g_stack_push (s, GINT_TO_POINTER (3));
|
||||
g_assert (g_list_length (s->list) == 3);
|
||||
g_stack_push (s, GINT_TO_POINTER (4));
|
||||
g_assert (g_list_length (s->list) == 4);
|
||||
g_stack_push (s, GINT_TO_POINTER (5));
|
||||
g_assert (g_list_length (s->list) == 5);
|
||||
|
||||
g_assert (g_stack_index (s, GINT_TO_POINTER (2)) == 3);
|
||||
|
||||
g_assert (g_stack_empty (s) == FALSE);
|
||||
|
||||
g_assert (g_stack_peek (s) == GINT_TO_POINTER (5));
|
||||
|
||||
g_assert (g_stack_pop (s) == GINT_TO_POINTER (5));
|
||||
g_assert (g_list_length (s->list) == 4);
|
||||
g_assert (g_stack_pop (s) == GINT_TO_POINTER (4));
|
||||
g_assert (g_list_length (s->list) == 3);
|
||||
g_assert (g_stack_pop (s) == GINT_TO_POINTER (3));
|
||||
g_assert (g_list_length (s->list) == 2);
|
||||
g_assert (g_stack_pop (s) == GINT_TO_POINTER (2));
|
||||
g_assert (g_list_length (s->list) == 1);
|
||||
g_assert (g_stack_pop (s) == GINT_TO_POINTER (1));
|
||||
g_assert (g_list_length (s->list) == 0);
|
||||
g_assert (g_stack_pop (s) == NULL);
|
||||
g_assert (g_list_length (s->list) == 0);
|
||||
g_assert (g_stack_pop (s) == NULL);
|
||||
g_assert (g_list_length (s->list) == 0);
|
||||
|
||||
g_stack_free (s);
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user