Salome HOME
Optimized algo for PlayGround
[modules/yacs.git] / src / engine / BagPoint.cxx
1 // Copyright (C) 2015-2016  CEA/DEN, EDF R&D
2 //
3 // This library is free software; you can redistribute it and/or
4 // modify it under the terms of the GNU Lesser General Public
5 // License as published by the Free Software Foundation; either
6 // version 2.1 of the License, or (at your option) any later version.
7 //
8 // This library is distributed in the hope that it will be useful,
9 // but WITHOUT ANY WARRANTY; without even the implied warranty of
10 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
11 // Lesser General Public License for more details.
12 //
13 // You should have received a copy of the GNU Lesser General Public
14 // License along with this library; if not, write to the Free Software
15 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
16 //
17 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
18 //
19
20 #include "BagPoint.hxx"
21 #include "Exception.hxx"
22 #include "LinkedBlocPoint.hxx"
23 #include "ForkBlocPoint.hxx"
24
25 #include <sstream>
26 #include <algorithm>
27
28 using namespace YACS::ENGINE;
29
30 BagPoint::BagPoint(const std::list<AbstractPoint *>& nodes, AbstractPoint *father):BlocPoint(nodes,father)
31 {
32 }
33
34 AbstractPoint *BagPoint::getUnique()
35 {
36   if(_nodes.size()!=1)
37     throw YACS::Exception("BagPoint::getUnique : invalid call !");
38   else
39     {
40       AbstractPoint *ret(*_nodes.begin());
41       if(!ret)
42         throw YACS::Exception("BagPoint::getUnique : Ooops !");
43       return ret;
44     }
45 }
46
47 const AbstractPoint *BagPoint::getUnique() const
48 {
49   if(_nodes.size()!=1)
50     throw YACS::Exception("BagPoint::getUnique const : invalid call !");
51   else
52     {
53       AbstractPoint *ret(*_nodes.begin());
54       if(!ret)
55         throw YACS::Exception("BagPoint::getUnique : Ooops !");
56       return ret;
57     }
58 }
59
60 AbstractPoint *BagPoint::getUniqueAndReleaseIt()
61 {
62   AbstractPoint *ret(getUnique());
63   getOutPoint(ret);
64   return ret;
65 }
66
67 Node *BagPoint::getFirstNode()
68 {
69   return getUnique()->getFirstNode();
70 }
71
72 Node *BagPoint::getLastNode()
73 {
74   return getUnique()->getLastNode();
75 }
76
77 int BagPoint::getMaxLevelOfParallelism() const
78 {
79   return getUnique()->getMaxLevelOfParallelism();
80 }
81
82 void BagPoint::getWeightRegardingDPL(ComplexWeight *weight)
83 {
84   getUnique()->getWeightRegardingDPL(weight);
85 }
86
87 void BagPoint::partitionRegardingDPL(const PartDefinition *pd, std::map<ComposedNode *, YACS::BASES::AutoRefCnt<PartDefinition> >& zeMap) const
88 {
89   getUnique()->partitionRegardingDPL(pd,zeMap);
90 }
91
92 std::string BagPoint::getRepr() const
93 {
94   std::ostringstream oss;
95   for(std::list<AbstractPoint *>::const_iterator it=_nodes.begin();it!=_nodes.end();it++)
96     oss << (*it)->getRepr() << " - ";
97   return oss.str();
98 }
99
100 #include <iostream>
101
102 void BagPoint::replaceInMe(BlocPoint *aSet)
103 {
104   const std::list<AbstractPoint *>& pts(aSet->getListOfPoints());
105   for(std::list<AbstractPoint *>::const_iterator it0=pts.begin();it0!=pts.end();it0++)
106     {
107       std::list<AbstractPoint *>::iterator it1(std::find(_nodes.begin(),_nodes.end(),*it0));
108       if(it1==_nodes.end())
109         throw Exception("SetOfPoints::replaceInMe : internal error !");
110       _nodes.erase(it1);
111     }
112   _nodes.push_back(aSet);
113 }
114
115 void BagPoint::deal1(bool& somethingDone)
116 {
117   somethingDone=false;
118   for(std::list<AbstractPoint *>::iterator it=_nodes.begin();it!=_nodes.end();it++)
119     {
120       if(!(*it)->isSimplyLinkedBeforeAfter(this))
121         if(!(*it)->isSimplyLinkedAfterNullBefore(this) && !(*it)->isSimplyLinkedBeforeNullAfter(this))
122            continue;
123       LinkedBlocPoint *try0((*it)->tryAsLink(this));
124       if(try0)
125         {
126           replaceInMe(try0);
127           somethingDone=true;
128           break;
129         }
130     }
131 }
132
133 void BagPoint::deal2(bool& somethingDone)
134 {
135   somethingDone=false;
136   for(std::list<AbstractPoint *>::iterator it=_nodes.begin();it!=_nodes.end();it++)
137     {
138       if(!(*it)->isSimplyLinkedBeforeAfter(this))
139         continue;
140       ForkBlocPoint *try1((*it)->tryAsFork(this));
141       if(try1)
142         {
143           replaceInMe(try1);
144           somethingDone=true;
145           break;
146         }
147     }
148 }
149
150 void BagPoint::deal2Bis(bool& somethingDone)
151 {
152   somethingDone=false;
153   for(std::list<AbstractPoint *>::iterator it=_nodes.begin();it!=_nodes.end();it++)
154     {
155       if(!(*it)->isSimplyLinkedAfterNullBefore(this))
156         continue;
157       ForkBlocPoint *try1((*it)->tryAsForkBis(this));
158       if(try1)
159         {
160           replaceInMe(try1);
161           somethingDone=true;
162           break;
163         }
164     }
165 }
166
167 void BagPoint::deal2Ter(bool& somethingDone)
168 {
169   somethingDone=false;
170   for(std::list<AbstractPoint *>::iterator it=_nodes.begin();it!=_nodes.end();it++)
171     {
172       if(!(*it)->isSimplyLinkedBeforeNullAfter(this))
173         continue;
174       ForkBlocPoint *try1((*it)->tryAsForkTer(this));
175       if(try1)
176         {
177           replaceInMe(try1);
178           somethingDone=true;
179           break;
180         }
181       }
182 }
183