~mwshinn/+junk/neural

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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
/*----------------------------------------------------------------------------*
\Module{Dynamic Allocation of Arrays}

[...the grand discussion of dynamic arrays]
*/

#include <stdlib.h>
#include <stdio.h>
//#include <malloc.h>

#include "neural.h"
#include "protos.h"



/*----------------------------------------------------------------------------*
\Section{Allocate/Release Memory from Heap}

   This first routine is provided as a wrapper for the standard library
function 'calloc'.  If memory allocation should fail, a message is written to
the standard output device and the program will terminate.

\ENTRY: 'n' specifies the number of bytes to allocate.

\EXIT:  'Alloc' returns a pointer to 'n' bytes of contiguous memory, NOT
         guaranteed to be cleared to zero.  'Alloc' will never return null.
*/

void *Alloc (int n)
{
  void *p;

  Assert(n > 0);

  if (p = malloc(n))
    return p;

  else
//    FatalError("*** Error: Cannot allocate %d bytes ***\n", n);
    printf("*** Error: Cannot allocate %d bytes ***\n", n);
    exit(255);
}


/*----------------------------------------------------------------------------*
   This second routine is provided as a wrapper for the standard library
function 'free'.  This routine checks the memory pointer for null so that the
caller can be absolved of such remedial duties.

\ENTRY: 'p' points to a block of memory allocated earlier with 'Alloc'.

\EXIT:  'p' is undefined.
*/

void Release (void *p)
{
  Assert(p);

  free(p);
}



/*----------------------------------------------------------------------------*
\Section{Create/Destroy One-Dimensional Array}

   This routine allocates an array of a specified number of elements of a
specified element size.  The array is retured as a pointer into memory and
works on the principle that

\S x[i]

is shorthand for

\S *(x+i)

where 'x' is an array pointer and 'i' is an array index.

\ENTRY: 'di' specifies the length of the array.\n
        't' specifies the size in 'size_t' units (characters) of each array
        element.

\EXIT:  'CreateArray1' returns a pointer to the array.  Checking for null is
         unnecessary, since this routine is guaranteed to return a valid
         pointer.  Elements of the array are NOT cleared to zero.\n
*/

void *CreateArray1 (int di, size_t t)
{
  Assert(di > 0);
  Assert(t  > 0);

  return Alloc(di * t);
}


/*----------------------------------------------------------------------------*
This routine destroys arrays allocated by 'CreateArray1'.

\ENTRY: 'a' specifies an array.

\EXIT:  'a' is undefined.
*/

void DestroyArray1 (void *a)
{
  Assert(a);

  Release(a);
}



/*----------------------------------------------------------------------------*
\Section{Create/Destroy Two-Dimensional Array}

   This routine allocates an array of a specified height and width and of a
specified element size.  The array is returned as a pointer into memory and
works on the principle that

\S x[i][j]

is shorthand for

\S *(*(x+i)+j)

where x is an array pointer and i and j are row and column indices,
respectively.

   The array is allocated as a single contiguous block of memory with the first
portion comprising a small array of pointers and the second portion comprising
all the array elements:

\R ____    ________________________
\S  0 |-> |   |   |   |   |   |   |
\R ____    ________________________
\S  1 |-> |   |   |   |   |   |   |
\R ____    ________________________
\S  2 |-> |   |   |   |   |   |   |
\R ____    ________________________
\S    |   |   |   |   |   |   |   |

\ENTRY: 'di' specifies the length of the major portion of the array.\n
        'dj' specifies the length of the minor portion of the array.\n
        't' specifies the size in 'size_t' units (characters) of each array
        element.

\EXIT:  'CreateArray2' returns a pointer to the array.  Checking for null is
         unnecessary, since this routine is guaranteed to return a valid
         pointer.  Elements of the array are NOT cleared to zero.\n
*/

void **CreateArray2 (int di, int dj, size_t t)
{
  int n, m, i;
  char **a, *b;

  Assert(di > 0);
  Assert(dj > 0);
  Assert(t  > 0);

  m = di * sizeof(char *);                   // Calculate the size of each row
  n = dj * t;                                // and the size of the pointer
                                             // section.

  a = (char **)Alloc(m + di*n);              // Allocate the entire array as a
                                             // single contiguous block.

  b = (char *)a + m;
  for (i = 0; i < di; i++)                   // Thread the row pointers.
  {
    a[i] = b;
    b += n;
  }

  return (void *) a;                         // Return the completed array.
}


/*----------------------------------------------------------------------------*
This routine destroys arrays allocated by 'CreateArray2'.

\ENTRY: 'a' specifies an array.

\EXIT:  'a' is undefined.
*/

void DestroyArray2 (void *a)
{
  Assert(a);

  Release(a);
}


/*----------------------------------------------------------------------------*
TODD S. LEHMAN, DECEMBER 1992.








##############################################################################
                          OBSOLETE CODE FOLLOWS
##############################################################################

   For convenience the height $\delta i$ and width $\delta j$ of
two-dimensional arrays are stored in memory immediately preceding the array
data itself.  The macros 'DI' and 'DJ' access these data.

\S for (i = 0; i < DI(x); i++)
\S for (j = 0; j < DJ(x); j++)
     (operation involving 'x[i][j]')

The macros can be used on l-values as well:

\S DI(x) = di;
\S DJ(x) = dj;

In the one-dimensional case, only 'DI' is used.

One-dimensional arrays:

\ENTRY:  'x' points to an array.

\EXIT:   'DI(x)' is the height of the array.

Two-dimensional arrays:

\ENTRY:  'x' points to an array.

\EXIT:   'DI(x)' is the height of the array.
         'DJ(x)' is the width of the array.
*/

#define  DI(x)  (((int *)(x))[-1])
#define  DJ(x)  (((int *)(x))[-2])