Class: Bitwise

Inherits:
Object
  • Object
show all
Defined in:
lib/bitwise.rb,
ext/bitwise.c

Constant Summary collapse

BITS_TABLE =
(0..255).map do |i|
  (0..7).map do |j|
    j = 7 - j
    ((i & 2**j) > 0) ? (7 - j) : nil
  end.compact
end

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(string = "") ⇒ Bitwise

Returns a new instance of Bitwise.



8
9
10
# File 'lib/bitwise.rb', line 8

def initialize(string = "")
  self.raw = string
end

Instance Attribute Details

#valueObject

Returns the value of attribute value.



6
7
8
# File 'lib/bitwise.rb', line 6

def value
  @value
end

Class Method Details

.population_count(str) ⇒ Object



24
25
26
27
28
29
30
31
32
# File 'ext/bitwise.c', line 24

static VALUE bw_population_count(VALUE self, VALUE str) {
  int count, i;
  unsigned char *buffer = (unsigned char *)RSTRING_PTR(str);
  count = 0;
  for (i = 0; i < RSTRING_LEN(str); i++) {
    count += COUNT_TABLE[buffer[i]];
  }
  return INT2NUM(count);
}

.string_intersect(max, min) ⇒ Object



54
55
56
57
58
59
60
61
62
# File 'ext/bitwise.c', line 54

static VALUE bw_string_intersect(VALUE self, VALUE max, VALUE min)
{
  VALUE result = rb_str_new(RSTRING_PTR(min), RSTRING_LEN(min));
  int i;
  for (i = 0; i < RSTRING_LEN(min); i++) {
    RSTRING_PTR(result)[i] &= RSTRING_PTR(max)[i];
  }
  return result;
}

.string_not(str) ⇒ Object



34
35
36
37
38
39
40
41
42
# File 'ext/bitwise.c', line 34

static VALUE bw_string_not(VALUE self, VALUE str)
{
  VALUE result = rb_str_new(RSTRING_PTR(str), RSTRING_LEN(str));
  int i;
  for (i = 0; i < RSTRING_LEN(str); i++) {
    RSTRING_PTR(result)[i] = ~RSTRING_PTR(str)[i];
  }
  return result;
}

.string_union(max, min) ⇒ Object



44
45
46
47
48
49
50
51
52
# File 'ext/bitwise.c', line 44

static VALUE bw_string_union(VALUE self, VALUE max, VALUE min)
{
  VALUE result = rb_str_new(RSTRING_PTR(max), RSTRING_LEN(max));
  int i;
  for (i = 0; i < RSTRING_LEN(min); i++) {
    RSTRING_PTR(result)[i] |= RSTRING_PTR(min)[i];
  }
  return result;
}

.string_xor(max, min) ⇒ Object



64
65
66
67
68
69
70
71
72
73
# File 'ext/bitwise.c', line 64

static VALUE bw_string_xor(VALUE self, VALUE max, VALUE min)
{
  VALUE result = rb_str_new(RSTRING_PTR(max), RSTRING_LEN(max));
  int i;
  long min_len = RSTRING_LEN(min);
  for (i = 0; i < RSTRING_LEN(max); i++) {
    RSTRING_PTR(result)[i] ^= ((i < min_len) ? RSTRING_PTR(min)[i] : 0);
  }
  return result;
}

Instance Method Details

#assign_max_and_min(other) ⇒ Object



83
84
85
# File 'lib/bitwise.rb', line 83

def assign_max_and_min(other)
  @min, @max = [ self.raw, other.raw ].sort_by{|i| i.bytesize }
end

#bitmaskObject



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

def bitmask
  2**(7 - @mod)
end

#bitsObject



87
88
89
# File 'lib/bitwise.rb', line 87

def bits
  @value.unpack('B*').first
end

#bits=(string) ⇒ Object



91
92
93
94
95
96
# File 'lib/bitwise.rb', line 91

def bits=(string)
  @value = string.scan(/[01]{1,8}/).map do |slice|
    (slice.bytesize == 8 ? slice : (slice + '0' * (8 - slice.bytesize))).to_i(2).chr
  end.join
  @value.bytesize
end

#cardinalityObject



126
127
128
# File 'lib/bitwise.rb', line 126

def cardinality
  Bitwise.population_count(self.raw)
end

#clear_at(index) ⇒ Object



27
28
29
30
# File 'lib/bitwise.rb', line 27

def clear_at(index)
  warn 'Bitwise#clear_at is deprecated. Use Bitwise#unset_at instead.'
  unset_at(index)
end

#clear_at?(index) ⇒ Boolean

Returns:

  • (Boolean)


40
41
42
43
# File 'lib/bitwise.rb', line 40

def clear_at?(index)
  warn 'Bitwise#clear_at? is deprecated. Use Bitwise#unset_at? instead.'
  unset_at?(index)
end

#get_bit(index) ⇒ Object



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

def get_bit(index)
  get_byte(index)
  (@byte & bitmask) > 0 ? 1 : 0
end

#get_byte(index) ⇒ Object

Raises:

  • (IndexError)


54
55
56
57
58
# File 'lib/bitwise.rb', line 54

def get_byte(index)
  @div, @mod = index.divmod(8)
  raise IndexError, 'out of bounds' if @div < 0 or @div >= @value.bytesize
  @byte = @value.getbyte(@div)
end

#indexesObject



116
117
118
119
120
121
122
123
124
# File 'lib/bitwise.rb', line 116

def indexes
  indexes = []
  @value.each_byte.with_index do |c, position|
    BITS_TABLE[c].each do |i|
      indexes << (position*8 + i)
    end
  end
  indexes
end

#indexes=(array) ⇒ Object



107
108
109
110
111
112
113
114
# File 'lib/bitwise.rb', line 107

def indexes=(array)
  max_index = array.max
  @value = "\x00" * (max_index.div(8) + 1)
  array.each do |index|
    set_at(index)
  end
  @value.bytesize
end

#intersect(other) ⇒ Object Also known as: &



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

def intersect(other)
  assign_max_and_min(other)
  Bitwise.new Bitwise.string_intersect(@max, @min)
end

#notObject Also known as: ~



60
61
62
# File 'lib/bitwise.rb', line 60

def not
  Bitwise.new(Bitwise.string_not(self.raw))
end

#rawObject



98
99
100
# File 'lib/bitwise.rb', line 98

def raw
  @value
end

#raw=(string) ⇒ Object



102
103
104
105
# File 'lib/bitwise.rb', line 102

def raw=(string)
  @value = string.force_encoding(Encoding::BINARY)
  @value.bytesize
end

#set_at(index) ⇒ Object



17
18
19
20
# File 'lib/bitwise.rb', line 17

def set_at(index)
  get_byte(index)
  @value.setbyte(@div, @byte | bitmask)
end

#set_at?(index) ⇒ Boolean

Returns:

  • (Boolean)


32
33
34
# File 'lib/bitwise.rb', line 32

def set_at?(index)
  get_bit(index) == 1
end

#sizeObject Also known as: to_s



12
13
14
# File 'lib/bitwise.rb', line 12

def size
  @value.bytesize
end

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



71
72
73
74
# File 'lib/bitwise.rb', line 71

def union(other)
  assign_max_and_min(other)
  Bitwise.new Bitwise.string_union(@max, @min)
end

#unset_at(index) ⇒ Object



22
23
24
25
# File 'lib/bitwise.rb', line 22

def unset_at(index)
  get_byte(index)
  @value.setbyte(@div, @byte  & ~bitmask)
end

#unset_at?(index) ⇒ Boolean

Returns:

  • (Boolean)


36
37
38
# File 'lib/bitwise.rb', line 36

def unset_at?(index)
  get_bit(index) == 0
end

#xor(other) ⇒ Object Also known as: ^



77
78
79
80
# File 'lib/bitwise.rb', line 77

def xor(other)
  assign_max_and_min(other)
  Bitwise.new Bitwise.string_xor(@max, @min)
end