<commit_before><?php

/**
 * Class wrapping a PHP as a key/value store object.
 * 
 * This class enforces array key values as strings. When
 * accessing values in the array, all key references are coerced
 * to string values.
 *
 * @package Prb
 */
class Prb_Hash extends Prb_Abstract_Collection
	implements Prb_Interface_Enumerable
{
	private $default;
	
	// TODO: Document!
	public function get( $key )
	{
		$key = (string)$key;
		if ( !array_key_exists( $key, $this->array ) )
			return $this->getDefault();
		
		return $this->array[ $key ];
	}
	
	// TODO: Document!
	public function set( $key, $item )
	{
		$this->array[ $key ] = $item;
	}
	
	// TODO: Document!
	public function delete( $key )
	{
		$result = isset( $this->array[ $key ] ) ? $this->array[ $key ]
		                                        : $this->getDefault();
		unset( $this->array[ $key ] );
		return $result;
	}
	
		// TODO: Document!
	public function clear()
	{
		$this->array = array();
		return $this;
	}
	
	// TODO: Document!
	public function merge( $other )
	{
		$hash = clone $this;
		$hash->mergeInPlace( $other );
		return $hash;
	}
	
	// TODO: Document!
	public function mergeInPlace( $other )
	{
		foreach ( $other->raw() as $key => $value )
			$this->set( $key, $value );
		return $this;
	}
	
	public function update( $other ) { return $this->mergeInPlace( $other ); }
	
	// TODO: Document!
	public function each( $callback )
	{
		if ( !is_callable( $callback ) )
			throw new Prb_Exception_Callback();
		
		// I disagree with array_walk function.
		foreach ( $this->array as $key => $value )
			call_user_func( $callback, $key, $value );
	}
	
	// TODO: Document!
	public function inject( $accumulator, $callback )
	{
		if ( !is_callable( $callback ) )
			throw new Prb_Exception_Callback();
		
		$result = $accumulator;
		
		foreach ( $this->array as $key => $value )
		{
			if ( $value instanceof Prb_Array )
			{
				$args = $value->raw();
				array_unshift( $args, $result );
				$result = call_user_func_array( $callback, $args );
			}
			else
				$result = call_user_func( $callback, $result, $key, $value );
		}
		
		return $result;
	}

	// TODO: Document!
	public function collect( $callback )
	{
		if ( !is_callable( $callback ) )
			throw new Prb_Exception_Callback();
		
		$map = array();
		
		// I disagree with array_map function.
		foreach ( $this->array as $key => $item )
			array_push( $map, call_user_func( $callback, $key, $item ) );
		
		return Prb::_Array( $map );
	}
	
	public function map( $callback ) { return $this->collect( $callback ); }
	
	// TODO: Document!
	public function contains( $key )
	{
		return array_key_exists( $key, $this->array );
	}
	
	// TODO: Document!
	public function hasKey  ( $key ) { return $this->contains( $key ); }
	public function isMember( $key ) { return $this->contains( $key ); }
	
	// TODO: Document!<commit_msg>Prb_Hash: fixing contains and get for null key.<commit_after><?php

/**
 * Class wrapping a PHP as a key/value store object.
 * 
 * This class enforces array key values as strings. When
 * accessing values in the array, all key references are coerced
 * to string values.
 *
 * @package Prb
 */
class Prb_Hash extends Prb_Abstract_Collection
	implements Prb_Interface_Enumerable
{
	private $default;
	
	// TODO: Document!
	public function get( $key )
	{
		$key = (string)$key;
		if ( is_null( $key ) || !array_key_exists( $key, $this->array ) )
			return $this->getDefault();
		
		return $this->array[ $key ];
	}
	
	// TODO: Document!
	public function set( $key, $item )
	{
		$this->array[ $key ] = $item;
	}
	
	// TODO: Document!
	public function delete( $key )
	{
		$result = isset( $this->array[ $key ] ) ? $this->array[ $key ]
		                                        : $this->getDefault();
		unset( $this->array[ $key ] );
		return $result;
	}
	
		// TODO: Document!
	public function clear()
	{
		$this->array = array();
		return $this;
	}
	
	// TODO: Document!
	public function merge( $other )
	{
		$hash = clone $this;
		$hash->mergeInPlace( $other );
		return $hash;
	}
	
	// TODO: Document!
	public function mergeInPlace( $other )
	{
		foreach ( $other->raw() as $key => $value )
			$this->set( $key, $value );
		return $this;
	}
	
	public function update( $other ) { return $this->mergeInPlace( $other ); }
	
	// TODO: Document!
	public function each( $callback )
	{
		if ( !is_callable( $callback ) )
			throw new Prb_Exception_Callback();
		
		// I disagree with array_walk function.
		foreach ( $this->array as $key => $value )
			call_user_func( $callback, $key, $value );
	}
	
	// TODO: Document!
	public function inject( $accumulator, $callback )
	{
		if ( !is_callable( $callback ) )
			throw new Prb_Exception_Callback();
		
		$result = $accumulator;
		
		foreach ( $this->array as $key => $value )
		{
			if ( $value instanceof Prb_Array )
			{
				$args = $value->raw();
				array_unshift( $args, $result );
				$result = call_user_func_array( $callback, $args );
			}
			else
				$result = call_user_func( $callback, $result, $key, $value );
		}
		
		return $result;
	}

	// TODO: Document!
	public function collect( $callback )
	{
		if ( !is_callable( $callback ) )
			throw new Prb_Exception_Callback();
		
		$map = array();
		
		// I disagree with array_map function.
		foreach ( $this->array as $key => $item )
			array_push( $map, call_user_func( $callback, $key, $item ) );
		
		return Prb::_Array( $map );
	}
	
	public function map( $callback ) { return $this->collect( $callback ); }
	
	// TODO: Document!
	public function contains( $key )
	{
		return is_null( $key ) ? null : array_key_exists( $key, $this->array );
	}
	
	// TODO: Document!
	public function hasKey  ( $key ) { return $this->contains( $key ); }
	public function isMember( $key ) { return $this->contains( $key ); }
	
	// TODO: Document!
{'fraction_non_alphanumeric': 0.15342087076710437, 'fraction_numerical': 0.0, 'mean_word_length': 3.006228373702422, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 53, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 4, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3344196', 'n_tokens_mistral': 2168, 'n_tokens_neox': 1933, 'n_words': 547}