/* -*- mode: c++ -*- 
*/
/* 

    GIFT, a flexible content based image retrieval system.
    Copyright (C) 1998, 1999, 2000 CUI, University of Geneva

    This program is free software; you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation; either version 2 of the License, or
    (at your option) any later version.

    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.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with this program; if not, write to the Free Software
    Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA

*/
// -*- mode: c++ -*-
#ifndef _CPROBABILISTICSET
#define _CPROBABILISTICSET

#include <assert.h>
#include <math.h>
#include <list>
#include <vector>
#include <algorithm>
#include <functional>
#include "CSelfDestroyPointer.h"
#include "CInformationCalculator.h"
#include "CProbabilityCombiner.h"
#include "CDrawer.h"
#include "CProbabilisticSetElement.h"
#include "binary_find_closest.h"
#include "CCutoffFunction.h"

template<class T>
class CProbabilisticSet;

///We are going to hide implementation details
template<class T>
class CProbabilisticSet{
  ///
  typedef CProbabilisticSetElement<T> CContentElement;
  ///
  typedef CCutoffFunction<CContentElement> LCCutoffFunction;
  ///
  typedef vector<CContentElement> CElementVector;
  ///
  typedef CElementVector CContent;
  ///
  typedef vector<T> CSequence;
  ///
  bool mIsNormalized;
  ///
  mutable bool mIsDrawPrepared;
  ///
  mutable double mSquashValue;
  /**@name Strategies */
  //@{
  ///
  mutable CSelfClonePointer<LCCutoffFunction> mSetCutoff;
  ///
  mutable CSelfClonePointer<LCCutoffFunction> mInformationCutoff;
  ///
  mutable CSelfClonePointer<CProbabilityCombiner> mUniteStrategy;
  ///
  mutable CSelfClonePointer<CProbabilityCombiner> mIntersectStrategy;
  ///
  mutable CSelfClonePointer<CInformationCalculator> mInformationCalculator;
  ///
  CSelfClonePointer<CDrawer> mDrawStrategy;
  //@}
  ///
  CContent mContent;
  ///
  bool mIsSorted;
  ///
  bool mIsDoublesRemoved;

protected:
  ///
  void prepareDraw()const;

public:
  /** Giving parameters to this constructor, one obtains strategies, which will NOT be cloned
      when this set is cloned with or without content.
      If one uses the defaults, new strategies are generated which will be cloned, if this is cloned
      or copied.
      Actually the former  will be the normal case: We are rather interested in having
      strategies which we can access and modify from the outside of this,
  */
  CProbabilisticSet(LCCutoffFunction* inInformationCutoff=0,
		    LCCutoffFunction* inSetCutoff=0,
		    CProbabilityCombiner* inIntersectStrategy=0,
		    CProbabilityCombiner* inUniteStrategy=0,
		    CInformationCalculator* inInformationCalculator=0,
		    CDrawer* inDrawStrategy=0);
  ///
  CProbabilisticSet(const CProbabilisticSet<T>& inSet,
		    bool inWithContent=true);
  ///
  CProbabilisticSet<T>* cloneWithoutContent()const;
  ///
  CProbabilisticSet<T>* clone()const;
  ///
  ~CProbabilisticSet();
  ///unite two probabilistic sets
  void unite(CProbabilisticSet& inSet);
  /** intersect two probabilistic sets.
      the return value is the normalizing factor which
      has to be applied after intersection
  */
  double intersect(CProbabilisticSet& inSet);
  ///Is inElement a member?
  double probabilityThatMember(const T& inElement)const;
  ///add an Element to the set
  void addElement(const T&,
		  double inProbability=1.0,
		  double inWeight=1.0);
  ///add an Element to the set
  void addElement(CProbabilisticSetElement<T> const&);
  /**@name For different Ways of intersection/union etc */
  //@{
  /// sorts this if it is unsorted;
  void sortIfUnsorted();
  /// sorts this if it is unsorted;
  void removeDoubles();
  ///
  void setUniteStrategy(CProbabilityCombiner const*);
  ///
  void setIntersectStrategy(CProbabilityCombiner const*);
  ///
  void setInformationCalculator(CInformationCalculator const*);
  /// uniform etc.
  void setDrawStrategy(CDrawer const*);
  /// 
  void setValidInterval(double inLowerCutoff, double inUpperCutoff);
  //@}
  /**@name probabilistic */
  //@{
  /// Calculates the entropy (or something similar) of the distribution
  double calculateInformation()const;
  ///draws a ContentElement from the
  CContentElement const& draw()const;
  ///draws a (unique!) set from this set
  CProbabilisticSet<T>* drawSet(long inWantedSize)const;
  ///
  CProbabilisticSet<T>* getTopNSet(long inN)const;
  ///draws a (unique!) set from this set
  CSequence* drawSequence(long inWantedSize)const;
  ///
  CSequence* getTopNSequence(long inN)const;
  /// gets the current sum of probabilities in this set
  double getProbabilityMax()const;
  /// gets the current sum of probabilities in this set
  int countAboveThreshold()const;
  /// gets the current sum of probabilities in this set
  double getProbabilitySum()const;
  /// a helper function for normalizing
  void multiplyProbabilities(double inMultiplier);
  /// Normalizes the distribution
  double normalize();
  //@}
  ///
  long size()const;
  ///
  ostream& output(ostream&)const;
  ///
  void getContent(CElementVector& outVector)const;
  ///this makes CProbabilisticSet<T> its own input iterator
  CProbabilisticSet<T>& operator=(const T& inT);
  ///this makes CProbabilisticSet<T> its own input iterator
  CProbabilisticSet<T>& operator=(const pair<T,double>& inT);
  ///this makes CProbabilisticSet<T> its own input iterator
  CProbabilisticSet<T>& operator++();
  ///gives it a uniform way of being deleted
  void clear();
};

#include "../cc/CProbabilisticSet.cc"

#endif

Documentation generated by muellerw@pc7170 on Son Okt 8 16:04:40 CEST 2000