IFPACK  Development
 All Classes Namespaces Files Functions Variables Enumerations Friends Pages
SortedSet_dh.c
1 /*@HEADER
2 // ***********************************************************************
3 //
4 // Ifpack: Object-Oriented Algebraic Preconditioner Package
5 // Copyright (2002) Sandia Corporation
6 //
7 // Under terms of Contract DE-AC04-94AL85000, there is a non-exclusive
8 // license for use of this work by or on behalf of the U.S. Government.
9 //
10 // Redistribution and use in source and binary forms, with or without
11 // modification, are permitted provided that the following conditions are
12 // met:
13 //
14 // 1. Redistributions of source code must retain the above copyright
15 // notice, this list of conditions and the following disclaimer.
16 //
17 // 2. Redistributions in binary form must reproduce the above copyright
18 // notice, this list of conditions and the following disclaimer in the
19 // documentation and/or other materials provided with the distribution.
20 //
21 // 3. Neither the name of the Corporation nor the names of the
22 // contributors may be used to endorse or promote products derived from
23 // this software without specific prior written permission.
24 //
25 // THIS SOFTWARE IS PROVIDED BY SANDIA CORPORATION "AS IS" AND ANY
26 // EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
27 // IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
28 // PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL SANDIA CORPORATION OR THE
29 // CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
30 // EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
31 // PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
32 // PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
33 // LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
34 // NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
35 // SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
36 //
37 // Questions? Contact Michael A. Heroux (maherou@sandia.gov)
38 //
39 // ***********************************************************************
40 //@HEADER
41 */
42 
43 #include "SortedSet_dh.h"
44 #include "shellSort_dh.h"
45 #include "Mem_dh.h"
46 
47 
48 #undef __FUNC__
49 #define __FUNC__ "SortedSet_dhCreate"
50 void
51 SortedSet_dhCreate (SortedSet_dh * ss, int size)
52 {
53  START_FUNC_DH
54  struct _sortedset_dh *tmp =
55  (struct _sortedset_dh *) MALLOC_DH (sizeof (struct _sortedset_dh));
56  CHECK_V_ERROR;
57  *ss = tmp;
58 
59  tmp->n = size;
60  tmp->list = (int *) MALLOC_DH (size * sizeof (int));
61  CHECK_V_ERROR;
62  tmp->count = 0;
63 END_FUNC_DH}
64 
65 #undef __FUNC__
66 #define __FUNC__ "SortedSet_dhDestroy"
67 void
68 SortedSet_dhDestroy (SortedSet_dh ss)
69 {
70  START_FUNC_DH if (ss->list != NULL)
71  {
72  FREE_DH (ss->list);
73  CHECK_V_ERROR;
74  }
75  FREE_DH (ss);
76  CHECK_V_ERROR;
77 END_FUNC_DH}
78 
79 
80 #undef __FUNC__
81 #define __FUNC__ "SortedSet_dhInsert"
82 void
83 SortedSet_dhInsert (SortedSet_dh ss, int idx)
84 {
85  START_FUNC_DH bool isInserted = false;
86  int ct = ss->count;
87  int *list = ss->list;
88  int i, n = ss->n;
89 
90  /* determine if item was already inserted */
91  for (i = 0; i < ct; ++i)
92  {
93  if (list[i] == idx)
94  {
95  isInserted = true;
96  break;
97  }
98  }
99 
100  /* is we need to insert the item, first check for overflow
101  and reallocate if necessary, then append the index to the
102  end of the list.
103  */
104  if (!isInserted)
105  {
106  if (ct == n)
107  {
108  int *tmp = (int *) MALLOC_DH (n * 2 * sizeof (int));
109  CHECK_V_ERROR;
110  memcpy (tmp, list, n * sizeof (int));
111  FREE_DH (list);
112  CHECK_V_ERROR;
113  list = ss->list = tmp;
114  ss->n *= 2;
115  }
116 
117  list[ct] = idx;
118  ss->count += 1;
119  }
120 END_FUNC_DH}
121 
122 
123 #undef __FUNC__
124 #define __FUNC__ "SortedSet_dhGetList"
125 void
126 SortedSet_dhGetList (SortedSet_dh ss, int **list, int *count)
127 {
128  START_FUNC_DH shellSort_int (ss->count, ss->list);
129  *list = ss->list;
130  *count = ss->count;
131 END_FUNC_DH}