nodelist.c 2.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  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. #include <circogen/nodelist.h>
  11. #include <circogen/circular.h>
  12. #include <assert.h>
  13. #include <limits.h>
  14. #include <stddef.h>
  15. #include <string.h>
  16. void appendNodelist(nodelist_t *list, size_t one, Agnode_t *n) {
  17. assert(one <= nodelist_size(list));
  18. // expand the list by one element
  19. nodelist_append(list, NULL);
  20. // shuffle everything past where we will insert
  21. nodelist_sync(list);
  22. size_t to_move = sizeof(node_t*) * (nodelist_size(list) - one - 1);
  23. if (to_move > 0) {
  24. memmove(nodelist_at(list, one + 1), nodelist_at(list, one), to_move);
  25. }
  26. // insert the new node
  27. nodelist_set(list, one, n);
  28. }
  29. void realignNodelist(nodelist_t *list, size_t np) {
  30. assert(np < nodelist_size(list));
  31. for (size_t i = np; i != 0; --i) {
  32. // rotate the list by 1
  33. node_t *const head = nodelist_pop_front(list);
  34. nodelist_push_back(list, head);
  35. }
  36. }
  37. void
  38. insertNodelist(nodelist_t * list, Agnode_t * cn, Agnode_t * neighbor,
  39. int pos)
  40. {
  41. nodelist_remove(list, cn);
  42. for (size_t i = 0; i < nodelist_size(list); ++i) {
  43. Agnode_t *here = nodelist_get(list, i);
  44. if (here == neighbor) {
  45. if (pos == 0) {
  46. appendNodelist(list, i, cn);
  47. } else {
  48. appendNodelist(list, i + 1, cn);
  49. }
  50. break;
  51. }
  52. }
  53. }
  54. /// attach l2 to l1.
  55. static void concatNodelist(nodelist_t * l1, nodelist_t * l2)
  56. {
  57. for (size_t i = 0; i < nodelist_size(l2); ++i) {
  58. nodelist_append(l1, nodelist_get(l2, i));
  59. }
  60. }
  61. void reverseAppend(nodelist_t * l1, nodelist_t * l2)
  62. {
  63. nodelist_reverse(l2);
  64. concatNodelist(l1, l2);
  65. nodelist_free(l2);
  66. }
  67. #ifdef DEBUG
  68. void printNodelist(nodelist_t * list)
  69. {
  70. for (size_t i = 0; i < nodelist_size(list); ++i) {
  71. fprintf(stderr, "%s ", agnameof(nodelist_get(list, i)));
  72. }
  73. fputs("\n", stderr);
  74. }
  75. #endif