NEURON
ivecop.c
Go to the documentation of this file.
1 #include <../../nrnconf.h>
2 
3 /**************************************************************************
4 **
5 ** Copyright (C) 1993 David E. Steward & Zbigniew Leyk, all rights reserved.
6 **
7 ** Meschach Library
8 **
9 ** This Meschach Library is provided "as is" without any express
10 ** or implied warranty of any kind with respect to this software.
11 ** In particular the authors shall not be liable for any direct,
12 ** indirect, special, incidental or consequential damages arising
13 ** in any way from use of the software.
14 **
15 ** Everyone is granted permission to copy, modify and redistribute this
16 ** Meschach Library, provided:
17 ** 1. All copies contain this copyright notice.
18 ** 2. All modified copies shall carry a notice stating who
19 ** made the last modification and the date of such modification.
20 ** 3. No charge is made for this software or works derived from it.
21 ** This clause shall not be construed as constraining other software
22 ** distributed on the same medium as this software, nor is a
23 ** distribution fee considered a charge.
24 **
25 ***************************************************************************/
26 
27 
28 /* ivecop.c */
29 
30 #include <stdio.h>
31 #include "matrix.h"
32 
33 static char rcsid[] = "ivecop.c,v 1.1 1997/12/04 17:55:30 hines Exp";
34 
35 static char line[MAXLINE];
36 
37 
38 
39 /* iv_get -- get integer vector -- see also memory.c */
40 IVEC *iv_get(dim)
41 int dim;
42 {
43  IVEC *iv;
44  /* u_int i; */
45 
46  if (dim < 0)
47  error(E_NEG,"iv_get");
48 
49  if ((iv=NEW(IVEC)) == IVNULL )
50  error(E_MEM,"iv_get");
51  else if (mem_info_is_on()) {
52  mem_bytes(TYPE_IVEC,0,sizeof(IVEC));
54  }
55 
56  iv->dim = iv->max_dim = dim;
57  if ((iv->ive = NEW_A(dim,int)) == (int *)NULL )
58  error(E_MEM,"iv_get");
59  else if (mem_info_is_on()) {
60  mem_bytes(TYPE_IVEC,0,dim*sizeof(int));
61  }
62 
63  return (iv);
64 }
65 
66 /* iv_free -- returns iv & asoociated memory back to memory heap */
67 int iv_free(iv)
68 IVEC *iv;
69 {
70  if ( iv==IVNULL || iv->dim > MAXDIM )
71  /* don't trust it */
72  return (-1);
73 
74  if ( iv->ive == (int *)NULL ) {
75  if (mem_info_is_on()) {
76  mem_bytes(TYPE_IVEC,sizeof(IVEC),0);
78  }
79  free((char *)iv);
80  }
81  else
82  {
83  if (mem_info_is_on()) {
84  mem_bytes(TYPE_IVEC,sizeof(IVEC)+iv->max_dim*sizeof(int),0);
86  }
87  free((char *)iv->ive);
88  free((char *)iv);
89  }
90 
91  return (0);
92 }
93 
94 /* iv_resize -- returns the IVEC with dimension new_dim
95  -- iv is set to the zero vector */
96 IVEC *iv_resize(iv,new_dim)
97 IVEC *iv;
98 int new_dim;
99 {
100  int i;
101 
102  if (new_dim < 0)
103  error(E_NEG,"iv_resize");
104 
105  if ( ! iv )
106  return iv_get(new_dim);
107 
108  if (new_dim == iv->dim)
109  return iv;
110 
111  if ( new_dim > iv->max_dim )
112  {
113  if (mem_info_is_on()) {
114  mem_bytes(TYPE_IVEC,iv->max_dim*sizeof(int),
115  new_dim*sizeof(int));
116  }
117  iv->ive = RENEW(iv->ive,new_dim,int);
118  if ( ! iv->ive )
119  error(E_MEM,"iv_resize");
120  iv->max_dim = new_dim;
121  }
122  if ( iv->dim <= new_dim )
123  for ( i = iv->dim; i < new_dim; i++ )
124  iv->ive[i] = 0;
125  iv->dim = new_dim;
126 
127  return iv;
128 }
129 
130 /* iv_copy -- copy integer vector in to out
131  -- out created/resized if necessary */
132 IVEC *iv_copy(in,out)
133 IVEC *in, *out;
134 {
135  int i;
136 
137  if ( ! in )
138  error(E_NULL,"iv_copy");
139  out = iv_resize(out,in->dim);
140  for ( i = 0; i < in->dim; i++ )
141  out->ive[i] = in->ive[i];
142 
143  return out;
144 }
145 
146 /* iv_move -- move selected pieces of an IVEC
147  -- moves the length dim0 subvector with initial index i0
148  to the corresponding subvector of out with initial index i1
149  -- out is resized if necessary */
150 IVEC *iv_move(in,i0,dim0,out,i1)
151 IVEC *in, *out;
152 int i0, dim0, i1;
153 {
154  if ( ! in )
155  error(E_NULL,"iv_move");
156  if ( i0 < 0 || dim0 < 0 || i1 < 0 ||
157  i0+dim0 > in->dim )
158  error(E_BOUNDS,"iv_move");
159 
160  if ( (! out) || i1+dim0 > out->dim )
161  out = iv_resize(out,i1+dim0);
162 
163  MEM_COPY(&(in->ive[i0]),&(out->ive[i1]),dim0*sizeof(int));
164 
165  return out;
166 }
167 
168 /* iv_add -- integer vector addition -- may be in-situ */
169 IVEC *iv_add(iv1,iv2,out)
170 IVEC *iv1,*iv2,*out;
171 {
172  u_int i;
173  int *out_ive, *iv1_ive, *iv2_ive;
174 
175  if ( iv1==IVNULL || iv2==IVNULL )
176  error(E_NULL,"iv_add");
177  if ( iv1->dim != iv2->dim )
178  error(E_SIZES,"iv_add");
179  if ( out==IVNULL || out->dim != iv1->dim )
180  out = iv_resize(out,iv1->dim);
181 
182  out_ive = out->ive;
183  iv1_ive = iv1->ive;
184  iv2_ive = iv2->ive;
185 
186  for ( i = 0; i < iv1->dim; i++ )
187  out_ive[i] = iv1_ive[i] + iv2_ive[i];
188 
189  return (out);
190 }
191 
192 
193 
194 /* iv_sub -- integer vector addition -- may be in-situ */
195 IVEC *iv_sub(iv1,iv2,out)
196 IVEC *iv1,*iv2,*out;
197 {
198  u_int i;
199  int *out_ive, *iv1_ive, *iv2_ive;
200 
201  if ( iv1==IVNULL || iv2==IVNULL )
202  error(E_NULL,"iv_sub");
203  if ( iv1->dim != iv2->dim )
204  error(E_SIZES,"iv_sub");
205  if ( out==IVNULL || out->dim != iv1->dim )
206  out = iv_resize(out,iv1->dim);
207 
208  out_ive = out->ive;
209  iv1_ive = iv1->ive;
210  iv2_ive = iv2->ive;
211 
212  for ( i = 0; i < iv1->dim; i++ )
213  out_ive[i] = iv1_ive[i] - iv2_ive[i];
214 
215  return (out);
216 }
217 
218 /* iv_foutput -- print a representation of iv on stream fp */
219 void iv_foutput(fp,iv)
220 FILE *fp;
221 IVEC *iv;
222 {
223  int i;
224 
225  fprintf(fp,"IntVector: ");
226  if ( iv == IVNULL )
227  {
228  fprintf(fp,"**** NULL ****\n");
229  return;
230  }
231  fprintf(fp,"dim: %d\n",iv->dim);
232  for ( i = 0; i < iv->dim; i++ )
233  {
234  if ( (i+1) % 8 )
235  fprintf(fp,"%8d ",iv->ive[i]);
236  else
237  fprintf(fp,"%8d\n",iv->ive[i]);
238  }
239  if ( i % 8 )
240  fprintf(fp,"\n");
241 }
242 
243 
244 /* iv_finput -- input integer vector from stream fp */
246 FILE *fp;
247 IVEC *x;
248 {
249  IVEC *iiv_finput(),*biv_finput();
250 
251  if ( isatty(fileno(fp)) )
252  return iiv_finput(fp,x);
253  else
254  return biv_finput(fp,x);
255 }
256 
257 /* iiv_finput -- interactive input of IVEC iv */
259 FILE *fp;
260 IVEC *iv;
261 {
262  u_int i,dim,dynamic; /* dynamic set if memory allocated here */
263 
264  /* get dimension */
265  if ( iv != (IVEC *)NULL && iv->dim<MAXDIM )
266  { dim = iv->dim; dynamic = FALSE; }
267  else
268  {
269  dynamic = TRUE;
270  do
271  {
272  fprintf(stderr,"IntVector: dim: ");
273  if ( fgets(line,MAXLINE,fp)==NULL )
274  error(E_INPUT,"iiv_finput");
275  } while ( sscanf(line,"%u",&dim)<1 || dim>MAXDIM );
276  iv = iv_get(dim);
277  }
278 
279  /* input elements */
280  for ( i=0; i<dim; i++ )
281  do
282  {
283  redo:
284  fprintf(stderr,"entry %u: ",i);
285  if ( !dynamic )
286  fprintf(stderr,"old: %-9d new: ",iv->ive[i]);
287  if ( fgets(line,MAXLINE,fp)==NULL )
288  error(E_INPUT,"iiv_finput");
289  if ( (*line == 'b' || *line == 'B') && i > 0 )
290  { i--; dynamic = FALSE; goto redo; }
291  if ( (*line == 'f' || *line == 'F') && i < dim-1 )
292  { i++; dynamic = FALSE; goto redo; }
293  } while ( *line=='\0' || sscanf(line,"%d",&iv->ive[i]) < 1 );
294 
295  return (iv);
296 }
297 
298 /* biv_finput -- batch-file input of IVEC iv */
300 FILE *fp;
301 IVEC *iv;
302 {
303  u_int i,dim;
304  int io_code;
305 
306  /* get dimension */
307  skipjunk(fp);
308  if ((io_code=fscanf(fp," IntVector: dim:%u",&dim)) < 1 ||
309  dim>MAXDIM )
310  error(io_code==EOF ? 7 : 6,"biv_finput");
311 
312  /* allocate memory if necessary */
313  if ( iv==(IVEC *)NULL || iv->dim<dim )
314  iv = iv_resize(iv,dim);
315 
316  /* get entries */
317  skipjunk(fp);
318  for ( i=0; i<dim; i++ )
319  if ((io_code=fscanf(fp,"%d",&iv->ive[i])) < 1 )
320  error(io_code==EOF ? 7 : 6,"biv_finput");
321 
322  return (iv);
323 }
324 
325 /* iv_dump -- dumps all the contents of IVEC iv onto stream fp */
326 void iv_dump(fp,iv)
327 FILE*fp;
328 IVEC*iv;
329 {
330  int i;
331 
332  fprintf(fp,"IntVector: ");
333  if ( ! iv )
334  {
335  fprintf(fp,"**** NULL ****\n");
336  return;
337  }
338  fprintf(fp,"dim: %d, max_dim: %d\n",iv->dim,iv->max_dim);
339  fprintf(fp,"ive @ 0x%p\n", iv->ive);
340  for ( i = 0; i < iv->max_dim; i++ )
341  {
342  if ( (i+1) % 8 )
343  fprintf(fp,"%8d ",iv->ive[i]);
344  else
345  fprintf(fp,"%8d\n",iv->ive[i]);
346  }
347  if ( i % 8 )
348  fprintf(fp,"\n");
349 }
350 
351 #define MAX_STACK 60
352 
353 
354 /* iv_sort -- sorts vector x, and generates permutation that gives the order
355  of the components; x = [1.3, 3.7, 0.5] -> [0.5, 1.3, 3.7] and
356  the permutation is order = [2, 0, 1].
357  -- if order is NULL on entry then it is ignored
358  -- the sorted vector x is returned */
360 IVEC *x;
361 PERM *order;
362 {
363  int *x_ive, tmp, v;
364  /* int *order_pe; */
365  int dim, i, j, l, r, tmp_i;
366  int stack[MAX_STACK], sp;
367 
368  if ( ! x )
369  error(E_NULL,"v_sort");
370  if ( order != PNULL && order->size != x->dim )
371  order = px_resize(order, x->dim);
372 
373  x_ive = x->ive;
374  dim = x->dim;
375  if ( order != PNULL )
376  px_ident(order);
377 
378  if ( dim <= 1 )
379  return x;
380 
381  /* using quicksort algorithm in Sedgewick,
382  "Algorithms in C", Ch. 9, pp. 118--122 (1990) */
383  sp = 0;
384  l = 0; r = dim-1; v = x_ive[0];
385  for ( ; ; )
386  {
387  while ( r > l )
388  {
389  /* "i = partition(x_ive,l,r);" */
390  v = x_ive[r];
391  i = l-1;
392  j = r;
393  for ( ; ; )
394  {
395  while ( x_ive[++i] < v )
396  ;
397  --j;
398  while ( x_ive[j] > v && j != 0 )
399  --j;
400  if ( i >= j ) break;
401 
402  tmp = x_ive[i];
403  x_ive[i] = x_ive[j];
404  x_ive[j] = tmp;
405  if ( order != PNULL )
406  {
407  tmp_i = order->pe[i];
408  order->pe[i] = order->pe[j];
409  order->pe[j] = tmp_i;
410  }
411  }
412  tmp = x_ive[i];
413  x_ive[i] = x_ive[r];
414  x_ive[r] = tmp;
415  if ( order != PNULL )
416  {
417  tmp_i = order->pe[i];
418  order->pe[i] = order->pe[r];
419  order->pe[r] = tmp_i;
420  }
421 
422  if ( i-l > r-i )
423  { stack[sp++] = l; stack[sp++] = i-1; l = i+1; }
424  else
425  { stack[sp++] = i+1; stack[sp++] = r; r = i-1; }
426  }
427 
428  /* recursion elimination */
429  if ( sp == 0 )
430  break;
431  r = stack[--sp];
432  l = stack[--sp];
433  }
434 
435  return x;
436 }
IVEC * iv_add(IVEC *iv1, IVEC *iv2, IVEC *out)
Definition: ivecop.c:169
#define PNULL
Definition: matrix.h:633
#define MAX_STACK
Definition: ivecop.c:351
u_int max_dim
Definition: matrix.h:93
void iv_dump(FILE *fp, IVEC *iv)
Definition: ivecop.c:326
order
Definition: multicore.cpp:886
IVEC * iiv_finput(FILE *fp, IVEC *iv)
Definition: ivecop.c:258
#define E_INPUT
Definition: err.h:101
#define E_NEG
Definition: err.h:114
IVEC * iv_resize(IVEC *iv, int new_dim)
Definition: ivecop.c:96
IVEC * iv_sub(IVEC *iv1, IVEC *iv2, IVEC *out)
Definition: ivecop.c:195
#define NEW_A(num, type)
Definition: matrix.h:139
#define TRUE
Definition: err.c:57
#define TYPE_IVEC
Definition: meminfo.h:53
#define v
Definition: md1redef.h:4
u_int dim
Definition: matrix.h:93
#define stack
Definition: code.cpp:128
IVEC * iv_copy(IVEC *in, IVEC *out)
Definition: ivecop.c:132
int mem_info_is_on(void)
Definition: meminfo.c:221
void iv_foutput(FILE *fp, IVEC *iv)
Definition: ivecop.c:219
IVEC * iv_sort(IVEC *x, PERM *order)
Definition: ivecop.c:359
static Frame * fp
Definition: code.cpp:154
u_int size
Definition: matrix.h:88
#define E_SIZES
Definition: err.h:95
uint32_t u_int
Definition: machine.h:38
static char rcsid[]
Definition: ivecop.c:33
PERM * px_resize(PERM *, int)
Definition: memory.c:399
#define mem_bytes(type, old_size, new_size)
Definition: meminfo.h:136
#define E_BOUNDS
Definition: err.h:96
IVEC * iv_finput(FILE *fp, IVEC *x)
Definition: ivecop.c:245
void MEM_COPY(char *from, char *to, int len)
Definition: extras.c:37
int * ive
Definition: matrix.h:94
#define E_NULL
Definition: err.h:102
size_t j
fprintf(stderr, "Don't know the location of params at %p\, pp)
int iv_free(IVEC *iv)
Definition: ivecop.c:67
Definition: matrix.h:92
#define FALSE
Definition: err.c:56
#define i
Definition: md1redef.h:12
#define IVNULL
Definition: matrix.h:634
static char line[MAXLINE]
Definition: ivecop.c:35
#define error(err_num, fn_name)
Definition: err.h:73
#define MAXDIM
Definition: matrix.h:218
IVEC * iv_get(int dim)
Definition: ivecop.c:40
#define mem_numvar(type, num)
Definition: meminfo.h:139
VEC * x
Definition: iter.h:64
int skipjunk(FILE *fp)
Definition: matrixio.c:47
IVEC * biv_finput(FILE *fp, IVEC *iv)
Definition: ivecop.c:299
#define MAXLINE
Definition: matrix.h:168
Definition: matrix.h:87
PERM * px_ident(PERM *px)
Definition: init.c:108
#define RENEW(var, num, type)
Definition: matrix.h:142
return NULL
Definition: cabcode.cpp:461
#define NEW(type)
Definition: matrix.h:136
u_int * pe
Definition: matrix.h:88
#define E_MEM
Definition: err.h:97
IVEC * iv_move(IVEC *in, int i0, int dim0, IVEC *out, int i1)
Definition: ivecop.c:150