2001-05-16 22:44:03 +02:00
|
|
|
/* List implementation of a partition of consecutive integers.
|
2018-01-03 11:03:58 +01:00
|
|
|
Copyright (C) 2000-2018 Free Software Foundation, Inc.
|
2000-03-10 09:16:55 +01:00
|
|
|
Contributed by CodeSourcery, LLC.
|
|
|
|
|
|
|
|
This file is part of GNU CC.
|
|
|
|
|
|
|
|
GNU CC is free software; you can redistribute it and/or modify
|
|
|
|
it under the terms of the GNU General Public License as published by
|
|
|
|
the Free Software Foundation; either version 2, or (at your option)
|
|
|
|
any later version.
|
|
|
|
|
|
|
|
GNU CC is distributed in the hope that it will be useful,
|
|
|
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
GNU General Public License for more details.
|
|
|
|
|
|
|
|
You should have received a copy of the GNU General Public License
|
|
|
|
along with GNU CC; see the file COPYING. If not, write to
|
2005-05-10 17:33:18 +02:00
|
|
|
the Free Software Foundation, 51 Franklin Street - Fifth Floor,
|
|
|
|
Boston, MA 02110-1301, USA. */
|
2000-03-10 09:16:55 +01:00
|
|
|
|
|
|
|
#ifdef HAVE_CONFIG_H
|
|
|
|
#include "config.h"
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#ifdef HAVE_STDLIB_H
|
|
|
|
#include <stdlib.h>
|
|
|
|
#endif
|
|
|
|
|
2000-05-29 21:33:52 +02:00
|
|
|
#ifdef HAVE_STRING_H
|
|
|
|
#include <string.h>
|
|
|
|
#endif
|
|
|
|
|
2000-03-10 09:16:55 +01:00
|
|
|
#include "libiberty.h"
|
|
|
|
#include "partition.h"
|
|
|
|
|
xregex2.h (_RE_ARGS): Remove definition and uses.
include/
2005-03-27 Gabriel Dos Reis <gdr@integrable-solutions.net>
* xregex2.h (_RE_ARGS): Remove definition and uses.
libiberty/
2005-03-27 Gabriel Dos Reis <gdr@integrable-solutions.net>
Convert libiberty to use ISO C prototype style 7/n.
* regex.c (PARAMS): Remove definition.
(PREFIX): Unconditionaly define using ISO C ## operator.
(init_syntax_once, extract_number, extract_number_and_incr,
print_fastmap, print_partial_compiled_pattern,
print_compiled_pattern, print_double_string, printchar,
convert_mbs_to_wcs, re_set_syntax, regex_grow_registers,
regex_compile, store_op1, store_op2, insert_op1, insert_op2,
at_begline_loc_p, at_endline_p, group_in_compile_stack,
insert_space, wcs_compile_range, byte_compile_range,
truncate_wchar, re_compile_fastmap, re_compile_fastmap,
re_set_registers, re_search, re_search_2, re_search_3,
re_match,
re_match_2, count_mbs_length, wcs_re_match_2_internal,
byte_re_match_2_internal, group_match_null_string_p,
alt_match_null_string_p, common_op_match_null_string_p,
bcmp_translate, re_compile_pattern, re_comp, re_exec, regcomp,
regexec, regerror, regfree): Use ISO C prototype style.
* partition.c: (elem_compare): Likewise.
* cp-demangle.c (print_usage): Likewise.
From-SVN: r97129
2005-03-28 10:34:12 +02:00
|
|
|
static int elem_compare (const void *, const void *);
|
2000-06-07 23:13:14 +02:00
|
|
|
|
2000-03-10 09:16:55 +01:00
|
|
|
/* Creates a partition of NUM_ELEMENTS elements. Initially each
|
|
|
|
element is in a class by itself. */
|
|
|
|
|
|
|
|
partition
|
partition.h: Remove use of PARAMS.
include/
2005-03-27 Gabriel Dos Reis <gdr@integrable-solutions.net>
* partition.h: Remove use of PARAMS.
* obstack.h: Remove conditional prototypes __STDC__.
* objalloc.h: Remove use of PARAMS.
* splay-tree.h: Likewise.
libiberty/
2005-03-27 Gabriel Dos Reis <gdr@integrable-solutions.net>
Convert libiberty to use ISO C prototype style 5/n.
* random.c (srandom, initstate, setstate, random): Use ISO C
prototypes.
* putenv.c (putenv): Likewise.
* physmem.c (physmem_available, physmem_total, main):
Likewise.
* pex-win32.c (fix_argv, pexecute, pwait): Likewise.
* pex-unix.c (pexecute, pwait): Likewise.
* pex-msdos.c (pexecute, pwait): Likewise.
* pex-djgpp.c (pexecute, pwait): Likewise.
* partition.c (partition_new, partition_delete,
partition_union)
(elem_compare, partition_print): Likewise.
* obstack.c (_obstack_begin, _obstack_begin_1,
_obstack_newchunk,
_obstack_allocated_p, _obstack_free, obstack_free,
_obstack_memory_used, print_and_abort, obstack_next_free,
obstack_object_size, obstack_base): Likewise. Remove codes
predicated on !defined(__STDC__).
* objalloc.c (objalloc_create, _objalloc_alloc, objalloc_free,
objalloc_free_block): Use ISO C prototypes.
* mkstemps.c (mkstemps): Likewise.
* memset.c (memset): Likewise.
* mempcpy.c (mempcpy): Likewise.
* rename.c (rename): Likewise.
* rindex.c (rindex): Likewise.
* setenv.c (setenv, unsetenv): Likewise.
* sigsetmask.c (sigsetmask): Likewise.
* snprintf.c (snprintf): Likewise.
* sort.c (sort_pointers, xmalloc): Likewise.
* spaces.c (spaces): Likewise.
* splay-tree.c (splay_tree_delete_helper,
splay_tree_splay_helper, splay_tree_splay,
splay_tree_foreach_helper, splay_tree_xmalloc_allocate,
splay_tree_new, splay_tree_xmalloc_allocate,
splay_tree_new_with_allocator, splay_tree_delete,
splay_tree_insert, splay_tree_remove, splay_tree_lookup,
splay_tree_max, splay_tree_min, splay_tree_predecessor,
splay_tree_successor, splay_tree_foreach,
splay_tree_compare_ints, splay_tree_compare_pointers):
Likewise.
* stpcpy.c (stpcpy): Likewise.
* stpncpy.c (stpncpy): Likewise.
* strcasecmp.c (strcasecmp): Likewise.
* strchr.c (strchr): Likewise.
* strdup.c (strdup): Likewise.
From-SVN: r97125
2005-03-28 06:22:33 +02:00
|
|
|
partition_new (int num_elements)
|
2000-03-10 09:16:55 +01:00
|
|
|
{
|
|
|
|
int e;
|
|
|
|
|
|
|
|
partition part = (partition)
|
|
|
|
xmalloc (sizeof (struct partition_def) +
|
|
|
|
(num_elements - 1) * sizeof (struct partition_elem));
|
|
|
|
part->num_elements = num_elements;
|
|
|
|
for (e = 0; e < num_elements; ++e)
|
|
|
|
{
|
|
|
|
part->elements[e].class_element = e;
|
|
|
|
part->elements[e].next = &(part->elements[e]);
|
|
|
|
part->elements[e].class_count = 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
return part;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Freeds a partition. */
|
|
|
|
|
|
|
|
void
|
partition.h: Remove use of PARAMS.
include/
2005-03-27 Gabriel Dos Reis <gdr@integrable-solutions.net>
* partition.h: Remove use of PARAMS.
* obstack.h: Remove conditional prototypes __STDC__.
* objalloc.h: Remove use of PARAMS.
* splay-tree.h: Likewise.
libiberty/
2005-03-27 Gabriel Dos Reis <gdr@integrable-solutions.net>
Convert libiberty to use ISO C prototype style 5/n.
* random.c (srandom, initstate, setstate, random): Use ISO C
prototypes.
* putenv.c (putenv): Likewise.
* physmem.c (physmem_available, physmem_total, main):
Likewise.
* pex-win32.c (fix_argv, pexecute, pwait): Likewise.
* pex-unix.c (pexecute, pwait): Likewise.
* pex-msdos.c (pexecute, pwait): Likewise.
* pex-djgpp.c (pexecute, pwait): Likewise.
* partition.c (partition_new, partition_delete,
partition_union)
(elem_compare, partition_print): Likewise.
* obstack.c (_obstack_begin, _obstack_begin_1,
_obstack_newchunk,
_obstack_allocated_p, _obstack_free, obstack_free,
_obstack_memory_used, print_and_abort, obstack_next_free,
obstack_object_size, obstack_base): Likewise. Remove codes
predicated on !defined(__STDC__).
* objalloc.c (objalloc_create, _objalloc_alloc, objalloc_free,
objalloc_free_block): Use ISO C prototypes.
* mkstemps.c (mkstemps): Likewise.
* memset.c (memset): Likewise.
* mempcpy.c (mempcpy): Likewise.
* rename.c (rename): Likewise.
* rindex.c (rindex): Likewise.
* setenv.c (setenv, unsetenv): Likewise.
* sigsetmask.c (sigsetmask): Likewise.
* snprintf.c (snprintf): Likewise.
* sort.c (sort_pointers, xmalloc): Likewise.
* spaces.c (spaces): Likewise.
* splay-tree.c (splay_tree_delete_helper,
splay_tree_splay_helper, splay_tree_splay,
splay_tree_foreach_helper, splay_tree_xmalloc_allocate,
splay_tree_new, splay_tree_xmalloc_allocate,
splay_tree_new_with_allocator, splay_tree_delete,
splay_tree_insert, splay_tree_remove, splay_tree_lookup,
splay_tree_max, splay_tree_min, splay_tree_predecessor,
splay_tree_successor, splay_tree_foreach,
splay_tree_compare_ints, splay_tree_compare_pointers):
Likewise.
* stpcpy.c (stpcpy): Likewise.
* stpncpy.c (stpncpy): Likewise.
* strcasecmp.c (strcasecmp): Likewise.
* strchr.c (strchr): Likewise.
* strdup.c (strdup): Likewise.
From-SVN: r97125
2005-03-28 06:22:33 +02:00
|
|
|
partition_delete (partition part)
|
2000-03-10 09:16:55 +01:00
|
|
|
{
|
|
|
|
free (part);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Unites the classes containing ELEM1 and ELEM2 into a single class
|
|
|
|
of partition PART. If ELEM1 and ELEM2 are already in the same
|
|
|
|
class, does nothing. Returns the canonical element of the
|
|
|
|
resulting union class. */
|
|
|
|
|
|
|
|
int
|
partition.h: Remove use of PARAMS.
include/
2005-03-27 Gabriel Dos Reis <gdr@integrable-solutions.net>
* partition.h: Remove use of PARAMS.
* obstack.h: Remove conditional prototypes __STDC__.
* objalloc.h: Remove use of PARAMS.
* splay-tree.h: Likewise.
libiberty/
2005-03-27 Gabriel Dos Reis <gdr@integrable-solutions.net>
Convert libiberty to use ISO C prototype style 5/n.
* random.c (srandom, initstate, setstate, random): Use ISO C
prototypes.
* putenv.c (putenv): Likewise.
* physmem.c (physmem_available, physmem_total, main):
Likewise.
* pex-win32.c (fix_argv, pexecute, pwait): Likewise.
* pex-unix.c (pexecute, pwait): Likewise.
* pex-msdos.c (pexecute, pwait): Likewise.
* pex-djgpp.c (pexecute, pwait): Likewise.
* partition.c (partition_new, partition_delete,
partition_union)
(elem_compare, partition_print): Likewise.
* obstack.c (_obstack_begin, _obstack_begin_1,
_obstack_newchunk,
_obstack_allocated_p, _obstack_free, obstack_free,
_obstack_memory_used, print_and_abort, obstack_next_free,
obstack_object_size, obstack_base): Likewise. Remove codes
predicated on !defined(__STDC__).
* objalloc.c (objalloc_create, _objalloc_alloc, objalloc_free,
objalloc_free_block): Use ISO C prototypes.
* mkstemps.c (mkstemps): Likewise.
* memset.c (memset): Likewise.
* mempcpy.c (mempcpy): Likewise.
* rename.c (rename): Likewise.
* rindex.c (rindex): Likewise.
* setenv.c (setenv, unsetenv): Likewise.
* sigsetmask.c (sigsetmask): Likewise.
* snprintf.c (snprintf): Likewise.
* sort.c (sort_pointers, xmalloc): Likewise.
* spaces.c (spaces): Likewise.
* splay-tree.c (splay_tree_delete_helper,
splay_tree_splay_helper, splay_tree_splay,
splay_tree_foreach_helper, splay_tree_xmalloc_allocate,
splay_tree_new, splay_tree_xmalloc_allocate,
splay_tree_new_with_allocator, splay_tree_delete,
splay_tree_insert, splay_tree_remove, splay_tree_lookup,
splay_tree_max, splay_tree_min, splay_tree_predecessor,
splay_tree_successor, splay_tree_foreach,
splay_tree_compare_ints, splay_tree_compare_pointers):
Likewise.
* stpcpy.c (stpcpy): Likewise.
* stpncpy.c (stpncpy): Likewise.
* strcasecmp.c (strcasecmp): Likewise.
* strchr.c (strchr): Likewise.
* strdup.c (strdup): Likewise.
From-SVN: r97125
2005-03-28 06:22:33 +02:00
|
|
|
partition_union (partition part, int elem1, int elem2)
|
2000-03-10 09:16:55 +01:00
|
|
|
{
|
|
|
|
struct partition_elem *elements = part->elements;
|
|
|
|
struct partition_elem *e1;
|
|
|
|
struct partition_elem *e2;
|
|
|
|
struct partition_elem *p;
|
|
|
|
struct partition_elem *old_next;
|
|
|
|
/* The canonical element of the resulting union class. */
|
|
|
|
int class_element = elements[elem1].class_element;
|
|
|
|
|
|
|
|
/* If they're already in the same class, do nothing. */
|
|
|
|
if (class_element == elements[elem2].class_element)
|
|
|
|
return class_element;
|
|
|
|
|
|
|
|
/* Make sure ELEM1 is in the larger class of the two. If not, swap
|
|
|
|
them. This way we always scan the shorter list. */
|
|
|
|
if (elements[elem1].class_count < elements[elem2].class_count)
|
|
|
|
{
|
|
|
|
int temp = elem1;
|
|
|
|
elem1 = elem2;
|
|
|
|
elem2 = temp;
|
|
|
|
class_element = elements[elem1].class_element;
|
|
|
|
}
|
|
|
|
|
|
|
|
e1 = &(elements[elem1]);
|
|
|
|
e2 = &(elements[elem2]);
|
|
|
|
|
|
|
|
/* Keep a count of the number of elements in the list. */
|
|
|
|
elements[class_element].class_count
|
|
|
|
+= elements[e2->class_element].class_count;
|
|
|
|
|
|
|
|
/* Update the class fields in elem2's class list. */
|
|
|
|
e2->class_element = class_element;
|
|
|
|
for (p = e2->next; p != e2; p = p->next)
|
|
|
|
p->class_element = class_element;
|
|
|
|
|
|
|
|
/* Splice ELEM2's class list into ELEM1's. These are circular
|
|
|
|
lists. */
|
|
|
|
old_next = e1->next;
|
|
|
|
e1->next = e2->next;
|
|
|
|
e2->next = old_next;
|
|
|
|
|
|
|
|
return class_element;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Compare elements ELEM1 and ELEM2 from array of integers, given a
|
|
|
|
pointer to each. Used to qsort such an array. */
|
|
|
|
|
|
|
|
static int
|
partition.h: Remove use of PARAMS.
include/
2005-03-27 Gabriel Dos Reis <gdr@integrable-solutions.net>
* partition.h: Remove use of PARAMS.
* obstack.h: Remove conditional prototypes __STDC__.
* objalloc.h: Remove use of PARAMS.
* splay-tree.h: Likewise.
libiberty/
2005-03-27 Gabriel Dos Reis <gdr@integrable-solutions.net>
Convert libiberty to use ISO C prototype style 5/n.
* random.c (srandom, initstate, setstate, random): Use ISO C
prototypes.
* putenv.c (putenv): Likewise.
* physmem.c (physmem_available, physmem_total, main):
Likewise.
* pex-win32.c (fix_argv, pexecute, pwait): Likewise.
* pex-unix.c (pexecute, pwait): Likewise.
* pex-msdos.c (pexecute, pwait): Likewise.
* pex-djgpp.c (pexecute, pwait): Likewise.
* partition.c (partition_new, partition_delete,
partition_union)
(elem_compare, partition_print): Likewise.
* obstack.c (_obstack_begin, _obstack_begin_1,
_obstack_newchunk,
_obstack_allocated_p, _obstack_free, obstack_free,
_obstack_memory_used, print_and_abort, obstack_next_free,
obstack_object_size, obstack_base): Likewise. Remove codes
predicated on !defined(__STDC__).
* objalloc.c (objalloc_create, _objalloc_alloc, objalloc_free,
objalloc_free_block): Use ISO C prototypes.
* mkstemps.c (mkstemps): Likewise.
* memset.c (memset): Likewise.
* mempcpy.c (mempcpy): Likewise.
* rename.c (rename): Likewise.
* rindex.c (rindex): Likewise.
* setenv.c (setenv, unsetenv): Likewise.
* sigsetmask.c (sigsetmask): Likewise.
* snprintf.c (snprintf): Likewise.
* sort.c (sort_pointers, xmalloc): Likewise.
* spaces.c (spaces): Likewise.
* splay-tree.c (splay_tree_delete_helper,
splay_tree_splay_helper, splay_tree_splay,
splay_tree_foreach_helper, splay_tree_xmalloc_allocate,
splay_tree_new, splay_tree_xmalloc_allocate,
splay_tree_new_with_allocator, splay_tree_delete,
splay_tree_insert, splay_tree_remove, splay_tree_lookup,
splay_tree_max, splay_tree_min, splay_tree_predecessor,
splay_tree_successor, splay_tree_foreach,
splay_tree_compare_ints, splay_tree_compare_pointers):
Likewise.
* stpcpy.c (stpcpy): Likewise.
* stpncpy.c (stpncpy): Likewise.
* strcasecmp.c (strcasecmp): Likewise.
* strchr.c (strchr): Likewise.
* strdup.c (strdup): Likewise.
From-SVN: r97125
2005-03-28 06:22:33 +02:00
|
|
|
elem_compare (const void *elem1, const void *elem2)
|
2000-03-10 09:16:55 +01:00
|
|
|
{
|
2000-06-07 23:13:14 +02:00
|
|
|
int e1 = * (const int *) elem1;
|
|
|
|
int e2 = * (const int *) elem2;
|
2000-03-10 09:16:55 +01:00
|
|
|
if (e1 < e2)
|
|
|
|
return -1;
|
|
|
|
else if (e1 > e2)
|
|
|
|
return 1;
|
|
|
|
else
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Prints PART to the file pointer FP. The elements of each
|
|
|
|
class are sorted. */
|
|
|
|
|
|
|
|
void
|
partition.h: Remove use of PARAMS.
include/
2005-03-27 Gabriel Dos Reis <gdr@integrable-solutions.net>
* partition.h: Remove use of PARAMS.
* obstack.h: Remove conditional prototypes __STDC__.
* objalloc.h: Remove use of PARAMS.
* splay-tree.h: Likewise.
libiberty/
2005-03-27 Gabriel Dos Reis <gdr@integrable-solutions.net>
Convert libiberty to use ISO C prototype style 5/n.
* random.c (srandom, initstate, setstate, random): Use ISO C
prototypes.
* putenv.c (putenv): Likewise.
* physmem.c (physmem_available, physmem_total, main):
Likewise.
* pex-win32.c (fix_argv, pexecute, pwait): Likewise.
* pex-unix.c (pexecute, pwait): Likewise.
* pex-msdos.c (pexecute, pwait): Likewise.
* pex-djgpp.c (pexecute, pwait): Likewise.
* partition.c (partition_new, partition_delete,
partition_union)
(elem_compare, partition_print): Likewise.
* obstack.c (_obstack_begin, _obstack_begin_1,
_obstack_newchunk,
_obstack_allocated_p, _obstack_free, obstack_free,
_obstack_memory_used, print_and_abort, obstack_next_free,
obstack_object_size, obstack_base): Likewise. Remove codes
predicated on !defined(__STDC__).
* objalloc.c (objalloc_create, _objalloc_alloc, objalloc_free,
objalloc_free_block): Use ISO C prototypes.
* mkstemps.c (mkstemps): Likewise.
* memset.c (memset): Likewise.
* mempcpy.c (mempcpy): Likewise.
* rename.c (rename): Likewise.
* rindex.c (rindex): Likewise.
* setenv.c (setenv, unsetenv): Likewise.
* sigsetmask.c (sigsetmask): Likewise.
* snprintf.c (snprintf): Likewise.
* sort.c (sort_pointers, xmalloc): Likewise.
* spaces.c (spaces): Likewise.
* splay-tree.c (splay_tree_delete_helper,
splay_tree_splay_helper, splay_tree_splay,
splay_tree_foreach_helper, splay_tree_xmalloc_allocate,
splay_tree_new, splay_tree_xmalloc_allocate,
splay_tree_new_with_allocator, splay_tree_delete,
splay_tree_insert, splay_tree_remove, splay_tree_lookup,
splay_tree_max, splay_tree_min, splay_tree_predecessor,
splay_tree_successor, splay_tree_foreach,
splay_tree_compare_ints, splay_tree_compare_pointers):
Likewise.
* stpcpy.c (stpcpy): Likewise.
* stpncpy.c (stpncpy): Likewise.
* strcasecmp.c (strcasecmp): Likewise.
* strchr.c (strchr): Likewise.
* strdup.c (strdup): Likewise.
From-SVN: r97125
2005-03-28 06:22:33 +02:00
|
|
|
partition_print (partition part, FILE *fp)
|
2000-03-10 09:16:55 +01:00
|
|
|
{
|
|
|
|
char *done;
|
|
|
|
int num_elements = part->num_elements;
|
|
|
|
struct partition_elem *elements = part->elements;
|
|
|
|
int *class_elements;
|
|
|
|
int e;
|
|
|
|
|
|
|
|
/* Flag the elements we've already printed. */
|
|
|
|
done = (char *) xmalloc (num_elements);
|
|
|
|
memset (done, 0, num_elements);
|
|
|
|
|
|
|
|
/* A buffer used to sort elements in a class. */
|
|
|
|
class_elements = (int *) xmalloc (num_elements * sizeof (int));
|
|
|
|
|
|
|
|
fputc ('[', fp);
|
|
|
|
for (e = 0; e < num_elements; ++e)
|
|
|
|
/* If we haven't printed this element, print its entire class. */
|
|
|
|
if (! done[e])
|
|
|
|
{
|
|
|
|
int c = e;
|
|
|
|
int count = elements[elements[e].class_element].class_count;
|
|
|
|
int i;
|
|
|
|
|
|
|
|
/* Collect the elements in this class. */
|
|
|
|
for (i = 0; i < count; ++i) {
|
|
|
|
class_elements[i] = c;
|
|
|
|
done[c] = 1;
|
|
|
|
c = elements[c].next - elements;
|
|
|
|
}
|
|
|
|
/* Sort them. */
|
2000-06-08 09:29:53 +02:00
|
|
|
qsort ((void *) class_elements, count, sizeof (int), elem_compare);
|
2000-03-10 09:16:55 +01:00
|
|
|
/* Print them. */
|
|
|
|
fputc ('(', fp);
|
|
|
|
for (i = 0; i < count; ++i)
|
|
|
|
fprintf (fp, i == 0 ? "%d" : " %d", class_elements[i]);
|
|
|
|
fputc (')', fp);
|
|
|
|
}
|
|
|
|
fputc (']', fp);
|
|
|
|
|
2006-02-21 02:21:49 +01:00
|
|
|
free (class_elements);
|
2000-03-10 09:16:55 +01:00
|
|
|
free (done);
|
|
|
|
}
|
|
|
|
|