From: Florent Bruneau Date: Sat, 21 Jun 2008 15:41:09 +0000 (+0200) Subject: Rewrite FlagSet in order to run flag operation in constant time. X-Git-Tag: core/1.0.0~88 X-Git-Url: http://git.polytechnique.org/?a=commitdiff_plain;h=113f6de8c1546c1d1caf6e6b48f5e10ea85fd211;p=platal.git Rewrite FlagSet in order to run flag operation in constant time. Rename the class PlFlagSet to conforms to core coding rules. Signed-off-by: Florent Bruneau --- diff --git a/classes/flagset.php b/classes/plflagset.php similarity index 69% rename from classes/flagset.php rename to classes/plflagset.php index 3e72330..1971751 100644 --- a/classes/flagset.php +++ b/classes/plflagset.php @@ -21,22 +21,26 @@ /** class for describing flags */ -class Flagset +class PlFlagSet { - /** string that holds the flagset */ - private $value; + /** string that holds the PlFlagSet */ + private $values = array(); /** the boundary between flags */ - private $sep = ","; + private $sep; /** set flag * @param $flags services FROM coupures * @return VOID */ - public function __construct($flags = "") + public function __construct($flags = '', $sep = ',') { - $this->value = $flags; + $this->sep = $sep; + $splitted = explode($sep, $flags); + foreach ($splitted as $part) { + $this->values[$part] = true; + } } @@ -46,12 +50,10 @@ class Flagset */ public function addFlag($flag) { - if (!$flag) return; - if (!$this->hasflag($flag)) { - if ($this->value) - $this->value .= $this->sep; - $this->value .= $flag; + if (empty($flag)) { + return; } + $this->values[$flag] = true; } @@ -61,12 +63,7 @@ class Flagset */ public function hasFlag($flag) { - $tok = strtok($this->value,$this->sep); - while ($tok) { - if ($tok==$flag) return 1; - $tok = strtok($this->sep); - } - return 0; + return !empty($flag) && isset($this->values[$flag]) && $this->values[$flag]; } /** test flag combination @@ -96,25 +93,29 @@ class Flagset */ public function rmFlag($flag) { - if (!$flag) return; - $newvalue = ""; - $tok = strtok($this->value,$this->sep); - while ($tok) { - if ($tok!=$flag) { - if ($newvalue) - $newvalue .= $this->sep; - $newvalue .= $tok; - } - $tok = strtok($this->sep); + if (empty($flag)) { + return; + } + if (isset($this->values[$flag])) { + unset($this->values[$flag]); } - $this->value=$newvalue; } - /** return the flagset + + /** return the PlFlagSet */ public function flags() { - return $this->value; + $flags = ''; + foreach ($this->values as $key=>$value) { + if (!empty($flags)) { + $flags .= $this->sep; + } + if ($value) { + $flags .= $key; + } + } + return $flags; } } diff --git a/classes/session.php b/classes/session.php index 99a404c..b2874eb 100644 --- a/classes/session.php +++ b/classes/session.php @@ -31,8 +31,8 @@ class Session require_once 'xorg.misc.inc.php'; $_SESSION['xsrf_token'] = rand_url_id(); } - if (!isset($_SESSION['perms']) || !($_SESSION['perms'] instanceof FlagSet)) { - $_SESSION['perms'] = new FlagSet(); + if (!isset($_SESSION['perms']) || !($_SESSION['perms'] instanceof PlFlagSet)) { + $_SESSION['perms'] = new PlFlagSet(); } } diff --git a/classes/xdb.php b/classes/xdb.php index ad63364..a9cb068 100644 --- a/classes/xdb.php +++ b/classes/xdb.php @@ -166,7 +166,7 @@ class XDB return 'NULL'; case 'object': - if ($var instanceof FlagSet) { + if ($var instanceof PlFlagSet) { return "'" . addslashes($var->flags()) . "'"; } case 'array': diff --git a/include/banana/forum.inc.php b/include/banana/forum.inc.php index 89fa0c9..54502e3 100644 --- a/include/banana/forum.inc.php +++ b/include/banana/forum.inc.php @@ -179,7 +179,7 @@ class ForumsBanana extends Banana if (Post::has('action') && Post::has('banananame') && Post::has('bananasig') && Post::has('bananadisplay') && Post::has('bananamail') && Post::has('bananaupdate') && Post::v('action')=="Enregistrer" ) { - $flags = new FlagSet(); + $flags = new PlFlagSet(); if (Post::b('bananadisplay')) { $flags->addFlag('threads'); } diff --git a/include/emails.inc.php b/include/emails.inc.php index 7f4f17d..f9c131a 100644 --- a/include/emails.inc.php +++ b/include/emails.inc.php @@ -273,7 +273,7 @@ class EmailStorage extends Email $res = XDB::query("SELECT mail_storage FROM auth_user_md5 WHERE user_id = {?}", $this->uid); - return new FlagSet($res->fetchOneCell()); + return new PlFlagSet($res->fetchOneCell()); } // Updates the list of active storages. diff --git a/include/xorg/session.inc.php b/include/xorg/session.inc.php index 6520c44..b3f66bf 100644 --- a/include/xorg/session.inc.php +++ b/include/xorg/session.inc.php @@ -180,7 +180,7 @@ class XorgSession public static function &make_perms($perm) { - $flags = new FlagSet(); + $flags = new PlFlagSet(); if ($perm == 'disabled' || $perm == 'ext') { return $flags; } @@ -286,7 +286,7 @@ function start_connexion ($uid, $identified) if (check_ip('ban')) { send_warning_mail($mail_subject); $_SESSION = array(); - $_SESSION['perms'] = new FlagSet(); + $_SESSION['perms'] = new PlFlagSet(); global $page; $newpage = false; if (!$page) { diff --git a/modules/events.php b/modules/events.php index bf02c22..29c74e9 100644 --- a/modules/events.php +++ b/modules/events.php @@ -377,7 +377,7 @@ class EventsModule extends PLModule $action = 'edit'; } else { $res = XDB::query('SELECT flags FROM evenements WHERE id = {?}', $eid); - $flags = new FlagSet($res->fetchOneCell()); + $flags = new PlFlagSet($res->fetchOneCell()); $flags->addFlag('wiki'); if (Post::v('important')) { $flags->addFlag('important'); diff --git a/modules/payment/money.inc.php b/modules/payment/money.inc.php index 65cdf6a..c38d1c3 100644 --- a/modules/payment/money.inc.php +++ b/modules/payment/money.inc.php @@ -49,7 +49,7 @@ class Payment $this->montant_min = (float)$this->montant_min; $this->montant_max = (float)$this->montant_max; - $this->flags = new Flagset($flags); + $this->flags = new PlFlagSet($flags); } // }}} diff --git a/modules/profile.php b/modules/profile.php index cbe11bb..6b30017 100644 --- a/modules/profile.php +++ b/modules/profile.php @@ -654,7 +654,7 @@ class ProfileModule extends PLModule WHERE user_id={?}", S::v('uid')); list($nom, $usage_old, $flags, $alias_old) = $res->fetchOneRow(); - $flags = new flagset($flags); + $flags = new PlFlagSet($flags); $page->assign('usage_old', $usage_old); $page->assign('alias_old', $alias_old); diff --git a/modules/profile/addresses.inc.php b/modules/profile/addresses.inc.php index ec0f031..780ee03 100644 --- a/modules/profile/addresses.inc.php +++ b/modules/profile/addresses.inc.php @@ -117,7 +117,7 @@ class ProfileAddress extends ProfileGeoloc private function saveAddress($adrid, array &$address) { - $flags = new FlagSet(); + $flags = new PlFlagSet(); if ($address['secondaire']) { $flags->addFlag('res-secondaire'); } diff --git a/modules/xnetgrp.php b/modules/xnetgrp.php index d851442..88d0399 100644 --- a/modules/xnetgrp.php +++ b/modules/xnetgrp.php @@ -224,7 +224,7 @@ class XnetGrpModule extends PLModule if (Post::has('submit')) { S::assert_xsrf_token(); - $flags = new FlagSet('wiki_desc'); + $flags = new PlFlagSet('wiki_desc'); if (Post::has('notif_unsub') && Post::i('notif_unsub') == 1) { $flags->addFlag('notif_unsub'); } @@ -1186,7 +1186,7 @@ class XnetGrpModule extends PLModule if (Post::v('valid') == 'Enregistrer') { $promo_min = ($art['public'] ? 0 : $art['promo_min']); $promo_max = ($art['public'] ? 0 : $art['promo_max']); - $flags = new FlagSet(); + $flags = new PlFlagSet(); if ($art['public']) { $flags->addFlag('public'); }