Rewrite FlagSet in order to run flag operation in constant time.
authorFlorent Bruneau <florent.bruneau@polytechnique.org>
Sat, 21 Jun 2008 15:41:09 +0000 (17:41 +0200)
committerFlorent Bruneau <florent.bruneau@polytechnique.org>
Sat, 21 Jun 2008 15:41:09 +0000 (17:41 +0200)
Rename the class PlFlagSet to conforms to core coding rules.

Signed-off-by: Florent Bruneau <florent.bruneau@polytechnique.org>
classes/plflagset.php [moved from classes/flagset.php with 69% similarity]
classes/session.php
classes/xdb.php
include/banana/forum.inc.php
include/emails.inc.php
include/xorg/session.inc.php
modules/events.php
modules/payment/money.inc.php
modules/profile.php
modules/profile/addresses.inc.php
modules/xnetgrp.php

similarity index 69%
rename from classes/flagset.php
rename to classes/plflagset.php
index 3e72330..1971751 100644 (file)
 
 /** 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;
     }
 }
 
index 99a404c..b2874eb 100644 (file)
@@ -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();
         }
     }
 
index ad63364..a9cb068 100644 (file)
@@ -166,7 +166,7 @@ class XDB
             return 'NULL';
 
           case 'object':
-            if ($var instanceof FlagSet) {
+            if ($var instanceof PlFlagSet) {
                 return "'" . addslashes($var->flags()) . "'";
             }
           case 'array':
index 89fa0c9..54502e3 100644 (file)
@@ -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');
             }
index 7f4f17d..f9c131a 100644 (file)
@@ -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.
index 6520c44..b3f66bf 100644 (file)
@@ -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) {
index bf02c22..29c74e9 100644 (file)
@@ -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');
index 65cdf6a..c38d1c3 100644 (file)
@@ -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);
     }
 
     // }}}
index cbe11bb..6b30017 100644 (file)
@@ -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);
 
index ec0f031..780ee03 100644 (file)
@@ -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');
         }
index d851442..88d0399 100644 (file)
@@ -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');
             }