aboutsummaryrefslogtreecommitdiff
path: root/pkg/xtools/ranges/rgintersect.x
diff options
context:
space:
mode:
authorJoseph Hunkeler <jhunkeler@gmail.com>2015-07-08 20:46:52 -0400
committerJoseph Hunkeler <jhunkeler@gmail.com>2015-07-08 20:46:52 -0400
commitfa080de7afc95aa1c19a6e6fc0e0708ced2eadc4 (patch)
treebdda434976bc09c864f2e4fa6f16ba1952b1e555 /pkg/xtools/ranges/rgintersect.x
downloadiraf-linux-fa080de7afc95aa1c19a6e6fc0e0708ced2eadc4.tar.gz
Initial commit
Diffstat (limited to 'pkg/xtools/ranges/rgintersect.x')
-rw-r--r--pkg/xtools/ranges/rgintersect.x58
1 files changed, 58 insertions, 0 deletions
diff --git a/pkg/xtools/ranges/rgintersect.x b/pkg/xtools/ranges/rgintersect.x
new file mode 100644
index 00000000..5e4e4390
--- /dev/null
+++ b/pkg/xtools/ranges/rgintersect.x
@@ -0,0 +1,58 @@
+# Copyright(c) 1986 Association of Universities for Research in Astronomy Inc.
+
+include <pkg/rg.h>
+
+# RG_INTERSECT -- Intersect two sets of ordered and merged ranges.
+
+pointer procedure rg_intersect (rg1, rg2)
+
+pointer rg1 # First set of ranges
+pointer rg2 # Second set of ranges
+
+pointer rg3 # Pointer to intersection
+
+int i, j, k
+
+begin
+ # Error check the range pointers.
+
+ if ((rg1 == NULL) || (rg2 == NULL))
+ call error (0, "Range descriptor(s) undefined")
+
+ # Allocate the range points array.
+
+ k = RG_NRGS(rg1) + RG_NRGS(rg2) - 1
+ call malloc (rg3, LEN_RG + 2 * max (1, k), TY_STRUCT)
+
+ # Set the ranges.
+
+ i = 1
+ j = 1
+ k = 0
+
+ while (i <= RG_NRGS(rg1) && j <= RG_NRGS(rg2)) {
+ if (RG_X2(rg1, i) < RG_X1(rg2, j))
+ i = i + 1
+ else if (RG_X2(rg2, j) < RG_X1(rg1, i))
+ j = j + 1
+ else {
+ k = k + 1
+ RG_X1(rg3, k) = max (RG_X1(rg1, i), RG_X1(rg2, j))
+ RG_X2(rg3, k) = min (RG_X2(rg1, i), RG_X2(rg2, j))
+
+ if (RG_X2(rg1, i) < RG_X2(rg2, j))
+ i = i + 1
+ else
+ j = j + 1
+ }
+ }
+
+ call realloc (rg3, LEN_RG + 2 * max (1, k), TY_STRUCT)
+
+ RG_NRGS(rg3) = k
+ RG_NPTS(rg3) = 0
+ do i = 1, RG_NRGS(rg3)
+ RG_NPTS(rg3) = RG_NPTS(rg3) + RG_X2(rg3, i) - RG_X1(rg3, i) + 1
+
+ return (rg3)
+end