• Main Page
  • Data Structures
  • Files
  • File List
  • Globals

ne_dyn_vec.c

00001 /*===========================================================================
00002  NetEvo Foundation Library
00003  Copyright (C) 2009, 2010 Thomas E. Gorochowski <tgorochowski@me.com>
00004  Bristol Centre for Complexity Sciences, University of Bristol, Bristol, UK
00005  ---------------------------------------------------------------------------- 
00006  NetEvo is a computing framework designed to allow researchers to investigate 
00007  evolutionary aspects of dynamical complex networks. By providing tools to 
00008  easily integrate each of these factors in a coherent way, it is hoped a 
00009  greater understanding can be gained of key attributes and features displayed 
00010  by complex systems.
00011  
00012  NetEvo is open-source software released under the Open Source Initiative 
00013  (OSI) approved Non-Profit Open Software License ("Non-Profit OSL") 3.0. 
00014  Detailed information about this licence can be found in the COPYING file 
00015  included as part of the source distribution.
00016  
00017  This library is distributed in the hope that it will be useful, but
00018  WITHOUT ANY WARRANTY; without even the implied warranty of
00019  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
00020  ============================================================================*/
00021 
00022 
00023 #include "ne_dyn_vec.h"
00024 #include <stdlib.h>
00025 
00026 
00027 ne_dyn_vec_t * ne_dyn_vec_alloc (int dataSize)
00028 {  
00029    int i;
00030    ne_dyn_vec_t * vec = NULL;
00031    
00032    vec = (ne_dyn_vec_t *)malloc(sizeof(ne_dyn_vec_t));
00033    
00034    /* Set initial sizes */
00035    vec->curSize = 0;
00036    vec->totSize = 100;
00037    vec->dataSize = dataSize;
00038    vec->data = (double**)malloc(sizeof(double*)*vec->totSize);
00039    
00040    /* Set all pointers to NULL */
00041    for (i=0; i<vec->totSize; i++) {
00042       vec->data[i] = NULL;
00043    }
00044    
00045    return vec;
00046 }
00047 
00048 
00049 void ne_dyn_vec_free (ne_dyn_vec_t *vec)
00050 {  
00051    int i;
00052    
00053    if (vec != NULL) {
00054       
00055       /* Free each of the data items */
00056       for (i=0; i<vec->totSize; i++) {
00057          free(vec->data[i]);
00058       }
00059       
00060       /* Free the list of pointers */
00061       free(vec->data);
00062       
00063       /* Free the structure elements */
00064       free(vec);
00065    }
00066 }
00067 
00068 
00069 void ne_dyn_vec_append_item (ne_dyn_vec_t *vec, double *data) 
00070 {  
00071    int oldSize, newSize, i;
00072    double** newData = NULL;
00073    
00074    /* Check to see if data store is large enough */
00075    if (vec->totSize <= vec->curSize) {
00076       
00077       oldSize = vec->totSize;
00078       newSize = ne_dyn_vec_extend_fn(oldSize);
00079       
00080       /* Need to realloc and copy across data */
00081       newData = (double**)malloc(sizeof(double*)*newSize);
00082       
00083       for (i=0; i<newSize; i++) {
00084          
00085          if (i < vec->curSize)
00086             newData[i] = vec->data[i];
00087          else
00088             newData[i] = NULL;
00089       }
00090       
00091       /* Free the old pointer array */
00092       free(vec->data);
00093       
00094       /* Use the newly created array */
00095       vec->data = newData;
00096       
00097       /* Update the size */
00098       vec->totSize = newSize;
00099       
00100       /* Add the new item */
00101       vec->data[vec->curSize] = data;
00102       vec->curSize = vec->curSize + 1;
00103    }
00104    else {
00105       
00106       /* Add the new item */
00107       vec->data[vec->curSize] = data;
00108       vec->curSize = vec->curSize + 1;
00109    }
00110 }
00111 
00112 
00113 int ne_dyn_vec_extend_fn (int curSize)
00114 {  
00115    /* Standard approach is to use geometric scaling of vector size */
00116    return curSize * 2;
00117 }
00118 

Generated on Thu Aug 26 2010 11:04:24 for NetEvo by  doxygen 1.7.1