Class: R4r::RingBitsExt

Inherits:
Object
  • Object
show all
Defined in:
ext/r4r/ring_bits_ext/ring_bits_ext.c

Instance Method Summary collapse

Constructor Details

#initialize(capacity) ⇒ Object

Inits a new ring_bits_ext object.

Parameters:

  • capacity (Fixnum)

    a ring bits buffer size.

Raises:

  • (ArgumentError)

    if capacity is negative



54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
# File 'ext/r4r/ring_bits_ext/ring_bits_ext.c', line 54

static VALUE
ring_bits_ext_init(VALUE self, VALUE capacity) {
  struct ring_bits_ext *ptr;
  int ring_bits_ext_size = NUM2INT(capacity);

  if (0 >= ring_bits_ext_size) {
    rb_raise(rb_eArgError, "ring bit's size must be positive, got %i", ring_bits_ext_size);
    return Qnil;
  }

  size_t count_of_words_required = ring_bits_ext_word_index(ring_bits_ext_size - 1) + 1;

  Data_Get_Struct(self, struct ring_bits_ext, ptr);
  ptr->size = count_of_words_required << ADDRESS_BITS_PER_WORD;
  ptr->words = calloc(count_of_words_required, sizeof(uint64_t));

  return self;
}

Instance Method Details

#get(bit_index_value) ⇒ Object

Gets the bit at the specified index.

Parameters:

  • bit_index_value (Fixnum)

    a bit index

Returns:

  • state of bit_index that can be true or false

Raises:

  • (ArgumentError)

    if the specified index is negative



143
144
145
146
147
148
149
150
151
152
153
154
155
# File 'ext/r4r/ring_bits_ext/ring_bits_ext.c', line 143

static VALUE
ring_bits_ext_get(VALUE self, VALUE bit_index_value) {
  struct ring_bits_ext *ptr;
  Data_Get_Struct(self, struct ring_bits_ext, ptr);

  int bit_index = NUM2INT(bit_index_value);
  if (0 > bit_index) {
    rb_raise(rb_eArgError, "ring bit's index must be positive, got %i", bit_index);
  }

  bool value = _ring_bits_ext_get(ptr, bit_index);
  return value ? Qtrue : Qfalse;
}

#set(bit_index_value, value) ⇒ Object

Sets the bit at the specified index to value.

Parameters:

  • bit_index_value (Fixnum)

    a bit index

Returns:

  • previous state of bit_index that can be true or false

Raises:

  • (ArgumentError)

    if the specified index is negative



110
111
112
113
114
115
116
117
118
119
120
121
122
# File 'ext/r4r/ring_bits_ext/ring_bits_ext.c', line 110

static VALUE
ring_bits_ext_set(VALUE self, VALUE bit_index_value, VALUE value) {
  struct ring_bits_ext *ptr;
  Data_Get_Struct(self, struct ring_bits_ext, ptr);

  int bit_index = NUM2INT(bit_index_value);
  if (0 > bit_index) {
    rb_raise(rb_eArgError, "ring bit's index must be positive, got %i", bit_index);
  }

  bool previous = _ring_bits_ext_set(ptr, bit_index, value == Qtrue);
  return previous ? Qtrue : Qfalse;
}

#sizeObject

Returns an actual ring bits capacity.



76
77
78
79
80
81
# File 'ext/r4r/ring_bits_ext/ring_bits_ext.c', line 76

static VALUE
ring_bits_ext_get_size(VALUE self) {
  struct ring_bits_ext *ptr;
  Data_Get_Struct(self, struct ring_bits_ext, ptr);
  return SIZET2NUM(ptr->size);
}