Class: Cosmos::LowFragmentationArray

Inherits:
Array show all
Defined in:
ext/cosmos/ext/low_fragmentation_array/low_fragmentation_array.c

Instance Method Summary collapse

Methods inherited from Array

#as_json, #clone_to_f, #delete_item, #histogram, #includes_item?, #index_gt_eq, #index_lt_eq, #inspect, #max_with_index, #mean, #min_with_index, #nearest_index, #old_inspect, #range_containing, #range_within, #squared, #sum

Constructor Details

#initializeObject

Allocates space for an array but leaves the array length at 0



117
118
119
120
121
122
# File 'ext/cosmos/ext/low_fragmentation_array/low_fragmentation_array.c', line 117

static VALUE initialize (VALUE self, VALUE size)
{
  rb_call_super(1, &size);
  ARY_SET_LEN(self, 0);
  return self;
}

Instance Method Details

#[](*args) ⇒ Object

Array reference - creates new memory for range rather than shared object Note: Almost exactly the same as the implementation from Ruby 1.9.2 p0



163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
# File 'ext/cosmos/ext/low_fragmentation_array/low_fragmentation_array.c', line 163

static VALUE ary_aref(int argc, VALUE *argv, VALUE ary)
{
  volatile VALUE arg;
  long beg, len;

  if (argc == 2)
  {
    beg = NUM2LONG(argv[0]);
    len = NUM2LONG(argv[1]);
    if (beg < 0)
    {
      beg += RARRAY_LEN(ary);
    }
    return my_ary_subseq(ary, beg, len);
  }
  if (argc != 1)
  {
    rb_scan_args(argc, argv, "11", 0, 0);
  }
  arg = argv[0];
  /* special case - speeding up */
  if (FIXNUM_P(arg))
  {
    return rb_ary_entry(ary, FIX2LONG(arg));
  }
  /* check if idx is Range */
  switch (rb_range_beg_len(arg, &beg, &len, RARRAY_LEN(ary), 0))
  {
    case Qfalse:
      break;
    case Qnil:
      return Qnil;
    default:
      return my_ary_subseq(ary, beg, len);
  }
  return rb_ary_entry(ary, NUM2LONG(arg));
}

#capacityObject

Returns the size of memory allocated for the array



236
237
238
239
# File 'ext/cosmos/ext/low_fragmentation_array/low_fragmentation_array.c', line 236

static VALUE capacity (VALUE self)
{
  return LONG2NUM(ARY_CAPA(self));
}

#pointerObject

Returns the array’s pointer



244
245
246
247
248
249
250
251
# File 'ext/cosmos/ext/low_fragmentation_array/low_fragmentation_array.c', line 244

static VALUE pointer (VALUE self)
{
#if __x86_64__
  return LL2NUM((long long) RARRAY_PTR(self));
#else
  return LONG2NUM((long) RARRAY_PTR(self));
#endif
}

#remove_before!Object

Removes values before an index and shifts old values down without fragmenting memory by reallocating memory.



205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
# File 'ext/cosmos/ext/low_fragmentation_array/low_fragmentation_array.c', line 205

static VALUE remove_before_bang (VALUE self, VALUE index)
{
  int int_index = FIX2INT(index);
  long new_length = 0;

  if (int_index < 0)
  {
    int_index += RARRAY_LEN(self);
    if (int_index < 0)
    {
      return self;
    }
  }

  if (RARRAY_LEN(self) > int_index)
  {
    MEMMOVE(RARRAY_PTR(self), RARRAY_PTR(self) + int_index, VALUE, RARRAY_LEN(self) - int_index);
    new_length = RARRAY_LEN(self) - int_index;
    ARY_SET_LEN(self, new_length);
  }
  else
  {
    ARY_SET_LEN(self, 0);
  }

  return self;
}