automatic commit
[folded-ctf.git] / interval.h
diff --git a/interval.h b/interval.h
new file mode 100644 (file)
index 0000000..57810f6
--- /dev/null
@@ -0,0 +1,58 @@
+
+///////////////////////////////////////////////////////////////////////////
+// This program is free software: you can redistribute it and/or modify  //
+// it under the terms of the version 3 of the GNU General Public License //
+// as published by the Free Software Foundation.                         //
+//                                                                       //
+// This program is distributed in the hope that it will be useful, but   //
+// WITHOUT ANY WARRANTY; without even the implied warranty of            //
+// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU      //
+// General Public License for more details.                              //
+//                                                                       //
+// You should have received a copy of the GNU General Public License     //
+// along with this program. If not, see <http://www.gnu.org/licenses/>.  //
+//                                                                       //
+// Written by Francois Fleuret, (C) IDIAP                                //
+// Contact <francois.fleuret@idiap.ch> for comments & bug reports        //
+///////////////////////////////////////////////////////////////////////////
+
+#ifndef INTERVAL_H
+#define INTERVAL_H
+
+#include "misc.h"
+
+class Interval {
+public:
+  scalar_t min, max;
+
+  void set(scalar_t x);
+  void set(scalar_t a, scalar_t b);
+  void set(Interval *i);
+
+  // Set this interval to the k-th of the nb regular subintervals of i
+  void set_subinterval(Interval *i, int k, int nb);
+
+  // Grow to contain i
+  void swallow(Interval *i);
+
+  inline bool contains(scalar_t x) {
+    return x >= min && x < max;
+  }
+
+  inline void include(scalar_t x) {
+    if(x < min) min = x;
+    if(x > max) max = x;
+  }
+
+  inline scalar_t middle() {
+    return (min + max) / 2;
+  }
+
+  inline scalar_t length() {
+    return max - min;
+  }
+};
+
+ostream &operator << (ostream &out, const Interval &i);
+
+#endif