| [2aa6644] | 1 | /*BHEADER**********************************************************************
|
|---|
| 2 | * Copyright (c) 2008, Lawrence Livermore National Security, LLC.
|
|---|
| 3 | * Produced at the Lawrence Livermore National Laboratory.
|
|---|
| 4 | * This file is part of HYPRE. See file COPYRIGHT for details.
|
|---|
| 5 | *
|
|---|
| 6 | * HYPRE is free software; you can redistribute it and/or modify it under the
|
|---|
| 7 | * terms of the GNU Lesser General Public License (as published by the Free
|
|---|
| 8 | * Software Foundation) version 2.1 dated February 1999.
|
|---|
| 9 | *
|
|---|
| 10 | * $Revision: 2.4 $
|
|---|
| 11 | ***********************************************************************EHEADER*/
|
|---|
| 12 |
|
|---|
| 13 |
|
|---|
| 14 | /***************************************************************************
|
|---|
| 15 | *
|
|---|
| 16 | * Routines for linked list for boomerAMG
|
|---|
| 17 | *
|
|---|
| 18 | ****************************************************************************/
|
|---|
| 19 |
|
|---|
| 20 | #include "utilities.h"
|
|---|
| 21 |
|
|---|
| 22 | #define LIST_HEAD -1
|
|---|
| 23 | #define LIST_TAIL -2
|
|---|
| 24 |
|
|---|
| 25 |
|
|---|
| 26 | /**************************************************************
|
|---|
| 27 | *
|
|---|
| 28 | * dispose_elt(): dispose of memory space used by the element
|
|---|
| 29 | * pointed to by element_ptr. Use the 'free()'
|
|---|
| 30 | * system call to return it to the free memory
|
|---|
| 31 | * pool.
|
|---|
| 32 | *
|
|---|
| 33 | **************************************************************/
|
|---|
| 34 | void dispose_elt ( hypre_LinkList element_ptr )
|
|---|
| 35 | {
|
|---|
| 36 | free( element_ptr );
|
|---|
| 37 | }
|
|---|
| 38 |
|
|---|
| 39 |
|
|---|
| 40 |
|
|---|
| 41 | /*****************************************************************
|
|---|
| 42 | *
|
|---|
| 43 | * remove_point: removes a point from the lists
|
|---|
| 44 | *
|
|---|
| 45 | ****************************************************************/
|
|---|
| 46 | void
|
|---|
| 47 | remove_point(hypre_LinkList *LoL_head_ptr,
|
|---|
| 48 | hypre_LinkList *LoL_tail_ptr,
|
|---|
| 49 | int measure,
|
|---|
| 50 | int index,
|
|---|
| 51 | int *lists,
|
|---|
| 52 | int *where)
|
|---|
| 53 |
|
|---|
| 54 | {
|
|---|
| 55 | hypre_LinkList LoL_head = *LoL_head_ptr;
|
|---|
| 56 | hypre_LinkList LoL_tail = *LoL_tail_ptr;
|
|---|
| 57 | hypre_LinkList list_ptr;
|
|---|
| 58 |
|
|---|
| 59 | list_ptr = LoL_head;
|
|---|
| 60 |
|
|---|
| 61 |
|
|---|
| 62 | do
|
|---|
| 63 | {
|
|---|
| 64 | if (measure == list_ptr->data)
|
|---|
| 65 | {
|
|---|
| 66 |
|
|---|
| 67 | /* point to be removed is only point on list,
|
|---|
| 68 | which must be destroyed */
|
|---|
| 69 | if (list_ptr->head == index && list_ptr->tail == index)
|
|---|
| 70 | {
|
|---|
| 71 | /* removing only list, so num_left better be 0! */
|
|---|
| 72 | if (list_ptr == LoL_head && list_ptr == LoL_tail)
|
|---|
| 73 | {
|
|---|
| 74 | LoL_head = NULL;
|
|---|
| 75 | LoL_tail = NULL;
|
|---|
| 76 | dispose_elt(list_ptr);
|
|---|
| 77 |
|
|---|
| 78 | *LoL_head_ptr = LoL_head;
|
|---|
| 79 | *LoL_tail_ptr = LoL_tail;
|
|---|
| 80 | return;
|
|---|
| 81 | }
|
|---|
| 82 | else if (LoL_head == list_ptr) /*removing 1st (max_measure) list */
|
|---|
| 83 | {
|
|---|
| 84 | list_ptr -> next_elt -> prev_elt = NULL;
|
|---|
| 85 | LoL_head = list_ptr->next_elt;
|
|---|
| 86 | dispose_elt(list_ptr);
|
|---|
| 87 |
|
|---|
| 88 | *LoL_head_ptr = LoL_head;
|
|---|
| 89 | *LoL_tail_ptr = LoL_tail;
|
|---|
| 90 | return;
|
|---|
| 91 | }
|
|---|
| 92 | else if (LoL_tail == list_ptr) /* removing last list */
|
|---|
| 93 | {
|
|---|
| 94 | list_ptr -> prev_elt -> next_elt = NULL;
|
|---|
| 95 | LoL_tail = list_ptr->prev_elt;
|
|---|
| 96 | dispose_elt(list_ptr);
|
|---|
| 97 |
|
|---|
| 98 | *LoL_head_ptr = LoL_head;
|
|---|
| 99 | *LoL_tail_ptr = LoL_tail;
|
|---|
| 100 | return;
|
|---|
| 101 | }
|
|---|
| 102 | else
|
|---|
| 103 | {
|
|---|
| 104 | list_ptr -> next_elt -> prev_elt = list_ptr -> prev_elt;
|
|---|
| 105 | list_ptr -> prev_elt -> next_elt = list_ptr -> next_elt;
|
|---|
| 106 | dispose_elt(list_ptr);
|
|---|
| 107 |
|
|---|
| 108 | *LoL_head_ptr = LoL_head;
|
|---|
| 109 | *LoL_tail_ptr = LoL_tail;
|
|---|
| 110 | return;
|
|---|
| 111 | }
|
|---|
| 112 | }
|
|---|
| 113 | else if (list_ptr->head == index) /* index is head of list */
|
|---|
| 114 | {
|
|---|
| 115 | list_ptr->head = lists[index];
|
|---|
| 116 | where[lists[index]] = LIST_HEAD;
|
|---|
| 117 | return;
|
|---|
| 118 | }
|
|---|
| 119 | else if (list_ptr->tail == index) /* index is tail of list */
|
|---|
| 120 | {
|
|---|
| 121 | list_ptr->tail = where[index];
|
|---|
| 122 | lists[where[index]] = LIST_TAIL;
|
|---|
| 123 | return;
|
|---|
| 124 | }
|
|---|
| 125 | else /* index is in middle of list */
|
|---|
| 126 | {
|
|---|
| 127 | lists[where[index]] = lists[index];
|
|---|
| 128 | where[lists[index]] = where[index];
|
|---|
| 129 | return;
|
|---|
| 130 | }
|
|---|
| 131 | }
|
|---|
| 132 | list_ptr = list_ptr -> next_elt;
|
|---|
| 133 | } while (list_ptr != NULL);
|
|---|
| 134 |
|
|---|
| 135 | printf("No such list!\n");
|
|---|
| 136 | return;
|
|---|
| 137 | }
|
|---|
| 138 |
|
|---|
| 139 | /*****************************************************************
|
|---|
| 140 | *
|
|---|
| 141 | * create_elt() : Create an element using Item for its data field
|
|---|
| 142 | *
|
|---|
| 143 | *****************************************************************/
|
|---|
| 144 | hypre_LinkList create_elt( int Item )
|
|---|
| 145 | {
|
|---|
| 146 | hypre_LinkList new_elt_ptr;
|
|---|
| 147 |
|
|---|
| 148 | /* Allocate memory space for the new node.
|
|---|
| 149 | * return with error if no space available
|
|---|
| 150 | */
|
|---|
| 151 |
|
|---|
| 152 | if ( (new_elt_ptr = (hypre_LinkList) malloc (sizeof(hypre_ListElement))) == NULL)
|
|---|
| 153 | {
|
|---|
| 154 | printf("\n create_elt: malloc failed \n\n");
|
|---|
| 155 | }
|
|---|
| 156 | else
|
|---|
| 157 |
|
|---|
| 158 | /* new_elt_ptr = hypre_CTAlloc(hypre_LinkList, 1); */
|
|---|
| 159 |
|
|---|
| 160 | {
|
|---|
| 161 | new_elt_ptr -> data = Item;
|
|---|
| 162 | new_elt_ptr -> next_elt = NULL;
|
|---|
| 163 | new_elt_ptr -> prev_elt = NULL;
|
|---|
| 164 | new_elt_ptr -> head = LIST_TAIL;
|
|---|
| 165 | new_elt_ptr -> tail = LIST_HEAD;
|
|---|
| 166 | }
|
|---|
| 167 |
|
|---|
| 168 | return (new_elt_ptr);
|
|---|
| 169 | }
|
|---|
| 170 |
|
|---|
| 171 | /*****************************************************************
|
|---|
| 172 | *
|
|---|
| 173 | * enter_on_lists places point in new list
|
|---|
| 174 | *
|
|---|
| 175 | ****************************************************************/
|
|---|
| 176 | void
|
|---|
| 177 | enter_on_lists(hypre_LinkList *LoL_head_ptr,
|
|---|
| 178 | hypre_LinkList *LoL_tail_ptr,
|
|---|
| 179 | int measure,
|
|---|
| 180 | int index,
|
|---|
| 181 | int *lists,
|
|---|
| 182 | int *where)
|
|---|
| 183 | {
|
|---|
| 184 | hypre_LinkList LoL_head = *LoL_head_ptr;
|
|---|
| 185 | hypre_LinkList LoL_tail = *LoL_tail_ptr;
|
|---|
| 186 |
|
|---|
| 187 | hypre_LinkList list_ptr;
|
|---|
| 188 | hypre_LinkList new_ptr;
|
|---|
| 189 |
|
|---|
| 190 | int old_tail;
|
|---|
| 191 |
|
|---|
| 192 | list_ptr = LoL_head;
|
|---|
| 193 |
|
|---|
| 194 | if (LoL_head == NULL) /* no lists exist yet */
|
|---|
| 195 | {
|
|---|
| 196 | new_ptr = create_elt(measure);
|
|---|
| 197 | new_ptr->head = index;
|
|---|
| 198 | new_ptr->tail = index;
|
|---|
| 199 | lists[index] = LIST_TAIL;
|
|---|
| 200 | where[index] = LIST_HEAD;
|
|---|
| 201 | LoL_head = new_ptr;
|
|---|
| 202 | LoL_tail = new_ptr;
|
|---|
| 203 |
|
|---|
| 204 | *LoL_head_ptr = LoL_head;
|
|---|
| 205 | *LoL_tail_ptr = LoL_tail;
|
|---|
| 206 | return;
|
|---|
| 207 | }
|
|---|
| 208 | else
|
|---|
| 209 | {
|
|---|
| 210 | do
|
|---|
| 211 | {
|
|---|
| 212 | if (measure > list_ptr->data)
|
|---|
| 213 | {
|
|---|
| 214 | new_ptr = create_elt(measure);
|
|---|
| 215 | new_ptr->head = index;
|
|---|
| 216 | new_ptr->tail = index;
|
|---|
| 217 | lists[index] = LIST_TAIL;
|
|---|
| 218 | where[index] = LIST_HEAD;
|
|---|
| 219 |
|
|---|
| 220 | if ( list_ptr->prev_elt != NULL)
|
|---|
| 221 | {
|
|---|
| 222 | new_ptr->prev_elt = list_ptr->prev_elt;
|
|---|
| 223 | list_ptr->prev_elt->next_elt = new_ptr;
|
|---|
| 224 | list_ptr->prev_elt = new_ptr;
|
|---|
| 225 | new_ptr->next_elt = list_ptr;
|
|---|
| 226 | }
|
|---|
| 227 | else
|
|---|
| 228 | {
|
|---|
| 229 | new_ptr->next_elt = list_ptr;
|
|---|
| 230 | list_ptr->prev_elt = new_ptr;
|
|---|
| 231 | new_ptr->prev_elt = NULL;
|
|---|
| 232 | LoL_head = new_ptr;
|
|---|
| 233 | }
|
|---|
| 234 |
|
|---|
| 235 | *LoL_head_ptr = LoL_head;
|
|---|
| 236 | *LoL_tail_ptr = LoL_tail;
|
|---|
| 237 | return;
|
|---|
| 238 | }
|
|---|
| 239 | else if (measure == list_ptr->data)
|
|---|
| 240 | {
|
|---|
| 241 | old_tail = list_ptr->tail;
|
|---|
| 242 | lists[old_tail] = index;
|
|---|
| 243 | where[index] = old_tail;
|
|---|
| 244 | lists[index] = LIST_TAIL;
|
|---|
| 245 | list_ptr->tail = index;
|
|---|
| 246 | return;
|
|---|
| 247 | }
|
|---|
| 248 |
|
|---|
| 249 | list_ptr = list_ptr->next_elt;
|
|---|
| 250 | } while (list_ptr != NULL);
|
|---|
| 251 |
|
|---|
| 252 | new_ptr = create_elt(measure);
|
|---|
| 253 | new_ptr->head = index;
|
|---|
| 254 | new_ptr->tail = index;
|
|---|
| 255 | lists[index] = LIST_TAIL;
|
|---|
| 256 | where[index] = LIST_HEAD;
|
|---|
| 257 | LoL_tail->next_elt = new_ptr;
|
|---|
| 258 | new_ptr->prev_elt = LoL_tail;
|
|---|
| 259 | new_ptr->next_elt = NULL;
|
|---|
| 260 | LoL_tail = new_ptr;
|
|---|
| 261 |
|
|---|
| 262 | *LoL_head_ptr = LoL_head;
|
|---|
| 263 | *LoL_tail_ptr = LoL_tail;
|
|---|
| 264 | return;
|
|---|
| 265 | }
|
|---|
| 266 | }
|
|---|
| 267 |
|
|---|
| 268 |
|
|---|