Monads

Overview

The monads interface is useful in all kinds of ways, since monads are so fundamental to Emdros.

C++ interface


#include <monads.h>

class BadMonadsException {};

class MonadSetElement {
public:
    MonadSetElement(monad_m first, monad_m last);
    MonadSetElement(monad_m monad);
    monad_m first(void) const; // Gets the member variable
    monad_m last(void) const;  // Gets the member variable
    void printConsole(EMdFOutput *pOut) const;
    void printXML(EMdFOutput *pOut) const;

    // Returns true on this and b having the same first_m and
    // this and b having the same last_m.
    bool equals(const MonadSetElement& b) const;

    // Returns true on this object being 
    // "lexicographically" before other object.
    // That is, true if and only if 
    // this->first_m < other.first_m 
    // or (this->first_m == other.first_m 
    //     and
    //     this->last_m < other.last_m)
    bool isBefore(const MonadSetElement& other) const;
};


class SOMConstIterator {
public:
  SOMConstIterator();
  ~SOMConstIterator();
  bool hasNext() const; // Is the iterator == end iterator?  Doesn't alter iterator
  const MonadSetElement& next(); // Gets current and advances iterator afterwards
  const MonadSetElement& current(); // Gets current without altering iterator
};


class SetOfMonads {
public:
  monad_m first() const;  // First in set
  monad_m last() const;   // Last in set
  SOMConstIterator const_iterator() const;  
  std::ostream& putme(std::ostream& s) const;
  void printConsole(EMdFOutput *pOut) const;
  void printXML(EMdFOutput *pOut) const;
  SetOfMonads() {};
  /** Constructor from compact string.
   * 
   * Constructs a set from a compact string.  This would previously
   * have been gotten from SetOfMonads::toCompactString().
   *
   * @param compactString The compact string to create from.
   */
  SetOfMonads(const std::string& compactString) : m_first(MAX_MONAD), m_last(0) { 
       fromCompactString(compactString); };
  /** Constructor for singleton set.
   * 
   * Constructs a set with (initially) only one monad, \p m.
   *
   * @param m The monad to add to the empty set.
   */
  SetOfMonads(monad_m m) { add(m); };
  /** Constructor for single range set.
   * 
   * Constructs a set with (initially) a single range, from \p first
   * to \p last.
   *
   * @param first The first monad of the range to add to an empty set.
   * @param last The last monad of the range to add to an empty set.
   */
  SetOfMonads(monad_m first, monad_m last) : m_first(first), m_last(last) { 
         monad_ms.push_back(MonadSetElement(first,last)); };
  ~SetOfMonads();
  SetOfMonads& operator=(const SetOfMonads& som); // Not SWIG-wrapped
  std::string toString(void) const; // Get string-representation
  bool part_of(const SetOfMonads& other) const;
  void unionWith(const SetOfMonads& other);
  void difference(const SetOfMonads& other);
  static SetOfMonads intersect(const SetOfMonads& Aset, const SetOfMonads& Bset);
  void addMSE(MonadSetElement mse);
  void add(monad_m monad);
  void add(monad_m first, monad_m last);
  bool equals(SetOfMonads& other) const; // A proxy for operator==
  bool operator==(SetOfMonads& other) const; // Not SWIG-wrapped
  bool isMemberOf(monad_m m) const;
  bool isEmpty(void) const;
  void getMonad_mVector(std::vector<monad_m>& monad_vec) const; // Not SWIG-wrapped
  void getMonad_mList(std::list<monad_m>& monad_list) const; // Not SWIG-wrapped
  void getGapVector(std::vector<monad_m>& gap_vec) const; // Not SWIG-wrapped
  void removeMSE(const MonadSetElement& mse);
  bool gapExists(monad_m Sm, monad_m& m) const; // Gap exists in
    // set, starting at Sm, finishing at m
  void offset(monad_m m); // Add m to all mse's in set
  void clear(); // Make empty	 
  std::string toCompactString(void) const;
  void fromCompactString(const std::string& inStr);
  /** Check whether set is a singleton or single range.
   *
   * Returns true if there is only one MonadSetElement in the set.
   * This can be either a singleton or a single range.
   *
   * @return true if there is only one MonadSetElement, false if not.
   */
  bool hasOnlyOneMSE(void) const; 
};

Examples

A SetOfMonads can be used like this:


  // Declare and fill som with monads
  SetOfMonads som;
  som.add(1);      // Now is { 1 }
  som.add(3,6);    // Now is { 1, 3-6 }
  som.add(10,13) ; // Now is { 1, 3-6, 10-13 }

  // Get string representation
  std::string stringRepresentation = som.toString();

  // Declare and fill som2 with monads
  SetOfMonads som2;
  som2.add(2,7);  // Now is { 2-7 }

  // Declare and fill som3 with monads
  SetOfMonads som3;
  som3.add(2,4);  // Now is { 2-4 }
  

  // Add the monads of som2 to som
  som.unionWith(som2) // som is now { 1-7, 10-13 }

  // Get set intersection of som and som3
  SetOfMonads som4;
  som4 = SetOfMonads::intersect(som, som3); // som4 is now { 2-4 }

  // Subtract the monads of som2 from som
  som.difference(som2); // som is now { 10-13 }

The SOMConstIterator can be used like this:


  SetOfMonads som; // Assumed to be initialized from somewhere
  SOMConstIterator sci = som.const_iterator();
  while (sci.hasNext()) {
    MonadSetElement mse = sci.current();
   
    // Process mse ...

    // Are there any left?
    if (sci.hasNext()) {
       // Do something if the just-processed mse is not the last
    }

    sci.next();
  }

  // Or like this:
  SetOfMonads som; // Assumed to be initialized from somewhere
  SOMConstIterator sci = som.const_iterator();
  while (sci.hasNext()) {
    MonadSetElement mse = sci.next();
   
    // Process mse ...

    // Note how there is no si.next() again, since next() 
    // first gets current element and then advances iterator.

  }


Previous:MQL language
Up:Part II: APIs
Next:Statement