src/work/johanna/unionfind.h
changeset 481 54d8feda437b
parent 462 0ab31578af67
     1.1 --- a/src/work/johanna/unionfind.h	Thu Apr 29 16:30:39 2004 +0000
     1.2 +++ b/src/work/johanna/unionfind.h	Thu Apr 29 16:45:40 2004 +0000
     1.3 @@ -264,6 +264,26 @@
     1.4      }
     1.5  
     1.6      /**
     1.7 +     * \brief Insert the given element into the component of the others.
     1.8 +     *
     1.9 +     * This methods insert the element \e a into the component of the
    1.10 +     * element \e comp. 
    1.11 +     */
    1.12 +
    1.13 +    void insert(const T &a, const T &comp) {
    1.14 +      
    1.15 +      IIter clit = _find(m[comp]);
    1.16 +      ItemList &c = *clit->my_class;
    1.17 +      c.push_back(ItemType(a,0));
    1.18 +      IIter ai = c.end();
    1.19 +      --ai;
    1.20 +      ai->parent = clit;
    1.21 +      m.set(a, ai);
    1.22 +      ++clit->size;
    1.23 +    }
    1.24 +
    1.25 +
    1.26 +    /**
    1.27       * \brief Find the leader of the component of the given element.
    1.28       *
    1.29       * The method returns the leader of the component of the given element.
    1.30 @@ -385,6 +405,58 @@
    1.31        la->parent = ia;
    1.32      }
    1.33  
    1.34 +    /**
    1.35 +     * \brief Move the given element to an other component.
    1.36 +     *
    1.37 +     * This method moves the element \e a from its component
    1.38 +     * to the component of \e comp.
    1.39 +     * If \e a and \e comp are in the same component then
    1.40 +     * it returns false otherwise it returns true.
    1.41 +     */
    1.42 +
    1.43 +    bool move(const T &a, const T &comp) {
    1.44 +
    1.45 +      IIter ai = m[a];
    1.46 +      IIter lai = _find(ai);
    1.47 +      IIter clit = _find(m[comp]);
    1.48 +
    1.49 +      if (lai == clit)
    1.50 +	return false;
    1.51 +
    1.52 +      ItemList &c = *clit->my_class;
    1.53 +
    1.54 +      bool is_leader = (lai == ai);
    1.55 +      bool singleton = false;
    1.56 +
    1.57 +      if (is_leader) {
    1.58 +	++lai;
    1.59 +      }
    1.60 +
    1.61 +      c.splice(c.end(), *lai->my_class, ai);
    1.62 +
    1.63 +      if (is_leader) {
    1.64 +	if (ai->size == 1) {
    1.65 +	  classes.erase(ai->my_class);
    1.66 +	  singleton = true;
    1.67 +	}
    1.68 +	else {
    1.69 +	  lai->size = ai->size; 
    1.70 +	  lai->my_class = ai->my_class;	
    1.71 +	}
    1.72 +      }
    1.73 +      if (!singleton) {
    1.74 +	for (IIter i = lai; i != lai->my_class->end(); ++i)
    1.75 +	  i->parent = lai;
    1.76 +	--lai->size;
    1.77 +      }
    1.78 +
    1.79 +      ai->parent = clit;
    1.80 +      ai->my_class = 0;
    1.81 +      ++clit->size;
    1.82 +
    1.83 +      return true;
    1.84 +    }
    1.85 +
    1.86  
    1.87      /**
    1.88       * \brief Remove the given element from the structure.