@matheusbsbs / matheus-bagliano / commits / 6b6dcfc297
Fork 0

Make it possible to not clobber object.util in sort_in_topological_order (take 2)

Signed-off-by: Fredrik Kuivinen <freku045@student.liu.se> Signed-off-by: Junio C Hamano <junkio@cox.net>

Fredrik Kuivinen committed Mar 10, 2006 at 10:21 UTC 6b6dcfc297722f5a5324c4cb22f45adccd4c84ef
2 files changed +44 -12
commit.c
+27 -9
@@ -569,10 +569,28 @@ int count_parents(struct commit * commit)
569 return count;
570 }
571
572 +void topo_sort_default_setter(struct commit *c, void *data)
573 +{
574 + c->object.util = data;
575 +}
576 +
577 +void *topo_sort_default_getter(struct commit *c)
578 +{
579 + return c->object.util;
580 +}
581 +
582 /*
583 * Performs an in-place topological sort on the list supplied.
584 */
585 void sort_in_topological_order(struct commit_list ** list, int lifo)
586 +{
587 + sort_in_topological_order_fn(list, lifo, topo_sort_default_setter,
588 + topo_sort_default_getter);
589 +}
590 +
591 +void sort_in_topological_order_fn(struct commit_list ** list, int lifo,
592 + topo_sort_set_fn_t setter,
593 + topo_sort_get_fn_t getter)
594 {
595 struct commit_list * next = *list;
596 struct commit_list * work = NULL, **insert;
@@ -596,7 +614,7 @@ void sort_in_topological_order(struct commit_list ** list, int lifo)
614 next=*list;
615 while (next) {
616 next_nodes->list_item = next;
599 - next->item->object.util = next_nodes;
617 + setter(next->item, next_nodes);
618 next_nodes++;
619 next = next->next;
620 }
@@ -606,8 +624,8 @@ void sort_in_topological_order(struct commit_list ** list, int lifo)
624 struct commit_list * parents = next->item->parents;
625 while (parents) {
626 struct commit * parent=parents->item;
609 - struct sort_node * pn = (struct sort_node *)parent->object.util;
610 -
627 + struct sort_node * pn = (struct sort_node *) getter(parent);
628 +
629 if (pn)
630 pn->indegree++;
631 parents=parents->next;
@@ -624,7 +642,7 @@ void sort_in_topological_order(struct commit_list ** list, int lifo)
642 next=*list;
643 insert = &work;
644 while (next) {
627 - struct sort_node * node = (struct sort_node *)next->item->object.util;
645 + struct sort_node * node = (struct sort_node *) getter(next->item);
646
647 if (node->indegree == 0) {
648 insert = &commit_list_insert(next->item, insert)->next;
@@ -637,15 +655,15 @@ void sort_in_topological_order(struct commit_list ** list, int lifo)
655 sort_by_date(&work);
656 while (work) {
657 struct commit * work_item = pop_commit(&work);
640 - struct sort_node * work_node = (struct sort_node *)work_item->object.util;
658 + struct sort_node * work_node = (struct sort_node *) getter(work_item);
659 struct commit_list * parents = work_item->parents;
660
661 while (parents) {
662 struct commit * parent=parents->item;
645 - struct sort_node * pn = (struct sort_node *)parent->object.util;
646 -
663 + struct sort_node * pn = (struct sort_node *) getter(parent);
664 +
665 if (pn) {
648 - /*
666 + /*
667 * parents are only enqueued for emission
668 * when all their children have been emitted thereby
669 * guaranteeing topological order.
@@ -667,7 +685,7 @@ void sort_in_topological_order(struct commit_list ** list, int lifo)
685 *pptr = work_node->list_item;
686 pptr = &(*pptr)->next;
687 *pptr = NULL;
670 - work_item->object.util = NULL;
688 + setter(work_item, NULL);
689 }
690 free(nodes);
691 }
commit.h
+17 -3
@@ -65,15 +65,29 @@ int count_parents(struct commit * commit);
65 /*
66 * Performs an in-place topological sort of list supplied.
67 *
68 - * Pre-conditions:
68 + * Pre-conditions for sort_in_topological_order:
69 * all commits in input list and all parents of those
70 * commits must have object.util == NULL
71 - *
72 - * Post-conditions:
71 + *
72 + * Pre-conditions for sort_in_topological_order_fn:
73 + * all commits in input list and all parents of those
74 + * commits must have getter(commit) == NULL
75 + *
76 + * Post-conditions:
77 * invariant of resulting list is:
78 * a reachable from b => ord(b) < ord(a)
79 * in addition, when lifo == 0, commits on parallel tracks are
80 * sorted in the dates order.
81 */
82 +
83 +typedef void (*topo_sort_set_fn_t)(struct commit*, void *data);
84 +typedef void* (*topo_sort_get_fn_t)(struct commit*);
85 +
86 +void topo_sort_default_setter(struct commit *c, void *data);
87 +void *topo_sort_default_getter(struct commit *c);
88 +
89 void sort_in_topological_order(struct commit_list ** list, int lifo);
90 +void sort_in_topological_order_fn(struct commit_list ** list, int lifo,
91 + topo_sort_set_fn_t setter,
92 + topo_sort_get_fn_t getter);
93 #endif /* COMMIT_H */