comparison srf2fastq/io_lib-1.12.2/io_lib/array.c @ 0:d901c9f41a6a default tip

Migrated tool version 1.0.1 from old tool shed archive to new tool shed repository
author dawe
date Tue, 07 Jun 2011 17:48:05 -0400
parents
children
comparison
equal deleted inserted replaced
-1:000000000000 0:d901c9f41a6a
1 /*
2 * Copyright (c) Medical Research Council 1994. All rights reserved.
3 *
4 * Permission to use, copy, modify and distribute this software and its
5 * documentation for any purpose is hereby granted without fee, provided that
6 * this copyright and notice appears in all copies.
7 *
8 * This file was written by James Bonfield, Simon Dear, Rodger Staden,
9 * as part of the Staden Package at the MRC Laboratory of Molecular
10 * Biology, Hills Road, Cambridge, CB2 2QH, United Kingdom.
11 *
12 * MRC disclaims all warranties with regard to this software.
13 */
14
15 /*
16 * File: array.c
17 * Version:
18 *
19 * Description:
20 *
21 * Created:
22 * Updated:
23 *
24 */
25
26 #include <stdio.h>
27 #include <sys/types.h>
28
29 #include "io_lib/array.h"
30 #include "io_lib/xalloc.h"
31
32
33 /*
34 * For error reporting
35 */
36 int ArrayError = 0;
37
38
39 char *ArrayErrorString(int err)
40 {
41 switch(err) {
42 case ARRAY_NO_ERROR: return "No error";
43 case ARRAY_FULL: return "Array full";
44 case ARRAY_INVALID_ARGUMENTS: return "Invalid arguments";
45 case ARRAY_OUT_OF_MEMORY: return "Out of memory";
46 default: return "Unknown error";
47 }
48 }
49
50
51
52 Array ArrayCreate(size_t size, size_t dim)
53 /*
54 * create a new array
55 */
56 {
57 Array a;
58
59 if ( (a = (Array) xmalloc(sizeof(ArrayStruct)) ) == NULL ) {
60 ArrayError = ARRAY_OUT_OF_MEMORY;
61 } else {
62 a->size = size;
63 a->dim = dim?dim:1;
64 a->max = 0;
65 if ( (a->base = (void *)xmalloc(a->size * a->dim)) == NULL ) {
66 ArrayError = ARRAY_OUT_OF_MEMORY;
67 xfree(a);
68 a = NULL;
69 }
70 }
71
72 return a;
73
74 }
75
76
77
78 int ArrayExtend(Array a, size_t dim)
79 /*
80 * extend array
81 */
82 {
83 void *newbase;
84 size_t old_dim;
85
86 if (a == NULL) return ArrayError = ARRAY_INVALID_ARGUMENTS;
87 if (dim < a->dim) return ArrayError = ARRAY_NO_ERROR;
88
89 old_dim = a->dim;
90 while (dim >= a->dim) {
91 a->dim = a->dim * 1.2 + 1;
92 }
93
94 if ( (newbase = (void *)xrealloc(a->base, a->size * a->dim)) == NULL ) {
95 a->dim = old_dim;
96 return ArrayError = ARRAY_OUT_OF_MEMORY;
97 } else {
98 a->base = newbase;
99 }
100
101 return ArrayError = ARRAY_NO_ERROR;
102 }
103
104
105
106
107 void *ArrayRef(Array a, size_t i)
108 {
109 if (a==NULL) {
110 ArrayError = ARRAY_INVALID_ARGUMENTS;
111 return NULL;
112 }
113
114 if (i >= a->max) {
115 if (i >= a->dim) {
116 if (ArrayExtend(a,i+1)) {
117 /* ArrayExtend sets ArrayError */
118 return NULL;
119 }
120 }
121 a->max = i+1;
122 }
123
124 return (void *) arrp(char,a,i*a->size);
125 }
126
127
128
129 int ArrayDestroy(Array a)
130 /*
131 * destroy array
132 */
133 {
134 if (a==NULL) return ArrayError = ARRAY_INVALID_ARGUMENTS;
135
136 if (a->base != NULL) xfree(a->base);
137 a->base= NULL;
138 xfree(a);
139
140 return ArrayError = ARRAY_NO_ERROR;
141
142
143 }
144
145