Update all #include directives to the currently preferred style.
[pspp-builds.git] / src / libpspp / range-set.c
index d238ea98c5f72e03bbc49a16adcf584ef33894ad..ed7b3f5edb6a50f4220b8a3d9c372cdd05bdc373 100644 (file)
@@ -1,5 +1,5 @@
 /* PSPP - a program for statistical analysis.
-   Copyright (C) 2007, 2009 Free Software Foundation, Inc.
+   Copyright (C) 2007, 2009, 2011 Free Software Foundation, Inc.
 
    This program is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
 
 #include <config.h>
 
-#include <libpspp/range-set.h>
+#include "libpspp/range-set.h"
 
 #include <limits.h>
 #include <stdlib.h>
 
-#include <libpspp/assertion.h>
-#include <libpspp/compiler.h>
-#include <libpspp/pool.h>
+#include "libpspp/assertion.h"
+#include "libpspp/compiler.h"
+#include "libpspp/pool.h"
 
-#include "minmax.h"
-#include "xalloc.h"
+#include "gl/minmax.h"
+#include "gl/xalloc.h"
 
 static int compare_range_set_nodes (const struct bt_node *,
                                     const struct bt_node *,
@@ -287,7 +287,7 @@ range_set_allocate_fully (struct range_set *rs, unsigned long int request,
 bool
 range_set_contains (const struct range_set *rs_, unsigned long int position)
 {
-  struct range_set *rs = (struct range_set *) rs_;
+  struct range_set *rs = CONST_CAST (struct range_set *, rs_);
   if (position < rs->cache_end && position >= rs->cache_start)
     return rs->cache_value;
   else
@@ -321,6 +321,40 @@ range_set_contains (const struct range_set *rs_, unsigned long int position)
         }
     }
 }
+
+/* Returns the smallest position of a 1-bit greater than or
+   equal to START.  Returns ULONG_MAX if there is no 1-bit with
+   position greater than or equal to START. */
+unsigned long int
+range_set_scan (const struct range_set *rs_, unsigned long int start)
+{
+  struct range_set *rs = CONST_CAST (struct range_set *, rs_);
+  unsigned long int retval = ULONG_MAX;
+  struct bt_node *bt_node;
+
+  if (start < rs->cache_end && start >= rs->cache_start && rs->cache_value)
+    return start;
+  bt_node = rs->bt.root;
+  while (bt_node != NULL)
+    {
+      struct range_set_node *node = range_set_node_from_bt__ (bt_node);
+      if (start < node->start)
+        {
+          retval = node->start;
+          bt_node = node->bt_node.down[0];
+        }
+      else if (start >= node->end)
+        bt_node = node->bt_node.down[1];
+      else
+        {
+          rs->cache_start = node->start;
+          rs->cache_end = node->end;
+          rs->cache_value = true;
+          return start;
+        }
+    }
+  return retval;
+}
 \f
 /* Compares `range_set_node's A and B and returns a strcmp-like
    return value. */