Class: RedShift::DVectorFloat

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

Overview

A linear collection of single-precision floats.

Intended primarily for access from C code, using the inline rs_dvf_push() and rs_dvf_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) ⇒ DVectorFloat

Returns a new instance of DVectorFloat.



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

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

Class Method Details

.[](*elts) ⇒ Object



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

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

Instance Method Details

#==(other) ⇒ Object



101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
# File 'ext/redshift/dvector-float/dvector-float.c', line 101

static VALUE dvf_method_equal(VALUE self, VALUE other)
{
    int i;
    RS_DVectorFloat *dvf1, *dvf2;

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

    Data_Get_Struct(self, RS_DVectorFloat, dvf1);
    Data_Get_Struct(other, RS_DVectorFloat, dvf2);
    if (dvf1->len != dvf2->len) return Qfalse;

    for (i=0; i < dvf1->len; i++) {
        if (dvf1->ptr[i] != dvf2->ptr[i]) return Qfalse;
    }
    return Qtrue;
}

#_dump_dataObject



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

static VALUE dvf_method_to_a(VALUE self)
{
    int i;
    RS_DVectorFloat *dvf;
    VALUE ary;
    
    Data_Get_Struct(self, RS_DVectorFloat, dvf);

    ary = rb_ary_new();
    if (!dvf->ptr) return ary;

    for (i=0; i < dvf->len; i++) {
	rb_ary_push(ary, rb_float_new(dvf->ptr[i]));
    }
    
    return ary;
}

#_load_data(from_array) ⇒ Object



148
149
150
151
152
153
154
155
156
157
158
159
# File 'ext/redshift/dvector-float/dvector-float.c', line 148

static VALUE dvf_method_load_data(VALUE self, VALUE from_array)
{
    long i;
    RS_DVectorFloat *dvf;
    Data_Get_Struct(self, RS_DVectorFloat, dvf);

    for (i=0; i < RARRAY_LEN(from_array); i++) {
        rs_dvf_push(dvf, NUM2DBL(RARRAY_PTR(from_array)[i]));
    }
    
    return self;
}

#dupObject



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

def dup
  self.class.new to_a
end

#eachObject



61
62
63
64
65
66
67
68
69
70
71
72
73
74
# File 'ext/redshift/dvector-float/dvector-float.c', line 61

static VALUE dvf_method_each(VALUE self)
{
    long i;
    RS_DVectorFloat *dvf;
    
    Data_Get_Struct(self, RS_DVectorFloat, dvf);

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

#eql?(other) ⇒ Boolean

Returns:

  • (Boolean)


101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
# File 'ext/redshift/dvector-float/dvector-float.c', line 101

static VALUE dvf_method_equal(VALUE self, VALUE other)
{
    int i;
    RS_DVectorFloat *dvf1, *dvf2;

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

    Data_Get_Struct(self, RS_DVectorFloat, dvf1);
    Data_Get_Struct(other, RS_DVectorFloat, dvf2);
    if (dvf1->len != dvf2->len) return Qfalse;

    for (i=0; i < dvf1->len; i++) {
        if (dvf1->ptr[i] != dvf2->ptr[i]) return Qfalse;
    }
    return Qtrue;
}

#hashObject



119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
# File 'ext/redshift/dvector-float/dvector-float.c', line 119

static VALUE dvf_method_hash(VALUE self)
{
    long i, h;
    RS_DVectorFloat *dvf;

    Data_Get_Struct(self, RS_DVectorFloat, dvf);

    h = dvf->len;
    for (i=0; i < dvf->len; i++) {
        int hash;
        unsigned int j;
        char *c;
        float f;
        
	h = (h << 1) | (h<0 ? 1 : 0);
        f = dvf->ptr[i];

        if (f == 0) f = fabs(f);
        c = (char*)&f;
        for (hash=0, j=0; j<sizeof(float); j++) {
	    hash = (hash * 971) ^ (unsigned char)c[j];
        }
        if (hash < 0) hash = -hash;

	h ^= hash;
    }
    return LONG2FIX(h);
}

#inspectObject



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

def inspect; to_a.inspect; end

#lengthObject Also known as: size



94
95
96
97
98
99
# File 'ext/redshift/dvector-float/dvector-float.c', line 94

static VALUE dvf_method_length(VALUE self)
{
    RS_DVectorFloat *dvf;
    Data_Get_Struct(self, RS_DVectorFloat, dvf);
    return INT2NUM(dvf->len);
}

#popObject



52
53
54
55
56
57
58
59
# File 'ext/redshift/dvector-float/dvector-float.c', line 52

static VALUE dvf_method_pop(VALUE self)
{
    RS_DVectorFloat *dvf;
    
    Data_Get_Struct(self, RS_DVectorFloat, dvf);

    return dvf->len == 0 ? Qnil : rb_float_new(rs_dvf_pop(dvf));
}

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



38
39
40
41
42
43
44
45
46
47
48
49
50
# File 'ext/redshift/dvector-float/dvector-float.c', line 38

static VALUE dvf_method_push(int argc, VALUE *argv, VALUE self)
{
    int i;
    RS_DVectorFloat *dvf;
    
    Data_Get_Struct(self, RS_DVectorFloat, dvf);
    
    for (i = 0; i < argc; i++) {
        rs_dvf_push(dvf, NUM2DBL(argv[i]));
    }

    return self;
}

#to_aObject



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

static VALUE dvf_method_to_a(VALUE self)
{
    int i;
    RS_DVectorFloat *dvf;
    VALUE ary;
    
    Data_Get_Struct(self, RS_DVectorFloat, dvf);

    ary = rb_ary_new();
    if (!dvf->ptr) return ary;

    for (i=0; i < dvf->len; i++) {
	rb_ary_push(ary, rb_float_new(dvf->ptr[i]));
    }
    
    return ary;
}

#to_sObject



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

def to_s; to_a.to_s; end