bpp-seq  2.1.0
Bpp/Seq/SymbolList.h
Go to the documentation of this file.
00001 //
00002 // File: SymbolList.h
00003 // Created by: Julien Dutheil
00004 // Created on: Fri Apr 9 2005
00005 //
00006 
00007 /*
00008 Copyright or © or Copr. Bio++ Development Team, (November 17, 2004)
00009 
00010 This software is a computer program whose purpose is to provide classes
00011 for sequences analysis.
00012 
00013 This software is governed by the CeCILL  license under French law and
00014 abiding by the rules of distribution of free software.  You can  use, 
00015 modify and/ or redistribute the software under the terms of the CeCILL
00016 license as circulated by CEA, CNRS and INRIA at the following URL
00017 "http://www.cecill.info". 
00018 
00019 As a counterpart to the access to the source code and  rights to copy,
00020 modify and redistribute granted by the license, users are provided only
00021 with a limited warranty  and the software's author,  the holder of the
00022 economic rights,  and the successive licensors  have only  limited
00023 liability. 
00024 
00025 In this respect, the user's attention is drawn to the risks associated
00026 with loading,  using,  modifying and/or developing or reproducing the
00027 software by the user in light of its specific status of free software,
00028 that may mean  that it is complicated to manipulate,  and  that  also
00029 therefore means  that it is reserved for developers  and  experienced
00030 professionals having in-depth computer knowledge. Users are therefore
00031 encouraged to load and test the software's suitability as regards their
00032 requirements in conditions enabling the security of their systems and/or 
00033 data to be ensured and,  more generally, to use and operate it in the 
00034 same conditions as regards security. 
00035 
00036 The fact that you are presently reading this means that you have had
00037 knowledge of the CeCILL license and that you accept its terms.
00038 */
00039 
00040 #ifndef _SYMBOLLIST_H_
00041 #define _SYMBOLLIST_H_
00042 
00043 #include "Alphabet/Alphabet.h"
00044 #include <Bpp/Clonable.h>
00045 
00046 // From the STL:
00047 #include <string>
00048 #include <vector>
00049 #include <algorithm>
00050 #include <iostream>
00051 
00052 namespace bpp
00053 {
00054 
00060   class SymbolList: 
00061     public virtual Clonable
00062   {
00063 
00064   public: 
00070 #ifndef NO_VIRTUAL_COV
00071     SymbolList* clone() const = 0;
00072 #endif
00073 
00075     // Class destructor
00076     virtual ~SymbolList() {}
00077 
00078   public:
00079 
00086     virtual const Alphabet* getAlphabet() const = 0;
00087 
00093     virtual size_t size() const = 0;
00094 
00106     virtual const std::vector<int>& getContent() const = 0;
00107 
00114     virtual void setContent(const std::vector<int>& list) throw (BadIntException) = 0;
00115 
00122     virtual void setContent(const std::vector<std::string>& list) throw (BadCharException) = 0;
00123 
00133     virtual std::string toString() const = 0;
00134 
00146     virtual void addElement(const std::string& c) throw (BadCharException) = 0;
00147 
00154     virtual void addElement(size_t pos, const std::string& c) throw (BadCharException, IndexOutOfBoundsException) = 0;
00155 
00162     virtual void setElement(size_t pos, const std::string& c) throw (BadCharException, IndexOutOfBoundsException) = 0;
00163 
00169     virtual void deleteElement(size_t pos) throw (IndexOutOfBoundsException) = 0;
00170 
00177     virtual void deleteElements(size_t pos, size_t len) throw (IndexOutOfBoundsException) = 0;
00178 
00179 
00185     virtual std::string getChar(size_t pos) const throw (IndexOutOfBoundsException) = 0;
00186 
00192     virtual void addElement(int v) throw (BadIntException) = 0;
00193 
00200     virtual void addElement(size_t pos, int v) throw (BadIntException, IndexOutOfBoundsException) = 0;
00201 
00208     virtual void setElement(size_t pos, int v) throw (BadIntException, IndexOutOfBoundsException) = 0;
00209 
00215     virtual int getValue(size_t pos) const throw (IndexOutOfBoundsException) = 0;
00216 
00235     virtual const int& operator[](size_t i) const = 0;
00242     virtual int& operator[](size_t i) = 0;
00243 
00247     virtual void shuffle() = 0;
00249   };
00250 
00251 
00264   class BasicSymbolList: 
00265     public virtual SymbolList
00266   {
00267 
00268   private:
00274     const Alphabet* alphabet_;
00275 
00276   protected:
00280     std::vector<int> content_;
00281 
00282   public: 
00288     BasicSymbolList(const Alphabet* alpha) : alphabet_(alpha), content_() {}
00289 
00298     BasicSymbolList(const std::vector<std::string>& list, const Alphabet* alpha) throw (BadCharException);
00299 
00308     BasicSymbolList(const std::vector<int>& list, const Alphabet* alpha) throw (BadIntException);
00309 
00313     BasicSymbolList(const SymbolList& list);
00314 
00318     BasicSymbolList(const BasicSymbolList& list);
00319 
00323     BasicSymbolList& operator=(const SymbolList& list);
00324 
00328     BasicSymbolList& operator=(const BasicSymbolList& list);
00329 
00335 #ifdef NO_VIRTUAL_COV
00336     Clonable*
00337 #else
00338     BasicSymbolList*
00339 #endif
00340     clone() const { return new BasicSymbolList(* this); }
00343     // Class destructor
00344     virtual ~BasicSymbolList() {}
00345 
00346   public:
00347 
00348     virtual const Alphabet* getAlphabet() const { return alphabet_; }
00349 
00350     virtual size_t size() const { return static_cast<size_t>(content_.size()); }
00351 
00352     virtual const std::vector<int>& getContent() const { return content_; }
00353     
00354     virtual void setContent(const std::vector<int>& list) throw (BadIntException);
00355 
00356     virtual void setContent(const std::vector<std::string>& list) throw (BadCharException);
00357 
00358     virtual std::string toString() const;
00359 
00360     virtual void addElement(const std::string& c) throw (BadCharException);
00361 
00362     virtual void addElement(size_t pos, const std::string& c) throw (BadCharException, IndexOutOfBoundsException);
00363 
00364     virtual void setElement(size_t pos, const std::string& c) throw (BadCharException, IndexOutOfBoundsException);
00365 
00366     virtual void deleteElement(size_t pos) throw (IndexOutOfBoundsException);
00367     
00368     virtual void deleteElements(size_t pos, size_t len) throw (IndexOutOfBoundsException);
00369 
00370     virtual std::string getChar(size_t pos) const throw (IndexOutOfBoundsException);
00371 
00372     virtual void addElement(int v) throw (BadIntException);
00373 
00374     virtual void addElement(size_t pos, int v) throw (BadIntException, IndexOutOfBoundsException);
00375 
00376     virtual void setElement(size_t pos, int v) throw (BadIntException, IndexOutOfBoundsException);
00377 
00378     virtual int getValue(size_t pos) const throw (IndexOutOfBoundsException);
00379 
00380     virtual const int& operator[](size_t i) const { return content_[i]; }
00381     
00382     virtual int& operator[](size_t i) { return content_[i]; }
00383 
00384     virtual void shuffle()
00385     {
00386       random_shuffle(content_.begin(), content_.end());
00387     }
00388   };
00389 
00390   class SymbolListEditionEvent
00391   {
00392   private:
00393     SymbolList* list_;
00394 
00395   public:
00396     SymbolListEditionEvent(SymbolList* list):
00397       list_(list) {}
00398 
00399     SymbolListEditionEvent(const SymbolListEditionEvent& slee): list_(slee.list_) {}
00400     
00401     SymbolListEditionEvent& operator=(const SymbolListEditionEvent& slee) { 
00402       list_ = slee.list_;
00403       return *this;
00404     }
00405 
00406     virtual ~SymbolListEditionEvent() {}
00407 
00408   public:
00409     virtual SymbolList* getSymbolList() { return list_; }
00410     virtual const SymbolList* getSymbolList() const { return list_; }
00411   };
00412 
00413 
00414   class SymbolListInsertionEvent:
00415     public SymbolListEditionEvent
00416   {
00417   private:
00418     size_t pos_;
00419     size_t len_;
00420 
00421   public:
00422     SymbolListInsertionEvent(SymbolList* list, size_t pos, size_t len):
00423       SymbolListEditionEvent(list), pos_(pos), len_(len) {}
00424 
00425   public:
00426     virtual size_t getPosition() const { return pos_; }
00427     virtual size_t getLength() const { return len_; }
00428   };
00429 
00430 
00431   class SymbolListDeletionEvent:
00432     public SymbolListEditionEvent
00433   {
00434   private:
00435     size_t pos_;
00436     size_t len_;
00437 
00438   public:
00439     SymbolListDeletionEvent(SymbolList* list, size_t pos, size_t len):
00440       SymbolListEditionEvent(list), pos_(pos), len_(len) {}
00441 
00442   public:
00443     virtual size_t getPosition() const { return pos_; }
00444     virtual size_t getLength() const { return len_; }
00445   };
00446 
00447 
00448   class SymbolListSubstitutionEvent:
00449     public SymbolListEditionEvent
00450   {
00451   private:
00452     size_t begin_;
00453     size_t end_;
00454 
00455   public:
00456     SymbolListSubstitutionEvent(SymbolList* list, size_t begin, size_t end) :
00457       SymbolListEditionEvent(list), begin_(begin), end_(end) {}
00458 
00459   public:
00460     virtual size_t getBegin() const { return begin_; }
00461     virtual size_t getEnd() const { return end_; }
00462   };
00463 
00464   class SymbolListListener :
00465     public virtual Clonable
00466   {
00467   public:
00468     virtual ~SymbolListListener() {}
00469 
00470 #ifndef NO_VIRTUAL_COV
00471     virtual SymbolListListener* clone() const = 0;
00472 #endif
00473 
00474   public:
00475     virtual bool isRemovable() const = 0;
00476     virtual bool isShared() const = 0;
00477     virtual void beforeSequenceChanged(const SymbolListEditionEvent& event) = 0;
00478     virtual void afterSequenceChanged(const SymbolListEditionEvent& event) = 0;
00479     virtual void beforeSequenceInserted(const SymbolListInsertionEvent& event) = 0;
00480     virtual void afterSequenceInserted(const SymbolListInsertionEvent& event) = 0;
00481     virtual void beforeSequenceDeleted(const SymbolListDeletionEvent& event) = 0;
00482     virtual void afterSequenceDeleted(const SymbolListDeletionEvent& event) = 0;
00483     virtual void beforeSequenceSubstituted(const SymbolListSubstitutionEvent& event) = 0;
00484     virtual void afterSequenceSubstituted(const SymbolListSubstitutionEvent& event) = 0;
00485   };
00486 
00487 
00500   class EdSymbolList: 
00501     public virtual SymbolList
00502   {
00503 
00504   private:
00510     const Alphabet* alphabet_;
00511 
00512     bool propagateEvents_;
00513 
00514   protected:
00518     std::vector<int> content_;
00519 
00523     std::vector<SymbolListListener*> listeners_;
00524 
00525 
00526   public: 
00532     EdSymbolList(const Alphabet* alpha) : alphabet_(alpha), propagateEvents_(true), content_(), listeners_() {}
00533 
00542     EdSymbolList(const std::vector<std::string>& list, const Alphabet* alpha) throw (BadCharException);
00543 
00552     EdSymbolList(const std::vector<int>& list, const Alphabet* alpha) throw (BadIntException);
00553 
00557     EdSymbolList(const SymbolList& list);
00558 
00562     EdSymbolList(const EdSymbolList& list);
00563 
00567     EdSymbolList& operator=(const SymbolList& list);
00568 
00572     EdSymbolList& operator=(const EdSymbolList& list);
00573 
00579 #ifdef NO_VIRTUAL_COV
00580     Clonable*
00581 #else
00582     EdSymbolList*
00583 #endif
00584     clone() const { return new EdSymbolList(* this); }
00587     // Class destructor
00588     virtual ~EdSymbolList()
00589     {
00590       for (size_t i = 0; i < listeners_.size(); ++i) {
00591         if (listeners_[i] && !listeners_[i]->isShared()) {
00592           delete listeners_[i];
00593         }
00594       }
00595     }
00596 
00597   public:
00598 
00599     virtual const Alphabet* getAlphabet() const { return alphabet_; }
00600 
00601     virtual size_t size() const { return static_cast<size_t>(content_.size()); }
00602 
00603     virtual const std::vector<int>& getContent() const { return content_; }
00604     
00605     virtual void setContent(const std::vector<int>& list) throw (BadIntException);
00606 
00607     virtual void setContent(const std::vector<std::string>& list) throw (BadCharException);
00608 
00609     virtual std::string toString() const;
00610 
00611     virtual void addElement(const std::string& c) throw (BadCharException);
00612 
00613     virtual void addElement(size_t pos, const std::string& c) throw (BadCharException, IndexOutOfBoundsException);
00614 
00615     virtual void setElement(size_t pos, const std::string& c) throw (BadCharException, IndexOutOfBoundsException);
00616 
00617     virtual void deleteElement(size_t pos) throw (IndexOutOfBoundsException);
00618     
00619     virtual void deleteElements(size_t pos, size_t len) throw (IndexOutOfBoundsException);
00620 
00621     virtual std::string getChar(size_t pos) const throw (IndexOutOfBoundsException);
00622 
00623     virtual void addElement(int v) throw (BadIntException);
00624 
00625     virtual void addElement(size_t pos, int v) throw (BadIntException, IndexOutOfBoundsException);
00626 
00627     virtual void setElement(size_t pos, int v) throw (BadIntException, IndexOutOfBoundsException);
00628 
00629     virtual int getValue(size_t pos) const throw (IndexOutOfBoundsException);
00630 
00631     virtual const int& operator[](size_t i) const { return content_[i]; }
00632     
00633     virtual int& operator[](size_t i) { return content_[i]; }
00634 
00635     virtual void shuffle()
00636     {
00637       random_shuffle(content_.begin(), content_.end());
00638     }
00639 
00645     virtual size_t getNumberOfListeners() const { return listeners_.size(); }
00646 
00647     virtual const SymbolListListener& getListener(size_t i) const {
00648       if (listeners_[i] == 0) std::cout << "aie!!!" << std::endl;
00649       return *listeners_[i];
00650     }
00651     
00652     virtual SymbolListListener& getListener(size_t i) { 
00653       if (listeners_[i] == 0) std::cout << "aie!!!" << std::endl;
00654       return *listeners_[i];
00655     }
00656 
00657     virtual void addSymbolListListener(SymbolListListener* listener) { 
00658       listeners_.push_back(listener);
00659     }
00660 
00661     virtual void removeSymbolListListener(SymbolListListener* listener) {
00662       if (listener->isRemovable())
00663         listeners_.erase(remove(listeners_.begin(), listeners_.end(), listener), listeners_.end());
00664       else
00665         throw Exception("EdSymbolList::removeSymbolListListener. Listener is not removable.");
00666     } 
00667  
00668   protected:
00669     virtual void beforeSequenceChanged(const SymbolListEditionEvent& event) {};
00670     virtual void afterSequenceChanged(const SymbolListEditionEvent& event) {};
00671     virtual void beforeSequenceInserted(const SymbolListInsertionEvent& event) {};
00672     virtual void afterSequenceInserted(const SymbolListInsertionEvent& event) {};
00673     virtual void beforeSequenceDeleted(const SymbolListDeletionEvent& event) {};
00674     virtual void afterSequenceDeleted(const SymbolListDeletionEvent& event) {};
00675     virtual void beforeSequenceSubstituted(const SymbolListSubstitutionEvent& event) {};
00676     virtual void afterSequenceSubstituted(const SymbolListSubstitutionEvent& event) {};
00677 
00678     void fireBeforeSequenceChanged(const SymbolListEditionEvent& event) {
00679       beforeSequenceChanged(event);
00680       if (propagateEvents_)
00681         for (size_t i = 0; i < listeners_.size(); ++i)
00682           listeners_[i]->beforeSequenceChanged(event);
00683     }
00684 
00685     void fireAfterSequenceChanged(const SymbolListEditionEvent& event) {
00686       afterSequenceChanged(event);
00687       if (propagateEvents_)
00688         for (size_t i = 0; i < listeners_.size(); ++i)
00689           listeners_[i]->afterSequenceChanged(event);
00690     }
00691    
00692     void fireBeforeSequenceInserted(const SymbolListInsertionEvent& event) {
00693       beforeSequenceInserted(event);
00694       if (propagateEvents_)
00695         for (size_t i = 0; i < listeners_.size(); ++i)
00696           listeners_[i]->beforeSequenceInserted(event);
00697     }
00698 
00699     void fireAfterSequenceInserted(const SymbolListInsertionEvent& event) {
00700       afterSequenceInserted(event);
00701       if (propagateEvents_)
00702         for (size_t i = 0; i < listeners_.size(); ++i)
00703           listeners_[i]->afterSequenceInserted(event);
00704     }
00705 
00706     void fireBeforeSequenceDeleted(const SymbolListDeletionEvent& event) {
00707       beforeSequenceDeleted(event);
00708       if (propagateEvents_)
00709         for (size_t i = 0; i < listeners_.size(); ++i)
00710           listeners_[i]->beforeSequenceDeleted(event);
00711     }
00712 
00713     void fireAfterSequenceDeleted(const SymbolListDeletionEvent& event) {
00714       afterSequenceDeleted(event);
00715       if (propagateEvents_)
00716         for (size_t i = 0; i < listeners_.size(); ++i)
00717           listeners_[i]->afterSequenceDeleted(event);
00718     }
00719 
00720     void fireBeforeSequenceSubstituted(const SymbolListSubstitutionEvent& event) {
00721       beforeSequenceSubstituted(event);
00722       if (propagateEvents_)
00723         for (size_t i = 0; i < listeners_.size(); ++i)
00724           listeners_[i]->beforeSequenceSubstituted(event);
00725     }
00726 
00727     void fireAfterSequenceSubstituted(const SymbolListSubstitutionEvent& event) {
00728       afterSequenceSubstituted(event);
00729       if (propagateEvents_)
00730         for (size_t i = 0; i < listeners_.size(); ++i)
00731           listeners_[i]->afterSequenceSubstituted(event);
00732     }
00735   protected:
00736     void propagateEvents(bool yn) { propagateEvents_ = yn; }
00737     bool propagateEvents() const { return propagateEvents_; }
00738 
00739   };
00740 
00741 
00742 } //end of namespace bpp.
00743 
00744 #endif // _SYMBOLLIST_H_
00745 
 All Classes Namespaces Files Functions Variables Typedefs Friends