Class: RedShift::DVector

Inherits:
Object show all
Includes:
Enumerable
Defined in:
ext/redshift/dvector/dvector.rb

Overview

A linear collection of objects, like Array.

Intended primarily for access from C code, using the inline rs_dv_push() and rs_dv_pop() functions.

Implements some of the same methods as Array, but not all.

Like an Array, a DVector grows implicitly as elements are pushed. But unlike an Array, a DVector shrinks only explicitly. This is to minimize realloc() calls when a DVector rapidly grows and shrinks.

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(elts = nil) ⇒ DVector

Returns a new instance of DVector.



23
24
25
# File 'ext/redshift/dvector/dvector.rb', line 23

def initialize(elts=nil)
  push(*elts) if elts
end

Class Method Details

.[](*elts) ⇒ Object



19
20
21
# File 'ext/redshift/dvector/dvector.rb', line 19

def self.[](*elts)
  new elts
end

Instance Method Details

#==(other) ⇒ Object



121
122
123
124
125
126
127
128
129
130
131
132
133
134
# File 'ext/redshift/dvector/dvector.c', line 121

static VALUE dv_method_equal(VALUE self, VALUE other)
{
    RS_DVector *dv1, *dv2;

    if (self == other) return Qtrue;
    if (CLASS_OF(self) != CLASS_OF(other)) return Qfalse;

    Data_Get_Struct(self, RS_DVector, dv1);
    Data_Get_Struct(other, RS_DVector, dv2);
    if (dv1->len != dv2->len) return Qfalse;
    if (dv1->len == 0) return Qtrue;

    return rb_exec_recursive(recursive_equal, self, other);
}

#_dump_dataObject



193
194
195
196
197
198
# File 'ext/redshift/dvector/dvector.c', line 193

static VALUE dv_method_dump_data(VALUE self)
{
    RS_DVector *dv;
    Data_Get_Struct(self, RS_DVector, dv);
    return dv->ptr ? rb_ary_new4(dv->len, dv->ptr) : rb_ary_new();
}

#_load_data(from_array) ⇒ Object



200
201
202
203
204
205
206
207
208
209
210
211
# File 'ext/redshift/dvector/dvector.c', line 200

static VALUE dv_method_load_data(VALUE self, VALUE from_array)
{
    long i;
    RS_DVector *dv;
    Data_Get_Struct(self, RS_DVector, dv);

    for (i=0; i < RARRAY_LEN(from_array); i++) {
        rs_dv_push(dv, RARRAY_PTR(from_array)[i]);
    }
    
    return self;
}

#dupObject



30
31
32
# File 'ext/redshift/dvector/dvector.rb', line 30

def dup
  self.class.new to_a
end

#eachObject



76
77
78
79
80
81
82
83
84
85
86
87
88
89
# File 'ext/redshift/dvector/dvector.c', line 76

static VALUE dv_method_each(VALUE self)
{
    long i;
    RS_DVector *dv;
    
    Data_Get_Struct(self, RS_DVector, dv);

    RETURN_ENUMERATOR(self, 0, 0);
    for (i=0; i < dv->len; i++) {
	rb_yield(dv->ptr[i]);
    }
    
    return self;
}

#eql?(other) ⇒ Boolean

Returns:

  • (Boolean)


152
153
154
155
156
157
158
159
160
161
162
163
164
165
# File 'ext/redshift/dvector/dvector.c', line 152

static VALUE dv_method_eql(VALUE self, VALUE other)
{
    RS_DVector *dv1, *dv2;

    if (self == other) return Qtrue;
    if (CLASS_OF(self) != CLASS_OF(other)) return Qfalse;

    Data_Get_Struct(self, RS_DVector, dv1);
    Data_Get_Struct(other, RS_DVector, dv2);
    if (dv1->len != dv2->len) return Qfalse;
    if (dv1->len == 0) return Qtrue;

    return rb_exec_recursive(recursive_eql, self, other);
}

#hashObject



188
189
190
191
# File 'ext/redshift/dvector/dvector.c', line 188

static VALUE dv_method_hash(VALUE self)
{
    return rb_exec_recursive(recursive_hash, self, 0);
}

#inspectObject



27
# File 'ext/redshift/dvector/dvector.rb', line 27

def inspect; to_a.inspect; end

#lengthObject Also known as: size



98
99
100
101
102
103
# File 'ext/redshift/dvector/dvector.c', line 98

static VALUE dv_method_length(VALUE self)
{
    RS_DVector *dv;
    Data_Get_Struct(self, RS_DVector, dv);
    return INT2NUM(dv->len);
}

#popObject



67
68
69
70
71
72
73
74
# File 'ext/redshift/dvector/dvector.c', line 67

static VALUE dv_method_pop(VALUE self)
{
    RS_DVector *dv;
    
    Data_Get_Struct(self, RS_DVector, dv);

    return rs_dv_pop(dv);    
}

#push(*args) ⇒ Object Also known as: <<



53
54
55
56
57
58
59
60
61
62
63
64
65
# File 'ext/redshift/dvector/dvector.c', line 53

static VALUE dv_method_push(int argc, VALUE *argv, VALUE self)
{
    int i;
    RS_DVector *dv;
    
    Data_Get_Struct(self, RS_DVector, dv);
    
    for (i = 0; i < argc; i++) {
        rs_dv_push(dv, argv[i]);
    }

    return self;
}

#to_aObject



91
92
93
94
95
96
# File 'ext/redshift/dvector/dvector.c', line 91

static VALUE dv_method_to_a(VALUE self)
{
    RS_DVector *dv;
    Data_Get_Struct(self, RS_DVector, dv);
    return dv->ptr ? rb_ary_new4(dv->len, dv->ptr) : rb_ary_new();
}

#to_sObject



28
# File 'ext/redshift/dvector/dvector.rb', line 28

def to_s; to_a.to_s; end