1:
  2:/*
  3: * $Id: relation.c,v 1.1 2002/10/15 10:06:48 koschke Exp $
  4: *
  5: * CONCEPTS
  6: * Copyright (C) 1994 Technical University of Braunschweig, Germany
  7: * Written by Christian Lindig (lindig@ips.cs.tu-bs.de)
  8: * 
  9: * This program is free software; you can redistribute it and/or modify
 10: * it under the terms of the GNU General Public License as published by
 11: * the Free Software Foundation; either version 2 of the License, or
 12: * (at your option) any later version.
 13: * 
 14: * This program is distributed in the hope that it will be useful,
 15: * but WITHOUT ANY WARRANTY; without even the implied warranty of
 16: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 17: * GNU General Public License for more details.
 18: * 
 19: * You should have received a copy of the GNU General Public License
 20: * along with this program; if not, write to the Free Software
 21: * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
 22: *
 23: *
 24: * Relation between objects and attributes, both represented by
 25: * integers but with diffrent maximum values */
 26:
 27:#include "config.h"
 28:#include <stdio.h>
 29:#if defined(STDC_HEADERS) || defined(HAVE_STDLIB_H)
 30:#  include <stdlib.h>
 31:#endif
 32:
 33:#ifdef DBMALLOC
 34:#  include <malloc.h>
 35:#endif
 36:
 37:#include "defines.h"
 38:#include "panic.h"
 39:#include "set.h"
 40:#include "relation.h"
 41:
 42:/* requires: SetAdd SetDelete SetMember SetNew SetPrint
 43:   SetRemove free malloc panic printf */
 44:
 45:/*
 46: * RelNew
 47: *
 48: * create a relation between [0..<atr>] x [0..<obj>] and initialize
 49: * it to the empty relation */
 50:
 51:VOID
 52:RelNew (rel,atr,obj) 
 53:
 54:	Relation *rel;
 55:	int atr,obj;
 56:
 57:{
 58:	int i;
 59:
 60:	rel->maxatr = atr ;
 61:	rel->maxobj = obj ;
 62:
 63:	rel->atr = (Set*) malloc (sizeof(Set) * atr) ;
 64:	if (rel->atr == NULL) {
 65:		panic ("malloc failed in %s:%i",__FILE__,__LINE__) ;
 66:	}
 67:	rel->obj = (Set*) malloc (sizeof(Set) * obj) ;
 68:	if (rel->obj == NULL) {
 69:		panic ("malloc failed in %s:%i",__FILE__,__LINE__) ;
 70:	}
 71:	for (i=0;i<obj;i++) {
 72:		SetNew(&rel->obj[i],atr);
 73:	}
 74:	for (i=0;i<atr;i++) {
 75:		SetNew(&rel->atr[i],obj);
 76:	}
 77:}
 78:
 79:/*
 80: * RelDelete
 81: *
 82: * free all memory associated to a Relation - does not free the
 83: * Relation struct itself */
 84:
 85:VOID 
 86:RelDelete (rel) 
 87:	
 88:	Relation *rel;
 89:{
 90:	int i,obj,atr ;
 91:
 92:	/* delete the sets */
 93:
 94:	atr = rel->maxatr ;
 95:	obj = rel->maxobj ;
 96:
 97:	for (i=0;i<obj;i++) {
 98:		SetDelete(&rel->obj[i]);
 99:	}
100:
101:	for (i=0;i<atr;i++) {
102:		SetDelete(&rel->atr[i]);
103:	}
104:
105:	/* free the array containing the pointers to the sets */
106:
107:	free((char*)rel->atr);
108:	free((char*)rel->obj);
109:	
110:	rel->atr = NULL ;
111:	rel->obj = NULL ;
112:	rel->maxatr = 0 ;
113:	rel->maxobj = 0 ;
114:}
115:
116:
117:/*
118: * RelRelate
119: *
120: * enter a relation (<atr>,<obj>) to a given relation */
121: 
122:VOID
123:RelRelate (rel, atr, obj) 
124:
125:	Relation *rel;
126:	int atr,obj;
127:{
128:
129:	if ( (atr >= rel->maxatr) || (obj >= rel->maxobj)) {
130:		panic("Size mismatch in %s:%i",__FILE__,__LINE__) ;
131:	}
132:	/* enter (atr,obj) in both Set arrays */
133:	SetAdd(&rel->obj[obj],atr);
134:	SetAdd(&rel->atr[atr],obj);
135:}
136:
137:/*
138: * RelUnrelate
139: *
140: * remove a (<atr>,<obj>) from a relation (regardless if it exists) */
141:
142:
143:VOID
144:RelUnrelate (rel, atr, obj) 
145:
146:	Relation *rel;
147:	int atr,obj;
148: {
149:	if ( (atr >= rel->maxatr) || (obj >= rel->maxobj)) {
150:		panic("Size mismatch in %s:%i",__FILE__,__LINE__) ;
151:	}
152:	SetRemove(&rel->obj[obj],atr);
153:	SetRemove(&rel->atr[atr],obj);
154:}
155:
156:/*
157: * RelRelated check if (<atr>,<obj>) are related */
158:
159:int 
160:RelRelated (rel, atr, obj) 
161:
162:	Relation *rel;
163:	int atr,obj;
164:
165:{
166:	return (SetMember(&rel->obj[obj],atr)) ;
167:}
168:
169:
170:/*
171: * RelObj
172: *
173: * return a pointer to a set containing all <atr> which are related
174: * to <obj>  */
175:
176:Set *
177:RelObj (rel,obj)
178:
179:	Relation *rel;
180:	int obj ;
181:{
182:	if (obj >= rel->maxobj) {
183:		panic ("Can't find object %i within %i objects: %s:%i",
184:		       obj,rel->maxobj,__FILE__,__LINE__) ;
185:	}
186:
187:	return (&rel->obj[obj]);
188:}
189:
190:/*
191: * RelAtr
192: *
193: * return a pointer to a set containing all <obj> which are related
194: * to <atr>  */
195:
196:Set *
197:RelAtr (rel,atr)
198:
199:	Relation *rel;
200:	int atr ;
201:{
202:
203:	if (atr >= rel->maxatr) {
204:		panic ("Can't find atribute %i within %i atributes: %s:%i",
205:		       atr,rel->maxatr,__FILE__,__LINE__) ;
206:	}
207:
208:	return (&rel->atr[atr]);
209:}
210:
211:
212:/*
213: * RelPrint
214: *
215: * prints out a relation - just for debugging */
216:
217:VOID
218:RelPrint (rel) 
219:	
220:	Relation *rel;
221:{
222:	int r,c;
223:
224:	printf ("atr: %i\n",rel->maxatr);
225:	printf ("obj: %i\n",rel->maxobj);
226:
227:	for (r=0;r<rel->maxatr;r++) {
228:		printf ("%i: ",r);
229:		SetPrint(&rel->atr[r]);
230:		printf ("\n");
231:	}
232:	printf ("\n\n");
233:	for (c=0;c<rel->maxobj;c++) {
234:		printf ("%i: ",c);
235:		SetPrint(&rel->obj[c]);
236:		printf ("\n");
237:	}
238:}



back ...