Class: Roaring::Bitmap

Inherits:
Object
  • Object
show all
Includes:
Enumerable
Defined in:
lib/roaring.rb,
ext/roaring/cext.c

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(enum = nil) ⇒ Bitmap

Returns a new instance of Bitmap.



22
23
24
25
26
27
28
29
30
# File 'lib/roaring.rb', line 22

def initialize(enum = nil)
  return unless enum

  if enum.instance_of?(Roaring::Bitmap)
    initialize_copy(enum)
  else
    enum.each { |x| self << x }
  end
end

Class Method Details

.[](*args) ⇒ Object



32
33
34
35
36
37
38
39
40
# File 'lib/roaring.rb', line 32

def self.[](*args)
  if args.size == 0
    new
  elsif args.size == 1 && !(Integer === args[0])
    new(args[0])
  else
    new(args)
  end
end

._load(args) ⇒ Object



54
55
56
# File 'lib/roaring.rb', line 54

def self._load args
  deserialize(args)
end

.deserialize(str) ⇒ Object



174
175
176
177
178
179
# File 'ext/roaring/cext.c', line 174

static VALUE rb_roaring_deserialize(VALUE self, VALUE str)
{
    roaring_bitmap_t *bitmap = roaring_bitmap_portable_deserialize_safe(RSTRING_PTR(str), RSTRING_LEN(str));

    return TypedData_Wrap_Struct(cRoaringBitmap, &roaring_type, bitmap);
}

Instance Method Details

#&(other) ⇒ Object



207
208
209
210
# File 'ext/roaring/cext.c', line 207

static VALUE rb_roaring_and(VALUE self, VALUE other)
{
    return rb_roaring_binary_op(self, other, roaring_bitmap_and);
}

#-(other) ⇒ Object



222
223
224
225
# File 'ext/roaring/cext.c', line 222

static VALUE rb_roaring_andnot(VALUE self, VALUE other)
{
    return rb_roaring_binary_op(self, other, roaring_bitmap_andnot);
}

#<(other) ⇒ Object



232
233
234
235
# File 'ext/roaring/cext.c', line 232

static VALUE rb_roaring_lt(VALUE self, VALUE other)
{
    return rb_roaring_binary_op_bool(self, other, roaring_bitmap_is_strict_subset);
}

#<<(val) ⇒ Object



68
69
70
71
72
73
74
75
76
# File 'ext/roaring/cext.c', line 68

static VALUE rb_roaring_add(VALUE self, VALUE val)
{
    roaring_bitmap_t *data;
    TypedData_Get_Struct(self, roaring_bitmap_t, &roaring_type, data);

    uint32_t num = NUM2UINT32(val);
    roaring_bitmap_add(data, num);
    return self;
}

#<=(other) ⇒ Object



237
238
239
240
# File 'ext/roaring/cext.c', line 237

static VALUE rb_roaring_lte(VALUE self, VALUE other)
{
    return rb_roaring_binary_op_bool(self, other, roaring_bitmap_is_subset);
}

#==(other) ⇒ Object



227
228
229
230
# File 'ext/roaring/cext.c', line 227

static VALUE rb_roaring_eq(VALUE self, VALUE other)
{
    return rb_roaring_binary_op_bool(self, other, roaring_bitmap_equals);
}

#>(other) ⇒ Object



42
43
44
# File 'lib/roaring.rb', line 42

def >(other)
  other < self
end

#>=(other) ⇒ Object



46
47
48
# File 'lib/roaring.rb', line 46

def >=(other)
  other <= self
end

#[](rankv) ⇒ Object



118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
# File 'ext/roaring/cext.c', line 118

static VALUE rb_roaring_aref(VALUE self, VALUE rankv)
{
    roaring_bitmap_t *data;
    TypedData_Get_Struct(self, roaring_bitmap_t, &roaring_type, data);

    uint32_t rank = NUM2UINT32(rankv);
    uint32_t val;

    if (roaring_bitmap_select(data, rank, &val)) {
        return UINT2NUM(val);
    } else {
        return Qnil;
    }
    return self;
}

#^(other) ⇒ Object



217
218
219
220
# File 'ext/roaring/cext.c', line 217

static VALUE rb_roaring_xor(VALUE self, VALUE other)
{
    return rb_roaring_binary_op(self, other, roaring_bitmap_xor);
}

#_dump(level) ⇒ Object



50
51
52
# File 'lib/roaring.rb', line 50

def _dump level
  serialize
end

#add(val) ⇒ Object



68
69
70
71
72
73
74
75
76
# File 'ext/roaring/cext.c', line 68

static VALUE rb_roaring_add(VALUE self, VALUE val)
{
    roaring_bitmap_t *data;
    TypedData_Get_Struct(self, roaring_bitmap_t, &roaring_type, data);

    uint32_t num = NUM2UINT32(val);
    roaring_bitmap_add(data, num);
    return self;
}

#cardinalityObject Also known as: size, length, count



59
60
61
62
63
64
65
66
# File 'ext/roaring/cext.c', line 59

static VALUE rb_roaring_cardinality(VALUE self)
{

    roaring_bitmap_t *data;
    TypedData_Get_Struct(self, roaring_bitmap_t, &roaring_type, data);
    uint64_t cardinality = roaring_bitmap_get_cardinality(data);
    return ULONG2NUM(cardinality);
}

#clearObject



95
96
97
98
99
100
101
102
# File 'ext/roaring/cext.c', line 95

static VALUE rb_roaring_clear(VALUE self)
{
    roaring_bitmap_t *data;
    TypedData_Get_Struct(self, roaring_bitmap_t, &roaring_type, data);

    roaring_bitmap_clear(data);
    return self;
}

#eachObject

iterate till the end



109
110
111
112
113
114
115
116
# File 'ext/roaring/cext.c', line 109

static VALUE rb_roaring_each(VALUE self)
{
    roaring_bitmap_t *data;
    TypedData_Get_Struct(self, roaring_bitmap_t, &roaring_type, data);

    roaring_iterate(data, rb_roaring_each_i, NULL);
    return self;
}

#empty?Boolean

Returns:

  • (Boolean)


87
88
89
90
91
92
93
# File 'ext/roaring/cext.c', line 87

static VALUE rb_roaring_empty_p(VALUE self)
{
    roaring_bitmap_t *data;
    TypedData_Get_Struct(self, roaring_bitmap_t, &roaring_type, data);

    return roaring_bitmap_is_empty(data) ? Qtrue : Qfalse;
}

#include?(val) ⇒ Boolean

Returns:

  • (Boolean)


78
79
80
81
82
83
84
85
# File 'ext/roaring/cext.c', line 78

static VALUE rb_roaring_include_p(VALUE self, VALUE val)
{
    roaring_bitmap_t *data;
    TypedData_Get_Struct(self, roaring_bitmap_t, &roaring_type, data);

    uint32_t num = NUM2UINT32(val);
    return roaring_bitmap_contains(data, num) ? Qtrue : Qfalse;
}

#initialize_copy(other) ⇒ Object



47
48
49
50
51
52
53
54
55
56
57
# File 'ext/roaring/cext.c', line 47

static VALUE rb_roaring_initialize_copy(VALUE self, VALUE other) {
    roaring_bitmap_t *self_data;
    TypedData_Get_Struct(self, roaring_bitmap_t, &roaring_type, self_data);

    roaring_bitmap_t *other_data;
    TypedData_Get_Struct(other, roaring_bitmap_t, &roaring_type, other_data);

    roaring_bitmap_overwrite(self_data, other_data);

    return self;
}

#inspectObject



66
67
68
# File 'lib/roaring.rb', line 66

def inspect
  "#<#{self.class} cardinality=#{cardinality}>"
end

#maxObject Also known as: last



143
144
145
146
147
148
149
150
# File 'ext/roaring/cext.c', line 143

static VALUE rb_roaring_max(VALUE self)
{
    roaring_bitmap_t *data;
    TypedData_Get_Struct(self, roaring_bitmap_t, &roaring_type, data);

    uint32_t val = roaring_bitmap_maximum(data);
    return UINT2NUM(val);
}

#minObject Also known as: first



134
135
136
137
138
139
140
141
# File 'ext/roaring/cext.c', line 134

static VALUE rb_roaring_min(VALUE self)
{
    roaring_bitmap_t *data;
    TypedData_Get_Struct(self, roaring_bitmap_t, &roaring_type, data);

    uint32_t val = roaring_bitmap_minimum(data);
    return UINT2NUM(val);
}

#run_optimizeObject



152
153
154
155
156
157
158
# File 'ext/roaring/cext.c', line 152

static VALUE rb_roaring_run_optimize(VALUE self)
{
    roaring_bitmap_t *data;
    TypedData_Get_Struct(self, roaring_bitmap_t, &roaring_type, data);

    return roaring_bitmap_run_optimize(data) ? Qtrue : Qfalse;
}

#serializeObject



160
161
162
163
164
165
166
167
168
169
170
171
172
# File 'ext/roaring/cext.c', line 160

static VALUE rb_roaring_serialize(VALUE self)
{
    roaring_bitmap_t *data;
    TypedData_Get_Struct(self, roaring_bitmap_t, &roaring_type, data);

    size_t size = roaring_bitmap_portable_size_in_bytes(data);
    VALUE str = rb_str_buf_new(size);

    size_t written = roaring_bitmap_portable_serialize(data, RSTRING_PTR(str));
    rb_str_set_len(str, written);

    return str;
}

#to_aObject



58
59
60
# File 'lib/roaring.rb', line 58

def to_a
  map(&:itself)
end

#to_setObject



62
63
64
# File 'lib/roaring.rb', line 62

def to_set
  ::Set.new(to_a)
end

#|(other) ⇒ Object Also known as: +



212
213
214
215
# File 'ext/roaring/cext.c', line 212

static VALUE rb_roaring_or(VALUE self, VALUE other)
{
    return rb_roaring_binary_op(self, other, roaring_bitmap_or);
}