require 'forwardable'

module Twigg
  class PairMatrix
    extend Forwardable
    def_delegators :pairs, :[], :keys

    def initialize(commit_set)
      @commit_set = commit_set
    end

    def pairs
      @pairs ||= Hash.new do |hash, pairer|
        hash[pairer] = Hash.new { |hash, pairee| hash[pairee] = 0 }
      end.tap do |matrix|
        @commit_set.each do |commit|
          authors = commit.author_names

          # if you're solo, that's equivalent to pairing with yourself
          authors *= 2 if authors.size == 1

          authors.permutation(2).each do |pairer, pairee|
            matrix[pairer][pairee] += 1
          end
        end
      end
    end

    def authors
      @authors ||= pairs.keys.sort
    end

    def max_solo
      @max_solo ||= pairs.inject(0) do |max, (pairee, pairs)|
        [pairs.inject(0) do |max, (pairer, count)|
          [pairee == pairer ? count : 0, max].max
        end, max].max
      end
    end

    def max_pair
      @max_pair ||= pairs.inject(0) do |max, (pairee, pairs)|
        [pairs.inject(0) do |max, (pairer, count)|
          [pairee == pairer ? 0 : count, max].max
        end, max].max
      end
    end
  end
end

require 'forwardable'

module Twigg
  class PairMatrix
    extend Forwardable
    def_delegators :pairs, :[], :keys

    def initialize(commit_set)
      @commit_set = commit_set
    end

    def pairs
      @pairs ||= Hash.new do |hash, pairer|
        hash[pairer] = Hash.new { |hash, pairee| hash[pairee] = 0 }
      end.tap do |matrix|
        @commit_set.each do |commit|
          authors = commit.author_names

          # if you're solo, that's equivalent to pairing with yourself
          authors *= 2 if authors.size == 1

          authors.permutation(2).to_a.uniq.each do |pairer, pairee|
            matrix[pairer][pairee] += 1
          end
        end
      end
    end

    def authors
      @authors ||= pairs.keys.sort
    end

    def max_solo
      @max_solo ||= pairs.inject(0) do |max, (pairee, pairs)|
        [pairs.inject(0) do |max, (pairer, count)|
          [pairee == pairer ? count : 0, max].max
        end, max].max
      end
    end

    def max_pair
      @max_pair ||= pairs.inject(0) do |max, (pairee, pairs)|
        [pairs.inject(0) do |max, (pairer, count)|
          [pairee == pairer ? 0 : count, max].max
        end, max].max
      end
    end
  end
end

Fix bug with double-counting solo commits in PairMatrix
Fix bug with double-counting solo commits in PairMatrix

I mistakenly thought that an array like `['Joe', 'Joe']` would get
permuted to `[['Joe', 'Joe']]`, but it actually gets permuted to
`[['Joe', 'Joe'], ['Joe', 'Joe']]`.

Will follow-up with a spec.

Change-Id: Ia897cd89d4d969283fbf54abb379cb5e32bbf485
Reviewed-on: https://gerrit.causes.com/26077
Reviewed-by: Greg Hurrell <79e2475f81a6317276bf7cbb3958b20d289b78df@causes.com>
Tested-by: Greg Hurrell <79e2475f81a6317276bf7cbb3958b20d289b78df@causes.com>
