1 /* mnemon - A memory training library
3 * Copyright © 2006,2011 Carl Worth
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 3, or (at your option)
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software Foundation,
17 * Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA."
30 #include <sys/types.h>
39 #include <readline/readline.h>
40 #include <readline/history.h>
42 #define ASSERT_NOT_REACHED \
44 static const int NOT_REACHED = 0; \
45 assert (NOT_REACHED); \
55 fprintf (stderr, "Error: out of memory\n");
63 xrealloc (void *ptr, size_t size)
67 ret = realloc (ptr, size);
69 fprintf (stderr, "Error: out of memory\n");
77 xstrdup (const char *s)
83 fprintf (stderr, "Error: out of memory\n");
91 xasprintf (char **strp, const char *fmt, ...)
97 ret = vasprintf (strp, fmt, ap);
101 fprintf (stderr, "Error: out of memory\n");
107 item_init (item_t *item,
109 const char *challenge,
110 const char *response)
114 item->challenge = xmalloc (strlen (challenge) + 1 +
115 strlen (response) + 1);
116 item->response = item->challenge + strlen (challenge) + 1;
118 strcpy (item->challenge, challenge);
119 strcpy (item->response, response);
123 item_fini (item_t *item)
125 /* item->response shares allocation with item->challenge, so
126 * doesn't require a separate call to free */
127 free (item->challenge);
131 category_init (category_t *category,
134 category->name = xstrdup (name);
136 category->items_size = 0;
137 category->num_items = 0;
138 category->items = NULL;
139 category->order = CATEGORY_ORDER_RANDOM;
140 category->time_limit = 0.0;
141 category->bin_zero_head = 0;
142 category->challenge_type = xstrdup("");
143 category->repeat = 0;
147 category_fini (category_t *category)
151 for (i = 0; i < category->num_items; i++)
152 item_fini (&category->items[i]);
154 free (category->items);
156 free (category->name);
158 free (category->challenge_type);
162 category_grow (category_t *category)
164 if (category->items_size)
165 category->items_size *= 2;
167 category->items_size = 1;
169 category->items = xrealloc (category->items,
170 category->items_size * sizeof (item_t));
174 category_add_item (category_t *category,
176 const char *challenge,
177 const char *response)
181 if (category->num_items == category->items_size)
182 category_grow (category);
184 item = &category->items[category->num_items++];
186 item_init (item, score, challenge, response);
192 category_next_bin_zero_item (category_t *category)
194 unsigned int *i = &category->bin_zero_head;
196 for ( ; *i < category->num_items; *i = *i + 1)
197 if (category->items[*i].score == 0)
198 return &category->items[*i];
204 category_print (category_t *category,
210 fprintf (file, "order = %s\n\n",
211 category->order == CATEGORY_ORDER_RANDOM ? "random" : "sequential");
212 fprintf (file, "time = %f\n\n",
213 category->time_limit);
215 fprintf (file, "challenge = %s\n\n", category->challenge_type);
217 fprintf (file, "repeat = %d\n\n", category->repeat);
219 for (i = 0; i < category->num_items; i++) {
220 item = &category->items[i];
222 fprintf (file, "\n");
223 fprintf (file, "%d\n%s\n%s\n",
231 bin_init (bin_t *bin,
242 bin_fini (bin_t *bin)
248 bin_grow (bin_t *bin)
251 bin->items_size *= 2;
255 bin->items = xrealloc (bin->items,
256 bin->items_size * sizeof (item_t*));
260 bin_add_item (bin_t *bin,
263 assert (item->score == bin->score);
265 if (bin->num_items == bin->items_size)
268 bin->items[bin->num_items++] = item;
272 bin_remove_item (bin_t *bin,
275 /* Replace the current item with the last item, (no need to shift
276 * any more than that since we don't care about the order of the
277 * items within a bin). */
280 bin->items[item_index] = bin->items[bin->num_items];
283 /* Find the index for an item within a bin.
285 * XXX: This is currently a linear search, so is a potential
286 * performance problem.
289 bin_item_index (bin_t *bin,
294 for (i = 0; i < bin->num_items; i++)
295 if (bin->items[i] == item)
302 mnemon_init (mnemon_t *mnemon)
306 home = getenv ("HOME");
310 xasprintf (&mnemon->dir_name, "%s/.mnemon", getenv ("HOME"));
312 mnemon->categories_size = 0;
313 mnemon->num_categories = 0;
314 mnemon->categories = NULL;
316 mnemon->bins_size = 0;
317 mnemon->num_bins = 0;
322 mnemon_fini (mnemon_t *mnemon)
326 for (i = 0; i < mnemon->num_bins; i++)
327 bin_fini (&mnemon->bins[i]);
330 for (i = 0; i < mnemon->num_categories; i++)
331 category_fini (&mnemon->categories[i]);
332 free (mnemon->categories);
334 free (mnemon->dir_name);
338 mnemon_categories_grow (mnemon_t *mnemon)
340 if (mnemon->categories_size)
341 mnemon->categories_size *= 2;
343 mnemon->categories_size = 1;
345 mnemon->categories = xrealloc (mnemon->categories,
346 mnemon->categories_size * sizeof (category_t));
349 /* Get a category by name if it exists */
351 mnemon_get_category_if_exists (mnemon_t *mnemon,
356 for (i = 0; i < mnemon->num_categories; i++)
357 if (strcmp (mnemon->categories[i].name, name) == 0)
358 return &mnemon->categories[i];
363 /* Get a category by name, creating new one if necessary. */
365 mnemon_get_category (mnemon_t *mnemon,
368 category_t *category;
370 category = mnemon_get_category_if_exists (mnemon, name);
374 mnemon_categories_grow (mnemon);
376 category = &mnemon->categories[mnemon->num_categories++];
378 category_init (category, name);
384 mnemon_bins_grow (mnemon_t *mnemon)
386 if (mnemon->bins_size)
387 mnemon->bins_size *= 2;
389 mnemon->bins_size = 1;
391 mnemon->bins = xrealloc (mnemon->bins,
392 mnemon->bins_size * sizeof (bin_t));
396 mnemon_get_bin (mnemon_t *mnemon,
402 for (i = 0; i < mnemon->num_bins; i++)
403 if (mnemon->bins[i].score == score)
404 return &mnemon->bins[i];
405 else if (mnemon->bins[i].score > score)
408 if (mnemon->num_bins == mnemon->bins_size)
409 mnemon_bins_grow (mnemon);
411 bin = &mnemon->bins[i];
413 /* Make room to insert new bin at its sorted location. */
414 if (i < mnemon->num_bins)
415 memmove (bin + 1, bin, (mnemon->num_bins - i) * sizeof (bin_t));
418 bin_init (bin, score);
424 mnemon_remove_bin (mnemon_t *mnemon, int bin_number)
426 bin_t *bin = mnemon_get_bin (mnemon, bin_number);
432 i = bin - mnemon->bins;
436 memmove (bin, bin + 1, (mnemon->num_bins - i) * sizeof (bin_t));
443 int len = strlen (s);
446 if (s[len - 1] == '\n')
451 trim_space (char *string)
456 while (*s && isspace (*s))
461 s = string + strlen (string) - 1;
462 while (s > string && isspace (*s)) {
471 mnemon_load_category (mnemon_t *mnemon,
475 char *line = NULL, *end;
476 size_t line_size = 0;
480 category_t *category;
484 path = xmalloc (strlen (mnemon->dir_name) + 1 + strlen (name) + 1);
485 sprintf (path, "%s/%s", mnemon->dir_name, name);
487 file = fopen (path, "r");
489 fprintf (stderr, "Error: Failed to open %s: %s\n",
490 path, strerror (errno));
494 fstat (fileno(file), &st);
495 if (! S_ISREG(st.st_mode)) {
496 fprintf (stderr, "Error: File %s is not a regular file.\n", path);
500 category = mnemon_get_category (mnemon, name);
502 #define READ_LINE do { \
503 bytes_read = getline (&line, &line_size, file); \
504 if (bytes_read == -1) \
512 char *name, *equal, *value;
514 /* Ignore blank lines */
519 /* An initial digit means we hit an item. Trigger the
520 * spaghetti machine. */
521 if ((*line >= '0' && *line <= '9') || *line == '-')
524 equal = strchr (line, '=');
526 fprintf (stderr, "Malformed option, (expected name=value): \"%s\" at %s:%d\n",
527 line, path, line_count);
535 name = trim_space (name);
536 value = trim_space (value);
538 if (strcmp (name, "order") == 0) {
539 if (strcmp (value, "sequential") == 0) {
540 category->order = CATEGORY_ORDER_SEQUENTIAL;
541 } else if (strcmp (value, "random") == 0) {
542 category->order = CATEGORY_ORDER_RANDOM;
544 fprintf (stderr, "Unknown value for \"order\" option \"%s\" at %s:%d\n",
545 value, path, line_count);
548 } else if (strcmp (name, "time") == 0) {
551 limit = strtod (value, &end);
552 while (isspace (*end))
555 category->time_limit = limit;
557 fprintf (stderr, "Failed to parse time value: %s at %s:%d\n",
558 value, path, line_count);
561 } else if (strcmp (name, "challenge") == 0) {
562 /* XXX: Need to switch to talloc here. */
563 free (category->challenge_type);
564 category->challenge_type = xstrdup (value);
565 } else if (strcmp (name, "repeat") == 0) {
566 if (strcmp (value, "0") == 0)
567 category->repeat = 0;
569 category->repeat = 1;
571 fprintf (stderr, "Unknown option %s at %s:%d\n",
572 name, path, line_count);
580 char *challenge, *response;
582 /* Ignore blank lines */
587 /* Read bin number */
589 score = strtol (line, &end, 10);
591 fprintf (stderr, "Failed to parse bin number from \"%s\" at %s:%d\n",
592 line, path, line_count);
598 challenge = strdup (line);
604 category_add_item (category, score, challenge, response);
614 /* Resize category items to fit exactly. */
615 category->items_size = category->num_items;
616 category->items = xrealloc (category->items, category->items_size * sizeof (item_t));
618 /* Now that the category is completely loaded, with stable
619 * pointers to every item, we can add each item to its appropriate
621 for (i = 0; i < category->num_items; i++) {
622 item_t *item = &category->items[i];
623 bin_t *bin = mnemon_get_bin (mnemon, item->score);
625 bin_add_item (bin, item);
630 mnemon_load (mnemon_t *mnemon)
633 struct dirent *dirent;
635 dir = opendir (mnemon->dir_name);
637 fprintf (stderr, "Error: Failed to open directory %s: %s\n",
638 mnemon->dir_name, strerror (errno));
643 dirent = readdir (dir);
647 if (dirent->d_type == DT_REG) {
648 /* Ignore files matching *~, (yes, this shouldn't be
649 * hard-coded in such an ad-hoc way, but there you go. */
650 if (dirent->d_name[strlen(dirent->d_name)-1] != '~')
651 mnemon_load_category (mnemon, dirent->d_name);
659 mnemon_save (mnemon_t *mnemon)
662 char *filename, *lock_filename;
664 category_t *category;
666 for (i = 0; i < mnemon->num_categories; i++) {
667 category = &mnemon->categories[i];
669 xasprintf (&filename, "%s/%s",
670 mnemon->dir_name, category->name);
671 xasprintf (&lock_filename, "%s/.#%s",
672 mnemon->dir_name, category->name);
674 file = fopen (lock_filename, "w");
676 fprintf (stderr, "Error: Failed to open %s for writing: %s\n",
677 lock_filename, strerror (errno));
681 category_print (category, file);
683 fsync (fileno (file));
686 err = rename (lock_filename, filename);
688 fprintf (stderr, "Error: Failed to rename %s to %s: %s\n",
689 lock_filename, filename, strerror (errno));
694 free (lock_filename);
698 /* Return a uniformly-distributed pseudo-random integer within the
701 * 0 <= result < num_values
704 rand_within (int num_values)
706 return (int) (num_values * (rand() / (RAND_MAX + 1.0)));
709 /* Return an exponentially-distributed pseudo-random integer within
712 * 0 <= result < num_values
714 * The distribution is such that each successively larger value will
715 * occur with a probability of half of the previous value.
718 rand_within_exponential (int num_values)
721 static uint32_t mask = 0;
725 /* Optimize the constant case. */
735 while (mask > RAND_MAX)
742 if (ones == num_values)
751 mnemon_item_category (mnemon_t *mnemon,
754 category_t *category;
757 for (i = 0; i < mnemon->num_categories; i++) {
758 category = &mnemon->categories[i];
759 item_index = item - category->items;
760 if (item_index >= 0 && item_index < category->num_items)
768 mnemon_select_item (mnemon_t *mnemon,
771 category_t **category_ret,
774 int bin_index, item_index;
777 category_t *category;
779 bin_index = rand_within_exponential (mnemon->num_bins);
780 bin = &mnemon->bins[bin_index];
782 /* The most intuitive understanding of the introduced flag that
783 * it's tracking never-before-learned items as they are pulled
784 * from the bin with score 0. But that bin can become empty. So
785 * the refined rule is that we also set introduced whenever we
786 * pull from the lowest-indexed bin with a non-negative score. */
787 if (bin->score >=0 &&
788 (bin_index == 0 || mnemon->bins[bin_index-1].score < 0))
797 item_index = rand_within (bin->num_items);
799 item = bin->items[item_index];
800 category = mnemon_item_category (mnemon, item);
802 if (bin->score == 0) {
803 if (category->order == CATEGORY_ORDER_SEQUENTIAL) {
804 item = category_next_bin_zero_item (category);
806 item_index = bin_item_index (bin, item);
811 *item_index_ret = item_index;
812 *category_ret = category;
816 mnemon_score_item (mnemon_t *mnemon,
818 unsigned int item_index,
823 if (item_index >= bin->num_items)
826 item = bin->items[item_index];
827 bin_remove_item (bin, item_index);
829 /* If the bin is now empty, we must remove it. */
830 if (bin->num_items == 0)
832 mnemon_remove_bin (mnemon, bin->score);
838 /* We reserve an item score of 0 for an item that has
839 * never been asked. */
840 if (item->score == 0)
845 /* Penalize an incorrect response by forcing the score
847 if (item->score >= 0) {
848 /* We go to -2 to force a little extra reinforcement
849 * when re-learning an item, (otherwise, it will often
850 * get asked again immediately where it is easy to get
851 * a correct response without any learning). */
858 bin = mnemon_get_bin (mnemon, item->score);
860 bin_add_item (bin, item);