summaryrefslogtreecommitdiffstats
path: root/sca-cpp/trunk/kernel/tree.hpp
blob: 77f2f1ea549a45e62fe7a0e76dc7a0577085780c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
/*
 * Licensed to the Apache Software Foundation (ASF) under one
 * or more contributor license agreements.  See the NOTICE file
 * distributed with this work for additional information
 * regarding copyright ownership.  The ASF licenses this file
 * to you under the Apache License, Version 2.0 (the
 * "License"); you may not use this file except in compliance
 * with the License.  You may obtain a copy of the License at
 *
 *   http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing,
 * software distributed under the License is distributed on an
 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
 * KIND, either express or implied.  See the License for the
 * specific language governing permissions and limitations
 * under the License.
 */

/* $Rev$ $Date$ */

#ifndef tuscany_tree_hpp
#define tuscany_tree_hpp

/**
 * Functions to work with trees.
 */

#include "stream.hpp"
#include "string.hpp"
#include "function.hpp"
#include "list.hpp"
#include "monad.hpp"

namespace tuscany {

/**
 * Delete assocs matching a path of keys in a tree of assocs.
 * The path can be a complete or partial path to an assoc.
 * Requires T to support isList, isAssoc, and cast to list<T>.
 */
template<typename T> inline const list<T> treeDelAssoc(const list<T>& k, const list<T>& l) noexcept {
    if (isNull(k) || isNull(l))
        return l;
    const list<T> lv = l;

    // If list is an assoc and matches, skip it
    if (isAssoc(lv)) {
        if (car<T>(lv) == car(k) && isNull(cdr(k)))
            return list<T>();
    }

    // If list element is not an assoc, lookup children and rest of the list
    const T a = car(lv);
    if (!isAssoc(a)) {
        if (!isList(a))
            return cons<T>(a, treeDelAssoc<T>(k, cdr(lv)));
        const list<T> da = treeDelAssoc<T>(k, a);
        return isNull(da)? treeDelAssoc<T>(k, cdr(lv)) : cons<T>(da, treeDelAssoc<T>(k, cdr(lv)));
    }

    // If we found a match, skip it and lookup children and rest of the list
    if (car<T>(a) == car(k)) {
        if (isNull(cdr(k)))
            return treeDelAssoc<T>(k, cdr(lv));
        return cons<T>(cons<T>(car<T>(a), treeDelAssoc<T>(cdr(k), cdr<T>(a))), treeDelAssoc<T>(k, cdr(lv)));
    }

    // No match, lookup children and rest of the list
    if (isNull(cdr<T>(a)))
        return cons<T>(a, treeDelAssoc<T>(k, cdr(lv)));
    if (!isList(cadr<T>(a)))
        return cons<T>(cons<T>(car<T>(a), cons<T>(cadr<T>(a), treeDelAssoc<T>(k, cddr<T>(a)))), treeDelAssoc<T>(k, cdr(lv)));
    const list<T> da = treeDelAssoc<T>(k, cadr<T>(a));
    if (isNull(da))
        return cons<T>(cons<T>(car<T>(a), treeDelAssoc<T>(k, cddr<T>(a))), treeDelAssoc<T>(k, cdr(lv)));
    return cons<T>(cons<T>(car<T>(a), cons<T>(da, treeDelAssoc<T>(k, cddr<T>(a)))), treeDelAssoc<T>(k, cdr(lv)));
}

/**
 * Substitute assocs matching a path of keys in a tree of assocs.
 * The path can be a complete or partial path to an assoc.
 * Requires T to support isList, isAssoc, and cast to list<T>.
 */
template<typename T> inline const list<T> treeSubstAssoc(const list<T>& k, const list<T>& n, const list<T>& lv) noexcept {
    if (isNull(k) || isNull(lv))
        return lv;

    // If list is an assoc and matches, substitute it
    if (isAssoc(lv)) {
        if (car<T>(lv) == car(k) && isNull(cdr(k)))
            return n;
    }

    // If list element is not an assoc, lookup children and rest of the list
    const T a = car(lv);
    if (!isAssoc(a)) {
        if (!isList(a))
            return cons<T>(a, treeSubstAssoc<T>(k, n, cdr(lv)));
        return cons<T>(treeSubstAssoc<T>(k, n, a), treeSubstAssoc<T>(k, n, cdr(lv)));
    }

    // If we found a match, substitute it and lookup children and rest of the list
    if (car<T>(a) == car(k)) {
        if (isNull(cdr(k)))
            return cons<T>(n, treeSubstAssoc<T>(k, n, cdr(lv)));
        return cons<T>(cons<T>(car<T>(a), treeSubstAssoc<T>(cdr(k), n, cdr<T>(a))), treeSubstAssoc<T>(k, n, cdr(lv)));
    }

    // No match, lookup children and rest of the list
    if (isNull(cdr<T>(a)))
        return cons<T>(a, treeSubstAssoc<T>(k, n, cdr(lv)));
    if (!isList(cadr<T>(a)))
        return cons<T>(cons<T>(car<T>(a), cons<T>(cadr<T>(a), treeSubstAssoc<T>(k, n, cddr<T>(a)))), treeSubstAssoc<T>(k, n, cdr(lv)));
    return cons<T>(cons<T>(car<T>(a), cons<T>(treeSubstAssoc<T>(k, n, cadr<T>(a)), treeSubstAssoc<T>(k, n, cddr<T>(a)))), treeSubstAssoc<T>(k, n, cdr(lv)));
}

/**
 * Select assocs matching a path of keys in a tree of assocs.
 * The path can be a complete or partial path to an assoc.
 * Requires T to support isList, isAssoc, and cast to list<T>.
 */
template<typename T> inline const list<T> treeSelectAssoc(const list<T>& k, const list<T>& lv) noexcept {
    if (isNull(k) || isNull(lv))
        return list<T>();

    // If list is an assoc and matches, select it
    if (isAssoc(lv)) {
        if (car<T>(lv) == car(k) && isNull(cdr(k)))
            return mklist<T>(lv);
    }

    // If list element is not an assoc, lookup children and rest of the list
    const T a = car(lv);
    if (!isAssoc(a)) {
        if (!isList(a))
            return treeSelectAssoc<T>(k, cdr(lv));
        return append<T>(treeSelectAssoc<T>(k, a), treeSelectAssoc<T>(k, cdr(lv)));
    }

    // If we found a match, select it and lookup children and rest of the list
    if (car<T>(a) == car(k)) {
        if (isNull(cdr(k)))
            return cons<T>(a, treeSelectAssoc<T>(k, cdr(lv)));
        return append<T>(treeSelectAssoc<T>(cdr(k), cdr<T>(a)), treeSelectAssoc<T>(k, cdr(lv)));
    }

    // No match, lookup children and rest of the list
    if (isNull(cdr<T>(a)))
        return treeSelectAssoc<T>(k, cdr(lv));
    if (!isList(cadr<T>(a)))
        return append<T>(treeSelectAssoc<T>(k, cddr<T>(a)), treeSelectAssoc<T>(k, cdr(lv)));
    return append<T>(append<T>(treeSelectAssoc<T>(k, cadr<T>(a)), treeSelectAssoc<T>(k, cddr<T>(a))), treeSelectAssoc<T>(k, cdr(lv)));
}

/**
 * Make a rooted binary tree from a leaf and two branches.
 */
template<typename T> inline const list<T> mkrbtree(const T& e, const list<T>& left, const list<T>& right) {
    return mklist<T>(e, left, right);
}

/**
 * Find a leaf with the given key in a rooted binary tree.
 */
template<typename T> inline const list<T> rbtreeAssoc(const T& k, const list<T>& tree) {
    if (isNull(tree))
        return tree;
    if (k == car<T>(car(tree)))
        return car(tree);
    if (k < car<T>(car(tree)))
        return rbtreeAssoc<T>(k, cadr(tree));
    return rbtreeAssoc<T>(k, caddr(tree));
}

/**
 * Default function used to compare two values while building a rooted binary tree.
 */
template<typename T> inline const int rbtreeComp(const T& a, const T& b) {
    if (a == b)
        return 0;
    if (a < b)
        return -1;
    return 1;
}

/**
 * Construct a new rooted binary tree from a leaf and a tree.
 */
template<typename T> inline const list<T> rbtreeCons(const T& e, const list<T>& tree, const lambda<int(const T&, const T&)>& comp = rbtreeComp<T>) {
    if (isNull(tree))
        return mkrbtree(e, list<T>(), list<T>());
    const int c = comp(e, car(tree));
    if (c == 0)
        return tree;
    if (c == -1)
        return mkrbtree<T>(car(tree), rbtreeCons<T>(e, cadr(tree), comp), caddr(tree));
    return mkrbtree<T>(car(tree), cadr(tree), rbtreeCons<T>(e, caddr(tree), comp));
}

/**
 * Make a rooted binary tree from an unordered list of leaves.
 */
template<typename T> inline const list<T> mkrbtree(const list<T>& l, const lambda<int(const T&, const T&)>& comp = rbtreeComp<T>) {
    if (isNull(l))
        return l;
    return rbtreeCons(car(l), mkrbtree(cdr(l), comp), comp);
}

/**
 * Convert a rooted binary tree to an ordered list of leaves.
 */
template<typename T> inline const list<T> flatten(const list<T>& tree) {
    if (isNull(tree))
        return tree;
    return append<T>(flatten<T>(cadr(tree)), cons<T>(car(tree), flatten<T>(caddr(tree))));
}

/**
 * Sort a list, using a rooted binary tree.
 */
template<typename T> inline const list<T> sort(const list<T>& l,  const lambda<int(const T&, const T&)>& comp = rbtreeComp<T>) {
    return flatten(mkrbtree(l, comp));
}

/**
 * Make a balanced rooted binary tree from an ordered list of leaves.
 */
template<typename T> inline const list<T> brbtreeHelper(const list<T>& elements, const size_t n) {
    if (n == 0)
        return cons<T>(list<T>(), elements);
    const size_t leftSize = (n - 1) / 2; {
        const list<T> leftResult = brbtreeHelper<T>(elements, leftSize); {
            const list<T> leftTree = car(leftResult);
            const list<T> nonLeftElements = cdr(leftResult);
            const size_t rightSize = n - (leftSize + 1); {
                const T thisEntry = car(nonLeftElements);
                const list<T> rightResult = brbtreeHelper<T>(cdr(nonLeftElements), rightSize); {
                    const list<T> rightTree = car(rightResult);
                    const list<T> remainingElements = cdr(rightResult); {
                        return cons<T>(mkrbtree<T>(thisEntry, leftTree, rightTree), remainingElements);
                    }
                }
            }
        }
    }
}

template<typename T> inline const list<T> mkbrbtree(const list<T>& elements) {
    return car(brbtreeHelper<T>(elements, length(elements)));
}

}

#endif /* tuscany_tree_hpp */