Class: RubyLens::Model::DependencyAggregation

Inherits:
Object
  • Object
show all
Defined in:
lib/rubylens/model/dependency_aggregation.rb

Constant Summary collapse

DEFAULT_ROW_LIMIT =
18_000
SIGNAL_COLUMNS =
(1..6).freeze

Instance Method Summary collapse

Constructor Details

#initialize(package_count:, row_limit: DEFAULT_ROW_LIMIT, seed: 0x51A7_E11A) ⇒ DependencyAggregation

Returns a new instance of DependencyAggregation.

Raises:

  • (ArgumentError)


9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
# File 'lib/rubylens/model/dependency_aggregation.rb', line 9

def initialize(package_count:, row_limit: DEFAULT_ROW_LIMIT, seed: 0x51A7_E11A)
  raise ArgumentError, "package_count must be nonnegative" if package_count.negative?
  raise ArgumentError, "row_limit must be nonnegative" if row_limit.negative?

  @row_limit = row_limit
  @random = Random.new(seed)
  @ordinal = 0
  @seen_nonrepresentative = 0
  @counts = Array.new(package_count, 0)
  @ruby_counts = Array.new(package_count) { Array.new(4, 0) }
  @signal_maxima = Array.new(6, 0)
  @representatives = Array.new(package_count)
  @represented_packages = []
  @representative_count = 0
  @nonempty_package_count = 0
  @reservoir = []
end

Instance Method Details

#add(package_index:, row:, construct_index:) ⇒ Object



27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
# File 'lib/rubylens/model/dependency_aggregation.rb', line 27

def add(package_index:, row:, construct_index:)
  @counts[package_index] += 1
  @ruby_counts[package_index][construct_index] += 1 if construct_index
  SIGNAL_COLUMNS.each_with_index do |column, index|
    @signal_maxima[index] = [@signal_maxima[index], row[column]].max
  end

  entry = [package_index, @ordinal, row.dup.freeze]
  @ordinal += 1
  if @counts[package_index] == 1
    add_first_package_entry(package_index, entry)
  elsif @representatives[package_index] && @random.rand(@counts[package_index]).zero?
    displaced = @representatives[package_index]
    @representatives[package_index] = entry
    add_to_reservoir(displaced)
  else
    add_to_reservoir(entry)
  end
end

#packagesObject



47
48
49
50
51
52
53
54
55
56
57
58
# File 'lib/rubylens/model/dependency_aggregation.rb', line 47

def packages
  sampled_rows = Array.new(@counts.length) { [] }
  (@representatives.compact + @reservoir).sort_by { |_package_index, ordinal, _row| ordinal }
    .each { |package_index, _ordinal, row| sampled_rows[package_index] << row }
  @counts.each_index.map do |index|
    {
      declaration_count: @counts[index],
      ruby_counts: @ruby_counts[index].dup.freeze,
      declarations: sampled_rows[index].freeze,
    }.freeze
  end.freeze
end

#signal_maximaObject



60
61
62
# File 'lib/rubylens/model/dependency_aggregation.rb', line 60

def signal_maxima
  @signal_maxima.dup.freeze
end