modules/up/src/rpsl/rpsl/rangelist.cc
/* [<][>][^][v][top][bottom][index][help] */
FUNCTIONS
This source file includes following functions.
- MAX
- complement
- contains
1 // $Id: rangelist.cc,v 1.1.1.1 2000/03/10 16:32:24 engin Exp $
2 //
3 // Copyright (c) 1994 by the University of Southern California
4 // All rights reserved.
5 //
6 // Permission to use, copy, modify, and distribute this software and its
7 // documentation in source and binary forms for lawful non-commercial
8 // purposes and without fee is hereby granted, provided that the above
9 // copyright notice appear in all copies and that both the copyright
10 // notice and this permission notice appear in supporting documentation,
11 // and that any documentation, advertising materials, and other materials
12 // related to such distribution and use acknowledge that the software was
13 // developed by the University of Southern California, Information
14 // Sciences Institute. The name of the USC may not be used to endorse or
15 // promote products derived from this software without specific prior
16 // written permission.
17 //
18 // THE UNIVERSITY OF SOUTHERN CALIFORNIA DOES NOT MAKE ANY
19 // REPRESENTATIONS ABOUT THE SUITABILITY OF THIS SOFTWARE FOR ANY
20 // PURPOSE. THIS SOFTWARE IS PROVIDED "AS IS" AND WITHOUT ANY EXPRESS OR
21 // IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
22 // WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE,
23 // TITLE, AND NON-INFRINGEMENT.
24 //
25 // IN NO EVENT SHALL USC, OR ANY OTHER CONTRIBUTOR BE LIABLE FOR ANY
26 // SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES, WHETHER IN CONTRACT, TORT,
27 // OR OTHER FORM OF ACTION, ARISING OUT OF OR IN CONNECTION WITH, THE USE
28 // OR PERFORMANCE OF THIS SOFTWARE.
29 //
30 // Questions concerning this software should be directed to
31 // ratoolset@isi.edu.
32 //
33 // Author(s): Cengiz Alaettinoglu <cengiz@ISI.EDU>
34
35 #include "config.h"
36 #include "rangelist.hh"
37
38 #ifndef MAX
39 #define MAX(x,y) ((x) > (y) ? (x) : (y))
/* [<][>][^][v][top][bottom][index][help] */
40 #endif
41
42 // static member initialization
43 char *RangeList::prefix_str = "AS";
44
45 ostream& operator<<(ostream& out, const RangeList &no) {
46 RangeList::Range *pi, *qi;
47
48 if (no.universal())
49 out << ".";
50 else if (no.empty())
51 out << "E";
52 else
53 for (pi = no.ranges.head(); pi; pi = no.ranges.next(pi)) {
54 out << " " << no.prefix_str << pi->low;
55 if (pi->low != pi->high)
56 out << "-" << no.prefix_str << pi->high;
57 }
58
59 return out;
60 }
61
62 RangeList& RangeList::operator+=(RangeList& no) { // destroys no
63 Range *pi, *qi, *r;
64
65 pi = ranges.head();
66 qi = no.ranges.head();
67 while (pi && qi) {
68 while (pi && pi->low <= qi->low)
69 pi = ranges.next(pi);
70
71 if (pi)
72 while (qi && qi->low < pi->low) {
73 r = qi;
74 qi = no.ranges.next(qi);
75 no.ranges.remove(r);
76 ranges.insertBefore(pi, r);
77 }
78 }
79
80 // add remaining ranges of no
81 ranges.splice(no.ranges);
82
83 for (pi = ranges.head(); pi; ) {
84 qi = ranges.next(pi);
85 if (qi && pi->high >= qi->low - 1) {
86 pi->high = MAX(qi->high, pi->high);
87 ranges.remove(qi);
88 delete qi;
89 } else
90 pi = qi;
91 }
92
93 return *this;
94 }
95
96 void RangeList::complement() {
/* [<][>][^][v][top][bottom][index][help] */
97 Range *pi = ranges.head();
98 Range *qi;
99
100 if (universal()) {
101 clear();
102 return;
103 }
104
105 if (empty()) {
106 ranges.append(new Range(0, RANGE_TOP));
107 return;
108 }
109
110 if (pi->low != 0)
111 ranges.prepend(new Range(0, (*pi).low-1));
112
113 for (qi = ranges.next(pi);
114 qi;
115 pi = qi, qi = ranges.next(qi)) {
116 pi->low = pi->high + 1;
117 pi->high = qi->low - 1;
118 }
119
120 if (pi->high != RANGE_TOP) {
121 pi->low = pi->high + 1;
122 pi->high = RANGE_TOP;
123 } else {
124 ranges.remove(pi);
125 delete pi;
126 }
127 }
128
129 int RangeList::contains(const Range r) const {
/* [<][>][^][v][top][bottom][index][help] */
130 for (Range *i = ranges.head(); i; i = ranges.next(i))
131 if (i->low <= r.low && i->high >= r.high)
132 return 1;
133
134 return 0;
135 }