2 * Copyright © 2006 Carl Worth
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; either version 2, or (at your option)
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the Free Software Foundation,
16 * Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA."
27 #include <sys/types.h>
33 #include <readline/readline.h>
34 #include <readline/history.h>
38 typedef struct _item {
52 CATEGORY_ORDER_RANDOM,
53 CATEGORY_ORDER_SEQUENTIAL
56 typedef struct _category {
62 /* Support sequential introduction of items from bin 0 */
63 category_order_t order;
67 typedef struct _mnemon {
72 category_t *categories;
91 fprintf (stderr, "Error: out of memory\n");
99 xrealloc (void *ptr, size_t size)
103 ret = realloc (ptr, size);
105 fprintf (stderr, "Error: out of memory\n");
113 xstrdup (const char *s)
119 fprintf (stderr, "Error: out of memory\n");
127 xasprintf (char **strp, const char *fmt, ...)
133 ret = vasprintf (strp, fmt, ap);
137 fprintf (stderr, "Error: out of memory\n");
143 item_init (item_t *item,
145 const char *challenge,
146 const char *response)
150 item->challenge = xmalloc (strlen (challenge) + 1 +
151 strlen (response) + 1);
152 item->response = item->challenge + strlen (challenge) + 1;
154 strcpy (item->challenge, challenge);
155 strcpy (item->response, response);
159 item_fini (item_t *item)
161 /* item->response shares allocation with item->challenge, so
162 * doesn't require a separate call to free */
163 free (item->challenge);
167 category_init (category_t *category,
170 category->name = xstrdup (name);
172 category->items_size = 0;
173 category->num_items = 0;
174 category->items = NULL;
175 category->order = CATEGORY_ORDER_RANDOM;
176 category->bin_zero_head = 0;
180 category_fini (category_t *category)
184 for (i = 0; i < category->num_items; i++)
185 item_fini (&category->items[i]);
187 free (category->items);
189 free (category->name);
193 category_grow (category_t *category)
195 if (category->items_size)
196 category->items_size *= 2;
198 category->items_size = 1;
200 category->items = xrealloc (category->items,
201 category->items_size * sizeof (item_t));
205 category_add_item (category_t *category,
207 const char *challenge,
208 const char *response)
212 if (category->num_items == category->items_size)
213 category_grow (category);
215 item = &category->items[category->num_items++];
217 item_init (item, score, challenge, response);
223 category_next_bin_zero_item (category_t *category)
225 int *i = &category->bin_zero_head;
227 for ( ; *i < category->num_items; *i = *i + 1)
228 if (category->items[*i].score == 0)
229 return &category->items[*i];
235 category_print (category_t *category,
241 fprintf (file, "order = %s\n\n",
242 category->order == CATEGORY_ORDER_RANDOM ? "random" : "sequential");
244 for (i = 0; i < category->num_items; i++) {
245 item = &category->items[i];
247 fprintf (file, "\n");
248 fprintf (file, "%d\n%s\n%s\n",
256 bin_init (bin_t *bin,
267 bin_fini (bin_t *bin)
273 bin_grow (bin_t *bin)
276 bin->items_size *= 2;
280 bin->items = xrealloc (bin->items,
281 bin->items_size * sizeof (item_t*));
285 bin_add_item (bin_t *bin,
288 assert (item->score == bin->score);
290 if (bin->num_items == bin->items_size)
293 bin->items[bin->num_items++] = item;
297 bin_remove_item (bin_t *bin,
300 /* Replace the current item with the last item, (no need to shift
301 * any more than that since we don't care about the order of the
302 * items within a bin). */
305 bin->items[item_index] = bin->items[bin->num_items];
308 /* Find the index for an item within a bin.
310 * XXX: This is currently a linear search, so is a potential
311 * performance problem.
314 bin_item_index (bin_t *bin,
319 for (i = 0; i < bin->num_items; i++)
320 if (bin->items[i] == item)
327 mnemon_init (mnemon_t *mnemon)
331 home = getenv ("HOME");
335 xasprintf (&mnemon->dir_name, "%s/.mnemon", getenv ("HOME"));
337 mnemon->categories_size = 0;
338 mnemon->num_categories = 0;
339 mnemon->categories = NULL;
341 mnemon->bins_size = 0;
342 mnemon->num_bins = 0;
345 mnemon->to_introduce = 3;
346 mnemon->to_master = 0;
347 mnemon->unlearned = 0;
348 mnemon->mastered = -1;
352 mnemon_fini (mnemon_t *mnemon)
356 for (i = 0; i < mnemon->num_bins; i++)
357 bin_fini (&mnemon->bins[i]);
360 for (i = 0; i < mnemon->num_categories; i++)
361 category_fini (&mnemon->categories[i]);
362 free (mnemon->categories);
364 free (mnemon->dir_name);
368 mnemon_categories_grow (mnemon_t *mnemon)
370 if (mnemon->categories_size)
371 mnemon->categories_size *= 2;
373 mnemon->categories_size = 1;
375 mnemon->categories = xrealloc (mnemon->categories,
376 mnemon->categories_size * sizeof (category_t));
380 mnemon_get_category (mnemon_t *mnemon,
384 category_t *category;
386 for (i = 0; i < mnemon->num_categories; i++)
387 if (strcmp (mnemon->categories[i].name, name) == 0)
388 return &mnemon->categories[i];
390 mnemon_categories_grow (mnemon);
392 category = &mnemon->categories[mnemon->num_categories++];
394 category_init (category, name);
400 mnemon_bins_grow (mnemon_t *mnemon)
402 if (mnemon->bins_size)
403 mnemon->bins_size *= 2;
405 mnemon->bins_size = 1;
407 mnemon->bins = xrealloc (mnemon->bins,
408 mnemon->bins_size * sizeof (bin_t));
412 mnemon_get_bin (mnemon_t *mnemon,
418 for (i = 0; i < mnemon->num_bins; i++)
419 if (mnemon->bins[i].score == score)
420 return &mnemon->bins[i];
421 else if (mnemon->bins[i].score > score)
424 if (mnemon->num_bins == mnemon->bins_size)
425 mnemon_bins_grow (mnemon);
427 bin = &mnemon->bins[i];
429 /* Make room to insert new bin at its sorted location. */
430 if (i < mnemon->num_bins)
431 memmove (bin + 1, bin, (mnemon->num_bins - i) * sizeof (bin_t));
434 bin_init (bin, score);
440 mnemon_remove_bin (mnemon_t *mnemon,
443 int i = bin - mnemon->bins;
447 memmove (bin, bin + 1, (mnemon->num_bins - i) * sizeof (bin_t));
454 int len = strlen (s);
457 if (s[len - 1] == '\n')
462 trim_space (char *string)
467 while (*s && isspace (*s))
472 s = string + strlen (string) - 1;
473 while (s > string && isspace (*s)) {
482 mnemon_load_category (mnemon_t *mnemon,
486 char *line = NULL, *end;
487 size_t line_size = 0;
491 category_t *category;
494 path = xmalloc (strlen (mnemon->dir_name) + 1 + strlen (name) + 1);
495 sprintf (path, "%s/%s", mnemon->dir_name, name);
497 file = fopen (path, "r");
499 fprintf (stderr, "Error: Failed to open %s: %s\n",
500 path, strerror (errno));
504 category = mnemon_get_category (mnemon, name);
506 #define READ_LINE do { \
507 bytes_read = getline (&line, &line_size, file); \
508 if (bytes_read == -1) \
516 char *name, *equal, *value;
518 /* Ignore blank lines */
523 /* An initial digit means we hit an item. Trigger the
524 * spaghetti machine. */
525 if (*line >= '0' && *line <= '9')
528 equal = strchr (line, '=');
530 fprintf (stderr, "Malformed option, (expected name=value): \"%s\" at %s:%d\n",
531 line, path, line_count);
539 name = trim_space (name);
540 value = trim_space (value);
542 if (strcmp (name, "order") == 0) {
543 if (strcmp (value, "sequential") == 0) {
544 category->order = CATEGORY_ORDER_SEQUENTIAL;
545 } else if (strcmp (value, "random") == 0) {
546 category->order = CATEGORY_ORDER_RANDOM;
548 fprintf (stderr, "Unknown value for \"order\" option \"%s\" at %s:%d\n",
549 value, path, line_count);
553 fprintf (stderr, "Unknown option %s at %s:%d\n",
554 name, path, line_count);
562 char *challenge, *response;
564 /* Ignore blank lines */
569 /* Read bin number */
571 score = strtol (line, &end, 10);
573 fprintf (stderr, "Failed to parse bin number from \"%s\" at %s:%d\n",
574 line, path, line_count);
580 challenge = strdup (line);
586 category_add_item (category, score, challenge, response);
596 /* Resize category items to fit exactly. */
597 category->items_size = category->num_items;
598 category->items = xrealloc (category->items, category->items_size * sizeof (item_t));
600 /* Now that the category is completely loaded, with stable
601 * pointers to every item, we can add each item to its appropriate
603 for (i = 0; i < category->num_items; i++) {
604 item_t *item = &category->items[i];
605 bin_t *bin = mnemon_get_bin (mnemon, item->score);
607 bin_add_item (bin, item);
612 mnemon_load (mnemon_t *mnemon)
615 struct dirent *dirent;
617 dir = opendir (mnemon->dir_name);
619 fprintf (stderr, "Error: Failed to open directory %s: %s\n",
620 mnemon->dir_name, strerror (errno));
625 dirent = readdir (dir);
629 if (dirent->d_type == DT_REG) {
630 /* Ignore files matching *~, (yes, this shouldn't be
631 * hard-coded in such an ad-hoc way, but there you go. */
632 if (dirent->d_name[strlen(dirent->d_name)-1] != '~')
633 mnemon_load_category (mnemon, dirent->d_name);
641 mnemon_save (mnemon_t *mnemon)
644 char *filename, *lock_filename;
646 category_t *category;
648 for (i = 0; i < mnemon->num_categories; i++) {
649 category = &mnemon->categories[i];
651 xasprintf (&filename, "%s/%s",
652 mnemon->dir_name, category->name);
653 xasprintf (&lock_filename, "%s/.#%s",
654 mnemon->dir_name, category->name);
656 file = fopen (lock_filename, "w");
658 fprintf (stderr, "Error: Failed to open %s for writing: %s\n",
659 lock_filename, strerror (errno));
663 category_print (category, file);
667 err = rename (lock_filename, filename);
669 fprintf (stderr, "Error: Failed to rename %s to %s: %s\n",
670 lock_filename, filename, strerror (errno));
675 free (lock_filename);
679 /* Return a uniformly-distributed pseudo-random integer within the
682 * 0 <= result < num_values
685 rand_within (int num_values)
687 return (int) (num_values * (rand() / (RAND_MAX + 1.0)));
690 /* Return an exponentially-distributed pseudo-random integer within
693 * 0 <= result < num_values
695 * The distribution is such that each successively larger value will
696 * occur with a probability of half of the previous value.
699 rand_within_exponential (int num_values)
702 static uint32_t mask = 0;
706 /* Optimize the constant case. */
716 while (mask > RAND_MAX)
723 if (ones == num_values)
731 /* Find the category to which an item belongs. */
733 mnemon_item_category (mnemon_t *mnemon,
736 category_t *category;
739 for (i = 0; i < mnemon->num_categories; i++) {
740 category = &mnemon->categories[i];
741 item_index = item - category->items;
742 if (item_index >= 0 && item_index < category->num_items)
750 mnemon_select_item (mnemon_t *mnemon,
754 int bin_index, item_index;
757 bin_index = rand_within_exponential (mnemon->num_bins);
759 bin = &mnemon->bins[bin_index];
761 item_index = rand_within (bin->num_items);
763 if (bin->score == 0) {
764 category_t *category;
767 item = bin->items[item_index];
769 category = mnemon_item_category (mnemon, item);
771 if (category->order == CATEGORY_ORDER_SEQUENTIAL) {
772 item = category_next_bin_zero_item (category);
774 item_index = bin_item_index (bin, item);
779 *item_index_ret = item_index;
783 #define HISTOGRAM_ROW_FORMAT "%3d: %3d"
784 #define HISTOGRAM_BAR_WIDTH 63
787 print_histogram_bar (double size,
790 int units_per_cell = (int) ceil (max / HISTOGRAM_BAR_WIDTH);
791 static char const *boxes[8] = {
796 while (size > units_per_cell) {
798 size -= units_per_cell;
801 size /= units_per_cell;
805 else if (size > 6.5/8.0)
807 else if (size > 5.5/8.0)
809 else if (size > 4.5/8.0)
811 else if (size > 3.5/8.0)
813 else if (size > 2.5/8.0)
815 else if (size > 1.5/8.0)
817 else if (size > 0.5/8.0)
824 mnemon_print_histogram (mnemon_t *mnemon)
826 int i, last_score, max;
829 if (mnemon->num_bins == 0)
832 max = mnemon->bins[0].num_items;
833 for (i = 1; i < mnemon->num_bins; i++)
834 if (mnemon->bins[i].num_items > max)
835 max = mnemon->bins[i].num_items;
837 for (i = 0; i < mnemon->num_bins; i++) {
838 bin = &mnemon->bins[i];
840 while (bin->score - last_score > 1)
841 printf (HISTOGRAM_ROW_FORMAT "\n", ++last_score, 0);
842 printf (HISTOGRAM_ROW_FORMAT " ", bin->score, bin->num_items);
843 print_histogram_bar (bin->num_items, max);
844 last_score = bin->score;
849 mnemon_handle_command (mnemon_t *mnemon,
852 switch (command[0]) {
854 mnemon_print_histogram (mnemon);
857 printf ("Unknown command: %s\n", command);
863 mnemon_handle_response (mnemon_t *mnemon,
867 const char *response)
871 correct = (strcmp (response, item->response) == 0);
873 bin_remove_item (bin, item_index);
875 /* If the bin is now empty, we must remove it. Also if we just
876 * picked the last word we'll ever pick from the bin with
877 * score 0, then we can remove that as well. */
878 if (bin->num_items == 0 ||
879 (bin->score == 0 && mnemon->to_introduce == 0))
881 mnemon_remove_bin (mnemon, bin);
886 /* We reserve an item score of 0 for an item that has
887 * never been asked. */
888 if (item->score == 0) {
891 printf ("You got it!");
892 } else if (item->score < 0) {
893 printf ("Yes---just give me %d more.",
895 } else if (item->score == 1) {
896 printf ("On your first try, no less!");
898 printf ("Masterful (%dx).", item->score);
899 if (mnemon->to_master)
903 printf (" %s is the correct answer.",
905 /* Penalize an incorrect response by forcing the score
907 if (item->score >= 0) {
909 printf ( " Oops, you knew that, right?\n ");
911 /* We go to -2 to force a little extra reinforcement
912 * when re-learning an item, (otherwise, it will often
913 * get asked again immediately where it is easy to get
914 * a correct response without any learning). */
921 if (mnemon->to_introduce == 0 &&
922 mnemon->unlearned == 0 &&
923 mnemon->to_master == 0)
925 mnemon->to_master = 10;
926 mnemon->mastered = 0;
930 if (mnemon->to_introduce)
931 printf ("%d to come. ", mnemon->to_introduce);
932 if (mnemon->unlearned)
933 printf ("%d still unlearned. ", mnemon->unlearned);
934 if (mnemon->to_master) {
935 if (mnemon->mastered < mnemon->to_master)
936 printf ("%d items to master",
937 mnemon->to_master - mnemon->mastered);
939 printf ("Great job!");
943 bin = mnemon_get_bin (mnemon, item->score);
945 bin_add_item (bin, item);
949 mnemon_do_challenges (mnemon_t *mnemon)
957 /* Count the number of items with negative scores. */
958 mnemon->unlearned = 0;
959 for (i = 0; i < mnemon->num_bins; i++) {
960 bin = &mnemon->bins[i];
963 mnemon->unlearned += bin->num_items;
966 mnemon->to_introduce -= mnemon->unlearned;
967 if (mnemon->to_introduce < 0)
968 mnemon->to_introduce = 0;
970 /* Get rid of bin with score of 0 if we aren't going to be
971 * introducing anything from it. */
972 if (mnemon->to_introduce == 0) {
973 bin = mnemon_get_bin (mnemon, 0);
974 mnemon_remove_bin (mnemon, bin);
977 if (mnemon->unlearned) {
978 printf ("You've got %d items to learn already. ", mnemon->unlearned);
979 if (mnemon->to_introduce)
980 printf ("I'll introduce %d more as we go.", mnemon->to_introduce);
983 printf ("Introducing %d new items.\n", mnemon->to_introduce);
988 mnemon_select_item (mnemon, &bin, &item_index);
989 item = bin->items[item_index];
992 mnemon->to_introduce--;
995 printf ("%s\n", item->challenge);
997 response = readline ("> ");
998 /* Terminate on EOF */
999 if (response == NULL) {
1004 if (response[0] == '/')
1005 mnemon_handle_command (mnemon, response + 1);
1010 mnemon_handle_response (mnemon, bin, item_index,
1012 } while (mnemon->mastered < mnemon->to_master);
1016 main (int argc, char *argv[])
1020 srand (time (NULL));
1022 mnemon_init (&mnemon);
1024 mnemon_load (&mnemon);
1026 mnemon_do_challenges (&mnemon);
1028 mnemon_save (&mnemon);
1030 mnemon_fini (&mnemon);