Class: Algorithms::Containers::CDeque

Inherits:
Object
  • Object
show all
Defined in:
ext/containers/deque/deque.c

Instance Method Summary collapse

Constructor Details

#initialize(*args) ⇒ Object



201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
# File 'ext/containers/deque/deque.c', line 201

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;
}

Instance Method Details

#backObject



140
141
142
143
144
145
146
# File 'ext/containers/deque/deque.c', line 140

static VALUE deque_back(VALUE self) {
	deque *deque = get_deque_from_self(self);
	if(deque->back)
		return deque->back->obj;
	
	return Qnil;
}

#clearObject



165
166
167
168
169
# File 'ext/containers/deque/deque.c', line 165

static VALUE deque_clear(VALUE self) {
	deque *deque = get_deque_from_self(self);
	clear_deque(deque);
	return Qnil;
}

#each_backwardObject Also known as: reverse_each



191
192
193
194
195
196
197
198
199
# File 'ext/containers/deque/deque.c', line 191

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_forwardObject Also known as: each



181
182
183
184
185
186
187
188
189
# File 'ext/containers/deque/deque.c', line 181

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?Boolean

Returns:

  • (Boolean)


176
177
178
179
# File 'ext/containers/deque/deque.c', line 176

static VALUE deque_is_empty(VALUE self) {
	deque *deque = get_deque_from_self(self);
	return (deque->size == 0) ? Qtrue : Qfalse;
}

#frontObject



132
133
134
135
136
137
138
# File 'ext/containers/deque/deque.c', line 132

static VALUE deque_front(VALUE self) {
	deque *deque = get_deque_from_self(self);
	if(deque->front)
		return deque->front->obj;
	
	return Qnil;
}

#pop_backObject



148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
# File 'ext/containers/deque/deque.c', line 148

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_frontObject



115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
# File 'ext/containers/deque/deque.c', line 115

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(obj) ⇒ Object



99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
# File 'ext/containers/deque/deque.c', line 99

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(obj) ⇒ Object



83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
# File 'ext/containers/deque/deque.c', line 83

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;
}

#sizeObject Also known as: length



171
172
173
174
# File 'ext/containers/deque/deque.c', line 171

static VALUE deque_size(VALUE self) {
	deque *deque = get_deque_from_self(self);
	return INT2NUM(deque->size);
}