Test-List.C
Go to the documentation of this file.
1 /*---------------------------------------------------------------------------*\
2  ========= |
3  \\ / F ield | OpenFOAM: The Open Source CFD Toolbox
4  \\ / O peration |
5  \\ / A nd | Copyright (C) 2011-2015 OpenFOAM Foundation
6  \\/ M anipulation |
7 -------------------------------------------------------------------------------
8 License
9  This file is part of OpenFOAM.
10 
11  OpenFOAM is free software: you can redistribute it and/or modify it
12  under the terms of the GNU General Public License as published by
13  the Free Software Foundation, either version 3 of the License, or
14  (at your option) any later version.
15 
16  OpenFOAM is distributed in the hope that it will be useful, but WITHOUT
17  ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
18  FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
19  for more details.
20 
21  You should have received a copy of the GNU General Public License
22  along with OpenFOAM. If not, see <http://www.gnu.org/licenses/>.
23 
24 Application
25  Test-List
26 
27 Description
28  Simple tests and examples of use of List
29 
30 See Also
31  Foam::List
32 
33 \*---------------------------------------------------------------------------*/
34 
35 #include "OSspecific.H"
36 #include "argList.H"
37 #include "wordReList.H"
38 
39 #include "IOstreams.H"
40 #include "IStringStream.H"
41 #include "scalar.H"
42 #include "vector.H"
43 #include "ListOps.H"
44 
45 #include<list>
46 
47 using namespace Foam;
48 
49 
50 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
51 // Main program:
52 
53 int main(int argc, char *argv[])
54 {
56  argList::addOption("reList", "reList");
57  argList::addOption("wordList", "wordList");
58  argList::addOption("stringList", "stringList");
59  argList::addOption("float", "xx");
60  argList::addBoolOption("flag");
61 
62  #include "setRootCase.H"
63 
64  List<vector> list1(IStringStream("1 ((0 1 2))")());
65  Info<< "list1: " << list1 << endl;
66 
67  List<vector> list2(IStringStream("((0 1 2) (3 4 5) (6 7 8))")());
68  Info<< "list2: " << list2 << endl;
69 
70  list1.append(list2);
71  Info<< "list1.append(list2): " << list1 << endl;
72 
73  Info<< findIndex(list2, vector(3, 4, 5)) << endl;
74 
75  list2.setSize(10, vector(1, 2, 3));
76  Info<< "list2: " << list2 << endl;
77 
78  List<vector> list3(list2.xfer());
79  Info<< "Transferred via the xfer() method" << endl;
80  Info<< "list2: " << list2 << nl
81  << "list3: " << list3 << endl;
82 
83  List<vector> list4(IStringStream("((0 1 2) (3 4 5) (6 7 8))")());
84  List<vector> list5(IStringStream("((5 3 1) (10 2 2) (8 1 0))")());
85  Info<< "list4: " << list4 << nl
86  << "list5: " << list5 << endl;
87  list4.swap(list5);
88  Info<< "Swapped via the swap() method" << endl;
89  Info<< "list4: " << list4 << nl
90  << "list5: " << list5 << endl;
91 
92  // Subset
93  const labelList map(IStringStream("2 (0 2)")());
94  List<vector> subList3(list3, map);
95  Info<< "Elements " << map << " out of " << list3
96  << " => " << subList3 << endl;
97 
98  wordReList reLst;
99  wordList wLst;
100  stringList sLst;
101 
102 
103  scalar xxx(-1);
104 
105  if (args.optionFound("flag"))
106  {
107  Info<<"-flag:" << args["flag"] << endl;
108  }
109 
110  if (args.optionReadIfPresent<scalar>("float", xxx))
111  {
112  Info<<"read float " << xxx << endl;
113  }
114 
115  if (args.optionFound("reList"))
116  {
117  reLst = args.optionReadList<wordRe>("reList");
118  }
119 
120  if (args.optionFound("wordList"))
121  {
122  wLst = args.optionReadList<word>("wordList");
123  }
124 
125  if (args.optionFound("stringList"))
126  {
127  sLst = args.optionReadList<string>("stringList");
128  }
129 
130  Info<< nl
131  << "-reList: " << reLst << nl
132  << "-wordList: " << wLst << nl
133  << "-stringList: " << sLst << endl;
134 
135 
136  Info<< nl
137  << "Test List Iterator Constuctor" << endl;
138 
139  List<vector> initialList(IStringStream("((0 1 2) (3 4 5) (6 7 8))")());
140 
141  Info<< " Initial List: " << initialList << endl;
142 
143  List<vector> iteratorList(initialList.begin(), initialList.end());
144 
145  Info<< " Foam::List constructed from Foam::List: "
146  << iteratorList << endl;
147 
148  std::list<vector> stlList(initialList.begin(), initialList.end());
149 
150  Info<< " std::list constructed from Foam::List: ";
151 
152  std::list<vector>::iterator it;
153  for (it=stlList.begin(); it != stlList.end(); ++it)
154  {
155  Info<< *it << " ";
156  }
157  Info<< endl;
158 
159  List<vector> iteratorSTLList(stlList.begin(), stlList.end());
160 
161  Info<< " Foam::List constructed from std::list: "
162  << iteratorList << endl;
163 
164  return 0;
165 }
166 
167 // ************************************************************************* //
OSspecific.H
Functions used by OpenFOAM that are specific to POSIX compliant operating systems and need to be repl...
IOstreams.H
Useful combination of include files which define Sin, Sout and Serr and the use of IO streams general...
Foam::word
A class for handling words, derived from string.
Definition: word.H:59
Foam::argList::addOption
static void addOption(const word &opt, const string &param="", const string &usage="")
Add to an option to validOptions with usage information.
Definition: argList.C:108
main
int main(int argc, char *argv[])
Definition: Test-List.C:50
Foam::findIndex
label findIndex(const ListType &, typename ListType::const_reference, const label start=0)
Find first occurence of given element and return index,.
Foam::List::xfer
Xfer< List< T > > xfer()
Transfer contents to the Xfer container.
Definition: ListI.H:90
Foam::argList::addBoolOption
static void addBoolOption(const word &opt, const string &usage="")
Add to a bool option to validOptions with usage information.
Definition: argList.C:98
Foam::endl
Ostream & endl(Ostream &os)
Add newline and flush stream.
Definition: Ostream.H:251
Foam::wordRe
A wordRe is a word, but can also have a regular expression for matching words.
Definition: wordRe.H:74
IStringStream.H
Foam::argList::optionReadList
List< T > optionReadList(const word &opt) const
Read a List of values from the named option.
Definition: argList.H:304
Foam::List::append
void append(const T &)
Append an element at the end of the list.
Foam::nl
static const char nl
Definition: Ostream.H:260
Foam::Info
messageStream Info
argList.H
scalar.H
Foam::IStringStream
Input from memory buffer stream.
Definition: IStringStream.H:49
Foam
Namespace for OpenFOAM.
Definition: combustionModel.C:30
Foam::vector
Vector< scalar > vector
A scalar version of the templated Vector.
Definition: vector.H:49
Foam::List::setSize
void setSize(const label)
Reset size of List.
setRootCase.H
wordReList.H
Foam::List< vector >
Foam::argList::optionFound
bool optionFound(const word &opt) const
Return true if the named option is found.
Definition: argListI.H:108
vector.H
ListOps.H
Various functions to operate on Lists.
Foam::argList::noParallel
static void noParallel()
Remove the parallel options.
Definition: argList.C:161
args
Foam::argList args(argc, argv)
Foam::argList::optionReadIfPresent
bool optionReadIfPresent(const word &opt, T &) const
Read a value from the named option if present.
Definition: argListI.H:198