DynamicFloatArray.c 1.2 KB
Newer Older
T
tickduan 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57
/**
 *  @file DynamicFloatArray.c
 *  @author Sheng Di
 *  @date May, 2016
 *  @brief Dynamic Float Array
 *  (C) 2015 by Mathematics and Computer Science (MCS), Argonne National Laboratory.
 *      See COPYRIGHT in top-level directory.
 */

#include <stdlib.h> 
#include <stdio.h>
#include <string.h>
#include "DynamicFloatArray.h"

void new_DFA(DynamicFloatArray **dfa, size_t cap) {
		*dfa = (DynamicFloatArray *)malloc(sizeof(DynamicFloatArray));
        (*dfa)->size = 0;
        (*dfa)->capacity = cap;
        (*dfa)->array = (float*)malloc(sizeof(float)*cap);
    }

void convertDFAtoFloats(DynamicFloatArray *dfa, float **data)
{
	size_t size = dfa->size;
	if(size>0)
		*data = (float*)malloc(size * sizeof(float));
	else
		*data = NULL;
	memcpy(*data, dfa->array, size*sizeof(float));	
}

void free_DFA(DynamicFloatArray *dfa)
{
	free(dfa->array);
	free(dfa);
}

float getDFA_Data(DynamicFloatArray *dfa, size_t pos)
{
	if(pos>=dfa->size)
	{
		printf("Error: wrong position of DIA.\n");
		exit(0);
	}
	return dfa->array[pos];
}

void addDFA_Data(DynamicFloatArray *dfa, float value)
{
	if(dfa->size==dfa->capacity)
	{
		dfa->capacity *= 2;
		dfa->array = (float *)realloc(dfa->array, dfa->capacity*sizeof(float));
	}
	dfa->array[dfa->size] = value;
	dfa->size++;
}