| 1 | /* $Id$ $Revision$ */ |
| 2 | /* vim:set shiftwidth=4 ts=8: */ |
| 3 | |
| 4 | /************************************************************************* |
| 5 | * Copyright (c) 2011 AT&T Intellectual Property |
| 6 | * All rights reserved. This program and the accompanying materials |
| 7 | * are made available under the terms of the Eclipse Public License v1.0 |
| 8 | * which accompanies this distribution, and is available at |
| 9 | * http://www.eclipse.org/legal/epl-v10.html |
| 10 | * |
| 11 | * Contributors: See CVS logs. Details at http://www.graphviz.org/ |
| 12 | *************************************************************************/ |
| 13 | |
| 14 | |
| 15 | #include <nodeset.h> |
| 16 | |
| 17 | |
| 18 | static nsitem_t *mkItem(Dt_t * d, nsitem_t * obj, Dtdisc_t * disc) |
| 19 | { |
| 20 | nsitem_t *ap = GNEW(nsitem_t); |
| 21 | |
| 22 | ap->np = obj->np; |
| 23 | return ap; |
| 24 | } |
| 25 | |
| 26 | static void freeItem(Dt_t * d, nsitem_t * obj, Dtdisc_t * disc) |
| 27 | { |
| 28 | free(obj); |
| 29 | } |
| 30 | |
| 31 | static int |
| 32 | cmpItem(Dt_t * d, Agnode_t ** key1, Agnode_t ** key2, Dtdisc_t * disc) |
| 33 | { |
| 34 | if (*key1 > *key2) |
| 35 | return 1; |
| 36 | else if (*key1 < *key2) |
| 37 | return -1; |
| 38 | else |
| 39 | return 0; |
| 40 | } |
| 41 | |
| 42 | static Dtdisc_t nodeDisc = { |
| 43 | offsetof(nsitem_t, np), /* key */ |
| 44 | sizeof(Agnode_t *), /* size */ |
| 45 | offsetof(nsitem_t, link), /* link */ |
| 46 | (Dtmake_f) mkItem, |
| 47 | (Dtfree_f) freeItem, |
| 48 | (Dtcompar_f) cmpItem, |
| 49 | (Dthash_f) 0, |
| 50 | (Dtmemory_f) 0, |
| 51 | (Dtevent_f) 0 |
| 52 | }; |
| 53 | |
| 54 | /* mkNodeset: |
| 55 | * Creates an empty node set. |
| 56 | */ |
| 57 | nodeset_t *mkNodeset() |
| 58 | { |
| 59 | nodeset_t *s = dtopen(&nodeDisc, Dtoset); |
| 60 | return s; |
| 61 | } |
| 62 | |
| 63 | /* freeNodeset: |
| 64 | * Deletes a node set, deleting all items as well. |
| 65 | * It does not delete the nodes. |
| 66 | */ |
| 67 | void freeNodeset(nodeset_t * s) |
| 68 | { |
| 69 | if (s != NULL) |
| 70 | dtclose(s); |
| 71 | } |
| 72 | |
| 73 | /* clearNodeset: |
| 74 | * Remove all items from a node set. |
| 75 | */ |
| 76 | void clearNodeset(nodeset_t * s) |
| 77 | { |
| 78 | dtclear(s); |
| 79 | } |
| 80 | |
| 81 | /* insertNodeset: |
| 82 | * Add a node into the nodeset. |
| 83 | */ |
| 84 | void insertNodeset(nodeset_t * ns, Agnode_t * n) |
| 85 | { |
| 86 | nsitem_t key; |
| 87 | |
| 88 | key.np = n; |
| 89 | dtinsert(ns, &key); |
| 90 | } |
| 91 | |
| 92 | void removeNodeset(nodeset_t * ns, Agnode_t * n) |
| 93 | { |
| 94 | nsitem_t key; |
| 95 | |
| 96 | key.np = n; |
| 97 | dtdelete(ns, &key); |
| 98 | } |
| 99 | |
| 100 | /* sizeNodeset: |
| 101 | * Report on the nodeset size. |
| 102 | */ |
| 103 | int sizeNodeset(nodeset_t * ns) |
| 104 | { |
| 105 | return dtsize(ns); |
| 106 | } |
| 107 | |
| 108 | /* As the node set is a Dt_t, traversal is done using standard |
| 109 | * functions from libcdt. |
| 110 | */ |
| 111 | |
| 112 | void printNodeset(nodeset_t * ns) |
| 113 | { |
| 114 | nsitem_t *ip; |
| 115 | for (ip = (nsitem_t *) dtfirst(ns); ip; |
| 116 | ip = (nsitem_t *) dtnext(ns, ip)) { |
| 117 | fprintf(stderr, "%s" , agnameof(ip->np)); |
| 118 | } |
| 119 | fputs("\n" , stderr); |
| 120 | } |
| 121 | |