xref: /netbsd-src/external/gpl3/gdb/dist/include/fibheap.h (revision e663ba6e3a60083e70de702e9d54bf486a57b6a7)
198b9484cSchristos /* A Fibonacci heap datatype.
2*e663ba6eSchristos    Copyright (C) 1998-2024 Free Software Foundation, Inc.
398b9484cSchristos    Contributed by Daniel Berlin (dan@cgsoftware.com).
498b9484cSchristos 
598b9484cSchristos This file is part of GCC.
698b9484cSchristos 
798b9484cSchristos GCC is free software; you can redistribute it and/or modify it
898b9484cSchristos under the terms of the GNU General Public License as published by
998b9484cSchristos the Free Software Foundation; either version 2, or (at your option)
1098b9484cSchristos any later version.
1198b9484cSchristos 
1298b9484cSchristos GCC is distributed in the hope that it will be useful, but
1398b9484cSchristos WITHOUT ANY WARRANTY; without even the implied warranty of
1498b9484cSchristos MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
1598b9484cSchristos General Public License for more details.
1698b9484cSchristos 
1798b9484cSchristos You should have received a copy of the GNU General Public License
1898b9484cSchristos along with GCC; see the file COPYING.  If not, write to
1998b9484cSchristos the Free Software Foundation, 51 Franklin Street - Fifth Floor,
2098b9484cSchristos Boston, MA 02110-1301, USA.  */
2198b9484cSchristos 
2298b9484cSchristos /* Fibonacci heaps are somewhat complex, but, there's an article in
2398b9484cSchristos    DDJ that explains them pretty well:
2498b9484cSchristos 
2598b9484cSchristos    http://www.ddj.com/articles/1997/9701/9701o/9701o.htm?topic=algoritms
2698b9484cSchristos 
2798b9484cSchristos    Introduction to algorithms by Corman and Rivest also goes over them.
2898b9484cSchristos 
2998b9484cSchristos    The original paper that introduced them is "Fibonacci heaps and their
3098b9484cSchristos    uses in improved network optimization algorithms" by Tarjan and
3198b9484cSchristos    Fredman (JACM 34(3), July 1987).
3298b9484cSchristos 
3398b9484cSchristos    Amortized and real worst case time for operations:
3498b9484cSchristos 
3598b9484cSchristos    ExtractMin: O(lg n) amortized. O(n) worst case.
3698b9484cSchristos    DecreaseKey: O(1) amortized.  O(lg n) worst case.
3798b9484cSchristos    Insert: O(2) amortized. O(1) actual.
3898b9484cSchristos    Union: O(1) amortized. O(1) actual.  */
3998b9484cSchristos 
4098b9484cSchristos #ifndef _FIBHEAP_H_
4198b9484cSchristos #define _FIBHEAP_H_
4298b9484cSchristos 
4398b9484cSchristos #include "ansidecl.h"
4498b9484cSchristos 
4598b9484cSchristos #ifdef __cplusplus
4698b9484cSchristos extern "C" {
4798b9484cSchristos #endif
4898b9484cSchristos 
4998b9484cSchristos typedef long fibheapkey_t;
5098b9484cSchristos 
5198b9484cSchristos typedef struct fibheap
5298b9484cSchristos {
5398b9484cSchristos   size_t nodes;
5498b9484cSchristos   struct fibnode *min;
5598b9484cSchristos   struct fibnode *root;
5698b9484cSchristos } *fibheap_t;
5798b9484cSchristos 
5898b9484cSchristos typedef struct fibnode
5998b9484cSchristos {
6098b9484cSchristos   struct fibnode *parent;
6198b9484cSchristos   struct fibnode *child;
6298b9484cSchristos   struct fibnode *left;
6398b9484cSchristos   struct fibnode *right;
6498b9484cSchristos   fibheapkey_t key;
6598b9484cSchristos   void *data;
6698b9484cSchristos #if defined (__GNUC__) && (!defined (SIZEOF_INT) || SIZEOF_INT < 4)
6798b9484cSchristos   __extension__ unsigned long int degree : 31;
6898b9484cSchristos   __extension__ unsigned long int mark : 1;
6998b9484cSchristos #else
7098b9484cSchristos   unsigned int degree : 31;
7198b9484cSchristos   unsigned int mark : 1;
7298b9484cSchristos #endif
7398b9484cSchristos } *fibnode_t;
7498b9484cSchristos 
7598b9484cSchristos extern fibheap_t fibheap_new (void);
7698b9484cSchristos extern fibnode_t fibheap_insert (fibheap_t, fibheapkey_t, void *);
7798b9484cSchristos extern int fibheap_empty (fibheap_t);
7898b9484cSchristos extern fibheapkey_t fibheap_min_key (fibheap_t);
7998b9484cSchristos extern fibheapkey_t fibheap_replace_key (fibheap_t, fibnode_t,
8098b9484cSchristos                                          fibheapkey_t);
8198b9484cSchristos extern void *fibheap_replace_key_data (fibheap_t, fibnode_t,
8298b9484cSchristos                                        fibheapkey_t, void *);
8398b9484cSchristos extern void *fibheap_extract_min (fibheap_t);
8498b9484cSchristos extern void *fibheap_min (fibheap_t);
8598b9484cSchristos extern void *fibheap_replace_data (fibheap_t, fibnode_t, void *);
8698b9484cSchristos extern void *fibheap_delete_node (fibheap_t, fibnode_t);
8798b9484cSchristos extern void fibheap_delete (fibheap_t);
8898b9484cSchristos extern fibheap_t fibheap_union (fibheap_t, fibheap_t);
8998b9484cSchristos 
9098b9484cSchristos #ifdef __cplusplus
9198b9484cSchristos }
9298b9484cSchristos #endif
9398b9484cSchristos 
9498b9484cSchristos #endif /* _FIBHEAP_H_ */
95