File indexing completed on 2024-04-21 05:43:36

0001 //
0002 // C++ Interface: flowconnectorlist
0003 //
0004 // Description:
0005 //
0006 //
0007 // Author: David Saxton, Alan Grimes, Zoltan Padrah <zoltan.padrah@gmail.com>, (C) 2009
0008 //
0009 // Copyright: See COPYING file that comes with this distribution
0010 //
0011 //
0012 #ifndef FLOWCONNECTORLIST_H
0013 #define FLOWCONNECTORLIST_H
0014 
0015 #include "flowconnector.h"
0016 
0017 #include <QList>
0018 
0019 class Connector;
0020 class FlowConnector;
0021 
0022 // these typedef's shoud go in a separate header one day
0023 typedef QList<QPointer<Connector>> ConnectorList;
0024 
0025 /**
0026  * @short a list of connector between FlowNodes
0027  * @author David Saxton, Alan Grimes, Zoltan Padrah <zoltan.padrah@gmail.com>
0028  *
0029  * This class implements a list of FlowConnector objects; this class fulfills two
0030  * requirements:
0031  * 1. it provides type safety for classes related to flowparts
0032  * 2. can be cast to a generic ConnectorList, to be used in other contexts
0033  *
0034  * For QList interface see http://doc.trolltech.com/3.3/qlist.html
0035  */
0036 
0037 /*
0038     Notations used in the source code:
0039      O(n) : the method has this complexitiy
0040      assert ? : in that method some assertions could be made, considering
0041             that the two list should have the same contents
0042  */
0043 
0044 class FlowConnectorList
0045 {
0046 public:
0047     // cast operator, for casting it to ConnectorList
0048     operator ConnectorList()
0049     {
0050         return list;
0051     }
0052 
0053     // QList's interface
0054     typedef QPointer<FlowConnector> T;
0055 
0056 #define CAST_POINTER_CONN(p) (static_cast<QPointer<Connector> >(static_cast<Connector*>(static_cast<FlowConnector*>(p))))
0057 
0058     typedef QList<T>::iterator iterator;
0059     typedef QList<T>::const_iterator const_iterator;
0060     typedef T value_type;
0061     typedef value_type *pointer;
0062     typedef const value_type *const_pointer;
0063     typedef value_type &reference;
0064     typedef const value_type &const_reference;
0065     typedef size_t size_type;
0066 
0067     FlowConnectorList()
0068         : list()
0069         , flowList()
0070     {
0071     }
0072 
0073     FlowConnectorList(const QList<T> &l); /* : flowList(l) { // O(n)
0074    FlowConnectorList::iterator it, end = flowList.end();
0075    for( it = flowList.begin(); it != end; it++)
0076        list.append( CAST_POINTER *it);
0077 } */
0078 
0079     /* FlowConnectorList ( const std::list<T> & l ) ; */ /* : flowList(l) {
0080         FlowConnectorList::iterator it, end = flowList.end();
0081         for( it = flowList.begin(); it != end; it++)
0082             list.append( CAST_POINTER *it);
0083     } */
0084 
0085     ~FlowConnectorList()
0086     {
0087     } // leak check ?
0088 
0089     QList<T> &operator=(const QList<T> &l); /* { // -> O(n)
0090   flowList = l;
0091   list.clear();
0092   FlowConnectorList::iterator it, end = flowList.end();
0093   for( it = flowList.begin(); it != end; it++)
0094       list.append( CAST_POINTER  *it);
0095   return flowList;
0096 } */
0097 
0098     /* QList<T> & operator= ( const std::list<T> & l ) ; */ /* {    // O(n)
0099         flowList = l;
0100         list.clear();
0101         FlowConnectorList::iterator it, end = flowList.end();
0102         for( it = flowList.begin(); it != end; it++)
0103             list.append( CAST_POINTER *it);
0104 
0105         return flowList;
0106     } */
0107 
0108     /* bool operator== ( const std::list<T> & l ) const ; */ /* {
0109        return flowList == l;
0110    } */
0111 
0112     bool operator==(const QList<T> &l) const
0113     {
0114         return flowList == l;
0115     }
0116 
0117     bool operator!=(const QList<T> &l) const
0118     {
0119         return flowList != l;
0120     }
0121 
0122     iterator begin()
0123     {
0124         return flowList.begin();
0125     }
0126 
0127     const_iterator begin() const
0128     {
0129         return flowList.begin(); // ?
0130     }
0131 
0132     const_iterator constBegin() const
0133     {
0134         return flowList.constBegin();
0135     }
0136 
0137     iterator end()
0138     {
0139         return flowList.end();
0140     }
0141 
0142     const_iterator end() const
0143     {
0144         return flowList.end(); // ?
0145     }
0146 
0147     const_iterator constEnd() const
0148     {
0149         return flowList.constEnd();
0150     }
0151 
0152     iterator insert(iterator it, const T &x); /* {  // O(n)
0153    list.insert(  convertIterator( it ), CAST_POINTER x);
0154    return flowList.insert(it,x);
0155 } */
0156 
0157     uint remove(const T &x); /* {
0158    list.remove( CAST_POINTER  x);
0159    return flowList.remove(x);
0160 } */
0161 
0162     void clear()
0163     {
0164         flowList.clear();
0165         list.clear();
0166     }
0167 
0168     QList<T> &operator<<(const T &x); /* {
0169   list << CAST_POINTER  x;
0170   return flowList << x;
0171 } */
0172 
0173     size_type size() const
0174     { // assert ?
0175         return flowList.size();
0176     }
0177 
0178     bool empty() const
0179     { // assert ?
0180         return flowList.empty();
0181     }
0182 
0183     void push_front(const T &x); /* {
0184    list.push_front(CAST_POINTER x);
0185    flowList.push_front(x);
0186 } */
0187 
0188     void push_back(const T &x); /* {
0189    list.push_back(CAST_POINTER x);
0190    flowList.push_back(x);
0191 } */
0192 
0193     iterator erase(iterator it)
0194     { // O(n)
0195         list.erase(convertIterator(it));
0196         return flowList.erase(it);
0197     }
0198 
0199     iterator erase(iterator first, iterator last)
0200     { // O(n)
0201         list.erase(convertIterator(first), convertIterator(last));
0202         return flowList.erase(first, last);
0203     }
0204 
0205     reference front()
0206     {
0207         return flowList.front();
0208     }
0209 
0210     const_reference front() const
0211     {
0212         return flowList.front();
0213     }
0214 
0215     reference back()
0216     {
0217         return flowList.back();
0218     }
0219 
0220     const_reference back() const
0221     {
0222         return flowList.back();
0223     }
0224 
0225     void pop_front()
0226     {
0227         flowList.pop_front();
0228         list.pop_front();
0229     }
0230 
0231     void pop_back()
0232     {
0233         flowList.pop_back();
0234         list.pop_back();
0235     }
0236 
0237     /* void insert ( iterator pos, size_type n, const T & x ) ; */ /* {     // O(n)
0238         list.insert( convertIterator(pos) ,n, CAST_POINTER x);
0239         flowList.insert(pos,n,x);
0240     } */
0241 
0242     QList<T> operator+(const QList<T> &l) const
0243     {
0244         return flowList + l;
0245     }
0246 
0247     QList<T> &operator+=(const QList<T> &l); /* {   // O(n)
0248   const_iterator end = l.end();
0249   for(const_iterator it = l.begin(); it != end; it++)
0250       list.append( CAST_POINTER  *it );
0251   return flowList += l;
0252 } */
0253 
0254     /* iterator fromLast () ; */ /* {
0255         return flowList.fromLast();
0256     } */
0257 
0258     /* const_iterator fromLast () const ; */ /* {
0259         return flowList.fromLast();
0260     } */
0261 
0262     bool isEmpty() const
0263     {
0264         return flowList.isEmpty();
0265     }
0266 
0267     iterator append(const T &x); /* {
0268    list.append(CAST_POINTER x);
0269    // return flowList.append(x);
0270    flowList.append(x);
0271    iterator ret = flowList.end();
0272    --ret;
0273    return ret;
0274 } */
0275 
0276     iterator prepend(const T &x); /* {
0277    list.prepend(CAST_POINTER x);
0278    //return flowList.prepend(x);
0279    flowList.prepend(x);
0280    return flowList.begin();
0281 } */
0282 
0283     iterator remove(iterator it)
0284     {
0285         // -> O(n)
0286         list.erase(convertIterator(it));
0287         return flowList.erase(it);
0288     }
0289 
0290     T &first()
0291     { // assert ?
0292         return flowList.first();
0293     }
0294 
0295     const T &first() const
0296     { // assert ?
0297         return flowList.first();
0298     }
0299 
0300     T &last()
0301     { // assert ?
0302         return flowList.last();
0303     }
0304 
0305     const T &last() const
0306     { // assert ?
0307         return flowList.last();
0308     }
0309 
0310     T &operator[](size_type i)
0311     { // assert ?
0312         return flowList[i];
0313     }
0314 
0315     const T &operator[](size_type i) const
0316     { // assert ?
0317         return flowList[i];
0318     }
0319 
0320     iterator at(size_type i)
0321     { // assert ?
0322         // return flowList.at(i);
0323         iterator ret = flowList.begin();
0324         ret += i;
0325         return ret;
0326     }
0327 
0328     const_iterator at(size_type i) const
0329     { // assert ?
0330         // return flowList.at(i);
0331         const_iterator ret = flowList.constBegin();
0332         ret += i;
0333         return ret;
0334     }
0335 
0336     iterator find(const T &x)
0337     { // assert ?
0338         int i = flowList.indexOf(x);
0339         return (i == -1 ? flowList.end() : (flowList.begin() + i));
0340         // return flowList.find(x); // 2018.11.30
0341     }
0342 
0343     const_iterator find(const T &x) const
0344     { // assert ?
0345         int i = flowList.indexOf(x);
0346         return (i == -1 ? flowList.end() : (flowList.begin() + i));
0347         // return flowList.find(x); // 2018.11.30
0348     }
0349 
0350     iterator find(iterator it, const T &x)
0351     { // assert ?
0352         // return flowList.find(it, x); // 2018.11.30
0353         int i = flowList.indexOf(x, it - flowList.begin());
0354         return i == -1 ? flowList.end() : flowList.begin() + i;
0355     }
0356 
0357     const_iterator find(const_iterator it, const T &x) const
0358     { // assert ?
0359         // return flowList.find(it, x); // 2018.11.30
0360         int i = flowList.indexOf(x, it - flowList.begin());
0361         return i == -1 ? flowList.end() : flowList.begin() + i;
0362     }
0363 
0364     int indexOf(const T &x) const
0365     { // assert ?
0366         return flowList.indexOf(x);
0367     }
0368 
0369     size_type contains(const T &x) const
0370     {
0371         // return flowList.contains(x);
0372         return flowList.count(x);
0373     }
0374 
0375     size_type count() const
0376     { // assert ?
0377         return flowList.count();
0378     }
0379 
0380     QList<T> &operator+=(const T &x); /* {
0381   list += CAST_POINTER x;
0382   return flowList += x;
0383 } */
0384 
0385 private:
0386     ConnectorList list;
0387     QList<T> flowList;
0388 
0389     /**
0390      *    Converts an iterator from FlowConnector list to Connector list. Complexity: O(n) !
0391      * @param orig original iterator from FlowConnector list
0392      * @return iterator converted to Connector list
0393      */
0394     ConnectorList::iterator convertIterator(QList<T>::iterator orig)
0395     {
0396         ConnectorList::iterator it2 = list.begin();
0397         for (QList<T>::iterator it = flowList.begin(); it != orig; it++)
0398             it2++;
0399         return it2;
0400     }
0401 };
0402 
0403 #endif