<commit_before>module Rubaship

  class Grid
    include Enumerable

    ROWS = ("A".."J").to_a

    POSITION_REGEXP = /^(?<anchor>[A-Z0-9]{2,3}):(?<orientation>[a-z]{1,10})$/i
    ANCHOR_REGEXP = /^(?:(?<row>[A-J])(?<col>([1-9])|10)|\g<col>\g<row>)$/i
    ORIENT_REGEXP = /^(?<ori>
      h(o(r(i(z(o(n(t(a(l)?)?)?)?)?)?)?)?)? |
      v(e(r(t(i(c(a(l)?)?)?)?)?)?)?)$/ix

    def initialize
      @grid = Array.new(10) { Array.new(10) { Sector.new } }
    end

    def initialize_copy(orig)
      s = orig.enum_for(:each_sector)
      @grid = Array.new(10) { Array.new(10) { s.next.dup } }
    end

    def ==(o)
      return case o
        when Hash  then to_hash == o
        when Array then self.to_a == o
        when Grid then self.to_a == o.to_a
      end
    end

    def [](index)
      @grid[Grid.row_to_idx(index)]
    end

    def add(ship, row, col, ori)
      self.dup.add!(ship, row, col, ori)
    end

    def add!(ship, row, col, ori=nil)
      row = Grid.row_to_idx(row)
      col = Grid.col_to_idx(col)
      unless ori.nil?
        ori = Grid.ori_to_sym(ori)
        row, col = Grid.rangify_pos(ship, row, col, ori)
      end
      raise InvalidPositionError.new unless Grid.position_valid?(ship, row, col)

      insert_ship = lambda { |sector| sector.ship = ship }
      if col.is_a? Range
        grid = @grid[row][col]
      elsif row.is_a? Range
        grid = @grid.transpose[col][row]
      end
      if v = grid.inject(nil) { |r, sector| r ||= sector.ship }
        raise OverlapShipError.new(v)
      end
      grid.each(&insert_ship)
    end

    def col(col)
      @grid.transpose[Grid.col_to_idx(col)]
    end

    def each
      if block_given?
        @grid.collect do |row|
          row.collect { |sector| yield sector }
        end
      else
        self.to_enum
      end
    end

    alias :each_sector :each

    alias :sectors :each

    def each_col
      if block_given?
        @grid.transpose.each { |col| yield col }
      else
        self.enum_for(:each_col)
      end
    end

    alias :cols :each_col

    def each_row
      if block_given?
        @grid.each { |row| yield row }
      else
        self.enum_for(:each_row)
      end
    end

    alias :rows :each_row

    alias :row :[]

    def sector(row, col)
      @grid[Grid.row_to_idx(row)][Grid.col_to_idx(col)]
    end

    def to_hash
      @grid.each_with_index.inject({}) do |row_hash, (row, i)|
        row_hash[ROWS[i].to_sym] = row.collect { |sector| sector.to_hash }
        row_hash
      end<commit_msg>Grid#row no longer aliases Grid#[]<commit_after>module Rubaship

  class Grid
    include Enumerable

    ROWS = ("A".."J").to_a

    POSITION_REGEXP = /^(?<anchor>[A-Z0-9]{2,3}):(?<orientation>[a-z]{1,10})$/i
    ANCHOR_REGEXP = /^(?:(?<row>[A-J])(?<col>([1-9])|10)|\g<col>\g<row>)$/i
    ORIENT_REGEXP = /^(?<ori>
      h(o(r(i(z(o(n(t(a(l)?)?)?)?)?)?)?)?)? |
      v(e(r(t(i(c(a(l)?)?)?)?)?)?)?)$/ix

    def initialize
      @grid = Array.new(10) { Array.new(10) { Sector.new } }
    end

    def initialize_copy(orig)
      s = orig.enum_for(:each_sector)
      @grid = Array.new(10) { Array.new(10) { s.next.dup } }
    end

    def ==(o)
      return case o
        when Hash  then to_hash == o
        when Array then self.to_a == o
        when Grid then self.to_a == o.to_a
      end
    end

    def [](index)
      @grid[Grid.row_to_idx(index)]
    end

    def add(ship, row, col, ori)
      self.dup.add!(ship, row, col, ori)
    end

    def add!(ship, row, col, ori=nil)
      row = Grid.row_to_idx(row)
      col = Grid.col_to_idx(col)
      unless ori.nil?
        ori = Grid.ori_to_sym(ori)
        row, col = Grid.rangify_pos(ship, row, col, ori)
      end
      raise InvalidPositionError.new unless Grid.position_valid?(ship, row, col)

      insert_ship = lambda { |sector| sector.ship = ship }
      if col.is_a? Range
        grid = @grid[row][col]
      elsif row.is_a? Range
        grid = @grid.transpose[col][row]
      end
      if v = grid.inject(nil) { |r, sector| r ||= sector.ship }
        raise OverlapShipError.new(v)
      end
      grid.each(&insert_ship)
    end

    def col(col)
      @grid.transpose[Grid.col_to_idx(col)]
    end

    def row(row)
      @grid[Grid.row_to_idx(row)]
    end

    def each
      if block_given?
        @grid.collect do |row|
          row.collect { |sector| yield sector }
        end
      else
        self.to_enum
      end
    end

    alias :each_sector :each

    alias :sectors :each

    def each_col
      if block_given?
        @grid.transpose.each { |col| yield col }
      else
        self.enum_for(:each_col)
      end
    end

    alias :cols :each_col

    def each_row
      if block_given?
        @grid.each { |row| yield row }
      else
        self.enum_for(:each_row)
      end
    end

    alias :rows :each_row

    def sector(row, col)
      @grid[Grid.row_to_idx(row)][Grid.col_to_idx(col)]
    end

    def to_hash
      @grid.each_with_index.inject({}) do |row_hash, (row, i)|
        row_hash[ROWS[i].to_sym] = row.collect { |sector| sector.to_hash }
        row_hash
      end
{'fraction_non_alphanumeric': 0.1504407443682664, 'fraction_numerical': 0.0074436826640548485, 'mean_word_length': 2.1267605633802815, 'pattern_counts': {'":': 0, '<': 17, '<?xml version=': 0, '>': 17, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '617717', 'n_tokens_mistral': 2018, 'n_tokens_neox': 1913, 'n_words': 401}