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