class Containers::CDeque

  1. ext/containers/bst/bst.c
Parent: Containers

Public Instance Aliases

each -> each_forward
length -> size
reverse_each -> each_backward

Public Class methods

new (*args)
[show source]
static VALUE deque_init(int argc, VALUE *argv, VALUE self)
{
        long len, i;
        VALUE ary;
        
        if(argc == 0) {
                return self;
        }
        else if(argc > 1) {
                rb_raise(rb_eArgError, "wrong number of arguments");
        }
        else {
                ary = rb_check_array_type(argv[0]);
                if(!NIL_P(ary)) {
                        len = RARRAY_LEN(ary);
                        for (i = 0; i < len; i++) {
                                deque_push_back(self, RARRAY_PTR(ary)[i]);
                        }
                }
        }
        return self;
}

Public Instance methods

back ()
[show source]
static VALUE deque_back(VALUE self) {
        deque *deque = get_deque_from_self(self);
        if(deque->back)
                return deque->back->obj;
        
        return Qnil;
}
clear ()
[show source]
static VALUE deque_clear(VALUE self) {
        deque *deque = get_deque_from_self(self);
        clear_deque(deque);
        return Qnil;
}
each_backward ()
[show source]
static VALUE deque_each_backward(VALUE self) {
        deque *deque = get_deque_from_self(self);
        deque_node *node = deque->back;
        while(node) {
                rb_yield(node->obj);
                node = node->left;
        }
        return self;
}
each_forward ()
[show source]
static VALUE deque_each_forward(VALUE self) {
        deque *deque = get_deque_from_self(self);
        deque_node *node = deque->front;
        while(node) {
                rb_yield(node->obj);
                node = node->right;
        }
        return self;
}
empty? ()
[show source]
static VALUE deque_is_empty(VALUE self) {
        deque *deque = get_deque_from_self(self);
        return (deque->size == 0) ? Qtrue : Qfalse;
}
front ()
[show source]
static VALUE deque_front(VALUE self) {
        deque *deque = get_deque_from_self(self);
        if(deque->front)
                return deque->front->obj;
        
        return Qnil;
}
pop_back ()
[show source]
static VALUE deque_pop_back(VALUE self) {
        deque *deque = get_deque_from_self(self);
        VALUE obj;
        if(!deque->back)
                return Qnil;
        deque_node *node = deque->back;
        obj = node->obj;
        if(deque->size == 1) {
                clear_deque(deque);
                return obj;
        }
        deque->back->left->right = NULL;
        deque->back = deque->back->left;
        deque->size--;
        return obj;
}
pop_front ()
[show source]
static VALUE deque_pop_front(VALUE self) {
        deque *deque = get_deque_from_self(self);
        VALUE obj;
        if(!deque->front)
                return Qnil;
        deque_node *node = deque->front;
        obj = node->obj;
        if(deque->size == 1) {
                clear_deque(deque);
                return obj;
        }
        deque->front->right->left = NULL;
        deque->front = deque->front->right;
        deque->size--;
        return obj;
}
push_back (p1)
[show source]
static VALUE deque_push_back(VALUE self, VALUE obj) {
        deque *deque = get_deque_from_self(self);
        deque_node *node = create_node(obj);
        if(deque->back) {
                node->left = deque->back;
                deque->back->right = node;
                deque->back = node; 
        }
        else {
                deque->front = node;
                deque->back = node;
        }
        deque->size++;
        return obj;
}
push_front (p1)
[show source]
static VALUE deque_push_front(VALUE self, VALUE obj) {
        deque *deque = get_deque_from_self(self);
        deque_node *node = create_node(obj);
        if(deque->front) {
                node->right = deque->front;
                deque->front->left = node;
                deque->front = node; 
        }
        else {
                deque->front = node;
                deque->back = node;
        }
        deque->size++;
        return obj;
}
size ()
[show source]
static VALUE deque_size(VALUE self) {
        deque *deque = get_deque_from_self(self);
        return INT2NUM(deque->size);
}