partition.h 2.8 KB

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