198b9484cSchristos /* List implementation of a partition of consecutive integers. 2*e663ba6eSchristos Copyright (C) 2000-2024 Free Software Foundation, Inc. 398b9484cSchristos Contributed by CodeSourcery, LLC. 498b9484cSchristos 598b9484cSchristos This file is part of GCC. 698b9484cSchristos 798b9484cSchristos GCC is free software; you can redistribute it and/or modify 898b9484cSchristos it 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, 1398b9484cSchristos but WITHOUT ANY WARRANTY; without even the implied warranty of 1498b9484cSchristos MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 1598b9484cSchristos GNU 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 /* This package implements a partition of consecutive integers. The 2398b9484cSchristos elements are partitioned into classes. Each class is represented 2498b9484cSchristos by one of its elements, the canonical element, which is chosen 2598b9484cSchristos arbitrarily from elements in the class. The principal operations 2698b9484cSchristos on a partition are FIND, which takes an element, determines its 2798b9484cSchristos class, and returns the canonical element for that class, and UNION, 2898b9484cSchristos which unites the two classes that contain two given elements into a 2998b9484cSchristos single class. 3098b9484cSchristos 3198b9484cSchristos The list implementation used here provides constant-time finds. By 3298b9484cSchristos storing the size of each class with the class's canonical element, 3398b9484cSchristos it is able to perform unions over all the classes in the partition 3498b9484cSchristos in O (N log N) time. */ 3598b9484cSchristos 3698b9484cSchristos #ifndef _PARTITION_H 3798b9484cSchristos #define _PARTITION_H 3898b9484cSchristos 3998b9484cSchristos #ifdef __cplusplus 4098b9484cSchristos extern "C" { 4198b9484cSchristos #endif /* __cplusplus */ 4298b9484cSchristos 4398b9484cSchristos #include "ansidecl.h" 4498b9484cSchristos #include <stdio.h> 4598b9484cSchristos 4698b9484cSchristos struct partition_elem 4798b9484cSchristos { 4898b9484cSchristos /* The next element in this class. Elements in each class form a 4998b9484cSchristos circular list. */ 5098b9484cSchristos struct partition_elem* next; 51212397c6Schristos /* The canonical element that represents the class containing this 52212397c6Schristos element. */ 53212397c6Schristos int class_element; 5498b9484cSchristos /* The number of elements in this class. Valid only if this is the 5598b9484cSchristos canonical element for its class. */ 5698b9484cSchristos unsigned class_count; 5798b9484cSchristos }; 5898b9484cSchristos 5998b9484cSchristos typedef struct partition_def 6098b9484cSchristos { 6198b9484cSchristos /* The number of elements in this partition. */ 6298b9484cSchristos int num_elements; 6398b9484cSchristos /* The elements in the partition. */ 6498b9484cSchristos struct partition_elem elements[1]; 6598b9484cSchristos } *partition; 6698b9484cSchristos 6798b9484cSchristos extern partition partition_new (int); 6898b9484cSchristos extern void partition_delete (partition); 6998b9484cSchristos extern int partition_union (partition, int, int); 7098b9484cSchristos extern void partition_print (partition, FILE*); 7198b9484cSchristos 7298b9484cSchristos /* Returns the canonical element corresponding to the class containing 7398b9484cSchristos ELEMENT__ in PARTITION__. */ 7498b9484cSchristos 7598b9484cSchristos #define partition_find(partition__, element__) \ 7698b9484cSchristos ((partition__)->elements[(element__)].class_element) 7798b9484cSchristos 7898b9484cSchristos #ifdef __cplusplus 7998b9484cSchristos } 8098b9484cSchristos #endif /* __cplusplus */ 8198b9484cSchristos 8298b9484cSchristos #endif /* _PARTITION_H */ 83