modified: makefile
[GalaxyCodeBases.git] / BGI / SOAPdenovo2 / standardPregraph / darray.c
blob66ca6683256895635cbecc10f5526e4c4803c874
1 /*
2 * darray.c
4 * Copyright (c) 2008-2012 BGI-Shenzhen <soap at genomics dot org dot cn>.
6 * This file is part of SOAPdenovo.
8 * SOAPdenovo is free software: you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation, either version 3 of the License, or
11 * (at your option) any later version.
13 * SOAPdenovo is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
18 * You should have received a copy of the GNU General Public License
19 * along with SOAPdenovo. If not, see <http://www.gnu.org/licenses/>.
23 #include "darray.h"
24 #include "check.h"
26 DARRAY * createDarray ( int num_items, size_t unit_size )
28 DARRAY * newDarray = ( DARRAY * ) malloc ( 1 * sizeof ( DARRAY ) );
29 newDarray->array_size = num_items;
30 newDarray->item_size = unit_size;
31 newDarray->item_c = 0;
32 newDarray->array = ( void * ) ckalloc ( num_items * unit_size );
33 return newDarray;
36 void * darrayPut ( DARRAY * darray, long long index )
38 int i = 2;
40 if ( index + 1 > darray->item_c )
42 darray->item_c = index + 1;
45 if ( index < darray->array_size )
47 return darray->array + darray->item_size * index;
50 while ( index > i * darray->array_size )
52 i++;
55 darray->array = ( void * ) ckrealloc ( darray->array, i * darray->array_size * darray->item_size, darray->array_size * darray->item_size );
56 darray->array_size *= i;
57 return ( void * ) ( ( void * ) darray->array + darray->item_size * index );
60 void * darrayGet ( DARRAY * darray, long long index )
62 if ( index < darray->array_size )
64 return ( void * ) ( ( void * ) darray->array + darray->item_size * index );
67 fprintf ( stderr, "Index %lld of the array is out of range and the size is %lld.\n", index, darray->array_size );
68 return NULL;
71 void emptyDarray ( DARRAY * darray )
73 darray->item_c = 0;
76 void freeDarray ( DARRAY * darray )
78 if ( !darray )
80 return;
83 if ( darray->array )
85 free ( ( void * ) darray->array );
88 free ( ( void * ) darray );