<commit_before><?php
/* This class is part of the XP framework
 *
 * $Id$ 
 */

  /**
   * LRU (last recently used) buffer.
   *
   * The last recently used (that is, the longest time unchanged) 
   * element will be deleted when calling add().
   *
   * @test     xp://net.xp_framework.unittest.util.collections.LRUBufferTest
   * @test     xp://net.xp_framework.unittest.util.collections.GenericsTest
   * @purpose  Abstract data type
   */
  class LRUBuffer extends Object {
    protected
      $size      = 0,
      $_access   = array(),
      $_elements = array();

    public
      $__generic = array();
    
    /**
     * Constructor
     *
     * @param   int size
     * @throws  lang.IllegalArgumentException is size is not greater than zero
     */
    public function __construct($size) {
      $this->setSize($size);
    }
    
    /**
     * Add an element to the buffer and return the id of the element 
     * which has been deleted in exchange. Returns NULL for the case 
     * that no element has been deleted (which is the case when the 
     * buffer's size has not yet been exceeded).
     *
     * <code>
     *   $deleted= $buf->add($key);
     * </code>
     *
     * @param   lang.Generic element
     * @return  lang.Generic victim
     */
    public function add(Generic $element) {
      if ($this->__generic && !$element instanceof $this->__generic[0]) {
        throw new IllegalArgumentException('Element '.xp::stringOf($element).' must be of '.$this->__generic[0]);
      }
      $h= $element->hashCode();
      $this->_access[$h]= microtime(TRUE);
      $this->_elements[$h]= $element;

      // Check if this buffer's size has been exceeded
      if (sizeof($this->_access) <= $this->size) return NULL;
      
      // Find the position of the smallest value and delete it
      $p= array_search(min($this->_access), $this->_access, TRUE);
      $victim= $this->_elements[$p];

      unset($this->_access[$p]);
      unset($this->_elements[$p]);

      return $victim;
    }
    
    /**
     * Update an element
     *
     * @param   lang.Generic element
     */
    public function update(Generic $element) {
      if ($this->__generic && !$element instanceof $this->__generic[0]) {
        throw new IllegalArgumentException('Element '.xp::stringOf($element).' must be of '.$this->__generic[0]);
      }
      $this->_access[$element->hashCode()]= microtime(TRUE);
    }
    
    /**
     * Get number of elements currently contained in this buffer
     *
     * @return  int
     */
    public function numElements() {
      return sizeof($this->_access);
    }
    <commit_msg>- Migrate to new generics syntax<commit_after><?php
/* This class is part of the XP framework
 *
 * $Id$ 
 */

  /**
   * LRU (last recently used) buffer.
   *
   * The last recently used (that is, the longest time unchanged) 
   * element will be deleted when calling add().
   *
   * @test     xp://net.xp_framework.unittest.util.collections.LRUBufferTest
   * @test     xp://net.xp_framework.unittest.util.collections.GenericsTest
   * @purpose  Abstract data type
   */
  #[@generic(self= 'T')]
  class LRUBuffer extends Object {
    protected
      $size      = 0,
      $_access   = array(),
      $_elements = array();

    /**
     * Constructor
     *
     * @param   int size
     * @throws  lang.IllegalArgumentException is size is not greater than zero
     */
    public function __construct($size) {
      $this->setSize($size);
    }
    
    /**
     * Add an element to the buffer and return the id of the element 
     * which has been deleted in exchange. Returns NULL for the case 
     * that no element has been deleted (which is the case when the 
     * buffer's size has not yet been exceeded).
     *
     * <code>
     *   $deleted= $buf->add($key);
     * </code>
     *
     * @param   T element
     * @return  T victim
     */
    #[@generic(params= 'T', return= 'T')]
    public function add($element) {
      $h= $element instanceof Generic ? $element->hashCode() : $element;
      $this->_access[$h]= microtime(TRUE);
      $this->_elements[$h]= $element;

      // Check if this buffer's size has been exceeded
      if (sizeof($this->_access) <= $this->size) return NULL;
      
      // Find the position of the smallest value and delete it
      $p= array_search(min($this->_access), $this->_access, TRUE);
      $victim= $this->_elements[$p];

      unset($this->_access[$p]);
      unset($this->_elements[$p]);

      return $victim;
    }
    
    /**
     * Update an element
     *
     * @param   T element
     */
    #[@generic(params= 'T')]
    public function update($element) {
      $h= $element instanceof Generic ? $element->hashCode() : $element;
      $this->_access[$h]= microtime(TRUE);
    }
    
    /**
     * Get number of elements currently contained in this buffer
     *
     * @return  int
     */
    public function numElements() {
      return sizeof($this->_access);
    }
    
{'fraction_non_alphanumeric': 0.13376309427880742, 'fraction_numerical': 0.0012087026591458502, 'mean_word_length': 2.3033932135728543, 'pattern_counts': {'":': 0, '<': 11, '<?xml version=': 0, '>': 43, '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': '5705117', 'n_tokens_mistral': 1490, 'n_tokens_neox': 1443, 'n_words': 434}