1/*========================================================================
2               Copyright (C) 1996-2001 by Jorn Lind-Nielsen
3                            All rights reserved
4
5    Permission is hereby granted, without written agreement and without
6    license or royalty fees, to use, reproduce, prepare derivative
7    works, distribute, and display this software and its documentation
8    for any purpose, provided that (1) the above copyright notice and
9    the following two paragraphs appear in all copies of the source code
10    and (2) redistributions, including without limitation binaries,
11    reproduce these notices in the supporting documentation. Substantial
12    modifications to this software may be copyrighted by their authors
13    and need not follow the licensing terms described here, provided
14    that the new terms are clearly indicated in all files where they apply.
15
16    IN NO EVENT SHALL JORN LIND-NIELSEN, OR DISTRIBUTORS OF THIS
17    SOFTWARE BE LIABLE TO ANY PARTY FOR DIRECT, INDIRECT, SPECIAL,
18    INCIDENTAL, OR CONSEQUENTIAL DAMAGES ARISING OUT OF THE USE OF THIS
19    SOFTWARE AND ITS DOCUMENTATION, EVEN IF THE AUTHORS OR ANY OF THE
20    ABOVE PARTIES HAVE BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
21
22    JORN LIND-NIELSEN SPECIFICALLY DISCLAIM ANY WARRANTIES, INCLUDING,
23    BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
24    FITNESS FOR A PARTICULAR PURPOSE. THE SOFTWARE PROVIDED HEREUNDER IS
25    ON AN "AS IS" BASIS, AND THE AUTHORS AND DISTRIBUTORS HAVE NO
26    OBLIGATION TO PROVIDE MAINTENANCE, SUPPORT, UPDATES, ENHANCEMENTS, OR
27    MODIFICATIONS.
28========================================================================*/
29
30/*************************************************************************
31  FILE:  imatrix.cc
32  DESCR: Interaction matrix
33  AUTH:  Jorn Lind
34  DATE:  (C) february 2000
35*************************************************************************/
36#include <stdlib.h>
37#include <assert.h>
38#include <string.h>
39#include "kernel.h"
40#include "imatrix.h"
41
42/*************************************************************************
43*************************************************************************/
44
45imatrix* imatrixNew(int size)
46{
47   imatrix *mtx = NEW(imatrix,1);
48   int n,m;
49
50   if (!mtx)
51      return NULL;
52
53   if ((mtx->rows=NEW(char*,size)) == NULL)
54   {
55      free(mtx);
56      return NULL;
57   }
58
59   for (n=0 ; n<size ; n++)
60   {
61      if ((mtx->rows[n]=NEW(char,size/8+1)) == NULL)
62      {
63	 for (m=0 ; m<n ; m++)
64	    free(mtx->rows[m]);
65	 free(mtx);
66	 return NULL;
67      }
68
69      memset(mtx->rows[n], 0, size/8+1);
70   }
71
72   mtx->size = size;
73
74   return mtx;
75}
76
77
78void imatrixDelete(imatrix *mtx)
79{
80   int n;
81
82   for (n=0 ; n<mtx->size ; n++)
83      free(mtx->rows[n]);
84   free(mtx->rows);
85   free(mtx);
86}
87
88
89/*======================================================================*/
90
91void imatrixFPrint(imatrix *mtx, FILE *ofile)
92{
93   int x,y;
94
95   fprintf(ofile, "    ");
96   for (x=0 ; x<mtx->size ; x++)
97      fprintf(ofile, "%c", x < 26 ? (x+'a') : (x-26)+'A');
98   fprintf(ofile, "\n");
99
100   for (y=0 ; y<mtx->size ; y++)
101   {
102      fprintf(ofile, "%2d %c", y, y < 26 ? (y+'a') : (y-26)+'A');
103      for (x=0 ; x<mtx->size ; x++)
104	 fprintf(ofile, "%c", imatrixDepends(mtx,y,x) ? 'x' : ' ');
105      fprintf(ofile, "\n");
106   }
107}
108
109
110void imatrixPrint(imatrix *mtx)
111{
112   imatrixFPrint(mtx, stdout);
113}
114
115
116void imatrixSet(imatrix *mtx, int a, int b)
117{
118   mtx->rows[a][b/8] |= 1<<(b%8);
119}
120
121
122void imatrixClr(imatrix *mtx, int a, int b)
123{
124   mtx->rows[a][b/8] &= ~(1<<(b%8));
125}
126
127
128int imatrixDepends(imatrix *mtx, int a, int b)
129{
130   return mtx->rows[a][b/8] & (1<<(b%8));
131}
132
133
134/*======================================================================*/
135
136#if 0
137void main(void)
138{
139   imatrix *m = imatrixNew(16);
140
141   imatrixSet(m,0,2);
142   imatrixSet(m,8,8);
143   imatrixSet(m,15,15);
144
145   imatrixPrint(m);
146}
147#endif
148
149/* EOF */
150