You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@mesos.apache.org by ji...@apache.org on 2014/03/04 20:54:55 UTC

[1/2] git commit: Updated Makefile.am for the new interval set tests.

Repository: mesos
Updated Branches:
  refs/heads/master f8fac30c2 -> 449f58181


Updated Makefile.am for the new interval set tests.

Review: https://reviews.apache.org/r/18333


Project: http://git-wip-us.apache.org/repos/asf/mesos/repo
Commit: http://git-wip-us.apache.org/repos/asf/mesos/commit/449f5818
Tree: http://git-wip-us.apache.org/repos/asf/mesos/tree/449f5818
Diff: http://git-wip-us.apache.org/repos/asf/mesos/diff/449f5818

Branch: refs/heads/master
Commit: 449f581819c046b0dc74a0048e64a44f027092e0
Parents: 5f499aa
Author: Jie Yu <yu...@gmail.com>
Authored: Thu Feb 20 15:43:53 2014 -0800
Committer: Jie Yu <yu...@gmail.com>
Committed: Tue Mar 4 11:53:56 2014 -0800

----------------------------------------------------------------------
 3rdparty/libprocess/3rdparty/Makefile.am | 1 +
 1 file changed, 1 insertion(+)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/mesos/blob/449f5818/3rdparty/libprocess/3rdparty/Makefile.am
----------------------------------------------------------------------
diff --git a/3rdparty/libprocess/3rdparty/Makefile.am b/3rdparty/libprocess/3rdparty/Makefile.am
index 51b118c..30e6f97 100644
--- a/3rdparty/libprocess/3rdparty/Makefile.am
+++ b/3rdparty/libprocess/3rdparty/Makefile.am
@@ -136,6 +136,7 @@ stout_tests_SOURCES =				\
   $(STOUT)/tests/gzip_tests.cpp			\
   $(STOUT)/tests/hashmap_tests.cpp		\
   $(STOUT)/tests/hashset_tests.cpp		\
+  $(STOUT)/tests/interval_tests.cpp		\
   $(STOUT)/tests/json_tests.cpp			\
   $(STOUT)/tests/linkedhashmap_tests.cpp	\
   $(STOUT)/tests/main.cpp			\


[2/2] git commit: Added stout/interval.hpp to model boost interval set.

Posted by ji...@apache.org.
Added stout/interval.hpp to model boost interval set.

Review: https://reviews.apache.org/r/18093


Project: http://git-wip-us.apache.org/repos/asf/mesos/repo
Commit: http://git-wip-us.apache.org/repos/asf/mesos/commit/5f499aae
Tree: http://git-wip-us.apache.org/repos/asf/mesos/tree/5f499aae
Diff: http://git-wip-us.apache.org/repos/asf/mesos/diff/5f499aae

Branch: refs/heads/master
Commit: 5f499aae044ba754d45adac3ef8f862289fe36ce
Parents: f8fac30
Author: Jie Yu <yu...@gmail.com>
Authored: Thu Feb 20 15:42:34 2014 -0800
Committer: Jie Yu <yu...@gmail.com>
Committed: Tue Mar 4 11:53:56 2014 -0800

----------------------------------------------------------------------
 3rdparty/libprocess/3rdparty/stout/Makefile.am  |   2 +
 .../3rdparty/stout/include/stout/interval.hpp   | 262 +++++++++++++++++++
 .../3rdparty/stout/tests/interval_tests.cpp     | 258 ++++++++++++++++++
 3 files changed, 522 insertions(+)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/mesos/blob/5f499aae/3rdparty/libprocess/3rdparty/stout/Makefile.am
----------------------------------------------------------------------
diff --git a/3rdparty/libprocess/3rdparty/stout/Makefile.am b/3rdparty/libprocess/3rdparty/stout/Makefile.am
index 5d5a760..d5c9c68 100644
--- a/3rdparty/libprocess/3rdparty/stout/Makefile.am
+++ b/3rdparty/libprocess/3rdparty/stout/Makefile.am
@@ -25,6 +25,7 @@ EXTRA_DIST =					\
   include/stout/gzip.hpp			\
   include/stout/hashmap.hpp			\
   include/stout/hashset.hpp			\
+  include/stout/interval.hpp			\
   include/stout/json.hpp			\
   include/stout/lambda.hpp			\
   include/stout/linkedhashmap.hpp		\
@@ -72,6 +73,7 @@ EXTRA_DIST =					\
   tests/gzip_tests.cpp				\
   tests/hashmap_tests.cpp			\
   tests/hashset_tests.cpp			\
+  tests/interval_tests.cpp			\
   tests/json_tests.cpp				\
   tests/linkedhashmap_tests.cpp			\
   tests/main.cpp				\

http://git-wip-us.apache.org/repos/asf/mesos/blob/5f499aae/3rdparty/libprocess/3rdparty/stout/include/stout/interval.hpp
----------------------------------------------------------------------
diff --git a/3rdparty/libprocess/3rdparty/stout/include/stout/interval.hpp b/3rdparty/libprocess/3rdparty/stout/include/stout/interval.hpp
new file mode 100644
index 0000000..b219605
--- /dev/null
+++ b/3rdparty/libprocess/3rdparty/stout/include/stout/interval.hpp
@@ -0,0 +1,262 @@
+/**
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *  http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+#ifndef __STOUT_INTERVAL_HPP__
+#define __STOUT_INTERVAL_HPP__
+
+#include <functional> // For std::less.
+
+#include <boost/icl/interval.hpp>
+#include <boost/icl/interval_set.hpp>
+
+
+// Forward declarations.
+template <typename T>
+class Interval;
+
+
+// Represents a bound (left or right) for an interval. A bound can
+// either be open or closed.
+template <typename T>
+class Bound
+{
+public:
+  // Creates an open bound.
+  static Bound<T> open(const T& value)
+  {
+    return Bound<T>(OPEN, value);
+  }
+
+  // Creates an closed bound.
+  static Bound<T> closed(const T& value)
+  {
+    return Bound<T>(CLOSED, value);
+  }
+
+  // Intervals can be created using the comma operator. For example:
+  //   (2, 6):  (Bound<int>::open(2), Bound<int>::open(6))
+  //   (3, 4]:  (Bound<int>::open(3), Bound<int>::closed(4))
+  //   [0, 5):  (Bound<int>::closed(0), Bound<int>::open(5))
+  //   [1, 2]:  (Bound<int>::closed(1), Bound<int>::closed(2))
+  Interval<T> operator , (const Bound<T>& right) const;
+
+private:
+  enum Type
+  {
+    OPEN,
+    CLOSED,
+  };
+
+  Bound(const Type _type, const T& _value)
+    : type(_type), value(_value) {}
+
+  const Type type;
+  const T value;
+};
+
+
+// Represents an interval.
+template <typename T>
+class Interval
+{
+public:
+  // We must provide a public default constructor as the boost
+  // interval set expects that.
+  Interval() {}
+
+  // Returns the inclusive lower bound of this interval.
+  T lower() const { return data.lower(); }
+
+  // Returns the exclusive upper bound of this interval.
+  T upper() const { return data.upper(); }
+
+private:
+  friend class Bound<T>;
+
+  Interval(const boost::icl::right_open_interval<T, std::less>& _data)
+    : data(_data) {}
+
+  // We store the interval in right open format: [lower, upper).
+  // Notice that we use composition here instead of inheritance to
+  // prevent Interval from being passed to bare boost icl containers.
+  boost::icl::right_open_interval<T, std::less> data;
+};
+
+
+template <typename T>
+Interval<T> Bound<T>::operator , (const Bound<T>& right) const
+{
+  if (type == OPEN) {
+    if (right.type == OPEN) {
+      // For example: (1, 3).
+      return boost::icl::static_interval<
+          boost::icl::right_open_interval<T, std::less>, // Target.
+          boost::icl::is_discrete<T>::value,
+          boost::icl::interval_bounds::static_open,      // Input bounds.
+          boost::icl::interval_bounds::static_right_open // Output bounds.
+      >::construct(value, right.value);
+    } else {
+      // For example: (1, 3].
+      return boost::icl::static_interval<
+          boost::icl::right_open_interval<T, std::less>, // Target.
+          boost::icl::is_discrete<T>::value,
+          boost::icl::interval_bounds::static_left_open, // Input bounds.
+          boost::icl::interval_bounds::static_right_open // Output bounds.
+      >::construct(value, right.value);
+    }
+  } else {
+    if (right.type == OPEN) {
+      // For example: [1, 3).
+      return boost::icl::static_interval<
+          boost::icl::right_open_interval<T, std::less>,  // Target.
+          boost::icl::is_discrete<T>::value,
+          boost::icl::interval_bounds::static_right_open, // Input bounds.
+          boost::icl::interval_bounds::static_right_open  // Output bounds.
+      >::construct(value, right.value);
+    } else {
+      // For example: [1, 3].
+      return boost::icl::static_interval<
+          boost::icl::right_open_interval<T, std::less>,  // Target.
+          boost::icl::is_discrete<T>::value,
+          boost::icl::interval_bounds::static_closed,     // Input bounds.
+          boost::icl::interval_bounds::static_right_open  // Output bounds.
+      >::construct(value, right.value);
+    }
+  }
+}
+
+
+// Modeled after boost interval_set. Provides a compact representation
+// of a set by merging adjacent elements into intervals.
+template <typename T>
+class IntervalSet : public boost::icl::interval_set<T, std::less, Interval<T> >
+{
+public:
+  IntervalSet() {}
+
+  explicit IntervalSet(const T& value)
+  {
+    Base::add(value);
+  }
+
+  explicit IntervalSet(const Interval<T>& interval)
+  {
+    Base::add(interval);
+  }
+
+  IntervalSet(const Bound<T>& lower, const Bound<T>& upper)
+  {
+    Base::add((lower, upper));
+  }
+
+  // Checks if the specified value is in this set.
+  bool contains(const T& value) const
+  {
+    return boost::icl::contains(static_cast<const Base&>(*this), value);
+  }
+
+  // Returns the number of intervals in this set.
+  size_t intervalCount() const
+  {
+    return boost::icl::interval_count(static_cast<const Base&>(*this));
+  }
+
+  // Overloaded operators.
+  template <typename X>
+  IntervalSet<T>& operator += (const X& x)
+  {
+    static_cast<Base&>(*this) += x;
+    return *this;
+  }
+
+  template <typename X>
+  IntervalSet<T>& operator -= (const X& x)
+  {
+    static_cast<Base&>(*this) -= x;
+    return *this;
+  }
+
+  template <typename X>
+  IntervalSet<T>& operator &= (const X& x)
+  {
+    static_cast<Base&>(*this) &= x;
+    return *this;
+  }
+
+  // Special overloads for IntervalSet. According to C++ standard,
+  // a non-template function always wins an ambiguous overload.
+  IntervalSet<T>& operator += (const IntervalSet<T>& set)
+  {
+    static_cast<Base&>(*this) += static_cast<const Base&>(set);
+    return *this;
+  }
+
+  IntervalSet<T>& operator -= (const IntervalSet<T>& set)
+  {
+    static_cast<Base&>(*this) -= static_cast<const Base&>(set);
+    return *this;
+  }
+
+  IntervalSet<T>& operator &= (const IntervalSet<T>& set)
+  {
+    static_cast<Base&>(*this) &= static_cast<const Base&>(set);
+    return *this;
+  }
+
+private:
+  // We use typedef here to make the code less verbose.
+  typedef boost::icl::interval_set<T, std::less, Interval<T> > Base;
+};
+
+
+// Defines type traits for the custom Interval above. These type
+// traits are required by the boost interval set.
+namespace boost {
+namespace icl {
+
+template <typename T>
+struct interval_traits<Interval<T> >
+{
+  typedef interval_traits type;
+  typedef T domain_type;
+  typedef std::less<T> domain_compare;
+
+  static Interval<T> construct(const T& lower, const T& upper)
+  {
+    return (Bound<T>::closed(lower), Bound<T>::open(upper));
+  }
+
+  static T lower(const Interval<T>& interval)
+  {
+    return interval.lower();
+  }
+
+  static T upper(const Interval<T>& interval)
+  {
+    return interval.upper();
+  }
+};
+
+
+template <typename T>
+struct interval_bound_type<Interval<T> >
+  : public interval_bound_type<right_open_interval<T, std::less> >
+{
+  typedef interval_bound_type type;
+};
+
+} // namespace icl {
+} // namespace boost {
+
+#endif // __STOUT_INTERVAL_HPP__

http://git-wip-us.apache.org/repos/asf/mesos/blob/5f499aae/3rdparty/libprocess/3rdparty/stout/tests/interval_tests.cpp
----------------------------------------------------------------------
diff --git a/3rdparty/libprocess/3rdparty/stout/tests/interval_tests.cpp b/3rdparty/libprocess/3rdparty/stout/tests/interval_tests.cpp
new file mode 100644
index 0000000..6f51607
--- /dev/null
+++ b/3rdparty/libprocess/3rdparty/stout/tests/interval_tests.cpp
@@ -0,0 +1,258 @@
+#include <gtest/gtest.h>
+
+#include <stout/foreach.hpp>
+#include <stout/interval.hpp>
+
+
+TEST(IntervalTest, Interval)
+{
+  Interval<int> i1 = (Bound<int>::open(1), Bound<int>::closed(3));
+
+  EXPECT_EQ(2, i1.lower());
+  EXPECT_EQ(4, i1.upper());
+
+  Interval<int> i2 = (Bound<int>::open(3), Bound<int>::open(5));
+
+  EXPECT_EQ(4, i2.lower());
+  EXPECT_EQ(5, i2.upper());
+
+  Interval<int> i3 = (Bound<int>::closed(5), Bound<int>::open(6));
+
+  EXPECT_EQ(5, i3.lower());
+  EXPECT_EQ(6, i3.upper());
+
+  Interval<int> i4 = (Bound<int>::closed(6), Bound<int>::closed(7));
+
+  EXPECT_EQ(6, i4.lower());
+  EXPECT_EQ(8, i4.upper());
+
+  IntervalSet<int> set;
+
+  set += i1;
+
+  EXPECT_FALSE(set.contains(1));
+  EXPECT_TRUE(set.contains(2));
+  EXPECT_TRUE(set.contains(3));
+  EXPECT_EQ(1u, set.intervalCount());
+  EXPECT_EQ(2u, set.size());
+
+  set += i2;
+
+  EXPECT_TRUE(set.contains(4));
+  EXPECT_FALSE(set.contains(5));
+  EXPECT_EQ(1u, set.intervalCount());
+  EXPECT_EQ(3u, set.size());
+
+  set += i3;
+
+  EXPECT_TRUE(set.contains(5));
+  EXPECT_FALSE(set.contains(6));
+  EXPECT_EQ(1u, set.intervalCount());
+  EXPECT_EQ(4u, set.size());
+
+  set += i4;
+
+  EXPECT_TRUE(set.contains(6));
+  EXPECT_TRUE(set.contains(7));
+  EXPECT_EQ(1u, set.intervalCount());
+  EXPECT_EQ(6u, set.size());
+}
+
+
+TEST(IntervalTest, InvalidInterval)
+{
+  Interval<int> i1 = (Bound<int>::closed(1), Bound<int>::open(0));
+
+  EXPECT_EQ(1, i1.lower());
+  EXPECT_EQ(0, i1.upper());
+
+  Interval<int> i2 = (Bound<int>::open(1), Bound<int>::closed(0));
+
+  EXPECT_EQ(2, i2.lower());
+  EXPECT_EQ(1, i2.upper());
+
+  IntervalSet<int> set;
+
+  set += i1;
+
+  EXPECT_TRUE(set.empty());
+  EXPECT_EQ(0u, set.intervalCount());
+
+  set += i2;
+
+  EXPECT_TRUE(set.empty());
+  EXPECT_EQ(0u, set.intervalCount());
+}
+
+
+TEST(IntervalTest, Constructor)
+{
+  IntervalSet<int> set(0);
+
+  EXPECT_TRUE(set.contains(0));
+  EXPECT_EQ(1u, set.intervalCount());
+  EXPECT_EQ(1u, set.size());
+
+  IntervalSet<int> set2(Bound<int>::closed(1), Bound<int>::closed(2));
+
+  EXPECT_TRUE(set2.contains(1));
+  EXPECT_TRUE(set2.contains(2));
+  EXPECT_EQ(1u, set2.intervalCount());
+  EXPECT_EQ(2u, set2.size());
+}
+
+
+TEST(IntervalTest, Addition)
+{
+  IntervalSet<int> set;
+
+  set += 1;
+  set += 2;
+  set += 3;
+
+  EXPECT_TRUE(set.contains(1));
+  EXPECT_TRUE(set.contains(2));
+  EXPECT_TRUE(set.contains(3));
+  EXPECT_EQ(1u, set.intervalCount());
+  EXPECT_EQ(3u, set.size());
+
+  set += (Bound<int>::closed(5), Bound<int>::closed(6));
+
+  EXPECT_FALSE(set.contains(4));
+  EXPECT_TRUE(set.contains(5));
+  EXPECT_TRUE(set.contains(6));
+  EXPECT_EQ(2u, set.intervalCount());
+  EXPECT_EQ(5u, set.size());
+
+  set += (Bound<int>::open(2), Bound<int>::open(5));
+
+  EXPECT_TRUE(set.contains(4));
+  EXPECT_EQ(1u, set.intervalCount());
+  EXPECT_EQ(6u, set.size());
+
+  IntervalSet<int> set2;
+
+  set2 += (Bound<int>::closed(8), Bound<int>::closed(9));
+
+  set += set2;
+
+  EXPECT_TRUE(set.contains(8));
+  EXPECT_TRUE(set.contains(9));
+  EXPECT_EQ(2u, set.intervalCount());
+  EXPECT_EQ(8u, set.size());
+}
+
+
+TEST(IntervalTest, Subtraction)
+{
+  IntervalSet<int> set;
+
+  set += (Bound<int>::closed(1), Bound<int>::closed(10));
+
+  EXPECT_EQ(1u, set.intervalCount());
+  EXPECT_EQ(10u, set.size());
+
+  set -= 5;
+
+  EXPECT_FALSE(set.contains(5));
+  EXPECT_EQ(2u, set.intervalCount());
+  EXPECT_EQ(9u, set.size());
+
+  set -= (Bound<int>::closed(2), Bound<int>::closed(8));
+
+  EXPECT_FALSE(set.contains(2));
+  EXPECT_FALSE(set.contains(8));
+  EXPECT_EQ(2u, set.intervalCount());
+  EXPECT_EQ(3u, set.size());
+
+  set -= (Bound<int>::open(0), Bound<int>::open(2));
+
+  EXPECT_FALSE(set.contains(1));
+  EXPECT_EQ(1u, set.intervalCount());
+  EXPECT_EQ(2u, set.size());
+
+  IntervalSet<int> set2;
+
+  set2 += (Bound<int>::open(5), Bound<int>::closed(10));
+
+  set -= set2;
+
+  EXPECT_TRUE(set.empty());
+  EXPECT_EQ(0u, set.intervalCount());
+}
+
+
+TEST(IntervalTest, Intersection)
+{
+  IntervalSet<int> set;
+
+  set += (Bound<int>::closed(1), Bound<int>::closed(3));
+
+  EXPECT_TRUE(set.contains(1));
+  EXPECT_EQ(1u, set.intervalCount());
+  EXPECT_EQ(3u, set.size());
+
+  set &= (Bound<int>::open(1), Bound<int>::open(5));
+
+  EXPECT_FALSE(set.contains(1));
+  EXPECT_EQ(1u, set.intervalCount());
+  EXPECT_EQ(2u, set.size());
+
+  IntervalSet<int> set2;
+
+  set2 += 6;
+
+  set &= set2;
+
+  EXPECT_TRUE(set.empty());
+  EXPECT_EQ(0u, set.intervalCount());
+}
+
+
+TEST(IntervalTest, LargeInterval)
+{
+  IntervalSet<int> set;
+
+  set += (Bound<int>::open(1), Bound<int>::open(100));
+  set += (Bound<int>::closed(100), Bound<int>::closed(1000000000));
+
+  EXPECT_FALSE(set.contains(1));
+  EXPECT_TRUE(set.contains(5));
+  EXPECT_TRUE(set.contains(100));
+  EXPECT_TRUE(set.contains(100000));
+  EXPECT_TRUE(set.contains(1000000000));
+  EXPECT_EQ(1u, set.intervalCount());
+  EXPECT_EQ(999999999u, set.size());
+}
+
+
+TEST(IntervalTest, IntervalIteration)
+{
+  IntervalSet<int> set;
+
+  set += (Bound<int>::closed(0), Bound<int>::closed(1));
+  set += (Bound<int>::open(2), Bound<int>::open(4));
+  set += (Bound<int>::closed(5), Bound<int>::open(7));
+  set += (Bound<int>::open(7), Bound<int>::closed(9));
+
+  EXPECT_EQ(4u, set.intervalCount());
+
+  int index = 0;
+
+  foreach (const Interval<int>& interval, set) {
+    if (index == 0) {
+      EXPECT_EQ(0, interval.lower());
+      EXPECT_EQ(2, interval.upper());
+    } else if (index == 1) {
+      EXPECT_EQ(3, interval.lower());
+      EXPECT_EQ(4, interval.upper());
+    } else if (index == 2) {
+      EXPECT_EQ(5, interval.lower());
+      EXPECT_EQ(7, interval.upper());
+    } else if (index == 3) {
+      EXPECT_EQ(8, interval.lower());
+      EXPECT_EQ(10, interval.upper());
+    }
+    index++;
+  }
+}