xref: /dflybsd-src/contrib/gcc-4.7/include/partition.h (revision 04febcfb30580676d3e95f58a16c5137ee478b32)
1*e4b17023SJohn Marino /* List implementation of a partition of consecutive integers.
2*e4b17023SJohn Marino    Copyright (C) 2000, 2001, 2002 Free Software Foundation, Inc.
3*e4b17023SJohn Marino    Contributed by CodeSourcery, LLC.
4*e4b17023SJohn Marino 
5*e4b17023SJohn Marino    This file is part of GCC.
6*e4b17023SJohn Marino 
7*e4b17023SJohn Marino    GCC is free software; you can redistribute it and/or modify
8*e4b17023SJohn Marino    it under the terms of the GNU General Public License as published by
9*e4b17023SJohn Marino    the Free Software Foundation; either version 2, or (at your option)
10*e4b17023SJohn Marino    any later version.
11*e4b17023SJohn Marino 
12*e4b17023SJohn Marino    GCC is distributed in the hope that it will be useful,
13*e4b17023SJohn Marino    but WITHOUT ANY WARRANTY; without even the implied warranty of
14*e4b17023SJohn Marino    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15*e4b17023SJohn Marino    GNU General Public License for more details.
16*e4b17023SJohn Marino 
17*e4b17023SJohn Marino    You should have received a copy of the GNU General Public License
18*e4b17023SJohn Marino    along with GCC; see the file COPYING.  If not, write to
19*e4b17023SJohn Marino    the Free Software Foundation, 51 Franklin Street - Fifth Floor,
20*e4b17023SJohn Marino    Boston, MA 02110-1301, USA.  */
21*e4b17023SJohn Marino 
22*e4b17023SJohn Marino /* This package implements a partition of consecutive integers.  The
23*e4b17023SJohn Marino    elements are partitioned into classes.  Each class is represented
24*e4b17023SJohn Marino    by one of its elements, the canonical element, which is chosen
25*e4b17023SJohn Marino    arbitrarily from elements in the class.  The principal operations
26*e4b17023SJohn Marino    on a partition are FIND, which takes an element, determines its
27*e4b17023SJohn Marino    class, and returns the canonical element for that class, and UNION,
28*e4b17023SJohn Marino    which unites the two classes that contain two given elements into a
29*e4b17023SJohn Marino    single class.
30*e4b17023SJohn Marino 
31*e4b17023SJohn Marino    The list implementation used here provides constant-time finds.  By
32*e4b17023SJohn Marino    storing the size of each class with the class's canonical element,
33*e4b17023SJohn Marino    it is able to perform unions over all the classes in the partition
34*e4b17023SJohn Marino    in O (N log N) time.  */
35*e4b17023SJohn Marino 
36*e4b17023SJohn Marino #ifndef _PARTITION_H
37*e4b17023SJohn Marino #define _PARTITION_H
38*e4b17023SJohn Marino 
39*e4b17023SJohn Marino #ifdef __cplusplus
40*e4b17023SJohn Marino extern "C" {
41*e4b17023SJohn Marino #endif /* __cplusplus */
42*e4b17023SJohn Marino 
43*e4b17023SJohn Marino #include "ansidecl.h"
44*e4b17023SJohn Marino #include <stdio.h>
45*e4b17023SJohn Marino 
46*e4b17023SJohn Marino struct partition_elem
47*e4b17023SJohn Marino {
48*e4b17023SJohn Marino   /* The canonical element that represents the class containing this
49*e4b17023SJohn Marino      element.  */
50*e4b17023SJohn Marino   int class_element;
51*e4b17023SJohn Marino   /* The next element in this class.  Elements in each class form a
52*e4b17023SJohn Marino      circular list.  */
53*e4b17023SJohn Marino   struct partition_elem* next;
54*e4b17023SJohn Marino   /* The number of elements in this class.  Valid only if this is the
55*e4b17023SJohn Marino      canonical element for its class.  */
56*e4b17023SJohn Marino   unsigned class_count;
57*e4b17023SJohn Marino };
58*e4b17023SJohn Marino 
59*e4b17023SJohn Marino typedef struct partition_def
60*e4b17023SJohn Marino {
61*e4b17023SJohn Marino   /* The number of elements in this partition.  */
62*e4b17023SJohn Marino   int num_elements;
63*e4b17023SJohn Marino   /* The elements in the partition.  */
64*e4b17023SJohn Marino   struct partition_elem elements[1];
65*e4b17023SJohn Marino } *partition;
66*e4b17023SJohn Marino 
67*e4b17023SJohn Marino extern partition partition_new (int);
68*e4b17023SJohn Marino extern void partition_delete (partition);
69*e4b17023SJohn Marino extern int partition_union (partition, int, int);
70*e4b17023SJohn Marino extern void partition_print (partition,	FILE*);
71*e4b17023SJohn Marino 
72*e4b17023SJohn Marino /* Returns the canonical element corresponding to the class containing
73*e4b17023SJohn Marino    ELEMENT__ in PARTITION__.  */
74*e4b17023SJohn Marino 
75*e4b17023SJohn Marino #define partition_find(partition__, element__) \
76*e4b17023SJohn Marino     ((partition__)->elements[(element__)].class_element)
77*e4b17023SJohn Marino 
78*e4b17023SJohn Marino #ifdef __cplusplus
79*e4b17023SJohn Marino }
80*e4b17023SJohn Marino #endif /* __cplusplus */
81*e4b17023SJohn Marino 
82*e4b17023SJohn Marino #endif /* _PARTITION_H */
83