glade-tsort.c 4.8 KB
Newer Older
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
/*
 * glade-tsort.c: a topological sorting algorithm implementation
 *
 * Copyright (C) 2013 Juan Pablo Ugarte.
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as
 * published by the Free Software Foundation; either version 2 of the
 * License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
 *
 * Authors:
 *   Juan Pablo Ugarte <juanpablougarte@gmail.com>
 */

#include "glade-tsort.h"

/**
 * _node_edge_prepend:
 * @node: a _NodeEdge pointer or NULL
 * @predecessor:
 * @successor:
 * 
 * Adds a new node with the values @predecessor and @successor to the start of
 * @node list.
 * 
 * Returns: the new start of the node list.
 */
37 38
GList *
_node_edge_prepend  (GList *list,
39 40 41 42 43 44 45 46
                     gpointer predecessor,
                     gpointer successor)
{
  _NodeEdge *edge = g_slice_new (_NodeEdge);

  edge->predecessor = predecessor;
  edge->successor = successor;
  
47 48 49 50 51 52 53
  return g_list_prepend (list, edge);
}

static void
_node_edge_free (gpointer data)
{
  g_slice_free (_NodeEdge, data);
54 55 56
}

void
57
_node_edge_list_free (GList *list)
58
{
59
  g_list_free_full (list, _node_edge_free);
60 61 62
}

static inline void
63
tsort_remove_non_start_nodes (GList **nodes, GList *edges)
64
{
65
  GList *l;
66

67 68 69 70 71
  for (l = edges; l; l = g_list_next (l))
    {
      _NodeEdge *edge = l->data;
      *nodes = g_list_remove (*nodes, edge->successor);
    }
72 73 74 75
}


static inline gboolean
76
tsort_node_has_no_incoming_edge (gpointer node, GList *edges)
77
{
78
  GList *l;
79

80
  for (l = edges; l; l = g_list_next (l))
81
    {
82 83
      _NodeEdge *edge = l->data;

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
      if (node == edge->successor)
        return FALSE;
    }

  return TRUE;
}

/**
 * _glade_tsort:
 * @nodes: list of pointers to sort
 * @edges: pointer to the list of #_NodeEdge that conform the dependency 
 *         graph of @nodes.
 * 
 * Topological sorting implementation.
 * After calling this funtion only graph cycles (circular dependencies) are left
 * in @edges list. So if @edges is NULL it means the returned list has all the 
 * elements topologically sorted if not it means there are at least one
 * circular dependency.
 *
 * L ← Empty list that will contain the sorted elements
 * S ← Set of all nodes with no incoming edges
 * while S is non-empty do
 *     remove a node n from S
 *     insert n into L
 *     for each node m with an edge e from n to m do
 *         remove edge e from the graph
 *         if m has no other incoming edges then
 *             insert m into S
 * return L (a topologically sorted order if graph has no edges)
 *
 * see: http://en.wikipedia.org/wiki/Topological_sorting
 * 
 * Returns: a new list sorted by dependency including nodes only present in @edges
 */
GList *
119
_glade_tsort (GList **nodes, GList **edges)
120 121 122 123 124 125 126 127 128 129 130 131
{
  GList *sorted_nodes;

  /* L ← Empty list that will contain the sorted elements */
  sorted_nodes = NULL;

  /* S ← Set of all nodes with no incoming edges */
  tsort_remove_non_start_nodes (nodes, *edges);

  /* while S is non-empty do */
  while (*nodes)
    {
132
      GList *l, *next;
133 134 135 136 137 138 139 140
      gpointer n;

      /* remove a node n from S */
      n = (*nodes)->data;
      *nodes = g_list_delete_link (*nodes, *nodes);

      /* insert n into L */
      /*if (!glade_widget_get_parent (n)) this would be a specific optimization */
141
      sorted_nodes = g_list_prepend (sorted_nodes, n);
142 143

      /* for each node m ... */
144
      for (l = *edges; l; l = next)
145
        {
146 147 148
          _NodeEdge *edge = l->data;

          next = g_list_next (l);
149 150 151 152 153

          /* ... with an edge e from n to m do */
          if (edge->predecessor == n)
            {
              /* remove edge e from the graph */
154
              *edges = g_list_delete_link (*edges, l);
155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170

              /* if m has no other incoming edges then */
              if (tsort_node_has_no_incoming_edge (edge->successor, *edges))
                /* insert m into S */
                *nodes = g_list_prepend (*nodes, edge->successor);
              
              g_slice_free (_NodeEdge, edge);
            }
        }
    }

  /* if graph has edges then return error (graph has at least one cycle) */
#if 0   /* We rather not return NULL, caller must check if edge */
  if (*edges)
    {      
      g_list_free (sorted_nodes);
171
      _node_edge_list_free (*edges);
172 173 174 175 176 177 178 179
      *edges = NULL;
      return NULL;
    }
#endif  

  /* return L (a topologically sorted order if edge is NULL) */
  return g_list_reverse (sorted_nodes);
}