Module: Ucode::Coordinator::RangeLookup

Defined in:
lib/ucode/coordinator/range_lookup.rb

Overview

Pure-function range lookups shared by the enrichment pipeline.

Extracted from Coordinator so that Enrichment modules can call them without inheriting Coordinator's instance context. Both methods are deterministic and side-effect free.

Class Method Summary collapse

Class Method Details

.all_range_values(cp, sorted_ranges) ⇒ Array

Returns every value whose range contains cp in a sorted tuple array. Most codepoint+property pairs match at most one range, but a codepoint can carry multiple binary properties from PropList or emoji-data, so we collect them all.

Ranges are sorted by range_first. Once we hit a range that starts after cp, every subsequent range also starts after cp, so we break. Ranges that end before cp are skipped.

Parameters:

  • cp (Integer)
  • sorted_ranges (Array)

    sorted by range_first

Returns:

  • (Array)

    values of every range containing cp



51
52
53
54
55
56
57
58
59
60
61
62
# File 'lib/ucode/coordinator/range_lookup.rb', line 51

def all_range_values(cp, sorted_ranges)
  return [] if sorted_ranges.nil? || sorted_ranges.empty?

  values = []
  sorted_ranges.each do |record|
    break if record.range_first > cp
    next if record.range_last < cp

    values << record.value
  end
  values
end

.compare_cp(cp, record) ⇒ Object



32
33
34
35
36
37
# File 'lib/ucode/coordinator/range_lookup.rb', line 32

def compare_cp(cp, record)
  return -1 if cp < record.range_first
  return 1 if cp > record.range_last

  0
end

.find_in_range(cp, sorted_ranges) ⇒ Object?

Finds the single range-containing record in a sorted array via bsearch. Records respond to range_first and range_last.

bsearch_index integer-mode convention: return -1 to search LEFT, +1 to search RIGHT, 0 for a match. cp < range_first means the target range lies in earlier (lower-indexed) records, so we return -1; cp > range_last means it lies in later records, so we return +1.

Parameters:

  • cp (Integer)
  • sorted_ranges (Array)

    sorted by range_first

Returns:

  • (Object, nil)

    the record whose range contains cp



25
26
27
28
29
30
# File 'lib/ucode/coordinator/range_lookup.rb', line 25

def find_in_range(cp, sorted_ranges)
  return nil if sorted_ranges.nil? || sorted_ranges.empty?

  idx = sorted_ranges.bsearch_index { |record| compare_cp(cp, record) }
  idx.nil? ? nil : sorted_ranges[idx]
end