Line data Source code
1 : /*******************************************************************************
2 : * *
3 : * Cosmos:(C)oncept et (O)utils (S)tatistique pour les (Mo)deles *
4 : * (S)tochastiques *
5 : * *
6 : * Copyright (C) 2009-2012 LSV & LACL *
7 : * Authors: Paolo Ballarini BenoƮt Barbot & Hilal Djafri *
8 : * Website: http://www.lsv.ens-cachan.fr/Software/cosmos *
9 : * *
10 : * This program is free software; you can redistribute it and/or modify *
11 : * it under the terms of the GNU General Public License as published by *
12 : * the Free Software Foundation; either version 3 of the License, or *
13 : * (at your option) any later version. *
14 : * *
15 : * This program is distributed in the hope that it will be useful, *
16 : * but WITHOUT ANY WARRANTY; without even the implied warranty of *
17 : * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
18 : * GNU General Public License for more details. *
19 : * *
20 : * You should have received a copy of the GNU General Public License along *
21 : * with this program; if not, write to the Free Software Foundation, Inc., *
22 : * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. *
23 : * file casesWriter.cpp *
24 : * Created by Benoit Barbot on 11/09/13. *
25 : *******************************************************************************
26 : */
27 :
28 :
29 : #include <iostream>
30 : #include <sstream>
31 :
32 : #include "casesWriter.hpp"
33 :
34 :
35 :
36 :
37 : using namespace std;
38 :
39 668 : casesHandler::casesHandler(string svar):maxc(0),scase(svar),hasDefault(false){};
40 :
41 4902 : void casesHandler::addCase(int c,const string &st,const string &comment){
42 4902 : const char* cst = st.c_str();
43 4902 : map<const string,int>::iterator it = cases.find(cst);
44 4902 : if(it == cases.end()){
45 2238 : cases[st]=1;
46 2238 : mapping[c]=st;
47 : }else{
48 2664 : it->second++;
49 2664 : mapping[c]=st;
50 : }
51 4902 : mapcomment[c]=comment;
52 4902 : maxc = max(maxc,cases[st]);
53 4902 : }
54 :
55 66 : void casesHandler::addDefault(const string &st){
56 66 : defaultvalue =st;
57 66 : hasDefault =true;
58 66 : }
59 :
60 664 : void casesHandler::writeCases(ostream &s){
61 664 : if(cases.size()==0)return;
62 664 : if(cases.size()==1) {
63 335 : s << cases.begin()->first << endl;
64 : }else{
65 329 : s << "\tswitch (" << scase << "){" << endl;
66 329 : map<const string,int>::iterator itmax = cases.begin();
67 2225 : for(map<const string,int>::iterator it = cases.begin(); it != cases.end(); ++it){
68 1896 : if(it->second != maxc || maxc ==1){
69 152987 : for(map<int,std::string>::iterator it2 = mapping.begin(); it2 != mapping.end(); ++it2){
70 151321 : if(it2->second.compare(it->first)==0)
71 1908 : s << "\t\tcase " << it2->first << ":\t//" << mapcomment[it2->first] << endl;
72 : }
73 1666 : s << it->first << endl;
74 1666 : s << "\t\tbreak;" << endl;
75 : }else{
76 230 : itmax = it;
77 230 : maxc++;
78 : }
79 : }
80 329 : if(itmax->first.compare("")!=0 && maxc != 1 && !hasDefault){
81 212 : s<< "\t\tdefault:\t//";
82 7056 : for(map<int,std::string>::iterator it2 = mapping.begin();
83 4704 : it2 != mapping.end(); ++it2)
84 2140 : if(it2->second.compare(itmax->first)==0)
85 1138 : s << mapcomment[it2->first] << ",";
86 :
87 212 : s << endl << itmax->first << endl;
88 212 : s << "\t\tbreak;" << endl;
89 117 : } else if(hasDefault){
90 0 : s<< "\t\tdefault:"<< endl;
91 0 : s<< defaultvalue;
92 0 : s << "\t\tbreak;" << endl;
93 : }
94 :
95 329 : s << "\t}" << endl;
96 : }
97 : }
98 :
99 12 : std::ostream& operator<<(std::ostream& os, casesHandler& obj){
100 12 : obj.writeCases(os);
101 12 : return os;
102 105 : }
103 :
|