Re: new gstack, gqueue



Jeff Garzik wrote:
> 
> The following are two new ADTs I would like to add to Glib.  Are there
> any comments or suggestions before the initial commit?
> 
> Code is in the GLIB_1_3_HACKS branch.
> 
> Thanks,
> 
>         Jeff
> 
> /* Stacks
>  */
> GStack * g_stack_new    (void);
> void     g_stack_free   (GStack *stack);
> void     _g_stack_push  (GStack *stack, gpointer data);
> gpointer g_stack_pop    (GStack *stack);
> 
> #if G_STACK_MACROIZE
> #  define g_stack_push(stack) G_STMT_START {                            \
>             if ((GStack *)(stack))                                      \
>               ((GStack *)(stack))->list =                               \
>                   g_list_prepend (((GStack *)(stack))->list, data);     \
>           } G_STMT_END
> #else
> #  define  g_stack_push _g_stack_push
> #endif


Looking at the Java Stack API, you may want to add:

 gpointer g_stack_peek     (GStack *stack);
 gboolean g_stack_is_empty (void);
 gint     g_stack_search   (gpointer data);
    (Returns the position of data in the stack, or -1 if not found)


(Also, since you don't need to insert elements into the middle of a stack,
a GPtrArray implementation may be more efficient.)


> 
> /* 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_push            g_queue_push_back
> #define         g_queue_pop             g_queue_pop_front


Similar functions may be useful here as well.

Damon



[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]