Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035
00036
00037 #include "rect_list.hpp"
00038
00039 util::rectangle::rectangle()
00040 {
00041 }
00042
00043 util::rectangle::rectangle(int xl, int xh, int yl, int yh)
00044 :x_top_l(xl), y_top_l(yl), x_bot_r(xh),y_bot_r(yh)
00045 {}
00046 bool util::rectangle::empty() const
00047 {
00048 return (x_bot_r < x_top_l || y_bot_r < y_top_l);
00049 }
00050
00051 template<typename Pr>
00052 bool util::rectangle::valid_bound( const Pr & prefixSum) const
00053 {
00054 if(x_top_l <= 0 ||
00055 y_top_l <= 0 ||
00056 x_bot_r >= prefixSum.prefixsizeX() ||
00057 y_bot_r >= prefixSum.prefixsizeY())
00058 return false;
00059 else
00060 return true;
00061 }
00062
00063 template <typename T, typename Pr>
00064 T util::rectangle::get_load(const Pr& prefixSum) const
00065 {
00066 if(empty())
00067 return 0;
00068 else
00069 return prefixSum[x_bot_r][y_bot_r] + prefixSum[x_top_l - 1][y_top_l - 1]
00070 - prefixSum[x_bot_r][y_top_l-1] - prefixSum[x_top_l-1][y_bot_r];
00071 }
00072
00073 int util::rectangle::get_area() const
00074 {
00075 return (x_bot_r - x_top_l + 1) * (y_bot_r - y_top_l + 1);
00076 }
00077
00078 std::ostream& util::operator<< (std::ostream& out, const rectangle& r)
00079 {
00080 out<<"("<<r.x_top_l<<" "<<r.y_top_l<<" "<<r.x_bot_r<<" "<<r.y_bot_r<<")";
00081 return out;
00082 }
00083
00084
00085 template <typename T, typename Pr>
00086 util::RectList<T,Pr>::RectList(const Pr &ps)
00087 :prefixSum(ps)
00088 {}
00089
00090 template <typename T, typename Pr>
00091 util::RectList<T,Pr>::RectList(const RectList &rlist)
00092 :prefixSum(rlist.prefixSum)
00093 {
00094 copy_into (rlist);
00095 }
00096
00097 template <typename T, typename Pr>
00098 void util::RectList<T,Pr>::add_rect(const rectangle& r)
00099 {
00100 if(r.empty())
00101 return;
00102 else
00103 rectangles.push_back(r);
00104 }
00105
00106 template <typename T, typename Pr>
00107 void util::RectList<T,Pr>::copy_into(const RectList &rlist)
00108 {
00109 assert (&(prefixSum) == &(rlist.prefixSum));
00110
00111 rectangles.clear();
00112
00113 for (container::const_iterator list_iter1 = rlist.rectangles.begin();
00114 list_iter1 != rlist.rectangles.end(); list_iter1++)
00115 add_rect(*list_iter1);
00116 }
00117
00118 template <typename T, typename Pr>
00119 void util::RectList<T,Pr>::add_into(const RectList &rlist)
00120 {
00121 assert (&(prefixSum) == &(rlist.prefixSum));
00122
00123 for (container::const_iterator list_iter1 = rlist.rectangles.begin();
00124 list_iter1 != rlist.rectangles.end(); list_iter1++)
00125 add_rect(*list_iter1);
00126 }
00127
00128 template <typename T, typename Pr>
00129 void util::RectList<T,Pr>::clear()
00130 {
00131 rectangles.clear();
00132 }
00133
00134 template <typename T, typename Pr>
00135 T util::RectList<T,Pr>::get_maximum_load() const
00136 {
00137 assert (valid_part());
00138
00139 T maximum = 0;
00140 for (container::const_iterator list_iter1 = rectangles.begin();
00141 list_iter1 != rectangles.end(); list_iter1++)
00142 maximum = std::max (maximum, list_iter1->get_load<T,Pr> (prefixSum));
00143 return maximum;
00144 }
00145
00146 template <typename T, typename Pr>
00147 T util::RectList<T,Pr>::get_minimum_load() const
00148 {
00149 assert (valid_part());
00150
00151 T minimum = prefixSum[prefixSum.prefixsizeX()-1][prefixSum.prefixsizeY()-1];
00152 for (container::const_iterator list_iter1 = rectangles.begin();
00153 list_iter1 != rectangles.end(); list_iter1++)
00154 minimum = std::min (minimum, list_iter1->get_load<T,Pr> (prefixSum));
00155 return minimum;
00156 }
00157
00158 template <typename T, typename Pr>
00159 bool util::RectList<T,Pr>::valid_part() const
00160 {
00161 const bool DEBUG = false;
00162
00163
00164 {
00165 container::const_iterator list_iter1;
00166 for (list_iter1 = rectangles.begin(); list_iter1 != rectangles.end(); list_iter1++)
00167 {
00168 const rectangle& r = *list_iter1;
00169
00170 if(!r.valid_bound<Pr>(prefixSum))
00171 {
00172 if (DEBUG)
00173 std::cerr<<"bounds of "<<r<<" are wrong"<<std::endl;
00174 return false;
00175 }
00176 }
00177 }
00178
00179
00180 {
00181 container::const_iterator list_iter1;
00182 container::const_iterator list_iter2;
00183
00184 for (list_iter1 = rectangles.begin(); list_iter1 != rectangles.end(); list_iter1++)
00185 {
00186 const rectangle& r1 = *list_iter1;
00187 for (list_iter2 = rectangles.begin(); list_iter2 != rectangles.end(); list_iter2++)
00188 {
00189 const rectangle& r2 = *list_iter2;
00190 if (list_iter2 == list_iter1)
00191 continue;
00192
00193
00194 if((r1.y_top_l <= r2.y_top_l) && (r2.y_top_l <= r1.y_bot_r) &&
00195 (r1.x_top_l <= r2.x_top_l) && (r2.x_top_l <= r1.x_bot_r))
00196 {
00197 if (DEBUG)
00198 std::cerr<<r1<<" and "<<r2<<" overlap"<<std::endl;
00199 return false;
00200 }
00201
00202 if((r1.x_top_l <= r2.x_top_l) &&
00203 (r2.x_top_l <= r1.x_bot_r) &&
00204 (r1.x_top_l <= r2.x_bot_r) &&
00205 (r2.x_bot_r <= r1.x_bot_r) &&
00206 (r1.y_top_l <= r2.y_top_l) &&
00207 (r2.y_top_l <= r1.y_bot_r) &&
00208 (r1.y_top_l <= r2.y_bot_r) &&
00209 (r2.y_bot_r <= r1.y_bot_r))
00210 {
00211 if (DEBUG)
00212 std::cerr<<r1<<" is inside "<<r2<<" (or the contrary)"<<std::endl;
00213 return false;
00214 }
00215
00216 }
00217 }
00218 }
00219
00220
00221 {
00222 T tot_load = 0;
00223 int tot_area = 0;
00224 container::const_iterator list_iter1;
00225 for (list_iter1 = rectangles.begin(); list_iter1 != rectangles.end(); list_iter1++)
00226 {
00227 const rectangle& r = *list_iter1;
00228 tot_load += r.get_load<T,Pr>(prefixSum);
00229 tot_area += r.get_area();
00230 }
00231
00232
00233 int lastX = prefixSum.prefixsizeX() - 1;
00234 int lastY = prefixSum.prefixsizeY() - 1;
00235 if((tot_area != (lastX*lastY)))
00236 {
00237 if (DEBUG)
00238 std::cerr<<"The total area is wrong. The sum is "<<tot_area<<" but should be "<<lastX*lastY<<std::endl;
00239 return false;
00240 }
00241 }
00242
00243 return true;
00244 }
00245
00246 template <typename T, typename Pr>
00247 std::ostream& util::operator<< (std::ostream& out, const util::RectList<T, Pr>& r)
00248 {
00249 typedef typename util::RectList<T, Pr>::container::const_iterator container_it;
00250 container_it list_iter1;
00251
00252 for (list_iter1 = r.rectangles.begin();
00253 list_iter1 != r.rectangles.end();
00254 list_iter1++)
00255 out<<*list_iter1<<std::endl;
00256
00257 return out;
00258 }