home *** CD-ROM | disk | FTP | other *** search
-
- /*
- * Copyright (C) 1992-1993 Jeffrey Chilton
- *
- * Permission is granted to anyone to make or distribute copies of
- * this program, in any medium, provided that the copyright notice
- * and permission notice are preserved, and that the distributor
- * grants the recipient permission for further redistribution as
- * permitted by this notice.
- *
- * Author's E-mail address: 172-9221@mcimail.com
- *
- */
-
- static char *whatstring = "@(#)subjset.c 2.3 JWC";
-
- #include <stdio.h>
- #include <time.h>
-
- #include "class.h"
- #include "subjset.h"
- #include "random.h"
-
- extern Random *Squizzer;
-
- static int matchSubject();
-
- SubjectSet *
- SubjectSet_new()
- {
- SubjectSet *self;
-
- self = (SubjectSet *)malloc(sizeof (SubjectSet));
- if (!self)
- {
- fprintf(stderr, "SubjectSet_new: malloc fails\n");
- goto out;
- }
-
- self->allSubjects = Collection_new(matchSubject, Subject_destroy);
- if (!self->allSubjects)
- {
- free(self);
- self = (SubjectSet *)0;
- fprintf(stderr, "SubjectSet_new: Collection new fails\n");
- goto out;
- }
-
- out:
-
- return self;
-
- }
-
- static int
- matchSubject(subj, key)
- Subject *subj;
- char *key;
- {
- return strcmp(Subject_getText(subj), key);
- }
-
- void
- SubjectSet_addOne(self, subj)
- SubjectSet *self;
- Subject *subj;
- {
- Collection_add(self->allSubjects, (char *)subj);
-
- }
-
- Subject *
- SubjectSet_getOneByName(self, name)
- SubjectSet *self;
- char *name;
- {
- return (Subject *)Collection_atKeyGet(self->allSubjects, name);
- }
-
- Subject *
- SubjectSet_getOneAtRandom(self)
- SubjectSet *self;
- {
- register int n;
- Subject *subj;
-
- n = Random_long(Squizzer) % Collection_size(self->allSubjects);
- subj = (Subject *)Collection_atGet(self->allSubjects, n);
-
- return subj;
-
- }
-
- void
- SubjectSet_destroy(self)
- SubjectSet *self;
- {
-
- Collection_destroy(self->allSubjects);
- free(self);
-
- }
-
-