<commit_before>require 'ffi'

module FFI
  module BitMasks
    class BitMask

      include DataConverter

      #
      # Fields of the bitmask.
      #
      # @return [Hash{Symbol => Integer}]
      #   The mapping of bit-flags to bitmasks.
      #
      attr_reader :flags

      #
      # The bitmasks of the bitmask.
      #
      # @return [Hash{Integer => Symbol}]
      #   The mapping of bitmasks to bit-flags.
      #   
      attr_reader :bitmasks

      #
      # The underlying native type.
      #
      # @return [FFI::Type]
      #   The FFI primitive.
      #
      attr_reader :native_type

      #
      # @param [Hash{Symbol => Integer}] flags
      #
      # @param [Symbol] type
      #   The underlying type.
      #
      def initialize(flags,type=:uint)
        @flags       = flags
        @bitmasks    = flags.invert
        @native_type = FFI.find_type(type)
      end

      #
      # @return [Array<Symbol>]
      #
      # @note For compatibility with `FFI::Enum`.
      #
      def symbols
        @flags.keys
      end

      #
      # @return [Hash{Symbol => Integer}]
      #
      # @note For compatibility with `FFI::Enum`.
      #
      def symbol_map
        @flags
      end

      alias to_h    symbol_map
      alias to_hash symbol_map

      #
      # @overload [](query)
      #
      #   @param [Symbol] query
      #
      #   @return [Integer]
      #
      # @overload [](query)
      #
      #   @param [Integer] query
      #
      #   @return [Symbol]
      #
      def [](query)
        case query
        when Symbol  then @flags[query]
        when Integer then @bitmasks[query]
        end
      end

      #
      # @note For compatibility with `FFI::Enum`.
      #
      alias find []

      #
      # @param [Hash, #to_int] value
      #
      # @return [Integer]
      #
      # @raise [ArgumentError]
      #
      def to_native(value,ctx=nil)
        uint = 0

        case value
        when Hash
          uint = 0

          value.each do |flag,value|
            if value
              uint |= @flags[flag]
            end
          end

          return uint
        else
          if value.respond_to?(:to_int)
            value.to_int
          else<commit_msg>Ignore keys that do not correspond to known flags.<commit_after>require 'ffi'

module FFI
  module BitMasks
    class BitMask

      include DataConverter

      #
      # Fields of the bitmask.
      #
      # @return [Hash{Symbol => Integer}]
      #   The mapping of bit-flags to bitmasks.
      #
      attr_reader :flags

      #
      # The bitmasks of the bitmask.
      #
      # @return [Hash{Integer => Symbol}]
      #   The mapping of bitmasks to bit-flags.
      #   
      attr_reader :bitmasks

      #
      # The underlying native type.
      #
      # @return [FFI::Type]
      #   The FFI primitive.
      #
      attr_reader :native_type

      #
      # @param [Hash{Symbol => Integer}] flags
      #
      # @param [Symbol] type
      #   The underlying type.
      #
      def initialize(flags,type=:uint)
        @flags       = flags
        @bitmasks    = flags.invert
        @native_type = FFI.find_type(type)
      end

      #
      # @return [Array<Symbol>]
      #
      # @note For compatibility with `FFI::Enum`.
      #
      def symbols
        @flags.keys
      end

      #
      # @return [Hash{Symbol => Integer}]
      #
      # @note For compatibility with `FFI::Enum`.
      #
      def symbol_map
        @flags
      end

      alias to_h    symbol_map
      alias to_hash symbol_map

      #
      # @overload [](query)
      #
      #   @param [Symbol] query
      #
      #   @return [Integer]
      #
      # @overload [](query)
      #
      #   @param [Integer] query
      #
      #   @return [Symbol]
      #
      def [](query)
        case query
        when Symbol  then @flags[query]
        when Integer then @bitmasks[query]
        end
      end

      #
      # @note For compatibility with `FFI::Enum`.
      #
      alias find []

      #
      # @param [Hash, #to_int] value
      #
      # @return [Integer]
      #
      # @raise [ArgumentError]
      #
      def to_native(value,ctx=nil)
        uint = 0

        case value
        when Hash
          uint = 0

          value.each do |flag,value|
            if (@flags.has_key?(flag) && value)
              uint |= @flags[flag]
            end
          end

          return uint
        else
          if value.respond_to?(:to_int)
            value.to_int
          else
{'fraction_non_alphanumeric': 0.09814323607427056, 'fraction_numerical': 0.0008841732979664014, 'mean_word_length': 1.3433454168824444, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 13, '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': '4013621', 'n_tokens_mistral': 1398, 'n_tokens_neox': 1362, 'n_words': 346}