1 
2 // This file is part of the Alliance Project.
3 // Copyright (C) Laboratoire LIP6 - Departement ASIM
4 // Universite Pierre et Marie Curie
5 //
6 // The Alliance Project  is free software;  you can  redistribute it and/or
7 // modify  it  under the  terms  of  the  GNU  General  Public License  as
8 // published by  the Free  Software Foundation; either  version 2  of  the
9 // License, or (at your option) any later version.
10 //
11 // The Alliance Project  is distributed in the hope that it will be useful,
12 // but  WITHOUT  ANY  WARRANTY;  without  even  the  implied  warranty  of
13 // MERCHANTABILITY  or  FITNESS  FOR A  PARTICULAR PURPOSE.   See  the GNU
14 // General Public License for more details.
15 //
16 // You should have received a copy  of  the  GNU  General  Public  License
17 // along with  the Alliance Project;  if  not,  write to the  Free Software
18 // Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
19 //
20 //
21 // License-Tag
22 //
23 // Date   : 29/01/2004
24 // Author : Christophe Alexandre  <Christophe.Alexandre@lip6.fr>
25 //
26 // Authors-Tag
27 #include "PBin.h"
28 #include "PToPlaceIns.h"
29 #include "PConstants.h"
30 
31 #include "PSubRow.h"
32 
PSubRow()33 PSubRow::PSubRow()
34   : PContainer()
35   , _row      (NULL)
36   , _bins     ()
37   , _binsXMax ()
38   , _size     (0.0)
39   , _capa     (0.0)
40   , _max      (0.0)
41   , _nBins    (0)
42 {}
43 
~PSubRow()44 PSubRow::~PSubRow()
45 {
46     for (PBins::iterator bit = _bins.begin();
47 	    bit !=_bins.end();
48 	    bit++)
49 	delete *bit;
50 }
51 
52 unsigned
GetNBins() const53 PSubRow::GetNBins() const
54 {
55     return _bins.size();
56 }
57 
58 void
Init(PRow * row,double y,double minx,double maxx,double margin,double maxbinwidth,double minbinwidth)59 PSubRow::Init(PRow* row, double y, double minx, double maxx, double margin, double maxbinwidth, double minbinwidth)
60 {
61     _row = row;
62     PBBox binbbox;
63     double xpos;
64     int binswidth;
65     int modulo = 0;
66 
67     _bBox.SetMinY(y);
68     _bBox.SetMaxY(y + ROWHEIGHT);
69     _bBox.SetMinX(minx);
70     _bBox.SetMaxX(maxx);
71     _size = 0.0;
72     _capa = (maxx - minx) * (1.0 - margin);
73     _max = maxx - minx;
74 
75     binbbox.SetMinY(_bBox.GetMinY());
76     binbbox.SetMaxY(_bBox.GetMaxY());
77     xpos = minx;
78 
79     if ( ((int)(maxx - minx) % (int)(maxbinwidth)) > 0)
80       _nBins = (unsigned)((maxx - minx)/maxbinwidth) + 1;
81     else
82       _nBins = (unsigned)((maxx - minx)/maxbinwidth);
83 
84     modulo = ((int)(maxx - minx) % _nBins);
85 
86     binswidth = (int)((maxx - minx) / _nBins);
87 
88     _bins.reserve(_nBins);
89     for (unsigned binnumber = 0; binnumber < _nBins; binnumber++)
90     {
91         _bins.push_back(new PBin());
92         binbbox.SetMinX(xpos);
93 	if (modulo > 0)
94 	{
95 	    xpos += (binswidth+1);
96 	    binbbox.SetMaxX(xpos);
97 	    modulo--;
98 	}
99 	else
100 	{
101 	    xpos += binswidth;
102 	    binbbox.SetMaxX(xpos);
103 	}
104 	_binsXMax[xpos] = binnumber;
105         _bins.back()->Init(binbbox, margin, *this);
106     }
107     _row->MergeBBox(_bBox);
108 }
109 
110 PBin&
GetBin(const double X)111 PSubRow::GetBin(const double X)
112 {
113     if (X >= (*_bins.rbegin())->GetMaxX())
114 	return **(_bins.rbegin());
115 
116     if (X <= (*_bins.begin())->GetMinX())
117 	return **(_bins.begin());
118 
119     PBinsXMax::iterator srpos = _binsXMax.upper_bound(X);
120     return *_bins[srpos->second];
121 }
122 
123 bool
InsertIns(PToPlaceIns & Ins,int BinNumber)124 PSubRow::InsertIns(PToPlaceIns& Ins, int BinNumber)
125 {
126     if ((_bins[BinNumber]->GetCapa() - _bins[BinNumber]->GetSize())
127             < Ins.GetWidth())
128         return false;
129     else
130     {
131         _bins[BinNumber]->AddIns(&Ins);
132         return true;
133     }
134 }
135 
136 unsigned
GetNIns() const137 PSubRow::GetNIns() const
138 {
139     unsigned nins = 0;
140     for (PBins::const_iterator bit =_bins.begin(); bit !=_bins.end(); bit++)
141 	nins += (*bit)->GetNIns();
142     return nins;
143 }
144 
145 void
ForceIns(PToPlaceIns & Ins,int BinNumber)146 PSubRow::ForceIns(PToPlaceIns& Ins, int BinNumber)
147 {
148     _bins[BinNumber]->AddIns(&Ins);
149 }
150 
151 #ifndef Abs
152 #define Abs(x) ((x) < 0.0 ? -(x) : (x))
153 #endif
GetBinCost() const154 double PSubRow::GetBinCost() const
155 {
156     double binCost = 0.0;
157     for (PBins::const_iterator bit = _bins.begin(); bit != _bins.end(); bit++)
158     {
159         binCost += Abs((*bit)->GetSize() - (*bit)->GetCapa());
160     }
161     return binCost;
162 }
163 
164 ofstream&
Plot(ofstream & out) const165 PSubRow::Plot(ofstream& out) const
166 {
167   for (PBins::const_iterator bit=_bins.begin(); bit!=_bins.end(); bit++)
168   {
169       (*bit)->Plot(out);
170   }
171   return out;
172 }
173 
174 ofstream&
PlotLabel(ofstream & out,unsigned TotalMoves) const175 PSubRow::PlotLabel(ofstream& out, unsigned TotalMoves) const
176 {
177     for (PBins::const_iterator bit=_bins.begin(); bit!=_bins.end(); bit++)
178     {
179 	(*bit)->PlotLabel(out, TotalMoves);
180     }
181     return out;
182 }
183 
184 ostream&
Print(ostream & os) const185 PSubRow::Print(ostream& os) const
186 {
187     return os << "PSubRow: " << GetMinX() << ',' << GetMinY() << " : " << GetMaxX() << ',' << GetMaxY();
188 }
189 
190 
GetBinsSize() const191 double PSubRow::GetBinsSize() const
192 {
193     double binsSize = 0.0;
194     for (PBins::const_iterator bit=_bins.begin(); bit!=_bins.end(); bit++)
195     {
196         binsSize += (*bit)->GetSize();
197     }
198     return binsSize;
199 }
200 
GetBinsCapa() const201 double PSubRow::GetBinsCapa() const
202 {
203     double binsCapa = 0.0;
204     for (PBins::const_iterator bit=_bins.begin(); bit!=_bins.end(); bit++)
205     {
206         binsCapa += (*bit)->GetCapa();
207     }
208     return binsCapa;
209 }
210