Debian Codegraph @

lib/circogen/nodelist.c

raw
1 /*************************************************************************
2 * Copyright (c) 2011 AT&T Intellectual Property
3 * All rights reserved. This program and the accompanying materials
4 * are made available under the terms of the Eclipse Public License v1.0
5 * which accompanies this distribution, and is available at
6 * https://www.eclipse.org/legal/epl-v10.html
7 *
8 * Contributors: Details at https://graphviz.org
9 *************************************************************************/
10
11 #include "config.h"
12
13 #include <circogen/nodelist.h>
14 #include <circogen/circular.h>
15 #include <assert.h>
16 #include <limits.h>
17 #include <stddef.h>
18 #include <string.h>
19 #include <util/list.h>
20
21 void appendNodelist(nodelist_t *list, size_t one, Agnode_t *n) {
22 assert(one <= LIST_SIZE(list));
23
24 // expand the list by one element
25 LIST_APPEND(list, NULL);
26
27 // shuffle everything past where we will insert
28 LIST_SYNC(list);
29 size_t to_move = sizeof(node_t*) * (LIST_SIZE(list) - one - 1);
30 if (to_move > 0) {
31 memmove(LIST_AT(list, one + 1), LIST_AT(list, one), to_move);
32 }
33
34 // insert the new node
35 LIST_SET(list, one, n);
36 }
37
38 void realignNodelist(nodelist_t *list, size_t np) {
39 assert(np < LIST_SIZE(list));
40 for (size_t i = np; i != 0; --i) {
41 // rotate the list by 1
42 node_t *const head = LIST_POP_FRONT(list);
43 LIST_PUSH_BACK(list, head);
44 }
45 }
46
47 void
48 insertNodelist(nodelist_t * list, Agnode_t * cn, Agnode_t * neighbor,
49 int pos)
50 {
51 LIST_REMOVE(list, cn);
52
53 for (size_t i = 0; i < LIST_SIZE(list); ++i) {
54 Agnode_t *here = LIST_GET(list, i);
55 if (here == neighbor) {
56 if (pos == 0) {
57 appendNodelist(list, i, cn);
58 } else {
59 appendNodelist(list, i + 1, cn);
60 }
61 break;
62 }
63 }
64 }
65
66 /// attach l2 to l1.
67 static void concatNodelist(nodelist_t * l1, nodelist_t * l2)
68 {
69 for (size_t i = 0; i < LIST_SIZE(l2); ++i) {
70 LIST_APPEND(l1, LIST_GET(l2, i));
71 }
72 }
73
74 void reverseAppend(nodelist_t * l1, nodelist_t * l2)
75 {
76 LIST_REVERSE(l2);
77 concatNodelist(l1, l2);
78 LIST_FREE(l2);
79 }
80
81 #ifdef DEBUG
82 void printNodelist(nodelist_t * list)
83 {
84 for (size_t i = 0; i < LIST_SIZE(list); ++i) {
85 fprintf(stderr, "%s ", agnameof(LIST_GET(list, i)));
86 }
87 fputs("\n", stderr);
88 }
89 #endif