<?php # Copyright(C) 2004-2007 INL # Written by Eric Leblond <regit@inl.fr> # Vincent Deffontaines <gryzor@inl.fr> # Jean Gillaux <jean@inl.fr> # Damien Boucard <damien.boucard AT inl.fr> # # $Id: set.class.php 17927 2009-02-16 13:16:09Z haypo $ # # This program is free software; you can redistribute it and/or modify # it under the terms of the GNU General Public License as published by # the Free Software Foundation, version 3 of the License. # # This program is distributed in the hope that it will be useful, # but WITHOUT ANY WARRANTY; without even the implied warranty of # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the # GNU General Public License for more details. # # You should have received a copy of the GNU General Public License # along with this program; if not, see <http://www.gnu.org/licenses/>. require_once('function.php'); // modifiedTimestamp() class set { var $elts; function add_elt($elt, $id=null) { if (is_null($id)) { $id = $elt->get_id(); } $this->_check_new_element($elt, $id); if (array_key_exists($id, $this->elts)) { throw new Exception(sprintf(_('Duplicate identifier (%s)!'), $id)); } $this->elts[$id] = $elt; } function replace_elt($elt, $id=null) { if (is_null($id)) { $id = $elt->get_id(); } if (array_key_exists($id, $this->elts)) { $old_elt = $this->elts[$id]; unset ($this->elts[$id]); } else { $old_elt = null; } try { // Try to add the new element $this->add_elt($elt, $id); } catch (Exception $err) { // On error: restore previous element $this->elts[$id] = $old_elt; throw $err; } } function _check_new_element($elt, $id) { if (array_key_exists($id, $this->elts)) { throw new Exception(sprintf(_('Duplicate identifier (%s)!'), $id)); } } function has_elt($elt) { return array_key_exists($elt, $this->elts); } function get_elt($elt){ if (array_key_exists($elt, $this->elts)) return $this->elts[$elt]; else return null; } function del_elt($index) { unset ($this->elts[$index]); } } ?>