xref: /dflybsd-src/contrib/gcc-4.7/include/fibheap.h (revision 04febcfb30580676d3e95f58a16c5137ee478b32)
1*e4b17023SJohn Marino /* A Fibonacci heap datatype.
2*e4b17023SJohn Marino    Copyright 1998, 1999, 2000, 2001, 2002, 2003, 2004, 2005, 2009
3*e4b17023SJohn Marino    Free Software Foundation, Inc.
4*e4b17023SJohn Marino    Contributed by Daniel Berlin (dan@cgsoftware.com).
5*e4b17023SJohn Marino 
6*e4b17023SJohn Marino This file is part of GCC.
7*e4b17023SJohn Marino 
8*e4b17023SJohn Marino GCC is free software; you can redistribute it and/or modify it
9*e4b17023SJohn Marino under the terms of the GNU General Public License as published by
10*e4b17023SJohn Marino the Free Software Foundation; either version 2, or (at your option)
11*e4b17023SJohn Marino any later version.
12*e4b17023SJohn Marino 
13*e4b17023SJohn Marino GCC is distributed in the hope that it will be useful, but
14*e4b17023SJohn Marino WITHOUT ANY WARRANTY; without even the implied warranty of
15*e4b17023SJohn Marino MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
16*e4b17023SJohn Marino General Public License for more details.
17*e4b17023SJohn Marino 
18*e4b17023SJohn Marino You should have received a copy of the GNU General Public License
19*e4b17023SJohn Marino along with GCC; see the file COPYING.  If not, write to
20*e4b17023SJohn Marino the Free Software Foundation, 51 Franklin Street - Fifth Floor,
21*e4b17023SJohn Marino Boston, MA 02110-1301, USA.  */
22*e4b17023SJohn Marino 
23*e4b17023SJohn Marino /* Fibonacci heaps are somewhat complex, but, there's an article in
24*e4b17023SJohn Marino    DDJ that explains them pretty well:
25*e4b17023SJohn Marino 
26*e4b17023SJohn Marino    http://www.ddj.com/articles/1997/9701/9701o/9701o.htm?topic=algoritms
27*e4b17023SJohn Marino 
28*e4b17023SJohn Marino    Introduction to algorithms by Corman and Rivest also goes over them.
29*e4b17023SJohn Marino 
30*e4b17023SJohn Marino    The original paper that introduced them is "Fibonacci heaps and their
31*e4b17023SJohn Marino    uses in improved network optimization algorithms" by Tarjan and
32*e4b17023SJohn Marino    Fredman (JACM 34(3), July 1987).
33*e4b17023SJohn Marino 
34*e4b17023SJohn Marino    Amortized and real worst case time for operations:
35*e4b17023SJohn Marino 
36*e4b17023SJohn Marino    ExtractMin: O(lg n) amortized. O(n) worst case.
37*e4b17023SJohn Marino    DecreaseKey: O(1) amortized.  O(lg n) worst case.
38*e4b17023SJohn Marino    Insert: O(2) amortized. O(1) actual.
39*e4b17023SJohn Marino    Union: O(1) amortized. O(1) actual.  */
40*e4b17023SJohn Marino 
41*e4b17023SJohn Marino #ifndef _FIBHEAP_H_
42*e4b17023SJohn Marino #define _FIBHEAP_H_
43*e4b17023SJohn Marino 
44*e4b17023SJohn Marino #include "ansidecl.h"
45*e4b17023SJohn Marino 
46*e4b17023SJohn Marino #ifdef __cplusplus
47*e4b17023SJohn Marino extern "C" {
48*e4b17023SJohn Marino #endif
49*e4b17023SJohn Marino 
50*e4b17023SJohn Marino typedef long fibheapkey_t;
51*e4b17023SJohn Marino 
52*e4b17023SJohn Marino typedef struct fibheap
53*e4b17023SJohn Marino {
54*e4b17023SJohn Marino   size_t nodes;
55*e4b17023SJohn Marino   struct fibnode *min;
56*e4b17023SJohn Marino   struct fibnode *root;
57*e4b17023SJohn Marino } *fibheap_t;
58*e4b17023SJohn Marino 
59*e4b17023SJohn Marino typedef struct fibnode
60*e4b17023SJohn Marino {
61*e4b17023SJohn Marino   struct fibnode *parent;
62*e4b17023SJohn Marino   struct fibnode *child;
63*e4b17023SJohn Marino   struct fibnode *left;
64*e4b17023SJohn Marino   struct fibnode *right;
65*e4b17023SJohn Marino   fibheapkey_t key;
66*e4b17023SJohn Marino   void *data;
67*e4b17023SJohn Marino #if defined (__GNUC__) && (!defined (SIZEOF_INT) || SIZEOF_INT < 4)
68*e4b17023SJohn Marino   __extension__ unsigned long int degree : 31;
69*e4b17023SJohn Marino   __extension__ unsigned long int mark : 1;
70*e4b17023SJohn Marino #else
71*e4b17023SJohn Marino   unsigned int degree : 31;
72*e4b17023SJohn Marino   unsigned int mark : 1;
73*e4b17023SJohn Marino #endif
74*e4b17023SJohn Marino } *fibnode_t;
75*e4b17023SJohn Marino 
76*e4b17023SJohn Marino extern fibheap_t fibheap_new (void);
77*e4b17023SJohn Marino extern fibnode_t fibheap_insert (fibheap_t, fibheapkey_t, void *);
78*e4b17023SJohn Marino extern int fibheap_empty (fibheap_t);
79*e4b17023SJohn Marino extern fibheapkey_t fibheap_min_key (fibheap_t);
80*e4b17023SJohn Marino extern fibheapkey_t fibheap_replace_key (fibheap_t, fibnode_t,
81*e4b17023SJohn Marino                                          fibheapkey_t);
82*e4b17023SJohn Marino extern void *fibheap_replace_key_data (fibheap_t, fibnode_t,
83*e4b17023SJohn Marino                                        fibheapkey_t, void *);
84*e4b17023SJohn Marino extern void *fibheap_extract_min (fibheap_t);
85*e4b17023SJohn Marino extern void *fibheap_min (fibheap_t);
86*e4b17023SJohn Marino extern void *fibheap_replace_data (fibheap_t, fibnode_t, void *);
87*e4b17023SJohn Marino extern void *fibheap_delete_node (fibheap_t, fibnode_t);
88*e4b17023SJohn Marino extern void fibheap_delete (fibheap_t);
89*e4b17023SJohn Marino extern fibheap_t fibheap_union (fibheap_t, fibheap_t);
90*e4b17023SJohn Marino 
91*e4b17023SJohn Marino #ifdef __cplusplus
92*e4b17023SJohn Marino }
93*e4b17023SJohn Marino #endif
94*e4b17023SJohn Marino 
95*e4b17023SJohn Marino #endif /* _FIBHEAP_H_ */
96