<commit_before>class Join
  def self.new(&block)
    raise ArgumentError, "No block given" unless block
    c = Class.new self

    c.instance_eval do
      @max = -1
      @indices = {}
      @chords = []
    end

    def c.chord(*names, &action)
      raise ArgumentError, "No channel names specified" if names.empty?
      raise ArgumentError, "No block given" unless action
      names.map! { |n| n.to_sym }
      indices = names.map { |n| @indices[n] ||= ( @max += 1 ) }
      mask = indices.inject(0) { |acc, i| acc | ( 1 << i ) }
      @chords.push [ mask, indices, action ]
      names.each_with_index do |n, i|
        eval <<-EOS
          def #{ n }(value=nil)
            __channel_send__( #{ indices[i] }, value )
          end
        EOS
      end
      self
    end

    c.class_eval &block
    c.instance_eval { @chords.reverse! }

    class << c
      undef :chord
      def new
        obj = allocate
        chords = @chords
        max = @max
        obj.instance_eval do
          @join_pending = (0..max).map { Array.new }
          @join_pending_mask = 0
          @join_chords = chords
          @join_lock = Channel.new
          @join_lock.send nil
        end
        obj
      end
    end

    c
  end

  def __channel_send__(index, value)
    @join_lock.receive
    begin
      @join_pending[index].push value
      @join_pending_mask |= 1 << index
      @join_chords.each do |mask, indices, action|
        if mask & @join_pending_mask == mask
          args = indices.map do |i|
            pending = @join_pending[i]
            value = pending.shift
            @join_pending_mask ^= 1 << i if pending.empty?
            value
          end
          Thread.new { instance_exec(*args, &action) }
          break
        end
      end<commit_msg>fix initialization<commit_after>class Join
  def self.new(&block)
    raise ArgumentError, "No block given" unless block
    c = Class.new self

    c.instance_eval do
      @max = -1
      @indices = {}
      @chords = []
    end

    def c.chord(*names, &action)
      raise ArgumentError, "No channel names specified" if names.empty?
      raise ArgumentError, "No block given" unless action
      names.map! { |n| n.to_sym }
      indices = names.map { |n| @indices[n] ||= ( @max += 1 ) }
      mask = indices.inject(0) { |acc, i| acc | ( 1 << i ) }
      @chords.push [ mask, indices, action ]
      names.each_with_index do |n, i|
        eval <<-EOS
          def #{ n }(value=nil)
            __channel_send__( #{ indices[i] }, value )
          end
        EOS
      end
      self
    end

    c.class_eval &block
    c.instance_eval { @chords.reverse! }

    class << c
      undef :chord
      def new(*args, &block)
        obj = allocate
        chords = @chords
        max = @max
        obj.instance_eval do
          @join_pending = (0..max).map { Array.new }
          @join_pending_mask = 0
          @join_chords = chords
          @join_lock = Channel.new
          @join_lock.send nil
          initialize(*args, &block)
        end
        obj
      end
    end

    c
  end

  def __channel_send__(index, value)
    @join_lock.receive
    begin
      @join_pending[index].push value
      @join_pending_mask |= 1 << index
      @join_chords.each do |mask, indices, action|
        if mask & @join_pending_mask == mask
          args = indices.map do |i|
            pending = @join_pending[i]
            value = pending.shift
            @join_pending_mask ^= 1 << i if pending.empty?
            value
          end
          Thread.new { instance_exec(*args, &action) }
          break
        end
      end
{'fraction_non_alphanumeric': 0.10680686220254565, 'fraction_numerical': 0.004427227448810182, 'mean_word_length': 1.711927981995499, 'pattern_counts': {'":': 0, '<': 23, '<?xml version=': 0, '>': 3, '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': '2534328', 'n_tokens_mistral': 1147, 'n_tokens_neox': 1118, 'n_words': 305}