// ---------------------------------------------------------------------------
// - set.hpp                                                                 -
// - standard object library - set class definition                          -
// ---------------------------------------------------------------------------
// - This program is free software;  you can redistribute it  and/or  modify -
// - it provided that this copyright notice is kept intact.                  -
// -                                                                         -
// - This program  is  distributed in  the hope  that it will be useful, but -
// - without  any  warranty;  without  even   the   implied    warranty   of -
// - merchantability or fitness for a particular purpose.  In no event shall -
// - the copyright holder be liable for any  direct, indirect, incidental or -
// - special damages arising in any way out of the use of this software.     -
// ---------------------------------------------------------------------------
// - copyright (c) 2005-2005 amaury darsch                                   -
// ---------------------------------------------------------------------------

#ifndef  AFNIX_SET_HPP
#define  AFNIX_SET_HPP

#ifndef  AFNIX_ITERABLE_HPP
#include "Iterable.hpp"
#endif

#ifndef  AFNIX_SERIAL_HPP
#include "Serial.hpp"
#endif

namespace afnix {

  /// The Set class is a base container class used to represent object set.
  /// Unlike other container, a set class store object uniquely using its
  /// pointer as a discriminator. Adding two times the same object result
  /// only in one object at the end. This property differentiate it from
  /// the other container.
  /// @author amaury darsch

  class Set : public Iterable, public Serial {
  private:
    /// the number of objects
    long d_size;
    /// the container structure
    struct s_selm* p_elem;
    
  public:
    /// create an empty set
    Set (void);

    /// create a set woth an object
    /// @param obj the object to add in the set
    Set (Object* obj);

    /// destroy this set
    ~Set (void);

    /// @return the class name
    String repr (void) const;

    /// make this set a shared object
    void mksho (void);

    /// @return the set serial code
    t_byte serialid (void) const;

    /// serialize this set cell to an output stream
    /// @param os the output stream to write
    void wrstream (class Output& os) const;

    /// deserialize a set from an input stream
    /// @param is the input steam to read in
    void rdstream (class Input& is);

    /// reset this set
    void reset (void);

    /// @return the cardinal of this set
    long cardinal (void) const;

    /// @return true if the object exists in this set
    bool exists (Object* obj) const;

    /// add an object to the set
    /// @param obj the object to add
    void add (Object* obj);

    /// @return a new iterator for this set
    Iterator* makeit (void);

    /// create a new set in a generic way
    /// @param argv the argument vector
    static Object* mknew (Vector* argv);

    /// apply this set cell with a set of arguments and a quark
    /// @param robj   robj the current runnable
    /// @param nset   the current nameset    
    /// @param quark  the quark to apply these arguments
    /// @param argv   the arguments to apply
    Object* apply (Runnable* robj, Nameset* nset, const long quark,
		   Vector* argv);

  private:
    // make the copy constructor private
    Set (const Set&);
    // make the assignment operator private
    Set& operator = (const Set&);
    // make the set iterator a friend
    friend class Setit;
  };

  /// The Setit class is the iterator for the set class. Such iterator
  /// is constructed with the "makeit" set method. The iterator is reset to
  /// the beginning of the set.
  /// @author amaury darsch

  class Setit : public Iterator {
  private:
    /// the set to iterate
    Set* p_set;
    /// the current set cell
    struct s_selm* p_elem;

  public:
    /// create a new iterator from a set
    /// @param set the set to iterate
    Setit (Set* set);

    /// destroy this set iterator
    ~Setit (void);

    /// @return the class name
    String repr (void) const;

    /// make this set iterator a shared object
    void mksho (void);

    /// reset the iterator to the begining
    void begin (void);

    /// reset the iterator to the end
    void end (void);

    /// move the iterator to the next position
    void next (void);

    /// move the iterator to the previous position
    void prev (void);

    /// @return the object at the current position
    Object* getobj (void);

    /// @return true if the iterator is at the end
    bool isend (void);

  private:
    // make the copy constructor private
    Setit (const Setit&);
    // make the assignment operator private
    Setit& operator = (const Setit&);
  };
}

#endif
