summaryrefslogtreecommitdiffstats
path: root/src/libnr
diff options
context:
space:
mode:
authorMenTaLguY <mental@rydia.net>2007-03-09 00:25:51 +0000
committermental <mental@users.sourceforge.net>2007-03-09 00:25:51 +0000
commitb617f8eb8c9c9467caa4c15ba71259661d9222ce (patch)
tree515a14bc288f031bbb9b082dc5839e66fdb961cd /src/libnr
parentfix reference maybes by separate specialization (diff)
downloadinkscape-b617f8eb8c9c9467caa4c15ba71259661d9222ce.tar.gz
inkscape-b617f8eb8c9c9467caa4c15ba71259661d9222ce.zip
specialize MaybeStorage for Rect, and start using reference maybes to
avoid some copies (bzr r2582)
Diffstat (limited to 'src/libnr')
-rw-r--r--src/libnr/nr-rect.cpp4
-rw-r--r--src/libnr/nr-rect.h81
2 files changed, 51 insertions, 34 deletions
diff --git a/src/libnr/nr-rect.cpp b/src/libnr/nr-rect.cpp
index 21d3f470a..9047e4e1c 100644
--- a/src/libnr/nr-rect.cpp
+++ b/src/libnr/nr-rect.cpp
@@ -284,7 +284,7 @@ void Rect::expandTo(Point p) {
}
/** Returns the set of points shared by both rectangles. */
-Maybe<Rect> Rect::intersection(Maybe<Rect> const &a, Maybe<Rect> const &b) {
+Maybe<Rect> intersection(Maybe<Rect const &> a, Maybe<Rect const &> b) {
if ( !a || !b ) {
return Nothing();
} else {
@@ -301,7 +301,7 @@ Maybe<Rect> Rect::intersection(Maybe<Rect> const &a, Maybe<Rect> const &b) {
}
/** returns the smallest rectangle containing both rectangles */
-Rect Rect::union_bounds(Rect const &a, Rect const &b) {
+Rect union_bounds(Rect const &a, Rect const &b) {
Rect r;
for ( int i=0 ; i < 2 ; i++ ) {
r._min[i] = MIN(a._min[i], b._min[i]);
diff --git a/src/libnr/nr-rect.h b/src/libnr/nr-rect.h
index dd7caa897..5e2332bf2 100644
--- a/src/libnr/nr-rect.h
+++ b/src/libnr/nr-rect.h
@@ -125,36 +125,6 @@ public:
_max[NR::Y] += by;
}
- /** Returns the set of points shared by both rectangles. */
- static Maybe<Rect> intersection(Maybe<Rect> const &a, Maybe<Rect> const &b);
-
- /** Returns the smallest rectangle that encloses both rectangles. */
- static Maybe<Rect> union_bounds(Maybe<Rect> const &a, Maybe<Rect> const &b)
- {
- if (!a) {
- return b;
- } else if (!b) {
- return a;
- } else {
- return union_bounds(*a, *b);
- }
- }
- static Rect union_bounds(Maybe<Rect> const &a, Rect const &b) {
- if (a) {
- return union_bounds(*a, b);
- } else {
- return b;
- }
- }
- static Rect union_bounds(Rect const &a, Maybe<Rect> const &b) {
- if (b) {
- return union_bounds(a, *b);
- } else {
- return a;
- }
- }
- static Rect union_bounds(Rect const &a, Rect const &b);
-
/** Scales the rect by s, with origin at 0, 0 */
inline Rect operator*(double const s) const {
return Rect(s * min(), s * max());
@@ -172,6 +142,8 @@ public:
friend inline std::ostream &operator<<(std::ostream &out_file, NR::Rect const &in_rect);
private:
+ Rect(Nothing) : _min(1, 1), _max(-1, -1) {}
+
static double _inf() {
return std::numeric_limits<double>::infinity();
}
@@ -203,10 +175,55 @@ private:
Point _min, _max;
- /* evil, but temporary */
- friend class Maybe<Rect>;
+ friend class MaybeStorage<Rect>;
};
+template <>
+class MaybeStorage<Rect> {
+public:
+ MaybeStorage() : _rect(Nothing()) {}
+ MaybeStorage(Rect const &rect) : _rect(rect) {}
+
+ bool is_nothing() const {
+ return _rect._min[X] > _rect._max[X];
+ }
+ Rect const &value() const { return _rect; }
+ Rect &value() { return _rect; }
+
+private:
+ Rect _rect;
+};
+
+/** Returns the set of points shared by both rectangles. */
+Maybe<Rect> intersection(Maybe<Rect const &> a, Maybe<Rect const &> b);
+
+/** Returns the smallest rectangle that encloses both rectangles. */
+Rect union_bounds(Rect const &a, Rect const &b);
+inline Rect union_bounds(Maybe<Rect const &> a, Rect const &b) {
+ if (a) {
+ return union_bounds(*a, b);
+ } else {
+ return b;
+ }
+}
+inline Rect union_bounds(Rect const &a, Maybe<Rect const &> b) {
+ if (b) {
+ return union_bounds(a, *b);
+ } else {
+ return a;
+ }
+}
+inline Maybe<Rect> union_bounds(Maybe<Rect const &> a, Maybe<Rect const &> b)
+{
+ if (!a) {
+ return b;
+ } else if (!b) {
+ return a;
+ } else {
+ return union_bounds(*a, *b);
+ }
+}
+
/** A function to print out the rectange if sent to an output
stream. */
inline std::ostream