xref: /netbsd-src/external/gpl3/binutils.old/dist/include/fibheap.h (revision e992f068c547fd6e84b3f104dc2340adcc955732)
175fd0b74Schristos /* A Fibonacci heap datatype.
2*e992f068Schristos    Copyright (C) 1998-2022 Free Software Foundation, Inc.
375fd0b74Schristos    Contributed by Daniel Berlin (dan@cgsoftware.com).
475fd0b74Schristos 
575fd0b74Schristos This file is part of GCC.
675fd0b74Schristos 
775fd0b74Schristos GCC is free software; you can redistribute it and/or modify it
875fd0b74Schristos under the terms of the GNU General Public License as published by
975fd0b74Schristos the Free Software Foundation; either version 2, or (at your option)
1075fd0b74Schristos any later version.
1175fd0b74Schristos 
1275fd0b74Schristos GCC is distributed in the hope that it will be useful, but
1375fd0b74Schristos WITHOUT ANY WARRANTY; without even the implied warranty of
1475fd0b74Schristos MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
1575fd0b74Schristos General Public License for more details.
1675fd0b74Schristos 
1775fd0b74Schristos You should have received a copy of the GNU General Public License
1875fd0b74Schristos along with GCC; see the file COPYING.  If not, write to
1975fd0b74Schristos the Free Software Foundation, 51 Franklin Street - Fifth Floor,
2075fd0b74Schristos Boston, MA 02110-1301, USA.  */
2175fd0b74Schristos 
2275fd0b74Schristos /* Fibonacci heaps are somewhat complex, but, there's an article in
2375fd0b74Schristos    DDJ that explains them pretty well:
2475fd0b74Schristos 
2575fd0b74Schristos    http://www.ddj.com/articles/1997/9701/9701o/9701o.htm?topic=algoritms
2675fd0b74Schristos 
2775fd0b74Schristos    Introduction to algorithms by Corman and Rivest also goes over them.
2875fd0b74Schristos 
2975fd0b74Schristos    The original paper that introduced them is "Fibonacci heaps and their
3075fd0b74Schristos    uses in improved network optimization algorithms" by Tarjan and
3175fd0b74Schristos    Fredman (JACM 34(3), July 1987).
3275fd0b74Schristos 
3375fd0b74Schristos    Amortized and real worst case time for operations:
3475fd0b74Schristos 
3575fd0b74Schristos    ExtractMin: O(lg n) amortized. O(n) worst case.
3675fd0b74Schristos    DecreaseKey: O(1) amortized.  O(lg n) worst case.
3775fd0b74Schristos    Insert: O(2) amortized. O(1) actual.
3875fd0b74Schristos    Union: O(1) amortized. O(1) actual.  */
3975fd0b74Schristos 
4075fd0b74Schristos #ifndef _FIBHEAP_H_
4175fd0b74Schristos #define _FIBHEAP_H_
4275fd0b74Schristos 
4375fd0b74Schristos #include "ansidecl.h"
4475fd0b74Schristos 
4575fd0b74Schristos #ifdef __cplusplus
4675fd0b74Schristos extern "C" {
4775fd0b74Schristos #endif
4875fd0b74Schristos 
4975fd0b74Schristos typedef long fibheapkey_t;
5075fd0b74Schristos 
5175fd0b74Schristos typedef struct fibheap
5275fd0b74Schristos {
5375fd0b74Schristos   size_t nodes;
5475fd0b74Schristos   struct fibnode *min;
5575fd0b74Schristos   struct fibnode *root;
5675fd0b74Schristos } *fibheap_t;
5775fd0b74Schristos 
5875fd0b74Schristos typedef struct fibnode
5975fd0b74Schristos {
6075fd0b74Schristos   struct fibnode *parent;
6175fd0b74Schristos   struct fibnode *child;
6275fd0b74Schristos   struct fibnode *left;
6375fd0b74Schristos   struct fibnode *right;
6475fd0b74Schristos   fibheapkey_t key;
6575fd0b74Schristos   void *data;
6675fd0b74Schristos #if defined (__GNUC__) && (!defined (SIZEOF_INT) || SIZEOF_INT < 4)
6775fd0b74Schristos   __extension__ unsigned long int degree : 31;
6875fd0b74Schristos   __extension__ unsigned long int mark : 1;
6975fd0b74Schristos #else
7075fd0b74Schristos   unsigned int degree : 31;
7175fd0b74Schristos   unsigned int mark : 1;
7275fd0b74Schristos #endif
7375fd0b74Schristos } *fibnode_t;
7475fd0b74Schristos 
7575fd0b74Schristos extern fibheap_t fibheap_new (void);
7675fd0b74Schristos extern fibnode_t fibheap_insert (fibheap_t, fibheapkey_t, void *);
7775fd0b74Schristos extern int fibheap_empty (fibheap_t);
7875fd0b74Schristos extern fibheapkey_t fibheap_min_key (fibheap_t);
7975fd0b74Schristos extern fibheapkey_t fibheap_replace_key (fibheap_t, fibnode_t,
8075fd0b74Schristos                                          fibheapkey_t);
8175fd0b74Schristos extern void *fibheap_replace_key_data (fibheap_t, fibnode_t,
8275fd0b74Schristos                                        fibheapkey_t, void *);
8375fd0b74Schristos extern void *fibheap_extract_min (fibheap_t);
8475fd0b74Schristos extern void *fibheap_min (fibheap_t);
8575fd0b74Schristos extern void *fibheap_replace_data (fibheap_t, fibnode_t, void *);
8675fd0b74Schristos extern void *fibheap_delete_node (fibheap_t, fibnode_t);
8775fd0b74Schristos extern void fibheap_delete (fibheap_t);
8875fd0b74Schristos extern fibheap_t fibheap_union (fibheap_t, fibheap_t);
8975fd0b74Schristos 
9075fd0b74Schristos #ifdef __cplusplus
9175fd0b74Schristos }
9275fd0b74Schristos #endif
9375fd0b74Schristos 
9475fd0b74Schristos #endif /* _FIBHEAP_H_ */
95