PEBL 2.2
Psychology Experiment Building Language - Cross-platform psychological experiment development system
PEBLList.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/libs/PEBLList.cpp
4// Purpose: List Processing Function Library for PEBL
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 "PEBLList.h"
28#include "../base/Variant.h"
29#include "../base/PComplexData.h"
30#include "../base/PList.h"
31
32#include "../utility/PEBLUtility.h"
33#include "../utility/PError.h"
34#include "../utility/rc_ptrs.h"
35
36#include <list>
37#include <sstream>
38#include <string>
39#include <vector>
40#include <cmath>
41
42#include <iostream>
43
44using std::ostream;
45using std::cerr;
46using std::endl;
47using std::list;
48using std::vector;
50using std::string;
51
52
54{
55 //v is a list. v[1] should be have a list in it.
56 PList * plist = v.GetComplexData()->GetList();
57
58 Variant v1 = plist->First(); //plist->PopFront();
59
60 //v1 should be a list.
61 PError::AssertType(v1, PEAT_LIST, "Argument error in function [Shuffled(<list>)]: ");
62
63
64 PList * dataList = (PList*)(v1.GetComplexData()->GetObject().get());
65
66 //Now, make a keylist of random numbers, the length of datalist.
67 PList keyList;// = PList();
68
69 for(unsigned int i = 0; i < dataList->Length(); i++)
70 {
71 keyList.PushBack(RandomUniform());
72 }
73
74
75 //Now, sort by the key list
76 counted_ptr<PEBLObjectBase> newList = dataList->SortBy(keyList);
77 PComplexData * PCD =(new PComplexData(newList));
78
79 Variant tmp = Variant(PCD);
80 delete PCD;
81 PCD=NULL;
82 return tmp;
83
84 }
85
86
91{
92 //v is a list; v[1] is the object to repeat (can be anything)
93 PList * plist = v.GetComplexData()->GetList();
94 Variant v1 = plist->First(); //plist->PopFront();
95
96
97 //v[2] is the number of repeats: should be a number (will be rounded to integer if needed).
98 PError::AssertType(plist->Nth(2), PEAT_NUMBER, "Argument error in second parameter of function [Repeat(<object>,<number>)]: ");
99
100 Variant numVar = plist->Nth(2);
101 pDouble numDouble = numVar; // Use pDouble type to avoid ambiguity
102 int number = (int)round(numDouble);// Round to nearest integer to handle JSON floats
103
104
105 PList * returnList = new PList();
106 for(int i=0;i<number;i++)
107 {
108 returnList->PushBack(v1);
109 }
110
112 PComplexData * tmpPCD= (new PComplexData(tmplist));
113 Variant tmp = Variant(tmpPCD);
114 delete tmpPCD;
115 tmpPCD=NULL;
116 return tmp;
117
118}
119
122{
123 return v;
124}
125
126
131{
132 //v[1] should be a list. Just extract the iterators.
133 PList * plist = v.GetComplexData()->GetList();
134
135
136
137 Variant v1 = plist->First(); //plist->PopFront();
138 PError::AssertType(v1, PEAT_LIST, "Argument error in first parameter of function [RepeatList(<list>, <int>)]: ");
139
140 PList * arglist = (PList*)(v1.GetComplexData()->GetObject().get());
141 vector<Variant>::iterator p;
142 vector<Variant>::iterator pstart = arglist->Begin();
143 vector<Variant>::iterator pend = arglist->End();
144
145
146 //v[2] is the number of repeats: should be a number (will be rounded to integer if needed).
147 PError::AssertType(plist->Nth(2), PEAT_NUMBER, "Argument error in second parameter of function [RepeatList(<list>, <number>)]: ");
148 Variant numVar = plist->Nth(2);
149 pDouble numDouble = numVar; // Use pDouble type to avoid ambiguity
150 int number = (int)round(numDouble);// Round to nearest integer to handle JSON floats
151
152
153 //There is now an iterator p, and and end to compare it to, and
154 //an iterator to the pstart for resetting.
155 //Make a new list to return.
156
157 PList * returnList = new PList();
158
159 for(int i=0; i<number; i++)
160 {
161 p=pstart;
162 while (p != pend)
163 {
164 returnList->PushBack(*p);
165 p++;
166 }
167 }
168
170 PComplexData * tmpPCD= (new PComplexData(tmp2));
171 Variant tmp = Variant(tmpPCD);
172 delete tmpPCD;
173 tmpPCD=NULL;
174 return tmp;
175
176
177}
178
179
180
184
186{
187 //v is a list; v[1] is the bottom of the sequence
188 PList * plist = v.GetComplexData()->GetList();
189
190 PError::AssertType(plist->First(), PEAT_NUMBER, "Argument error in first parameter of function [Sequence(<first>, <last>, <step>)]");
191 Variant start = plist->First();// plist->PopFront();
192
193 //v[2] is the last number in the sequence
194 PError::AssertType(plist->Nth(2), PEAT_NUMBER, "Argument error in second parameter of function [Sequence(<first>, <last>, <step>)]: ");
195 Variant end = plist->Nth(2);// plist->PopFront();
196
197 //v[3] is the increment step of the sequence.
198 PError::AssertType(plist->Nth(3), PEAT_NUMBER, "Argument error in second parameter of function [Sequence(<first>, <last>, <step>)]: ");
199 Variant step = plist->Nth(3);// plist->PopFront();
200
201
202 //Make sure that the step is not equal to 0--we will never end if it is.
203 if(step == Variant(0))
204 {
205 PError::SignalFatalError("Step size in Sequence(<start>, <end>, <step>) is 0.");
206 }
207
208 Variant tolerance = .00000001;
209
210 PList * returnList = new PList();
211 //returnList->Clear();
212
213 Variant current;
214
215 //We need to operate differently if step is positive vs. negative.
216
217 if(step > Variant(0))
218 {
219 //If step is positive, end better be greater than start.
220 if(end < start)
221 {
222 PError::SignalFatalError("For positive <step>s, <end> must be greater than <start> in [Sequence(<start>, <end>, <step>)]");
223 }
224
225 //Step is positive, continue while less-than end
226 for(current = start; current <= end + tolerance; current = current + step)
227 {
228 returnList->PushBack(current);
229 }
230 }
231 else
232 {
233 //If step is negative, end better be smaller than start.
234 if(end > start)
235 {
236 PError::SignalFatalError("For negative <step>s, <end> must be less than <start> in [Sequence(<start>, <end>, <step>)]");
237 }
238
239 //Step is negative, continue while greater-than end.
240 for(current = start; current >= end - tolerance ; current = current + step)
241 {
242 returnList->PushBack(current);
243 }
244 }
245
247 PComplexData * tmpPCD= (new PComplexData(tmp2));
248 Variant tmp = Variant(tmpPCD);
249 delete tmpPCD;
250 tmpPCD=NULL;
251 return tmp;
252
253
254}
255
256
266{
267 //v[1] should be a list
268 /* counted_ptr<PList> plist = (v.GetComplexData())->GetList();
269 Variant v1 = plist->First(); plist->PopFront();
270 counted_ptr<PList> myList = v1.GetComplexData()->GetList();
271 */
272
273 PError::SignalFatalError("Function [RepeatExpression] not implemented");
274 return Variant(true);
275}
276
277
278
283{
284 PList * plist = v.GetComplexData()->GetList();
285
286 Variant v1 = plist->First(); //plist->PopFront();
287 //v[1] should be a list. Just extract the iterators.
288 PError::AssertType(v1, PEAT_LIST, "Argument error in first parameter of function [DesignFullCounterbalance(<list>, <list>)]: ");
289
290 PList * arglist1 = (PList*)(v1.GetComplexData()->GetObject().get());
291 vector<Variant>::iterator p1 = arglist1->Begin();
292 vector<Variant>::iterator p1end = arglist1->End();
293
294
295 Variant v2 = plist->Nth(2);// plist->PopFront();
296 //v[1] should be a list. Just extract the iterators.
297 PError::AssertType(v2, PEAT_LIST, "Argument error in second parameter of function [DesignFullCounterbalance(<list>, <list>)]: ");
298
299 PList * arglist2 = (PList*)(v2.GetComplexData()->GetObject().get());
300 vector<Variant>::iterator p2start = arglist2->Begin();
301 vector<Variant>::iterator p2;
302 vector<Variant>::iterator p2end = arglist2->End();
303
304 //There are now 2 list iterators: p1 and p2, and two ends to compare them to,
305 //and an iterator to the p2start for resetting.
306 //Make a new list to return.
307
308 PList * returnList = new PList();
309 PList * tmpList = NULL;
311 Variant tmpVariant;
312 PComplexData * tmpPCD;
313 while(p1 != p1end)
314 {
315 p2 = p2start;
316 while (p2 != p2end)
317 {
319 tmpList = new PList();
320 tmpList->PushBack(*p1);
321 tmpList->PushBack(*p2);
322
323 tmpobj = counted_ptr<PEBLObjectBase>(tmpList);
324
325 tmpPCD = (new PComplexData(tmpobj));
326 tmpVariant = Variant(tmpPCD);
327 delete tmpPCD;
328 tmpPCD=NULL;
329
330 //Put the sublist in the outer list
331 returnList->PushBack(tmpVariant);
332 p2++;
333 }
334
335 p1++;
336 }
337
338 tmpobj = counted_ptr<PEBLObjectBase>(returnList);
339 tmpPCD= (new PComplexData(tmpobj));
340 Variant tmp = Variant(tmpPCD);
341 delete tmpPCD;
342 tmpPCD=NULL;
343 return tmp;
344
345}
346
347
353{
354 PList * plist = v.GetComplexData()->GetList();
355
356 //v[1] should be a list. Just extract the iterators.
357
358 Variant v1 = plist->First();// plist->PopFront();
359 PError::AssertType(v1, PEAT_LIST, "Argument error in function [CrossFactorWithoutDuplicates(<list>)]: ");
360 PList * arglist = (PList*)(v1.GetComplexData()->GetObject().get());
361 vector<Variant>::iterator p1 = arglist->Begin();
362 vector<Variant>::iterator p2;
363 vector<Variant>::iterator pstart = p1;
364 vector<Variant>::iterator pend = arglist->End();
365
366 //There are now 2 list iterators: p1 and p2,
367 //and an iterator to the p2start for resetting.
368 //Make a new list to return.
369
370 PList * returnList = new PList();
371 PList * tmpList;
373 Variant tmpVariant;
374 PComplexData * tmpPCD;
375 int i1=0;
376 int i2=0;
377 while(p1 != pend)
378 {
379 p2 = pstart;
380 i2=0;
381 while (p2 != pend)
382 {
383 if(i1 != i2)
384 {
386 tmpList = new PList();
387 tmpList->PushBack(*p1);
388 tmpList->PushBack(*p2);
389
390
391 tmpObj = counted_ptr<PEBLObjectBase>(tmpList);
392
393 tmpPCD = (new PComplexData(tmpObj));
394 tmpVariant = Variant(tmpPCD);
395 delete tmpPCD;
396 tmpPCD=NULL;
397 //Put the sublist in the outer list
398 returnList->PushBack(tmpVariant);
399 }
400 p2++;
401 i2++;
402 }
403
404 p1++;
405 i1++;
406 }
407
408 tmpObj = counted_ptr<PEBLObjectBase>(returnList);
409 tmpPCD= new PComplexData(tmpObj);
410 Variant tmp = Variant(tmpPCD);
411 delete tmpPCD;
412 tmpPCD=NULL;
413 return tmp;
414
415}
416
418{
419 //v[1] should be a list. Just extract the iterators.
420 PList * plist = v.GetComplexData()->GetList();
421
422 Variant v1 = plist->First(); //plist->PopFront();
423 PError::AssertType(v1, PEAT_LIST, "Argument error in first parameter of function [Rotate(<list>, <n>)]: ");
424
425 Variant v2 = plist->Nth(2);// plist->PopFront();
426 PError::AssertType(v2, PEAT_INTEGER, "Argument error in second parameter of function [Rotate(<list>, <n>)]: ");
427
428
429 PList * arglist = v1.GetComplexData()->GetList();
430
431 vector<Variant>::iterator pi = arglist->Begin();
432 vector<Variant>::iterator pStart = pi;
433 vector<Variant>::iterator pListStart = pi;
434 vector<Variant>::iterator pEnd = arglist->End();
435
436
437 //startpoint is the 0-based index of which item to start the new list with.
438 int rotation = (int)v2;
439
440 long int length = arglist->Length();
441
442 //We need to find a startpoint as a positive number between 0 and length.
443 //The normal % and mod functions will not produce this.
444
445 int rem1 = rotation % length;
446
447 if(rem1<0)
448 {
449 rem1 = rem1 + (length);
450 }
451 pListStart += (rem1);
452
453
454
455 //Make a new list, starting with pListStart, and moving to
456 //the end, and then attaching pStart to pListStart.
457
458 PList * returnList = new PList();
460 Variant tmpVariant;
461
462 //Adjust the iterator to the start of the list.
463 pi=pListStart;
464 while(pi != pEnd)
465 {
466
467 //Put the item on the list
468 returnList->PushBack(*pi);
469 pi++;
470 }
471
472 pi=pStart;
473 while(pi != pListStart)
474 {
475
476 //Put the item on the list
477 returnList->PushBack(*pi);
478 pi++;
479 }
480
481 tmpObj = counted_ptr<PEBLObjectBase>(returnList);
482 PComplexData * tmpPCD= (new PComplexData(tmpObj));
483 Variant tmp = Variant(tmpPCD);
484 delete tmpPCD;
485 tmpPCD=NULL;
486 return tmp;
487
488
489}
490
493{
494
495 PList * plist = v.GetComplexData()->GetList();
496
497 //v[1] should be a list
498 Variant v1 = plist->First();// plist->PopFront();
499 PError::AssertType(v1, PEAT_LIST, "Argument error in function [Length(<list>)]: ");
500
501 PList * myList = (PList*)(v1.GetComplexData()->GetObject().get());
502 return Variant((long unsigned int)(myList->Length()));
503}
504
505
509{
510 //v[1] should be a list
511
512 PList * plist = v.GetComplexData()->GetList();
513
514 Variant v1 = plist->First(); //plist->PopFront();
515 PError::AssertType(v1, PEAT_LIST, "Argument error in function [First(<list>)]: ");
516
517 PList * myList = (PList*)(v1.GetComplexData()->GetObject().get());
518
519 return Variant(myList->First());
520}
522{
523 //v[1] should be a list
524
525 PList * plist = v.GetComplexData()->GetList();
526
527 Variant v1 = plist->First();// plist->PopFront();
528 PError::AssertType(v1, PEAT_LIST, "Argument error in function [Second(<list>)]: ");
529
530 PList * myList = (PList*)(v1.GetComplexData()->GetObject().get());
531 return myList->Nth(2);
532}
533
535{
536 //v[1] should be a list
537
538 PList * plist = v.GetComplexData()->GetList();
539
540 Variant v1 = plist->First(); //plist->PopFront();
541 PError::AssertType(v1, PEAT_LIST, "Argument error in function [Second(<list>)]: ");
542
543 PList * myList = (PList*)(v1.GetComplexData()->GetObject().get());
544 //vector<Variant>::iterator p = myList->Begin();
545 return myList->Nth(3);
546}
547
549{
550 //v[1] should be a list
551
552 PList * plist = v.GetComplexData()->GetList();
553
554 Variant v1 = plist->First(); //plist->PopFront();
555 PError::AssertType(v1, PEAT_LIST, "Argument error in function [Fourth(<list>)]: ");
556
557 PList * myList = (PList*)(v1.GetComplexData()->GetObject().get());
558 return myList->Nth(4);
559}
560
562{
563 //v[1] should be a list
564
565 PList * plist = v.GetComplexData()->GetList();
566
567 Variant v1 = plist->First(); //plist->PopFront();
568 PError::AssertType(v1, PEAT_LIST, "Argument error in function [Fifth(<list>)]: ");
569
570 PList * myList = (PList*)(v1.GetComplexData()->GetObject().get());
571 return myList->Nth(5);
572}
573
574//This returns the nth element of the list, counting from 1.
575//if it is longer than the list, it returns the last item.
577{
578 PList * plist = v.GetComplexData()->GetList();
579
580
581 //v[1] should be a list
582 Variant v1 = plist->First(); //plist->PopFront();
583 PError::AssertType(v1, PEAT_LIST, "Argument error in first parameter of function [Nth(<list>, <integer>)]: ");
584 PList * myList = (PList*)(v1.GetComplexData()->GetObject().get());
585
586 //v[2] should be an integer
587 PError::AssertType(plist->Nth(2), PEAT_INTEGER, "Argument error in second parameter of function [Nth(<list>, <integer>)]: ");
588 int n = plist->Nth(2);// plist->PopFront();
589 return Variant(myList->Nth(n));
590}
591
592
594{
595 //v[1] should be a list
596 PList * plist = v.GetComplexData()->GetList();
597
598 Variant v1 = plist->First();// plist->PopFront();
599 PError::AssertType(v1, PEAT_LIST, "Argument error in function [Last(<list>)]: ");
600
601 PList * myList = (PList*)(v1.GetComplexData()->GetObject().get());
602
603 return Variant(myList->Last());
604}
605
609{
610 PList * plist = v.GetComplexData()->GetList();
611
612 Variant v1 = plist->First();// plist->PopFront();
613 //v[1] should be a list. Just extract the iterators.
614 PError::AssertType(v1, PEAT_LIST, "Argument error in first parameter of function [Merge(<list>, <list>)]: ");
615 PList * tmpList = (PList*)(v1.GetComplexData()->GetObject().get());
616 vector<Variant>::iterator p1start = tmpList->Begin();
617 vector<Variant>::iterator p1 = p1start;
618 vector<Variant>::iterator p1end = tmpList->End();
619
620
621
622 Variant v2 = plist->Nth(2);// plist->PopFront();
623 //v[1] should be a list. Just extract the iterators.
624 PError::AssertType(v2, PEAT_LIST, "Argument error in second parameter of function [Merge(<list>, <list>)]: ");
625
626 tmpList = (PList*)(v2.GetComplexData()->GetObject().get());
627 vector<Variant>::iterator p2start = tmpList->Begin();
628 vector<Variant>::iterator p2 = p2start;
629 vector<Variant>::iterator p2end = tmpList->End();
630
631 //There are now 2 list iterators: p1 and p2, and two ends to compare them to,
632 //and an iterator to the p2start for resetting.
633 //Make a new list to return.
634
635 PList * returnList = new PList();
636
637 Variant tmpVariant;
638
639 //Add the first list.
640 while(p1 != p1end)
641 {
642 returnList->PushBack(*p1);
643 p1++;
644 }
645
646 //Add the second list.
647 while (p2 != p2end)
648 {
649 returnList->PushBack(*p2);
650 p2++;
651 }
652
654 PComplexData * tmpPCD= (new PComplexData(tmpObj));
655
656 Variant tmp = Variant(tmpPCD);
657 delete tmpPCD;
658 tmpPCD=NULL;
659 return tmp;
660
661
662
663}
664
665
666
670{
671
672 PList * plist = v.GetComplexData()->GetList();
673
674
675 Variant v1 = plist->First(); //plist->PopFront();
676 //v[1] should be a list. Just extract the iterators.
677 PError::AssertType(v1, PEAT_LIST, "Argument error in first parameter of function [Append(<list>, <list>)]: ");
678
679 PList * tmpList = (PList*)(v1.GetComplexData()->GetObject().get());
680 vector<Variant>::iterator p1 = tmpList->Begin();
681 vector<Variant>::iterator p1end = tmpList->End();
682
683 Variant v2 = plist->Nth(2); //plist->PopFront();
684 //v[2] can be anything.
685
686
687 //Make a new list to return.
688 PList * returnList = new PList();
689
690 Variant tmpVariant;
691
692 //Add the first list.
693 while(p1 != p1end)
694 {
695 returnList->PushBack(*p1);
696 p1++;
697 }
698
699 //Append v2.
700 returnList->PushBack(v2);
702 PComplexData * tmpPCD= (new PComplexData(tmpObj));
703 Variant tmp = Variant(tmpPCD);
704 delete tmpPCD;
705 tmpPCD=NULL;
706 return tmp;
707
708}
709
711{
712 PList * plist = v.GetComplexData()->GetList();
713 Variant v1 = plist->First();
714 PError::AssertType(v1, PEAT_LIST, "Argument error in first parameter of function [SetElement(<list>,<index>,<listitem>)]: ");
715
716
717 Variant v2 = plist->Nth(2);
718 PError::AssertType(v2, PEAT_INTEGER, "Argument error in second parameter of function [SetElement(<list>,<index>,<listitem>)]: ");
719
720
721 Variant v3 = plist->Nth(3);
722 //No need to check v3 for type.
723
724 PList * pl = v1.GetComplexData()->GetList();
725 pl->SetElement((int)v2,v3);
726
727 return Variant(true);
728}
729
730
731
732// This adds an element to a list
735{
736
737 PList * plist = v.GetComplexData()->GetList();
738
739 Variant v1 = plist->First(); //plist->PopFront();
740 //v[1] should be a list. Just extract the iterators.
741 PError::AssertType(v1, PEAT_LIST, "Argument error in first parameter of function [PushOnEnd(<list>, <listitem>)]: ");
742
743 //Get the list object, and iterators to it:
744 PList * tmpList = (PList*)(v1.GetComplexData()->GetObject().get());
745
746 //Get the other object:
747 Variant v2 = plist->Nth(2);// plist->PopFront();
748 //v[2] can be anything.
749
750 tmpList->PushBack(v2);
751 //v should still contain the list, so return it now.
752 return v1;
753}
754
755
756
757
760{
761 //v[1] should be a list
762 PList * plist = v.GetComplexData()->GetList();
763
764
765 Variant v1 = plist->First(); //plist->PopFront();
766 PError::AssertType(v1, PEAT_LIST, "Argument error in function [Sort(<list>)]: ");
767
768 PList * dataList = (PList*)(v1.GetComplexData()->GetObject().get());
769
770 counted_ptr<PEBLObjectBase> newList = dataList->SortBy(*dataList);
771 PComplexData * PCD = (new PComplexData(newList));
772 Variant tmp = Variant(PCD);
773 delete PCD;
774 PCD=NULL;
775 return tmp;
776
777}
778
779
782{
783 //v[1] should be a list
784 PList * plist = v.GetComplexData()->GetList();
785
786 Variant v1 = plist->First();// plist->PopFront();
787 PError::AssertType(v1, PEAT_LIST, "Argument error in first parameter of function [SortBy(<list>, <list>)]: ");
788
789 PList * dataList = (PList*)(v1.GetComplexData()->GetObject().get());
790
791 //v[2] should be the keys which the list should be sorted by.
792 Variant v2 = plist->Nth(2);// plist->PopFront();
793 PError::AssertType(v2, PEAT_LIST, "Argument error in second parameter of function [SortBy(<list>, <list>)]: ");
794 PList * keyList = (PList*)(v2.GetComplexData()->GetObject().get());
795
796 counted_ptr<PEBLObjectBase> newList = dataList->SortBy(*keyList);
797 PComplexData * PCD = ( new PComplexData(newList));
798 Variant tmp = Variant(PCD);
799 delete PCD;
800 PCD=NULL;
801 return tmp;
802
803}
804
805
806
808{
809 PList * plist = v.GetComplexData()->GetList();
810
811 //v[1] should be an item to check for membership.
812 Variant v1 = plist->First();// plist->PopFront();
813
814
815 Variant v2 = plist->Nth(2); // plist->PopFront();
816 PError::AssertType(v2, PEAT_LIST, "Argument error in second parameter of function [IsMember(<list>, <item>)]: ");
817
818 PList * tmpList = (PList*)(v2.GetComplexData()->GetObject().get());
819 vector<Variant>::iterator p = tmpList->Begin();
820 vector<Variant>::iterator pEnd = tmpList->End();
821
822 while(p != pEnd)
823 {
824 if(*p == v1)
825 return Variant(true);
826 p++;
827 }
828 return Variant(false);
829}
830
832{
833 //v[1] should be a list
834 /* counted_ptr<PList> plist = (v.GetComplexData())->GetList();
835 Variant v1 = plist->First(); plist->PopFront();
836 counted_ptr<PList> myList = v1.GetComplexData()->GetList();
837 */
838 PError::SignalFatalError("Function [RemoveDuplicates] not implemented");
839 return Variant(true);
840}
841
843{
844 //v[1] should be a list
845 /* counted_ptr<PList> plist = (v.GetComplexData())->GetList();
846 Variant v1 = plist->First(); plist->PopFront();
847 counted_ptr<PList> myList = v1.GetComplexData()->GetList();
848 */
849 PError::SignalFatalError("Function [MakeMap] not implemented");
850 return Variant(true);
851}
852
853
854
855
856
860{
861 //v[1] should be a list
862 PList * plist = v.GetComplexData()->GetList();
863 Variant v1 = plist->First(); //plist->PopFront();
864 PError::AssertType(v1, PEAT_LIST, "Argument error in function [Transpose(<list-of-lists>)]: ");
865
866 PList * tmpList = (PList*)(v1.GetComplexData()->GetObject().get());
867 vector<Variant>::iterator pstart = tmpList->Begin();
868 vector<Variant>::iterator p;
869// vector<Variant>::iterator pend = tmpList->End();
870
871 unsigned long int outerLength = tmpList->Length();
872 unsigned long int innerLength=0;
873
874 //Create a vector of iterators to elements of each sublist.
875 std::vector<vector<Variant>::iterator> listIterators(outerLength,pstart);
876
877 p = pstart;
878
879 for(unsigned int j = 0; j < outerLength; j++)
880 {
881 PError::AssertType(*p, PEAT_LIST, "Every item in Transpose(<list-of-lists>) must be a list. ");
882
883 tmpList = (PList*)((*p).GetComplexData()->GetObject().get());
884 if(j == 0)
885 {
886 innerLength = tmpList->Length();
887 }
888 else
889 {
890 if(tmpList->Length() != innerLength)
891 {
892 PError::SignalFatalError("All sublists must be of the same length in Transpose(<list-of-lists>).");
893 }
894 }
895
896 //assign the iterator to the first element of the sublist.
897 listIterators[j] = tmpList->Begin();
898 p++;
899 }
900
901
902 //Now, everything is in the clear, so transpose.
903
904 PComplexData * tmpPCD;
905 PList * returnList = new PList;
906 Variant tmpVariant;
908
909
910 for(unsigned int i = 0; i < innerLength; i++)
911 {
912 tmpList = new PList();
913
914 for(unsigned int j = 0; j < outerLength; j++)
915 {
916 tmpList->PushBack(*listIterators[j]); //Grab the item from the list
917 listIterators[j]++; //iterate
918 }
919 //The first item of each list has been added to tmpList, so add
920 //it to returnList.
921 tmpObj = counted_ptr<PEBLObjectBase>(tmpList);
922 tmpPCD = (new PComplexData(tmpObj));
923 tmpVariant = Variant(tmpPCD);
924 returnList->PushBack(tmpVariant);
925 delete tmpPCD;
926 tmpPCD=NULL;
927 }
928
929 tmpObj = counted_ptr<PEBLObjectBase>(returnList);
930 tmpPCD= (new PComplexData(tmpObj));
931
932 Variant tmp = Variant(tmpPCD);
933 delete tmpPCD;
934 tmpPCD=NULL;
935
936 return tmp;
937}
938
941{
942
943 //v[1] should be a list. Just extract the iterators.
944 PList * plist = v.GetComplexData()->GetList();
945 Variant v1 = plist->First(); //plist->PopFront();
946 PError::AssertType(v1, PEAT_LIST, "Argument error in first parameter of function [SubList(<list>, <int>, <int>)]: ");
947
948 PList * tmpList = (PList*)(v1.GetComplexData()->GetObject().get());
949 vector<Variant>::iterator pstart = tmpList->Begin();
950 vector<Variant>::iterator p;
951 vector<Variant>::iterator pend = tmpList->End();
952
953
954 PError::AssertType(plist->Nth(2), PEAT_INTEGER, "Argument error in second parameter of function [SubList(<list>, <int>, <int>)]: ");
955 int start = plist->Nth(2); //plist->PopFront();
956
957 PError::AssertType(plist->Nth(3), PEAT_INTEGER, "Argument error in third parameter of function [SubList(<list>, <int>, <int>)]: ");
958 int end = plist->Nth(3); //plist->PopFront();
959
960
961 //Check to see if everything is in bounds.
962 if(start < 1
963 || end > (int)(tmpList->Length())
964 || start > end )
965 {
966 Variant message;
967 message = Variant("[SubList] tried to extract items ")+
968 Variant((int)start)+ Variant(" to ") + Variant((int)end) +
969 Variant(" of a ") + Variant((int)(tmpList->Length())) + Variant( " item list.");
970
972 }
973
974
975
976 //There is now an iterator p, and an end to compare it to, and
977 //an iterator to the pstart for resetting.
978 //Make a new list to return.
979
980 PList * returnList = new PList();
981 Variant tmpVariant;
983 //Start at the beginning of the list. Keep count; if the count is
984 //between the two boundaries, add it to the list.
985 p=pstart;
986 int i = 0;
987 while (p != pend)
988 {
989 i++;
990 //If i is big enough, consider adding it to the list
991 if(i>=start)
992 {
993 if( i <=end) //But only if it isn't too big
994 {
995 returnList->PushBack(*p);
996 }
997 else //If it is too big, get out of the while loop.
998 {
999 break;
1000 }
1001 }
1002 p++;
1003 }
1004 tmpObj = counted_ptr<PEBLObjectBase>(returnList);
1005 PComplexData * tmpPCD = (new PComplexData(tmpObj));
1006 Variant tmp = Variant(tmpPCD);
1007 delete tmpPCD;
1008 tmpPCD=NULL;
1009 return tmp;
1010
1011}
1012
1013
1014
1015//This will remove the nth item from a list
1017{
1018
1019 PError::SignalFatalError("Function [Remove] not implemented");
1020 return Variant(1);
1021}
1022
1023
1025{
1026
1027 //v[1] should be a list. Just extract the iterators.
1028 PList * plist = v.GetComplexData()->GetList();
1029 Variant v1 = plist->First(); //plist->PopFront();
1030 PError::AssertType(v1, PEAT_LIST, "Argument error in first parameter of function [ListToString(<list>, <sep>, <pre>, <post>)]: ");
1031
1032
1033 PList * tmpList = (PList*)(v1.GetComplexData()->GetObject().get());
1034
1035 vector<Variant>::iterator pstart = tmpList->Begin();
1036 vector<Variant>::iterator p;
1037 vector<Variant>::iterator pend = tmpList->End();
1038
1039 Variant sep1 = "";
1040 Variant pre = "";
1041 Variant post = "";
1042
1043
1044 if(plist->Length()>=2) sep1 = plist->Nth(2);
1045 if(plist->Length()>=3) pre = plist->Nth(3);
1046 if(plist->Length()>=4) post = plist->Nth(4);
1047
1048 //cout << "processing List to string: " << sep1 << "|" << pre <<"|" << post << "|" << endl;
1049 Variant out="";
1050 Variant sep = "";
1051 p = pstart;
1052 while(p!=pend)
1053 {
1054
1055 out = out + sep + pre+ Variant(*p) + post;
1056
1057 if(p == pstart)
1058 sep =sep1;
1059
1060 p++;
1061 }
1062 return out;
1063}
1064
1065//This will add a pre and post string to each element of the list.
1066//This should non-destrtuctivley cerate a new list with pre/post values on each item
1068{
1069 //v[1] should be a list. Just extract the iterators.
1070 PList * plist = v.GetComplexData()->GetList();
1071 Variant v1 = plist->First(); //plist->PopFront();
1072 PError::AssertType(v1, PEAT_LIST, "Argument error in first parameter of function [ModList(<list>, <pre>, <post>)]: ");
1073
1074
1075 PList * tmpList = (PList*)(v1.GetComplexData()->GetObject().get());
1076 PList * newList = new PList(*tmpList);
1077
1078 vector<Variant>::iterator pstart = newList->Begin();
1079 vector<Variant>::iterator p;
1080 vector<Variant>::iterator pend = newList->End();
1081
1082 Variant pre = "";
1083 Variant post = "";
1084
1085
1086 if(plist->Length()>=2) pre = plist->Nth(2);
1087 if(plist->Length()>=3) post = plist->Nth(3);
1088
1089 p = pstart;
1090 while(p!=pend)
1091 {
1092 *p = (pre + *p + post);
1093 p++;
1094 }
1096
1097 PComplexData * PCD =(new PComplexData(newList2));
1098 Variant tmp = Variant(PCD);
1099 delete PCD;
1100 PCD=NULL;
1101 return tmp;
1102
1103}
1104
#define NULL
Definition BinReloc.cpp:317
#define pDouble
Definition Defs.h:7
@ PEAT_LIST
Definition PError.h:61
@ PEAT_INTEGER
Definition PError.h:44
@ PEAT_NUMBER
Definition PError.h:43
counted_ptr< PEBLObjectBase > GetObject() const
PList * GetList() const
Definition PList.h:45
std::vector< Variant >::const_iterator End() const
Definition PList.cpp:132
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
void PushBack(const Variant &v)
Definition PList.cpp:149
Variant First()
Definition PList.cpp:169
PComplexData * GetComplexData() const
Definition Variant.cpp:1299
X * get() const
Definition rc_ptrs.h:110
Variant Fifth(Variant v)
Definition PEBLList.cpp:561
Variant Last(Variant v)
Definition PEBLList.cpp:593
Variant Append(Variant v)
Definition PEBLList.cpp:669
Variant Transpose(Variant v)
Definition PEBLList.cpp:859
Variant Merge(Variant v)
Definition PEBLList.cpp:608
Variant SortBy(Variant v)
This sorts the list by another list.
Definition PEBLList.cpp:781
Variant Sequence(Variant v)
Definition PEBLList.cpp:185
Variant Rotate(Variant v)
Definition PEBLList.cpp:417
Variant Nth(Variant v)
Definition PEBLList.cpp:576
Variant Shuffle(Variant v)
Definition PEBLList.cpp:53
Variant CrossFactorWithoutDuplicates(Variant v)
Definition PEBLList.cpp:352
Variant Sort(Variant v)
This just sorts the list.
Definition PEBLList.cpp:759
Variant Second(Variant v)
Definition PEBLList.cpp:521
Variant IsMember(Variant v)
Definition PEBLList.cpp:807
Variant First(Variant v)
Definition PEBLList.cpp:508
Variant MakeMap(Variant v)
Definition PEBLList.cpp:842
Variant PushOnEnd(Variant v)
Given Merge([a,b,c],d), will return [a,b,c,d].
Definition PEBLList.cpp:734
Variant SetElement(Variant v)
Definition PEBLList.cpp:710
Variant RepeatList(Variant v)
Definition PEBLList.cpp:130
Variant Third(Variant v)
Definition PEBLList.cpp:534
Variant RemoveDuplicates(Variant v)
Definition PEBLList.cpp:831
Variant List(Variant v)
This creates a list functionally, rather than syntactically using the [] operators.
Definition PEBLList.cpp:121
Variant Repeat(Variant v)
Definition PEBLList.cpp:90
Variant SubList(Variant v)
This makes a list out of a sublist of a list. Or something like that.
Definition PEBLList.cpp:940
Variant RepeatExpression(Variant v)
Definition PEBLList.cpp:265
Variant Length(Variant v)
This returns the number of items in the list.
Definition PEBLList.cpp:492
Variant ListToString(Variant v)
Variant ModList(Variant v)
Variant Remove(Variant v)
Variant Fourth(Variant v)
Definition PEBLList.cpp:548
Variant DesignFullCounterbalance(Variant v)
Definition PEBLList.cpp:282
pDouble RandomUniform()
void AssertType(Variant v, int type, const std::string &outsidemessage)
void SignalFatalError(const std::string &message)