1 /*
2 * sl-merge.h: Part of GNU CSSC.
3 *
4 *
5 * Copyright (C) 1997, 2007, 2008, 2009, 2010, 2011, 2014, 2019 Free
6 * Software Foundation, Inc.
7 *
8 * This program is free software: you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation, either version 3 of the License, or
11 * (at your option) any later version.
12 *
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
17 *
18 * You should have received a copy of the GNU General Public License
19 * along with this program. If not, see <http://www.gnu.org/licenses/>.
20 *
21 * CSSC was originally Based on MySC, by Ross Ridge, which was
22 * placed in the Public Domain.
23 *
24 *
25 * Merge and remove member functions of the template range_list.
26 *
27 * @(#) CSSC sl-merge.c 1.1 93/11/09 17:18:03
28 *
29 */
30 #include "cssc.h"
31 #include "sid.h"
32 #include "sid_list.h"
33
34 #ifndef CSSC__SL_MERGE_H__
35 #define CSSC__SL_MERGE_H__
36
37 template <class TYPE>
38 range_list<TYPE> &
merge(range_list<TYPE> const & list)39 range_list<TYPE>::merge(range_list<TYPE> const &list) {
40 if (!valid() || !list.valid()) {
41 return *this;
42 }
43
44 range<TYPE> *sp = list.head;
45 if (sp == NULL) {
46 return *this;
47 }
48
49 ASSERT(valid());
50 sp = do_copy_list(sp);
51
52 if (head == NULL) {
53 head = sp;
54 } else {
55 range<TYPE> *dp = head;
56
57 while(dp->next != NULL) {
58 dp = dp->next;
59 }
60
61 dp->next = sp;
62 }
63 clean();
64 return *this;
65 }
66
67 template <class TYPE>
68 range_list<TYPE> &
remove(range_list<TYPE> const & list)69 range_list<TYPE>::remove(range_list<TYPE> const &list) {
70 if (!valid() || !list.valid()) {
71 return *this;
72 }
73
74 range<TYPE> *sp = list.head;
75 if (sp == NULL) {
76 return *this;
77 }
78
79 if (head == NULL) {
80 return *this;
81 }
82
83 while(sp != NULL) {
84 range<TYPE> *dp = head;
85 while(dp != NULL) {
86 if (sp->from <= dp->from
87 && sp->to >= dp->from) {
88 dp->from = sp->to;
89 ++dp->from;
90 }
91 if (sp->to >= dp->to
92 && sp->from <= dp->to) {
93 dp->to = sp->from;
94 --dp->to;
95 }
96 if (sp->from > dp->from && sp->to < dp->to) {
97 range<TYPE> *p = new range<TYPE>;
98 p->from = dp->from;
99 p->to = sp->from;
100 --p->to;
101
102 sp->from = dp->to;
103 ++sp->from;
104
105 p->next = head->next;
106 head = p;
107 }
108 dp = dp->next;
109 }
110 sp = sp->next;
111 }
112 clean();
113 return *this;
114 }
115
116 #endif /* __SL_MERGE_C__ */
117
118 /* Local variables: */
119 /* mode: c++ */
120 /* End: */
121