From broken mtmset, 1 Week ago, written in Plain Text.
This paste will perish in 1 Second.
  1. #ifndef MTM4_SET_H
  2. #define MTM4_SET_H
  3.  
  4. #include "exceptions.h"
  5.  
  6. namespace mtm {
  7.  
  8.     /**
  9.      * A set
  10.      * @tparam Type The type of an element on the group.
  11.      *      Required to implement:
  12.      *                             - operator ==
  13.      *                             - copy constructor
  14.      */
  15.     template<typename Type>
  16.     class MtmSet {
  17.         class Node;
  18.  
  19.         /**
  20.          * A node in the set
  21.          */
  22.         int set_size;//size of set, I declared this before set_start so that the operator == is a bit more efficient
  23.         //(it will check an integer before checking a Type)
  24.         Node *set_start; //this is the address of the first Node in the set, for saving the set beginning
  25.         class Node {
  26.         public:
  27.             Type data;
  28.             Node *next_node;
  29.             Node() : next_node(nullptr) {}
  30.             //empty constructor, next is initialized to nullpointer;
  31.  
  32.             explicit Node(Type node_data, Node *next = nullptr) : data(node_data), next_node(next) {}
  33.             //constructs a node and initializes its fields to the parameters.
  34.             // default value of next_node is nullpointer
  35.  
  36.             Node(const Node &other) = default; // copy constructor, uses default copy constructors for all fields
  37.  
  38.             ~Node() = default; // destructor, uses default destructor since no memory was allocated in the heap.
  39.  
  40.             Type &getData() {
  41.                 return this->data;
  42.             }//returns the Node's data.
  43.  
  44.             Node* getNext() {
  45.                 return this->next_node;
  46.             }//returns the Node's next_node.
  47.  
  48.             void setNext(Node *next) {
  49.                 this->next_node = next;
  50.             }//assigns the value of the Node's next_node to next.
  51.  
  52.             Node &operator=(const Node &other) = default;//assigns the value of the other node to this node,
  53.             //uses default assignment operators of all the Node's fields
  54.  
  55.         };
  56.  
  57.     public:
  58.         //Forward declaration
  59.         class const_iterator;
  60.  
  61.         /**
  62.          * A iterator for Set
  63.         */
  64.         class iterator {
  65.  
  66.         public:
  67.  
  68.             /**
  69.              * Empty constructor. Should not be dereferenced.
  70.              * Same as MtmSet::end()
  71.              */
  72.             iterator();
  73.  
  74.             /**
  75.              * Constructor of Set iterator
  76.              * @param node The node the iterator points to
  77.              */
  78.             explicit iterator(Node *node);
  79.  
  80.             /**
  81.              * Copy constructor
  82.              * @param it The iterator to copy
  83.              */
  84.             iterator(const iterator &it) = default;
  85.  
  86.             /**
  87.              * Destructor
  88.              */
  89.             ~iterator() = default;
  90.  
  91.             /**
  92.              * Copy assignment operator.
  93.              * @param rhs The iterator to copy.
  94.              * @return A reference to this iterator.
  95.              */
  96.             iterator &operator=(const iterator &rhs)= default;
  97.  
  98.             /**
  99.              * Dereference operator * .
  100.              * Used like dereference of a pointer with * .
  101.              * @return A reference of the data in the node the iterator
  102.              *  points to.
  103.              * @throws NodeIsEndException if the iterator doesn't point to
  104.              * an element in the set (end())
  105.              */
  106.             const Type &operator*() const;
  107.  
  108.             /**
  109.              * Dereference operator -> .
  110.              * Used like dereference of a pointer with -> .
  111.              * @return A pointer to the data in the node the iterator
  112.              *  points to.
  113.              * C++ use the -> operator on the returned pointer,
  114.              * this allows the user to treat the iterator like a pointer.
  115.              * @throws NodeIsEndException if the iterator doesn't point to
  116.              * an element in the set (end())
  117.              */
  118.             const Type *operator->() const;
  119.  
  120.             /**
  121.              * Prefix increment operator (++i)
  122.              * @return a reference to the iterator;
  123.              * @throws NodeIsEndException if the iterator doesn't point to
  124.              * an element in the set (end())
  125.              */
  126.             iterator &operator++();
  127.  
  128.             /**
  129.              * Postfix increment operator (i++)
  130.              * @return iterator that points to the same node as this before
  131.              * the increment
  132.              * @throws NodeIsEndException if the iterator doesn't point to
  133.              * an element in the set (end())
  134.              */
  135.             iterator operator++(int);
  136.  
  137.             /**
  138.              * Compare an iterator with const_iterator. (when there is a
  139.              *  regular iterator on the left.
  140.              * @param rhs the right const_iterator.
  141.              * @return true if the two iterators point to the same node
  142.              */
  143.             bool operator==(const const_iterator &rhs) const;
  144.  
  145.             /**
  146.              * Compare an iterator with const_iterator. (when there is a
  147.              *  regular iterator on the left.
  148.              * @param rhs the right const_iterator.
  149.              * @return true if the two iterators don't point to the same node
  150.              */
  151.             bool operator!=(const const_iterator &rhs) const;
  152.             friend class const_iterator;
  153.  
  154.         protected:
  155.             Node *node;//address of the current node in the set the iterator belongs to
  156.         };
  157.  
  158.         /**
  159.          * A const_iterator for Set
  160.         */
  161.         class const_iterator {
  162.             Node *node;//address of the current node in the set the iterator belongs to
  163.  
  164.         public:
  165.             /**
  166.              * Empty constructor. Should not be dereferenced.
  167.              * Same as MtmSet::end()
  168.              */
  169.             const_iterator();
  170.  
  171.             /**
  172.              * Constructor of Set const_iterator
  173.              * @param node The node the const_iterator points to
  174.              */
  175.             explicit const_iterator(Node *node);
  176.  
  177.             /**
  178.              * Copy constructor
  179.              * @param it The const_iterator to copy
  180.              */
  181.             const_iterator(const const_iterator &it)= default;
  182.  
  183.             /**
  184.              * Constructor from iterator (not const_iterator)
  185.              * Allows casting from iterator to const_iterator
  186.              * @param it The iterator to "copy" to a const_iterator
  187.              */
  188.             const_iterator(const iterator &it);
  189.  
  190.             /**
  191.              * Destructor
  192.              */
  193.             ~const_iterator()= default;
  194.  
  195.             /**
  196.              * Copy assignment operator.
  197.              * @param rhs The iterator to copy.
  198.              * @return A reference to this const_iterator.
  199.              */
  200.             const_iterator &operator=(const const_iterator &rhs);
  201.  
  202.             /**
  203.              * Dereference operator * .
  204.              * Used like dereference of a pointer with * .
  205.              * @return A reference of the data in the node the const_iterator
  206.              *  points to.
  207.              * @throws NodeIsEndException if the iterator doesn't point to
  208.              * an element in the set (end())
  209.              */
  210.             const Type &operator*() const;
  211.  
  212.             /**
  213.              * Dereference operator -> .
  214.              * Used like dereference of a pointer with -> .
  215.              * @return A pointer to the data in the node the const_iterator
  216.              *  points to.
  217.              *  C++ use the -> operator on the returned pointer,
  218.              *  this allows the user to treat the const_iterator like a pointer.
  219.              * @throws NodeIsEndException if the iterator doesn't point to
  220.              * an element in the set (end())
  221.              */
  222.             const Type *operator->() const;
  223.  
  224.             /**
  225.              * Prefix increment operator (++i)
  226.              * @return a reference to the iterator;
  227.              * @throws NodeIsEndException if the iterator doesn't point to
  228.              * an element in the set (end())
  229.              */
  230.             const_iterator &operator++();
  231.  
  232.             /**
  233.              * Postfix increment operator (i++)
  234.              * @return const_iterator that points to the same node as this
  235.              * before the increment
  236.              * @throws NodeIsEndException if the iterator doesn't point to
  237.              * an element in the set (end())
  238.              */
  239.             const_iterator operator++(int);
  240.  
  241.             /**
  242.              * Compare two const_iterators.
  243.              * @param rhs the right const_iterator
  244.              * @return true if the two const_iterators point to the same node
  245.              */
  246.             bool operator==(const const_iterator &rhs) const;
  247.  
  248.             /**
  249.              * Compare two const_iterators.
  250.              * @param rhs the right const_iterator
  251.              * @return true if the two const_iterators don't point to the same
  252.              * node
  253.              */
  254.             bool operator!=(const const_iterator &rhs) const;
  255.         };
  256.  
  257.         /**
  258.          * Empty constructor
  259.          * Creates an empty set
  260.          */
  261.         MtmSet();
  262.  
  263.         /**
  264.          * Copy constructor
  265.          * @param set the Set to copy
  266.          */
  267.         MtmSet(const MtmSet &set);
  268.  
  269.         /**
  270.          * Destructor
  271.          * Free all allocated memory in the set.
  272.          */
  273.         ~MtmSet();
  274.  
  275.         /**
  276.          * Insert a new element to the set, doesn't insert if there is already
  277.          *  an equal element in the set.
  278.          * If an element was't inserted, all iterators should stay valid.
  279.          * @param elem The element to insert to the set
  280.          * @return An iterator to the inserted element, or to the equal
  281.          * element if the element wasn't inserted.
  282.          */
  283.         iterator insert(const Type &elem);
  284.  
  285.         /**
  286.          * Remove an element from the set. If there is no element equal to
  287.          *  elem in the set, does nothing.
  288.          * If an element wasn't removed, all iterators should stay valid.
  289.          * @param elem the element to remove.
  290.          */
  291.         void erase(const Type &elem);
  292.  
  293.         /**
  294.          * Remove the element the iterator points to from the set.
  295.          * Iterator to another set is undefined.
  296.          * @param it The iterator to the element to the set.
  297.          */
  298.         void erase(const iterator &it);
  299.  
  300.         /**
  301.          * Get the amount of elements in the set.
  302.          * @return The amount of elements in the set.
  303.          */
  304.         int size() const;
  305.  
  306.         /**
  307.          * Check if the set is empty.
  308.          * @return true is the set is empty.
  309.          */
  310.         bool empty() const;
  311.  
  312.         /**
  313.          * Empty the set, free all allocated memory in the set.
  314.          */
  315.         void clear();
  316.  
  317.         /**
  318.          * Get an iterator to the first element in the set.
  319.          * If set is empty, return the same as end.
  320.          * @return
  321.          */
  322.         iterator begin();
  323.  
  324.         /**
  325.          * Get a const_iterator to the first element in the set.
  326.          * If set is empty, return the same as end.
  327.          * @return
  328.          */
  329.         const_iterator begin() const;
  330.  
  331.         /**
  332.          * Returns an iterator referring to the past-the-end element in the set.
  333.          * The past-the-end element is the theoretical element that would follow
  334.          * the last element in the set. It does not point to any element, and
  335.          * thus should not be dereferenced.
  336.          * @return Iterator to past-the-end element.
  337.          */
  338.         iterator end();
  339.  
  340.         /**
  341.          * Returns a const_iterator referring to the past-the-end element in
  342.          * the set.
  343.          * The past-the-end element is the theoretical element that would follow
  344.          * the last element in the set. It does not point to any element, and
  345.          * thus should not be dereferenced.
  346.          * @return const_iterator to past-the-end element.
  347.          */
  348.         const_iterator end() const;
  349.  
  350.         /**
  351.          * Find an element in the set.
  352.          * @param elem The element to find
  353.          * @return An iterator that points to the elem.
  354.          */
  355.         iterator find(const Type &elem);
  356.  
  357.         /**
  358.          * Find an element in the set.
  359.          * @param elem The element to find
  360.          * @return A const_iterator that points to the elem.
  361.          */
  362.         const_iterator find(const Type &elem) const;
  363.  
  364.         /**
  365.          * Check if an element is in the set.
  366.          * @param elem The element to check if it's in the set.
  367.          * @return True if the element is in the set, false otherwise.
  368.          */
  369.         bool contains(const Type &elem) const;
  370.  
  371.         /**
  372.          * Check if this set is a superset of a given set.
  373.          * Superset: A superset of B if B is a subset of A, that mean's that
  374.          *  A contains all the elements in B.
  375.          * @param subset The set to check if it's a subset.
  376.          * @return True if the given set is a subset of this set.
  377.          */
  378.         bool isSuperSetOf(const MtmSet &subset) const;
  379.  
  380.         /**
  381.          * Check if two set are equal, meaning, they contain the same elements.
  382.          * @param rhs The set right of the == operator.
  383.          * @return true if thw two set conatain the same elements, false
  384.          *  otherwise.
  385.          */
  386.         bool operator==(const MtmSet &rhs) const;
  387.  
  388.         /**
  389.          * Check if two set are equal, meaning, they contain the same elements.
  390.          * @param rhs The set right of the != operator.
  391.          * @return false if thw two set conatain the same elements, true
  392.          *  otherwise.
  393.          */
  394.         bool operator!=(const MtmSet &rhs) const;
  395.  
  396.         /**
  397.          * Insert all the elements in the given set to this set (union).
  398.          * @param set The set to insert all the elements from.
  399.          * @return A reference to this set.
  400.          */
  401.         MtmSet &unite(const MtmSet &set);
  402.  
  403.         /**
  404.          * Returns a new set that is an union of this set, and a given set.
  405.          * @param set The other set (other than this) to be in the union.
  406.          * @return The new set.
  407.          */
  408.         MtmSet unite(const MtmSet &set) const;
  409.  
  410.         /**
  411.          * Remove all the elements from this set, that are in the given set
  412.          * (intersection).
  413.          * @param set The other set in the intersection.
  414.          * @return A reference to this set.
  415.          */
  416.         MtmSet &intersect(const MtmSet &set);
  417.  
  418.         /**
  419.          * Returns a new set that is an intersection of this set,
  420.          *  and a given set.
  421.          * @param set The other set (other than this) to be in the intersection.
  422.          * @return The new set.
  423.          */
  424.         MtmSet intersect(const MtmSet &set) const;
  425.  
  426.         /**
  427.          * Remove all the elements in the set, that doesn't meet a given
  428.          *  condition.
  429.          * @tparam func - A function of an object-function that receive 1
  430.          *  argument, of the same type as an element in the set, and returns
  431.          *  a bool.
  432.          * @param condition - function (or object-function) that returns true
  433.          * if the element is meeting the condition and false otherwise.
  434.          * @return A reference to this set.
  435.          */
  436.         template<typename func>
  437.         MtmSet &getSubSet(func condition);
  438.  
  439.         /**
  440.          * Get a subset of this set, that contains all the elements in the set,
  441.          * that doesn't meet a given condition.
  442.          * @tparam func - A function of an object-function that receive 1
  443.          *  argument, of the same type as an element in the set, and returns
  444.          *  a bool.
  445.          * @param condition - function (or object-function) that returns true
  446.          * if the element is meeting the condition and false otherwise.
  447.          * @return A the new set.
  448.          */
  449.         template<typename func>
  450.         MtmSet getSubSet(func condition) const;
  451.     };
  452.  
  453.     /**
  454.  **MtmSet iterator functions
  455.  **/
  456.     template<class Type>
  457.     MtmSet<Type>::iterator::iterator() :
  458.             node(nullptr) {
  459.     }
  460.  
  461.     template<class Type>
  462.     MtmSet<Type>::iterator::iterator(Node *node) :
  463.             node(node) {
  464.     }
  465.  
  466.     template<class Type>
  467.     const Type& MtmSet<Type>::iterator::operator*() const {
  468.         if (node == nullptr) {
  469.             throw NodeIsEndException();
  470.         }
  471.         return node->getData();
  472.     }
  473.  
  474.     template<class Type>
  475.     const Type* MtmSet<Type>::iterator::operator->() const {
  476.         if (node == nullptr) {
  477.             throw NodeIsEndException();
  478.         }
  479.         return &(node->getData());
  480.     }
  481.  
  482.     template<class Type>
  483.     typename MtmSet<Type>::iterator& MtmSet<Type>::iterator::operator++() {
  484.         if (node == nullptr) {
  485.             throw NodeIsEndException();
  486.         }
  487.         node=(*node).getNext();
  488.         return (*this);
  489.     }
  490.  
  491.     template<class Type>
  492.     typename MtmSet<Type>::iterator MtmSet<Type>::iterator::operator++(int) {
  493.         if (node == nullptr) {
  494.             throw NodeIsEndException();
  495.         }
  496.         iterator result(*this);
  497.         ++(*this);
  498.         return result;
  499.     }
  500.  
  501.     template<class Type>
  502.     bool MtmSet<Type>::iterator::operator==(const const_iterator& rhs) const {
  503.         return rhs==(*this);//I can access both of their nodes' addresses in the constant_iterator equivilant function
  504.     }
  505.  
  506.     template<class Type>
  507.     bool MtmSet<Type>::iterator::operator!=(const const_iterator& rhs) const {
  508.         return (((*this) == rhs)==false);
  509.     }
  510.  
  511.     //------------ const_iterator methods ------------//
  512.  
  513.     template<class Type>
  514.     MtmSet<Type>::const_iterator::const_iterator() : node(nullptr){}
  515.  
  516.     template<class Type>
  517.     MtmSet<Type>::const_iterator::const_iterator(Node *node) : node(node){}
  518.  
  519.     template<class Type>
  520.     MtmSet<Type>::const_iterator::const_iterator(const iterator& it) : node(it.node){
  521.     }
  522.  
  523.     template<class Type>
  524.     typename MtmSet<Type>::const_iterator& MtmSet<Type>::const_iterator::operator=(
  525.             const const_iterator& rhs) {
  526.         this->node = rhs.node;
  527.         return (*this);
  528.     }
  529.  
  530.     template<class Type>
  531.     const Type& MtmSet<Type>::const_iterator::operator*() const {
  532.         if (node == nullptr) {
  533.             throw NodeIsEndException();
  534.         }
  535.         return node->getData();
  536.     }
  537.  
  538.     template<class Type>
  539.     const Type* MtmSet<Type>::const_iterator::operator->() const {
  540.         if (node == nullptr) {
  541.             throw NodeIsEndException();
  542.         }
  543.         return &(node->getData());
  544.     }
  545.  
  546.     template<class Type>
  547.     typename MtmSet<Type>::const_iterator& MtmSet<Type>::const_iterator::operator++() {
  548.         if (node == nullptr) {
  549.             throw NodeIsEndException();
  550.         }
  551.         this->node = node->getNext();
  552.         return (*this);
  553.     }
  554.  
  555.     template<class Type>
  556.     typename MtmSet<Type>::const_iterator MtmSet<Type>::const_iterator::operator++(
  557.             int) {
  558.         if (node == nullptr) {
  559.             throw NodeIsEndException();
  560.         }
  561.         const_iterator result = *this;
  562.         ++(*this);
  563.         return result;
  564.     }
  565.  
  566.     template<class Type>
  567.     bool MtmSet<Type>::const_iterator::operator==(const const_iterator& rhs) const {
  568.         return this->node == rhs.node;
  569.     }
  570.  
  571.     template<class Type>
  572.     bool MtmSet<Type>::const_iterator::operator!=(const const_iterator& rhs) const {
  573.         return !((*this) == rhs);
  574.     }
  575.  
  576.     /**
  577.      ** MtmSet functions
  578.      **/
  579.     template<class Type>
  580.     MtmSet<Type>::MtmSet() :  set_size(0), set_start(nullptr){}
  581.  
  582.     template<class Type>
  583.     MtmSet<Type>::MtmSet(const MtmSet &set) : MtmSet() {
  584.         for (const_iterator const_it = set.begin(); const_it != set.end(); const_it++) {
  585.             Type element_to_copy = (*const_it);
  586.             insert(element_to_copy);
  587.         }
  588.     }
  589.  
  590.     template<class Type>
  591.     MtmSet<Type>::~MtmSet() {
  592.         this->clear();
  593.     }
  594.  
  595.     template<class Type>
  596.     typename MtmSet<Type>::iterator MtmSet<Type>::insert(const Type& elem) {
  597.         if ((this->contains)(elem) == false) {
  598.             set_size++;
  599.             Node *new_node = new Node(elem, set_start);
  600.             set_start = new_node;
  601.             return begin();
  602.         }
  603.         return (this->find(elem));
  604.     }
  605.  
  606.     template<class Type>
  607.     void MtmSet<Type>::erase(const Type &elem) {
  608.         if ((this->contains)(elem) == false) {
  609.             return;
  610.         }
  611.         set_size--;
  612.         if(set_start->getData() == elem){
  613.             Node* temp = set_start;
  614.             set_start=set_start->getNext();
  615.             delete temp;
  616.             return;
  617.         }//for checking the beginning of set
  618.         Node *previous = set_start, *current, *next;
  619.         for(current = previous->getNext();current!= nullptr;current=current->getNext()) {
  620.             //for checking the rest of set
  621.             if (current->getData() == elem) {
  622.                 next = current->getNext();
  623.                 delete current;
  624.                 previous->setNext(next);
  625.                 return;
  626.             }
  627.         } //for checking the rest of set
  628. }
  629.  
  630.     template<class Type>
  631.     void MtmSet<Type>::erase(const iterator& it){
  632.         if(set_size > 0 && it!=end()) {
  633.             erase(*(it));
  634.         }
  635.     }
  636.  
  637.     template<class Type>
  638.     int MtmSet<Type>::size() const{
  639.         return set_size;
  640.     }
  641.  
  642.     template<class Type>
  643.     bool MtmSet<Type>::empty() const{
  644.         return (size()==0);
  645.     }
  646.  
  647.     template<class Type>
  648.     void MtmSet<Type>::clear(){
  649.         while(empty() == false){
  650.             iterator it = begin();
  651.             erase(it);
  652.         }
  653.     }
  654.  
  655.     template<class Type>
  656.     typename MtmSet<Type>::iterator MtmSet<Type>::begin(){
  657.         return iterator(set_start);
  658.     }
  659.  
  660.     template<class Type>
  661.     typename MtmSet<Type>::const_iterator MtmSet<Type>::begin() const{
  662.         return const_iterator(set_start);
  663.     }
  664.  
  665.     template<class Type>
  666.     typename MtmSet<Type>::iterator MtmSet<Type>::end(){
  667.         return iterator();
  668.     }
  669.  
  670.     template<class Type>
  671.     typename MtmSet<Type>::const_iterator MtmSet<Type>::end() const {
  672.         return const_iterator();
  673.     }
  674.  
  675.     template <class Type>
  676.     typename MtmSet<Type>::iterator MtmSet<Type>::find(const Type& elem){
  677.         iterator current = begin(), last = end();
  678.         if(set_size>0) {
  679.             while (current != last) {
  680.                 if (*current == elem) {
  681.                     return current;
  682.                 }
  683.                 ++current;
  684.             }
  685.         }
  686.         return current;//if element not found current points to beyond-end-element(nullptr)
  687.     }
  688.  
  689.     template<class Type>
  690.     typename MtmSet<Type>::const_iterator MtmSet<Type>::find(const Type& elem)
  691.     const{
  692.         const_iterator current = begin(), last = end();
  693.         if(set_size>0) {
  694.             while (current != last) {
  695.                 if ((*current) == elem) {
  696.                     return current;
  697.                 }
  698.                 ++current;
  699.             }
  700.         }
  701.         return current;//if element not found current is assigned to beyond-end-element(nullptr)
  702.     }
  703.  
  704.     template<class Type>
  705.     bool MtmSet<Type>::contains(const Type& elem) const{
  706.         if(find(elem) == iterator(nullptr)){
  707.             return false;
  708.         }//the find function returns nullptr if element is not in set.
  709.         return true;
  710.     }
  711.  
  712.     template<class Type>
  713.     bool MtmSet<Type>::isSuperSetOf(const MtmSet& subset) const{
  714.         const_iterator last = subset.end();
  715.         const_iterator current = subset.begin();
  716.         while(current != last) {
  717.             if(contains(*current) == false) {
  718.                 return false;
  719.             }
  720.             ++current;
  721.         }
  722.         return true;
  723.     }
  724.  
  725.     template<class Type>
  726.     bool MtmSet<Type>::operator==(const MtmSet& rhs) const{
  727.         return(set_size == rhs.set_size && isSuperSetOf(rhs));
  728.     }
  729.  
  730.     template<class Type>
  731.     bool MtmSet<Type>::operator!=(const MtmSet& rhs) const{
  732.         return (!((*this)==rhs));
  733.     }
  734.  
  735.     template <typename Type>
  736.     MtmSet<Type>& MtmSet<Type>::unite(const MtmSet& set){
  737.         const_iterator last=set.end();
  738.         const_iterator current=set.begin();
  739.         while(current!=last) {
  740.             (this->insert)(*current);
  741.             ++current;
  742.         }
  743.         return (*this);
  744.     }
  745.  
  746.     template <typename Type>
  747.     MtmSet<Type> MtmSet<Type>::unite(const MtmSet& set) const{
  748.         MtmSet<Type> new_set;
  749.         const_iterator last=set.end();
  750.         const_iterator current=set.begin();
  751.         while(current!=last) {
  752.             new_set.insert(*current);
  753.             ++current;
  754.         }
  755.         return new_set;
  756.     }
  757.  
  758.     template<class Type>
  759.     MtmSet<Type>& MtmSet<Type>::intersect(const MtmSet& set){
  760.         const_iterator last=this->end();
  761.         iterator current=this->begin();
  762.         while(current != last) {
  763.             if((set.contains(*current))==false) {
  764.                 this->erase(current);
  765.                 current=this->begin();
  766.             } else {
  767.                 ++current;
  768.             }
  769.         }
  770.         return (*this);
  771.     }
  772.  
  773.     template<class Type>
  774.     MtmSet<Type> MtmSet<Type>::intersect(const MtmSet& set) const{
  775.         MtmSet<Type> new_set;
  776.         iterator last=this->end();
  777.         iterator current=this->begin();
  778.         while(current != last) {
  779.             if(set.contains(*current)) {
  780.                 new_set.insert(*current);
  781.             }
  782.             ++current;
  783.         }
  784.         return new_set;
  785.     }
  786.  
  787.     template<class Type>
  788.     template<typename func>
  789.     MtmSet<Type>& MtmSet<Type>::getSubSet(func condition){
  790.         MtmSet<Type> subset_to_keep; //a subset of all the elements NOT to be removed.
  791.         iterator last=this->end();
  792.         iterator current=this->begin();
  793.         while(current != last) {
  794.             if(condition(*current)) {
  795.                 subset_to_keep.insert(*current);
  796.             }
  797.             ++current;
  798.         }
  799.         this->intersect(subset_to_keep);
  800.         return (*this);
  801.     }
  802.  
  803.     template<class Type>
  804.     template<typename func>
  805.     MtmSet<Type> MtmSet<Type>::getSubSet(func condition) const{
  806.         MtmSet<Type> new_set; //a subset of all the elements to be removed.
  807.         const_iterator last=this->end();
  808.         iterator current = this->begin();
  809.         while(current != last) {
  810.             if(condition(*current) == false) {
  811.                 new_set.insert(*current);
  812.             }
  813.             ++current;
  814.         }
  815.         return new_set;
  816.     }
  817. } // namespace mtm
  818.  
  819.  
  820. #endif //MTM4_SET_H