Module: Crosscounter::Compute

Extended by:
Compute
Included in:
Compute
Defined in:
lib/crosscounter/compute.rb

Instance Method Summary collapse

Instance Method Details

#compute(hashes, prop_a, prop_b = nil) ⇒ Object



33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
# File 'lib/crosscounter/compute.rb', line 33

def compute(hashes, prop_a, prop_b = nil)
  count = 0
  index = 0
  length = hashes.length

  while index < length
    if hashes[index].member?(prop_a) && (!prop_b || hashes[index].member?(prop_b))
      count += 1
    end

    index += 1
  end

  count
end

#compute_all(enumerable, rows, cols) ⇒ Object



7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
# File 'lib/crosscounter/compute.rb', line 7

def compute_all(enumerable, rows, cols)
  hashified = enumerable.map { |hash| Util.hashify(hash) }
  string_cols = Util.stringify(cols)
  string_rows = Util.stringify(rows)

  string_rows.map! do |row|
    initial = [row, compute(hashified, row)]

    string_cols.each do |col|
      initial << compute(hashified, row, col)
    end

    initial
  end
end

#compute_sum(enumerable, rows, cols) ⇒ Object



23
24
25
26
27
28
29
30
31
# File 'lib/crosscounter/compute.rb', line 23

def compute_sum(enumerable, rows, cols)
  hashified = enumerable.map { |hash| Util.hashify(hash) }
  string_rows = Util.stringify(rows)
  string_cols = Util.stringify(cols)

  string_rows.map! do |row|
    computex(hashified, row, string_cols).unshift(row)
  end
end

#computex(hashes, base_prop, other_props) ⇒ Object



49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
# File 'lib/crosscounter/compute.rb', line 49

def computex(hashes, base_prop, other_props)
  count = 0
  cross = 0
  index = 0
  length = hashes.length

  while index < length
    if hashes[index].member?(base_prop)
      count += 1
      cross += 1 if other_props.any? && other_props.all? { |prop| hashes[index].member?(prop) }
    end

    index += 1
  end

  [count, cross]
end