diff options
author | Christian Grothoff <christian@grothoff.org> | 2009-12-23 15:09:11 +0000 |
---|---|---|
committer | Christian Grothoff <christian@grothoff.org> | 2009-12-23 15:09:11 +0000 |
commit | 0acc583b6e411a5f1ebd6172458baaad992c456e (patch) | |
tree | 06f2acf19d889249e8c7256c8cbe07d0d3488349 /src/include | |
parent | 8149805b0539d29420b9e2b5a60fc09b2d69b348 (diff) | |
download | gnunet-0acc583b6e411a5f1ebd6172458baaad992c456e.tar.gz gnunet-0acc583b6e411a5f1ebd6172458baaad992c456e.zip |
updating heap API and implementation
Diffstat (limited to 'src/include')
-rw-r--r-- | src/include/gnunet_container_lib.h | 161 |
1 files changed, 87 insertions, 74 deletions
diff --git a/src/include/gnunet_container_lib.h b/src/include/gnunet_container_lib.h index 1f707e44c..db9e10ef8 100644 --- a/src/include/gnunet_container_lib.h +++ b/src/include/gnunet_container_lib.h | |||
@@ -710,7 +710,7 @@ int GNUNET_CONTAINER_multihashmap_get_multiple (const struct | |||
710 | /** | 710 | /** |
711 | * Cost by which elements in a heap can be ordered. | 711 | * Cost by which elements in a heap can be ordered. |
712 | */ | 712 | */ |
713 | typedef uint64_t GNUNET_CONTAINER_HeapCost; | 713 | typedef uint64_t GNUNET_CONTAINER_HeapCostType; |
714 | 714 | ||
715 | 715 | ||
716 | /* | 716 | /* |
@@ -736,132 +736,145 @@ enum GNUNET_CONTAINER_HeapOrder | |||
736 | */ | 736 | */ |
737 | struct GNUNET_CONTAINER_Heap; | 737 | struct GNUNET_CONTAINER_Heap; |
738 | 738 | ||
739 | |||
740 | |||
741 | /** | ||
742 | * Handle to a node in a heap. | ||
743 | */ | ||
744 | struct GNUNET_CONTAINER_HeapNode; | ||
745 | |||
746 | |||
739 | /** | 747 | /** |
740 | * Create a new heap. | 748 | * Create a new heap. |
741 | * | 749 | * |
742 | * @param type should the minimum or the maximum element be the root | 750 | * @param order how should the heap be sorted? |
743 | * @return NULL on error, otherwise a fresh heap | 751 | * @return handle to the heap |
744 | */ | 752 | */ |
745 | struct GNUNET_CONTAINER_Heap *GNUNET_CONTAINER_heap_create (enum | 753 | struct GNUNET_CONTAINER_Heap * |
746 | GNUNET_CONTAINER_HeapOrder | 754 | GNUNET_CONTAINER_heap_create (enum GNUNET_CONTAINER_HeapOrder order); |
747 | type); | ||
748 | 755 | ||
749 | 756 | ||
750 | /** | 757 | /** |
751 | * Free a heap | 758 | * Destroys the heap. Only call on a heap that |
759 | * is already empty. | ||
752 | * | 760 | * |
753 | * @param h heap to free. | 761 | * @param heap heap to destroy |
754 | */ | 762 | */ |
755 | void GNUNET_CONTAINER_heap_destroy (struct GNUNET_CONTAINER_Heap *h); | 763 | void GNUNET_CONTAINER_heap_destroy (struct GNUNET_CONTAINER_Heap *heap); |
756 | 764 | ||
757 | 765 | ||
758 | /** | 766 | /** |
759 | * Function called on elements of a heap. | 767 | * Get element stored at root of heap. |
760 | * | 768 | * |
761 | * @param cls closure | 769 | * @param heap heap to inspect |
762 | * @param element obj stored in heap | 770 | * @return NULL if heap is empty |
763 | * @param cost cost of the element | ||
764 | * @return GNUNET_YES if we should continue to iterate, | ||
765 | * GNUNET_NO if not. | ||
766 | */ | 771 | */ |
767 | typedef int (*GNUNET_CONTAINER_HeapIterator) (void *cls, | 772 | void * |
768 | void *element, | 773 | GNUNET_CONTAINER_heap_peek (const struct GNUNET_CONTAINER_Heap *heap); |
769 | GNUNET_CONTAINER_HeapCost cost); | ||
770 | 774 | ||
771 | 775 | ||
772 | /** | 776 | /** |
773 | * Iterate over all entries in the map. | 777 | * Get the current size of the heap |
774 | * | 778 | * |
775 | * @param heap the heap | 779 | * @param heap the heap to get the size of |
776 | * @param iterator function to call on each entry | 780 | * @return number of elements stored |
777 | * @param iterator_cls closure for iterator | ||
778 | * @return number of items handled | ||
779 | * GNUNET_SYSERR if iteration was aborted by iterator | ||
780 | */ | 781 | */ |
781 | int GNUNET_CONTAINER_heap_iterate (struct GNUNET_CONTAINER_Heap *heap, | 782 | unsigned int |
782 | GNUNET_CONTAINER_HeapIterator iterator, | 783 | GNUNET_CONTAINER_heap_get_size (const struct GNUNET_CONTAINER_Heap *heap); |
783 | void *iterator_cls); | ||
784 | |||
785 | 784 | ||
786 | 785 | ||
787 | /** | 786 | /** |
788 | * Inserts a new item into the heap, item is always neighbor now. | 787 | * Iterator for heap |
789 | * @param heap the heap | 788 | * |
790 | * @param element element to insert | 789 | * @param cls closure |
791 | * @param cost cost of the element | 790 | * @param node internal node of the heap |
792 | * @return FIXME | 791 | * @param element value stored at the node |
792 | * @param cost cost associated with the node | ||
793 | * @return GNUNET_YES if we should continue to iterate, | ||
794 | * GNUNET_NO if not. | ||
793 | */ | 795 | */ |
794 | int | 796 | typedef int (*GNUNET_CONTAINER_HeapIterator) (void *cls, |
795 | GNUNET_CONTAINER_heap_insert (struct GNUNET_CONTAINER_Heap *heap, | 797 | struct GNUNET_CONTAINER_HeapNode *node, |
796 | void *element, GNUNET_CONTAINER_HeapCost cost); | 798 | void *element, |
799 | GNUNET_CONTAINER_HeapCostType cost); | ||
797 | 800 | ||
798 | 801 | ||
799 | /** | 802 | /** |
800 | * Removes root of the tree, is remove max if a max heap and remove min | 803 | * Iterate over all entries in the heap. |
801 | * if a min heap, returns the data stored at the node. | ||
802 | * | 804 | * |
803 | * @param heap the heap | 805 | * @param heap the heap |
804 | * @return NULL if the heap is empty | 806 | * @param iterator function to call on each entry |
807 | * @param iterator_cls closure for iterator | ||
805 | */ | 808 | */ |
806 | void *GNUNET_CONTAINER_heap_remove_root (struct GNUNET_CONTAINER_Heap *heap); | 809 | void |
810 | GNUNET_CONTAINER_heap_iterate (const struct GNUNET_CONTAINER_Heap *heap, | ||
811 | GNUNET_CONTAINER_HeapIterator iterator, | ||
812 | void *iterator_cls); | ||
807 | 813 | ||
808 | 814 | ||
809 | /** | 815 | /** |
810 | * Returns element stored at root of tree, doesn't effect anything | 816 | * Perform a random walk of the tree. The walk is biased |
817 | * towards elements closer to the root of the tree (since | ||
818 | * each walk starts at the root and ends at a random leaf). | ||
819 | * The heap internally tracks the current position of the | ||
820 | * walk. | ||
811 | * | 821 | * |
812 | * @param heap the heap | 822 | * @param heap heap to walk |
813 | * @return NULL if the heap is empty | 823 | * @return data stored at the next random node in the walk; |
824 | * NULL if the tree is empty. | ||
814 | */ | 825 | */ |
815 | void *GNUNET_CONTAINER_heap_peek (struct GNUNET_CONTAINER_Heap *heap); | 826 | void * |
827 | GNUNET_CONTAINER_heap_walk_get_next (struct GNUNET_CONTAINER_Heap *heap); | ||
816 | 828 | ||
817 | 829 | ||
818 | /** | 830 | /** |
819 | * Removes any node from the tree based on the neighbor given, does | 831 | * Inserts a new element into the heap. |
820 | * not traverse the tree (backpointers) but may take more time due to | ||
821 | * percolation of nodes. | ||
822 | * | 832 | * |
823 | * @param heap the heap | 833 | * @param heap heap to modify |
824 | * @param element the element to remove | 834 | * @param element element to insert |
825 | * @return NULL if "element" was not found in the heap, otherwise element | 835 | * @param cost cost for the element |
836 | * @return node for the new element | ||
826 | */ | 837 | */ |
827 | void *GNUNET_CONTAINER_heap_remove_node (struct GNUNET_CONTAINER_Heap *heap, | 838 | struct GNUNET_CONTAINER_HeapNode * |
828 | void *element); | 839 | GNUNET_CONTAINER_heap_insert (struct GNUNET_CONTAINER_Heap *heap, |
840 | void *element, | ||
841 | GNUNET_CONTAINER_HeapCostType cost); | ||
829 | 842 | ||
830 | 843 | ||
831 | /** | 844 | /** |
832 | * Updates the cost of any node in the tree | 845 | * Remove root of the heap. |
833 | * | 846 | * |
834 | * @param heap the heap | 847 | * @param heap heap to modify |
835 | * @param element the element for which the cost is updated | 848 | * @return element data stored at the root node |
836 | * @param new_cost new cost for the element | ||
837 | * @return FIXME | ||
838 | */ | 849 | */ |
839 | int | 850 | void * |
840 | GNUNET_CONTAINER_heap_update_cost (struct GNUNET_CONTAINER_Heap *heap, | 851 | GNUNET_CONTAINER_heap_remove_root (struct GNUNET_CONTAINER_Heap *heap); |
841 | void *element, | ||
842 | GNUNET_CONTAINER_HeapCost new_cost); | ||
843 | 852 | ||
844 | 853 | ||
845 | /** | 854 | /** |
846 | * Random walk of the tree, returns the data stored at the next random node | 855 | * Removes a node from the heap. |
847 | * in the walk. Calls callee with the data, or NULL if the tree is empty | 856 | * |
848 | * or some other problem crops up. | 857 | * @param heap heap to modify |
849 | * | 858 | * @param node node to remove |
850 | * @param heap the heap | 859 | * @return element data stored at the node, NULL if heap is empty |
851 | * @return the next element from the random walk | ||
852 | */ | 860 | */ |
853 | void *GNUNET_CONTAINER_heap_walk_get_next (struct GNUNET_CONTAINER_Heap | 861 | void * |
854 | *heap); | 862 | GNUNET_CONTAINER_heap_remove_node (struct GNUNET_CONTAINER_Heap *heap, |
863 | struct GNUNET_CONTAINER_HeapNode *node); | ||
855 | 864 | ||
856 | 865 | ||
857 | /** | 866 | /** |
858 | * Returns the current size of the heap | 867 | * Updates the cost of any node in the tree |
859 | * | 868 | * |
860 | * @param heap the heap to get the size of | 869 | * @param heap heap to modify |
861 | * @return number of elements in the heap | 870 | * @param node node for which the cost is to be changed |
871 | * @param new_cost new cost for the node | ||
862 | */ | 872 | */ |
863 | unsigned int | 873 | void |
864 | GNUNET_CONTAINER_heap_get_size (struct GNUNET_CONTAINER_Heap *heap); | 874 | GNUNET_CONTAINER_heap_update_cost (struct GNUNET_CONTAINER_Heap *heap, |
875 | struct GNUNET_CONTAINER_HeapNode *node, | ||
876 | GNUNET_CONTAINER_HeapCostType new_cost); | ||
877 | |||
865 | 878 | ||
866 | /* ******************** Singly linked list *************** */ | 879 | /* ******************** Singly linked list *************** */ |
867 | 880 | ||