Module: DiverDown::Web::BitId

Defined in:
lib/diver_down/web/bit_id.rb

Overview

Use bitflag for a lightweight representation of a list of bound definitions on a URL. Each definition is related with a unique bitflag, and when merging, the bitflag is calculated with ‘OR` to represent the merged definitions in a lightweight and fast.

Class Method Summary collapse

Class Method Details

.bit_id_to_ids(bit_id) ⇒ Array<Integer>

Parameters:

  • id (Integer)

Returns:

  • (Array<Integer>)


17
18
19
20
21
22
23
24
25
26
27
28
29
# File 'lib/diver_down/web/bit_id.rb', line 17

def bit_id_to_ids(bit_id)
  ids = []
  shift = 0
  while bit_id.positive?
    if (bit_id & 1) == 1
      ids.push(shift + 1)
    end

    bit_id >>= 1
    shift += 1
  end
  ids
end

.ids_to_bit_id(ids) ⇒ Integer

Parameters:

  • ids (Array<Integer>)

Returns:

  • (Integer)


11
12
13
# File 'lib/diver_down/web/bit_id.rb', line 11

def ids_to_bit_id(ids)
  ids.inject(0) { _1 | id_to_bit_id(_2) }
end