SortedSet_dh.c

00001 /*@HEADER
00002 // ***********************************************************************
00003 //
00004 //       Ifpack: Object-Oriented Algebraic Preconditioner Package
00005 //                 Copyright (2009) Sandia Corporation
00006 //
00007 // Under terms of Contract DE-AC04-94AL85000, there is a non-exclusive
00008 // license for use of this work by or on behalf of the U.S. Government.
00009 //
00010 // This library is free software; you can redistribute it and/or modify
00011 // it under the terms of the GNU Lesser General Public License as
00012 // published by the Free Software Foundation; either version 2.1 of the
00013 // License, or (at your option) any later version.
00014 //
00015 // This library is distributed in the hope that it will be useful, but
00016 // WITHOUT ANY WARRANTY; without even the implied warranty of
00017 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00018 // Lesser General Public License for more details.
00019 //
00020 // You should have received a copy of the GNU Lesser General Public
00021 // License along with this library; if not, write to the Free Software
00022 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
00023 // USA
00024 // Questions? Contact Michael A. Heroux (maherou@sandia.gov)
00025 //
00026 // ***********************************************************************
00027 //@HEADER
00028 */
00029 
00030 #include "SortedSet_dh.h"
00031 #include "shellSort_dh.h"
00032 #include "Mem_dh.h"
00033 
00034 
00035 #undef __FUNC__
00036 #define __FUNC__ "SortedSet_dhCreate"
00037 void
00038 SortedSet_dhCreate (SortedSet_dh * ss, int size)
00039 {
00040   START_FUNC_DH
00041     struct _sortedset_dh *tmp =
00042     (struct _sortedset_dh *) MALLOC_DH (sizeof (struct _sortedset_dh));
00043   CHECK_V_ERROR;
00044   *ss = tmp;
00045 
00046   tmp->n = size;
00047   tmp->list = (int *) MALLOC_DH (size * sizeof (int));
00048   CHECK_V_ERROR;
00049   tmp->count = 0;
00050 END_FUNC_DH}
00051 
00052 #undef __FUNC__
00053 #define __FUNC__ "SortedSet_dhDestroy"
00054 void
00055 SortedSet_dhDestroy (SortedSet_dh ss)
00056 {
00057   START_FUNC_DH if (ss->list != NULL)
00058     {
00059       FREE_DH (ss->list);
00060       CHECK_V_ERROR;
00061     }
00062   FREE_DH (ss);
00063   CHECK_V_ERROR;
00064 END_FUNC_DH}
00065 
00066 
00067 #undef __FUNC__
00068 #define __FUNC__ "SortedSet_dhInsert"
00069 void
00070 SortedSet_dhInsert (SortedSet_dh ss, int idx)
00071 {
00072   START_FUNC_DH bool isInserted = false;
00073   int ct = ss->count;
00074   int *list = ss->list;
00075   int i, n = ss->n;
00076 
00077   /* determine if item was already inserted */
00078   for (i = 0; i < ct; ++i)
00079     {
00080       if (list[i] == idx)
00081     {
00082       isInserted = true;
00083       break;
00084     }
00085     }
00086 
00087   /* is we need to insert the item, first check for overflow
00088      and reallocate if necessary, then append the index to the
00089      end of the list.
00090    */
00091   if (!isInserted)
00092     {
00093       if (ct == n)
00094     {
00095       int *tmp = (int *) MALLOC_DH (n * 2 * sizeof (int));
00096       CHECK_V_ERROR;
00097       memcpy (tmp, list, n * sizeof (int));
00098       FREE_DH (list);
00099       CHECK_V_ERROR;
00100       list = ss->list = tmp;
00101       ss->n *= 2;
00102     }
00103 
00104       list[ct] = idx;
00105       ss->count += 1;
00106     }
00107 END_FUNC_DH}
00108 
00109 
00110 #undef __FUNC__
00111 #define __FUNC__ "SortedSet_dhGetList"
00112 void
00113 SortedSet_dhGetList (SortedSet_dh ss, int **list, int *count)
00114 {
00115   START_FUNC_DH shellSort_int (ss->count, ss->list);
00116   *list = ss->list;
00117   *count = ss->count;
00118 END_FUNC_DH}

Generated on Tue Jul 13 09:27:14 2010 for IFPACK by  doxygen 1.4.7