<commit_before><?php

namespace Diff;
use InvalidArgumentException;

/**
 * Base class for diffs. Diffs are collections of DiffOp objects,
 * and are themselves DiffOp objects as well.
 *
 * FIXME: current implementation forces ListDiff to override and nullify, which is bad.
 *
 * TOOD: when not assoc, only add and remove ops should be permitted
 *
 * @since 0.1
 *
 * @file
 * @ingroup Diff
 *
 * @licence GNU GPL v2+
 * @author <NAME> < <EMAIL> >
 */
class Diff extends \GenericArrayObject implements IDiff {

	/**
	 * @since 0.1
	 *
	 * @var boolean|null
	 */
	protected $isAssociative;

	/**
	 * Pointers to the operations of certain types for quick lookup.
	 *
	 * @since 0.1
	 *
	 * @var array
	 */
	protected $typePointers = array(
		'add' => array(),
		'remove' => array(),
		'change' => array(),
		'list' => array(),
		'map' => array(),
		'diff' => array(),
	);

	/**
	 * @see Diff::__construct
	 *
	 * @since 0.1
	 *
	 * @param DiffOp[] $operations
	 * @param boolean|null $isAssociative
	 */
	public function __construct( array $operations = array(), $isAssociative = null ) {
		foreach ( $operations as  $operation ) {
			if ( !( $operation instanceof DiffOp ) ) {
				throw new InvalidArgumentException( 'All elements fed to the Diff constructor should be of type DiffOp' );
			}
		}

		if ( $isAssociative !== null && !is_bool( $isAssociative ) ) {
			throw new InvalidArgumentException( '$isAssociative should be a boolean or null' );
		}

		$this->isAssociative = $isAssociative;

		parent::__construct( $operations );
	}

	/**
	 * @see GenericArrayObject::getObjectType
	 *
	 * @since 0.1
	 *
	 * @return string
	 */
	public function getObjectType() {
		return '\Diff\DiffOp';
	}

	/**
	 * @see Diff::getOperations
	 *
	 * @since 0.1
	 *
	 * @return DiffOp[]
	 */
	public function getOperations() {
		return $this->getArrayCopy();
	}

	/**
	 * @since 0.1
	 *
	 * @param string $type
	 *
	 * @return DiffOp[]
	 */
	public function getTypeOperations( $type ) {
		return array_intersect_key(
			$this->getArrayCopy(),
			array_flip( $this->typePointers[$type] )
		);
	}

	/**
	 * @see Diff::addOperations
	 *
	 * @since 0.1
	 *
	 * @param DiffOp[] $operations
	 */
	public function addOperations( array $operations ) {
		foreach ( $operations as $operation ) {
			$this->append( $operation );
		}
	}

	/**
	 * @see GenericArrayObject::preSetElement
	 *
	 * @since 0.1
	 *
	 * @param integer|string $index
	 * @param mixed $value
	 *
	 * @return boolean
	 * @throws Exception
	 */
	protected function preSetElement( $index, $value ) {
		/**
		 * @var DiffOp $value
		 */
		if ( $this->isAssociative === false && ( $value->getType() !== 'add' && $value->getType() !== 'remove' ) ) {
			throw new Exception( 'Diff operation with invalid type "' . $value->getType() . '" provided.' );
		}

		if ( array_key_exists( $value->getType(), $this->typePointers ) ) {
			$this->typePointers[$value->getType()][] = $index;
		}
		else {
			throw new Exception( 'Diff operation with invalid type "' . $value->getType() . '" provided.' );
		}

		return true;
	}

	/**
	 * @see GenericArrayObject::getSerializationData
	 *
	 * @since 0.1
	 *
	 * @return array
	 */
	protected function getSerializationData() {
		$assoc = $this->isAssociative === null ? 'n' : ( $this->isAssociative ? 't' : 'f' );

		return array_merge(
			parent::getSerializationData(),
			array(
				'typePointers' => $this->typePointers,
				'assoc' => $assoc
			)
		);
	}

	/**
	 * @see GenericArrayObject::unserialize
	 *
	 * @since 0.1
	 *
	 * @param string $serialization
	 *
	 * @return array
	 */
	public function unserialize( $serialization ) {
		$serializationData = parent::unserialize( $serialization );

		$this->typePointers = $serializationData['typePointers'];
		$this->isAssociative = $serializationData['assoc'] === 'n' ? null : $serializationData['assoc'] === 't';

		return $serializationData;
	}<commit_msg>Added compatibility with Diff < 0.4 in Diff::unserialize<commit_after><?php

namespace Diff;
use InvalidArgumentException;

/**
 * Base class for diffs. Diffs are collections of DiffOp objects,
 * and are themselves DiffOp objects as well.
 *
 * FIXME: current implementation forces ListDiff to override and nullify, which is bad.
 *
 * TOOD: when not assoc, only add and remove ops should be permitted
 *
 * @since 0.1
 *
 * @file
 * @ingroup Diff
 *
 * @licence GNU GPL v2+
 * @author <NAME> < <EMAIL> >
 */
class Diff extends \GenericArrayObject implements IDiff {

	/**
	 * @since 0.1
	 *
	 * @var boolean|null
	 */
	protected $isAssociative;

	/**
	 * Pointers to the operations of certain types for quick lookup.
	 *
	 * @since 0.1
	 *
	 * @var array
	 */
	protected $typePointers = array(
		'add' => array(),
		'remove' => array(),
		'change' => array(),
		'list' => array(),
		'map' => array(),
		'diff' => array(),
	);

	/**
	 * @see Diff::__construct
	 *
	 * @since 0.1
	 *
	 * @param DiffOp[] $operations
	 * @param boolean|null $isAssociative
	 */
	public function __construct( array $operations = array(), $isAssociative = null ) {
		foreach ( $operations as  $operation ) {
			if ( !( $operation instanceof DiffOp ) ) {
				throw new InvalidArgumentException( 'All elements fed to the Diff constructor should be of type DiffOp' );
			}
		}

		if ( $isAssociative !== null && !is_bool( $isAssociative ) ) {
			throw new InvalidArgumentException( '$isAssociative should be a boolean or null' );
		}

		$this->isAssociative = $isAssociative;

		parent::__construct( $operations );
	}

	/**
	 * @see GenericArrayObject::getObjectType
	 *
	 * @since 0.1
	 *
	 * @return string
	 */
	public function getObjectType() {
		return '\Diff\DiffOp';
	}

	/**
	 * @see Diff::getOperations
	 *
	 * @since 0.1
	 *
	 * @return DiffOp[]
	 */
	public function getOperations() {
		return $this->getArrayCopy();
	}

	/**
	 * @since 0.1
	 *
	 * @param string $type
	 *
	 * @return DiffOp[]
	 */
	public function getTypeOperations( $type ) {
		return array_intersect_key(
			$this->getArrayCopy(),
			array_flip( $this->typePointers[$type] )
		);
	}

	/**
	 * @see Diff::addOperations
	 *
	 * @since 0.1
	 *
	 * @param DiffOp[] $operations
	 */
	public function addOperations( array $operations ) {
		foreach ( $operations as $operation ) {
			$this->append( $operation );
		}
	}

	/**
	 * @see GenericArrayObject::preSetElement
	 *
	 * @since 0.1
	 *
	 * @param integer|string $index
	 * @param mixed $value
	 *
	 * @return boolean
	 * @throws Exception
	 */
	protected function preSetElement( $index, $value ) {
		/**
		 * @var DiffOp $value
		 */
		if ( $this->isAssociative === false && ( $value->getType() !== 'add' && $value->getType() !== 'remove' ) ) {
			throw new Exception( 'Diff operation with invalid type "' . $value->getType() . '" provided.' );
		}

		if ( array_key_exists( $value->getType(), $this->typePointers ) ) {
			$this->typePointers[$value->getType()][] = $index;
		}
		else {
			throw new Exception( 'Diff operation with invalid type "' . $value->getType() . '" provided.' );
		}

		return true;
	}

	/**
	 * @see GenericArrayObject::getSerializationData
	 *
	 * @since 0.1
	 *
	 * @return array
	 */
	protected function getSerializationData() {
		$assoc = $this->isAssociative === null ? 'n' : ( $this->isAssociative ? 't' : 'f' );

		return array_merge(
			parent::getSerializationData(),
			array(
				'typePointers' => $this->typePointers,
				'assoc' => $assoc
			)
		);
	}

	/**
	 * @see GenericArrayObject::unserialize
	 *
	 * @since 0.1
	 *
	 * @param string $serialization
	 *
	 * @return array
	 */
	public function unserialize( $serialization ) {
		$serializationData = parent::unserialize( $serialization );

		$this->typePointers = $serializationData['typePointers'];

		if ( array_key_exists( 'assoc', $serializationData ) ) {
			$this->isAssociative = $serializationData['assoc'] === 'n' ? null : $serializationData['assoc'] === 't';
		} // The below cases are compat with < 0.4.
		elseif ( $this instanceof MapDiff ) {
			$this->isAssociative = true;
		}
		elseif ( $this instanceof ListDiff ) {
			$this->isAssociative = false;
		}
		else {
			$this->isAssociative = null;
		}


		return $serializationData;
	}
{'fraction_non_alphanumeric': 0.15823095823095823, 'fraction_numerical': 0.006142506142506142, 'mean_word_length': 3.2667714884696015, 'pattern_counts': {'":': 0, '<': 13, '<?xml version=': 0, '>': 66, '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': '217514', 'n_tokens_mistral': 2932, 'n_tokens_neox': 2594, 'n_words': 678}