We now have a current svn snapshot of libqxt in our contrib dir, and
[quassel.git] / src / contrib / libqxt-2007-10-24 / src / core / qxtpairList.h
1 /****************************************************************************
2 **
3 ** Copyright (C) Qxt Foundation. Some rights reserved.
4 **
5 ** This file is part of the QxtCore module of the Qt eXTension library
6 **
7 ** This library is free software; you can redistribute it and/or modify it
8 ** under the terms of th Common Public License, version 1.0, as published by
9 ** IBM.
10 **
11 ** This file is provided "AS IS", without WARRANTIES OR CONDITIONS OF ANY
12 ** KIND, EITHER EXPRESS OR IMPLIED INCLUDING, WITHOUT LIMITATION, ANY
13 ** WARRANTIES OR CONDITIONS OF TITLE, NON-INFRINGEMENT, MERCHANTABILITY OR
14 ** FITNESS FOR A PARTICULAR PURPOSE.
15 **
16 ** You should have received a copy of the CPL along with this file.
17 ** See the LICENSE file and the cpl1.0.txt file included with the source
18 ** distribution for more information. If you did not receive a copy of the
19 ** license, contact the Qxt Foundation.
20 **
21 ** <http://libqxt.sourceforge.net>  <foundation@libqxt.org>
22 **
23 ****************************************************************************/
24
25
26 /**
27 \class QxtPairList QxtPairList
28
29 \ingroup kit
30
31 \brief Searchable List of Pairs
32
33
34 Pair list provides a list with two values, a bit like QHash, but with posibility to operate on both values.
35
36 in contrast to QHash, every entry has a unique id, you can work with.  like a QList.
37
38 \code
39 QxtPairList<int,int> list;
40
41 list.append(1,2);
42 list.append(1,5);
43 list.append(5,6);
44
45 qDebug()<< list.find(1);    //  "0,1"
46 qDebug()<< list.find(SKIP,5);    //  "2"
47 qDebug()<< list.find(5);    //  "2"
48
49 \endcode
50 you may allso play around with the list itself
51
52 \code
53 list.list.append(qMakePair(1,2));
54 \endcode
55
56
57 */
58
59
60 #include <QList>
61 #include <QPair>
62 #include <QxtNullable>
63 #include <qxtglobal.h>
64
65
66
67 template <typename T, typename K>
68 class QXT_CORE_EXPORT QxtPairList
69 {
70
71
72 public:
73
74     QxtPairList()
75     {}
76
77     QxtPairList( const QxtPairList<T,K> & other )
78     {
79         list= other.list;
80     }
81
82     QxtPairList operator= ( const QxtPairList<T,K> & other )
83     {
84         list= other.list;
85     }
86
87
88     void append(T v1, K v2)
89     {
90         list.append(qMakePair(v1,v2));
91     }
92
93
94     /** \brief search entrys by match
95
96     both arguments are optional, due to the use of QxtNullable
97
98     \code
99     find(SKIP,v2);
100     find(v1,SKIP);
101     find(v1);
102     \endcode
103     are all valid
104     */
105
106     QList<int> find( qxNull(T,v1) , qxNull(K,v2) )
107     {
108         QList<int> found;
109
110         if ((!v1.isNull()) and (!v2.isNull()))
111         {
112             for (int i=0;i<list.count();i++)
113                 if ((list[i].first()==v1)and(list[i].second()==v2))
114                     found.append(i);
115
116             return found;
117         }
118
119         if ((!v1.isNull()) and (v2.isNull()))
120         {
121             for (int i=0;i<list.count();i++)
122                 if (list[i].first()==v1)
123                     found.append(i);
124
125             return found;
126         }
127
128         if ((v1.isNull()) and (!v2.isNull()))
129         {
130             for (int i=0;i<list.count();i++)
131                 if (list[i].second()==v2)
132                     found.append(i);
133
134             return found;
135         }
136
137
138
139     }
140
141
142
143     ///remove an entrys position by position
144     void remove(int nr)
145     {
146         list.removeAt(nr);
147     }
148
149     ///remove a list of entrys by position
150     void remove(QList<int> nrs)
151     {
152         int i;
153         foreach (i,nrs)
154         list.removeAt(i);
155     }
156
157
158
159
160     /** \brief operate on the list directly
161
162     you may use the internal list directly, but be carefull 
163     dont expect to work the QxPairList to work normal if you mess around with it.               
164     */
165
166
167     QList<QPair<T,K> > list;
168 };
169