PEBL 2.2
Psychology Experiment Building Language - Cross-platform psychological experiment development system
PList.cpp
Go to the documentation of this file.
1//* -*- mode:C++; tab-width:4; c-basic-offset:4; indent-tabs-mode:nil -*- */
3// Name: src/base/PList.cpp
4// Purpose: Contains a simple list class
5// Author: Shane T. Mueller, Ph.D.
6// Copyright: (c) 2003-2026 Shane T. Mueller <smueller@obereed.net>
7// License: GPL 2
8//
9//
10//
11// This file is part of the PEBL project.
12//
13// PEBL is free software; you can redistribute it and/or modify
14// it under the terms of the GNU General Public License as published by
15// the Free Software Foundation; either version 2 of the License, or
16// (at your option) any later version.
17//
18// PEBL is distributed in the hope that it will be useful,
19// but WITHOUT ANY WARRANTY; without even the implied warranty of
20// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21// GNU General Public License for more details.
22//
23// You should have received a copy of the GNU General Public License
24// along with PEBL; if not, write to the Free Software
25// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
27#include "PList.h"
28#include "Variant.h"
29#include "../utility/PError.h"
30#include "PEBLObject.h"
31//#include <list>
32#include <vector>
33#include <map>
34#include <stdio.h>
35
36using std::cout;
37using std::cerr;
38using std::endl;
39using std::flush;
40//using std::list;
41//using std::list;
42using std::vector;
43using std::multimap;
44using std::pair;
45using std::ostream;
46
49{
50
51 // mCDT = CDT_LIST;
52}
53
54
56{
57
58 mList.reserve((int)(reservedsize));
59}
60
61
64{
65
66
67 //mCDT=CDT_LIST;
68 const std::vector<Variant> * tmp = tmpList.GetList();
69
70 //Make an item-by-item copy of list into mList.
71 //Couldn't figure out how to use the stl copy algorithm to
72 //do this, so I will do it by hand.
74 // std::copy(tmp->begin(), tmp->end(), mList.begin());
75
76 std::vector<Variant>::const_iterator p;
77 for(p = tmp->begin(); p!=tmp->end(); p++)
78 {
79 mList.push_back(Variant(*p));
80 }
81
82}
83
87{
88
89 cout << "Cloning list\n";
90 PList * newlist = new PList();
91
92 //Make an item-by-item copy of list into mList.
93 //Couldn't figure out how to use the stl copy algorithm to
94 //do this, so I will do it by hand.
96 // std::copy(tmp->begin(), tmp->end(), mList.begin());
97
98 std::vector<Variant>::iterator p;
99 for(p = mList.begin(); p!=mList.end(); p++)
100 {
101 // This does not work. Need to make the contained list and then embed it.
102 // newlist->push_back(Variant(*p));
103 }
104
105 return newlist;
106}
107
110{
111
112}
113
114
115std::vector<Variant>::iterator PList::Begin()
116{
117 return mList.begin();
118}
119
120std::vector<Variant>::iterator PList::End()
121{
122 return mList.end();
123}
124
125
126//const versions of above
127std::vector<Variant>::const_iterator PList::Begin() const
128{
129 return mList.begin();
130}
131
132std::vector<Variant>::const_iterator PList::End() const
133{
134 return mList.end();
135}
136
137
138
139//void PList::PushFront(const Variant & v)
140//{
141 // mList.push_front(v);
142 //}
143
144//void PList::PopFront()
145//{
146// mList.pop_front();
147//}
148
150{
151
152 //Let's try to reduce time overhead by expanding using reserve to 50% larger than the current list.
153 //which is really a std::vector
154 if(mList.capacity() < mList.size()+1)
155 {
156 //increase reserve capacity by 25%
157 mList.reserve((int)(mList.size()*1.25));
158 }
159 mList.push_back(v);
160}
161
163{
164 mList.pop_back();
165}
166
167
168//returns the first element of the list
170{
171 if(mList.empty())
172 PError::SignalFatalError("Attempting to get First() element of empty list.");
173
174 return mList.front();
175
176}
177
178
179
180//This returns the nth item in a list (first item is 1).
181Variant PList::Nth(unsigned int n)
182{
183 //This is messed up--need a NULL variant type
184 //or error codes etc.
185 if(mList.size() < n)
186 PError::SignalFatalError("Attempting to get Nth element element of too-short list.");
187 if(n==0)
188 PError::SignalFatalError("Attempting to get 0th element element of too-short list.");
189
190 //std::vector<Variant>::iterator p = mList[n];
191 //.begin();
192 // for(unsigned int i = 1; i< n; i++,p++);
193 //return *p;
194
195 //Note: this is 1-indexed, so 1 is 0, etc.
196 return mList[n-1];
197}
198
199
200
201#if 0
202unsigned int PList::Length() const
203{
204 unsigned int length = 0;
205
206 std::vector<Variant>::const_iterator p;
207 p = mList.begin();
208
209 //Print out the first item, so comma-ing works out ok.
210 while( p != mList.end())
211 {
212 length++;
213 p++;
214 }
215
216 return length;
217}
218#endif
219
220
221
222void PList::SetElement(unsigned int n,Variant value)
223{
224
225 //This is messed up--need a NULL variant type
226 //or error codes etc.
227 if(mList.size() < n)
228 PError::SignalFatalError("Attempting to get Nth element element of too-short list.");
229 if(n==0)
230 PError::SignalFatalError("Attempting to get 0th element element of too-short list.");
231 mList[n-1] = value;
232
233}
234
236{
237 //This is messed up--need a NULL variant type
238 //or error codes etc.
239 if(mList.empty())
240 PError::SignalFatalError("Attempting to get last element element of empty list.");
241
242
243 std::vector<Variant>::iterator p = mList.end();
244 p--;
245 return *p;
246}
247
248
252
254{
255 //This is the map that the variant pairs will be put into
256 multimap<Variant, Variant> sortMap;
257
258 //Iterators for the key/data lists. Woe to he who allows
259 //these to be of different sizes.
260 std::vector<Variant>::const_iterator keyIterator = key.Begin();
261 std::vector<Variant>::const_iterator dataIterator = Begin();
262
263 //Go through the entire data list, inserting pairs into the map.
264 while(dataIterator != End())
265 {
266 sortMap.insert( pair<Variant,Variant>(*keyIterator, *dataIterator));
267 keyIterator++; //move through both lists.
268 dataIterator++;
269 }
270
271
272 //Now, sortmap should be in order of keyIterator. Make
273 //a new PList to put the data in.
274 PList * newList = new PList();
275
276 multimap<Variant, Variant>::iterator i=sortMap.begin();
277
278 while(i != sortMap.end())
279 {
280 newList->PushBack(i->second);
281 i++;
282 }
283 return counted_ptr<PEBLObjectBase>(newList);
284}
285
286
287//std::vector<Variant> * PList::GetList()
288//{
289//
290// return &mList;
291//}
292
293ostream & operator <<(std::ostream & out, const PList & list )
294{
295 list.SendToStream(out);
296 return out;
297}
298
299
300
305ostream & PList::SendToStream(ostream& out) const
306{
307 std::vector<Variant>::const_iterator p;
308 p = mList.begin();
309
310 out << "[" ;
311 //out << this->Length() << ":";
312
313 //Print out the first item, so comma-ing works out ok.
314 if( p != mList.end())
315 {
316 out << *p ;
317 p++;
318 while(p != mList.end())
319 {
320 //send each item to stream with space and comma in between.
321 out << ", " << *p << flush;
322 p++;
323 }
324 }
325 out << "]" ;
326
327 return out;
328}
@ CDT_LIST
Definition PEBLObject.h:56
ostream & operator<<(std::ostream &out, const PList &list)
Definition PList.cpp:293
Definition PList.h:45
std::vector< Variant >::const_iterator End() const
Definition PList.cpp:132
const std::vector< Variant > * GetList() const
Definition PList.h:96
void SetElement(unsigned int n, Variant value)
Definition PList.cpp:222
counted_ptr< PEBLObjectBase > SortBy(const PList &key)
Definition PList.cpp:253
std::vector< Variant >::const_iterator Begin() const
Definition PList.cpp:127
Variant Nth(unsigned int n)
Definition PList.cpp:181
unsigned long Length() const
Definition PList.h:89
Variant Last()
Definition PList.cpp:235
virtual PList * Clone()
Makes and returns a deep copy–notworking because copy makes deep copy anyway?
Definition PList.cpp:86
PList()
Standard Constructor.
Definition PList.cpp:48
void PushBack(const Variant &v)
Definition PList.cpp:149
void PopBack()
Definition PList.cpp:162
Variant First()
Definition PList.cpp:169
virtual ~PList()
Standard Destructor.
Definition PList.cpp:109
std::ostream & SendToStream(std::ostream &out) const
Definition PList.cpp:305
void SignalFatalError(const std::string &message)