<commit_before><?php
namespace Darya\Storage;

/**
 * Filters storage results in-memory.
 * 
 * @author <NAME> <<EMAIL>>
 */
class Filterer {
	
	/**
	 * @var array Filter comparison operators
	 */
	protected $operators = array('=', '!=', '>', '<', '<>', '>=', '<=', 'in', 'not in', 'is', 'is not', 'like', 'not like');
	
	/**
	 * @var array A map of filter operators to methods that implement them
	 */
	protected $methods = array(
		'='  => 'equal',
		'!=' => 'notEqual',
		'>'  => 'greater',
		'<'  => 'smaller',
		'<>' => 'greaterOrSmaller',
		'>=' => 'greaterOrEqual',
		'<=' => 'smallerOrEqual',
		'in' => 'in',
		'not in' => 'notIn',
		'is'     => 'is',
		'is not' => 'isNot',
		'like'   => 'like',
		'not like' => 'notLike'
	);
	
	/**
	 * Escape the given value for use as a like query.
	 * 
	 * Precedes all underscore and percentage characters with a backwards slash.
	 * 
	 * @param string $value
	 * @return string
	 */
	public function escape($value) {
		return preg_replace('/([%_])/', '\\$1', $value);
	}
	
	/**
	 * Filter the given data.
	 * 
	 * @param array $data
	 * @param array $filter
	 * @return array
	 */
	public function filter(array $data, array $filter = array()) {
		if (empty($filter)) {
			return $data;
		}
		
		foreach ($filter as $field => $value) {
			$data = $this->process($data, $field, $value);
		}
		
		return $data;
	}
	
	/**
	 * Determine whether the given data item matches the given filter.
	 * 
	 * @param array $item
	 * @param array $filter
	 * @return bool
	 */
	public function matches(array $item, array $filter = array()) {
		if (empty($filter)) {
			return true;
		}
		
		$data = array();
		
		foreach ($filter as $field => $value) {
			$data = $this->process(array($item), $field, $value);
		}
		
		return !empty($data);
	}
	
	/**
	 * Apply a function to the elements of the given data that match a filter.
	 * 
	 * @param array    $data
	 * @param array    $filter [optional]
	 * @param callable $callback
	 * @return array
	 */
	public function map(array $data, array $filter, $callback) {
		if (!is_callable($callback)) {
			return $data;<commit_msg>Improved performance of Filterer::matches(). The root of all evil.<commit_after><?php
namespace Darya\Storage;

/**
 * Filters storage results in-memory.
 * 
 * @author <NAME> <<EMAIL>>
 */
class Filterer {
	
	/**
	 * @var array Filter comparison operators
	 */
	protected $operators = array('=', '!=', '>', '<', '<>', '>=', '<=', 'in', 'not in', 'is', 'is not', 'like', 'not like');
	
	/**
	 * @var array A map of filter operators to methods that implement them
	 */
	protected $methods = array(
		'='  => 'equal',
		'!=' => 'notEqual',
		'>'  => 'greater',
		'<'  => 'smaller',
		'<>' => 'greaterOrSmaller',
		'>=' => 'greaterOrEqual',
		'<=' => 'smallerOrEqual',
		'in' => 'in',
		'not in' => 'notIn',
		'is'     => 'is',
		'is not' => 'isNot',
		'like'   => 'like',
		'not like' => 'notLike'
	);
	
	/**
	 * Escape the given value for use as a like query.
	 * 
	 * Precedes all underscore and percentage characters with a backwards slash.
	 * 
	 * @param string $value
	 * @return string
	 */
	public function escape($value) {
		return preg_replace('/([%_])/', '\\$1', $value);
	}
	
	/**
	 * Filter the given data.
	 * 
	 * @param array $data
	 * @param array $filter
	 * @return array
	 */
	public function filter(array $data, array $filter = array()) {
		if (empty($filter)) {
			return $data;
		}
		
		foreach ($filter as $field => $value) {
			$data = $this->process($data, $field, $value);
		}
		
		return $data;
	}
	
	/**
	 * Determine whether the given data item matches the given filter.
	 * 
	 * @param array $item
	 * @param array $filter
	 * @return bool
	 */
	public function matches(array $item, array $filter = array()) {
		if (empty($filter)) {
			return true;
		}
		
		$data = array();
		
		foreach ($filter as $field => $value) {
			$data = $this->process(array($item), $field, $value);
			
			if (empty($data))
				return false;
		}
		
		return true;
	}
	
	/**
	 * Apply a function to the elements of the given data that match a filter.
	 * 
	 * @param array    $data
	 * @param array    $filter [optional]
	 * @param callable $callback
	 * @return array
	 */
	public function map(array $data, array $filter, $callback) {
		if (!is_callable($callback)) {
			return $data;
{'fraction_non_alphanumeric': 0.19036537118920177, 'fraction_numerical': 0.00046544100535257155, 'mean_word_length': 2.9722735674676524, 'pattern_counts': {'":': 0, '<': 23, '<?xml version=': 0, '>': 55, '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': '5605027', 'n_tokens_mistral': 1618, 'n_tokens_neox': 1412, 'n_words': 433}