home *** CD-ROM | disk | FTP | other *** search
/ Usenet 1994 January / usenetsourcesnewsgroupsinfomagicjanuary1994.iso / sources / misc / volume36 / formes / part01 / subjset.c < prev    next >
Encoding:
C/C++ Source or Header  |  1993-04-01  |  1.8 KB  |  104 lines

  1.  
  2. /*
  3.  *  Copyright (C) 1992-1993 Jeffrey Chilton
  4.  *
  5.  *  Permission is granted to anyone to make or distribute copies of
  6.  *  this program, in any medium, provided that the copyright notice
  7.  *  and permission notice are preserved, and that the distributor
  8.  *  grants the recipient permission for further redistribution as
  9.  *  permitted by this notice.
  10.  *  
  11.  *  Author's E-mail address:  172-9221@mcimail.com
  12.  *  
  13.  */
  14.  
  15. static char *whatstring = "@(#)subjset.c    2.3 JWC";
  16.  
  17. #include <stdio.h>
  18. #include <time.h>
  19.  
  20. #include "class.h"
  21. #include "subjset.h"
  22. #include "random.h"
  23.  
  24. extern Random *Squizzer;
  25.  
  26. static int matchSubject();
  27.  
  28. SubjectSet *
  29. SubjectSet_new()
  30. {
  31.     SubjectSet *self;
  32.  
  33.     self = (SubjectSet *)malloc(sizeof (SubjectSet));
  34.     if (!self)
  35.     {
  36.     fprintf(stderr, "SubjectSet_new: malloc fails\n");
  37.     goto out;
  38.     }
  39.  
  40.     self->allSubjects = Collection_new(matchSubject, Subject_destroy);
  41.     if (!self->allSubjects)
  42.     {
  43.     free(self);
  44.     self = (SubjectSet *)0;
  45.     fprintf(stderr, "SubjectSet_new: Collection new fails\n");
  46.     goto out;
  47.     }
  48.  
  49. out:
  50.  
  51.     return self;
  52.  
  53. }
  54.  
  55. static int
  56. matchSubject(subj, key)
  57. Subject *subj;
  58. char *key;
  59. {
  60.     return strcmp(Subject_getText(subj), key);
  61. }
  62.  
  63. void
  64. SubjectSet_addOne(self, subj)
  65. SubjectSet *self;
  66. Subject *subj;
  67. {
  68.     Collection_add(self->allSubjects, (char *)subj);
  69.  
  70. }
  71.  
  72. Subject *
  73. SubjectSet_getOneByName(self, name)
  74. SubjectSet *self;
  75. char *name;
  76. {
  77.     return (Subject *)Collection_atKeyGet(self->allSubjects, name);
  78. }
  79.  
  80. Subject *
  81. SubjectSet_getOneAtRandom(self)
  82. SubjectSet *self;
  83. {
  84.     register int n;
  85.     Subject *subj;
  86.  
  87.     n = Random_long(Squizzer) % Collection_size(self->allSubjects);
  88.     subj = (Subject *)Collection_atGet(self->allSubjects, n);
  89.  
  90.     return subj;
  91.  
  92. }
  93.  
  94. void
  95. SubjectSet_destroy(self)
  96. SubjectSet *self;
  97. {
  98.  
  99.     Collection_destroy(self->allSubjects);
  100.     free(self);
  101.  
  102. }
  103.  
  104.