Revision 2c47b73,
1.3 KB
checked in by Hal Finkel <hfinkel@…>, 6 years ago
(diff) |
more work on adding SZ (latest version)
|
-
Property mode set to
100644
|
Rev | Line | |
---|
[2c47b73] | 1 | /** |
---|
| 2 | * @file DynamicIntArray.c |
---|
| 3 | * @author Sheng Di |
---|
| 4 | * @date May, 2016 |
---|
| 5 | * @brief Dynamic Int Array |
---|
| 6 | * (C) 2016 by Mathematics and Computer Science (MCS), Argonne National Laboratory. |
---|
| 7 | * See COPYRIGHT in top-level directory. |
---|
| 8 | */ |
---|
| 9 | |
---|
| 10 | #include <stdlib.h> |
---|
| 11 | #include <stdio.h> |
---|
| 12 | #include <string.h> |
---|
| 13 | #include "DynamicIntArray.h" |
---|
| 14 | |
---|
| 15 | void new_DIA(DynamicIntArray **dia, size_t cap) { |
---|
| 16 | *dia = (DynamicIntArray *)malloc(sizeof(DynamicIntArray)); |
---|
| 17 | (*dia)->size = 0; |
---|
| 18 | (*dia)->capacity = cap; |
---|
| 19 | (*dia)->array = (unsigned char*)malloc(sizeof(unsigned char)*cap); |
---|
| 20 | } |
---|
| 21 | |
---|
| 22 | void convertDIAtoInts(DynamicIntArray *dia, unsigned char **data) |
---|
| 23 | { |
---|
| 24 | int size = dia->size; |
---|
| 25 | if(size>0) |
---|
| 26 | *data = (unsigned char*)malloc(size * sizeof(char)); |
---|
| 27 | else |
---|
| 28 | *data = NULL; |
---|
| 29 | memcpy(*data, dia->array, size*sizeof(unsigned char)); |
---|
| 30 | } |
---|
| 31 | |
---|
| 32 | void free_DIA(DynamicIntArray *dia) |
---|
| 33 | { |
---|
| 34 | free(dia->array); |
---|
| 35 | free(dia); |
---|
| 36 | } |
---|
| 37 | |
---|
| 38 | int getDIA_Data(DynamicIntArray *dia, size_t pos) |
---|
| 39 | { |
---|
| 40 | if(pos>=dia->size) |
---|
| 41 | { |
---|
| 42 | printf("Error: wrong position of DIA.\n"); |
---|
| 43 | exit(0); |
---|
| 44 | } |
---|
| 45 | return dia->array[pos]; |
---|
| 46 | } |
---|
| 47 | |
---|
| 48 | void addDIA_Data(DynamicIntArray *dia, int value) |
---|
| 49 | { |
---|
| 50 | if(dia->size==dia->capacity) |
---|
| 51 | { |
---|
| 52 | dia->capacity = dia->capacity << 1; |
---|
| 53 | dia->array = (unsigned char *)realloc(dia->array, dia->capacity*sizeof(unsigned char)); |
---|
| 54 | } |
---|
| 55 | dia->array[dia->size] = (unsigned char)value; |
---|
| 56 | dia->size ++; |
---|
| 57 | } |
---|
Note: See
TracBrowser
for help on using the repository browser.