~pixelherodev/DSM-9

10f27a36e4064ec6ac46d84b03a9a2231d692401 — Noam Preil a day ago e01de2e
tmp
6 files changed, 25 insertions(+), 73 deletions(-)

M Makefile
R sm2.c => dsm9.c
M dsm9.h
M due.c
M mkfile
D sm2.h
M Makefile => Makefile +3 -3
@@ 6,13 6,13 @@ CFLAGS=-std=c17 -pedantic -Wall -O1 -march=native -g -D _POSIX_C_SOURCE=200809 -

all:due log

due: sm2.o due.o time.o util.o
due: due.o time.o util.o dsm9.o
	$(CC) -o $@ $^ -lrt $(CFLAGS)

log: sm2.o log.o time.o util.o
log: log.o time.o util.o dsm9.o
	$(CC) -o $@ $^ -lrt $(CFLAGS)

%.o: %.c sm2.h dsm9.h sb.h
%.o: %.c dsm9.h sb.h
	$(CC) $(CFLAGS) $(EXTRA_CFLAGS) -c $< -o $@

clean:

R sm2.c => dsm9.c +14 -14
@@ 13,9 13,7 @@

#include <stdio.h>

/* v2 header */
#include "dsm9.h"

#include "util.h"
#include "sb.h"



@@ 31,7 29,13 @@ dsm9unload(dsm9deck *deck)
}

static void
dolog(dsm9deck *deck, uint32_t index, uint8_t grade, uint32_t timestamp)
markdue(dsm9deck *deck, uint16_t index)
{
	pushu16(&deck->accel.due, index);
}

static void
dolog(dsm9deck *deck, uint16_t index, uint8_t grade, uint32_t timestamp)
{
	double e = deck->easinesses[index];
	double i = deck->intervals[index];


@@ 41,8 45,6 @@ dolog(dsm9deck *deck, uint32_t index, uint8_t grade, uint32_t timestamp)
	if(grade < 3){
		deck->dues[index] = 0;
		deck->intervals[index] = 1;
		printf("Already due: %d\n",index);
		pushu16(&deck->accel.due, index);
		return;
	}
	e = e - 0.8 + (0.28 * g) - (0.02 * g * g);


@@ 50,10 52,8 @@ dolog(dsm9deck *deck, uint32_t index, uint8_t grade, uint32_t timestamp)
		e = 1.3;
	deck->easinesses[index] = 1.3;
	deck->dues[index] = (uint32_t)(ts + ((double)60 * (double)60 * (double)24 * i * e));
	if(deck->dues[index] < time(NULL)){
		printf("Already due: %d\n",index);
		pushu16(&deck->accel.due, index);
	}
	if(deck->dues[index] < time(NULL))
		markdue(deck, index);
	deck->intervals[index] = deck->reps[index] == 1 ? 3 : deck->reps[index] == 2 ? 6 : deck->easinesses[index] * (double)deck->intervals[index];
}



@@ 75,7 75,7 @@ openlogs(dsm9deck *deck)
}

int
dsm9log(dsm9deck *deck, uint32_t index, uint8_t grade, uint32_t timestamp)
dsm9log(dsm9deck *deck, uint16_t index, uint8_t grade, uint32_t timestamp)
{
	char *id = dsm9cardname(deck, index);
	if(id == NULL)


@@ 260,7 260,7 @@ accelload(dsm9deck *deck, char *basepath)
}

static int
dsm9stream(dsm9deck *deck, uint32_t index)
dsm9stream(dsm9deck *deck, uint16_t index)
{
	uint32_t id, front, back, end;
	int field = 0;


@@ 290,7 290,7 @@ dsm9stream(dsm9deck *deck, uint32_t index)
}

char *
dsm9cardback(dsm9deck *deck, uint32_t index)
dsm9cardback(dsm9deck *deck, uint16_t index)
{
	/* For this, it'd be more efficient to have id and front stored together - however, this also only uses *eight bytes*, so the effect on the cache is neglible */
	uint32_t start = deck->backs[index];


@@ 311,7 311,7 @@ dsm9cardback(dsm9deck *deck, uint32_t index)


char *
dsm9cardfront(dsm9deck *deck, uint32_t index)
dsm9cardfront(dsm9deck *deck, uint16_t index)
{
	/* For this, it'd be more efficient to have id and front stored together - however, this also only uses *eight bytes*, so the effect on the cache is neglible */
	uint32_t start = deck->fronts[index];


@@ 333,7 333,7 @@ dsm9cardfront(dsm9deck *deck, uint32_t index)
}

char *
dsm9cardname(dsm9deck *deck, uint32_t index)
dsm9cardname(dsm9deck *deck, uint16_t index)
{
	/* For this, it'd be more efficient to have id and front stored together - however, this also only uses *eight bytes*, so the effect on the cache is neglible */
	uint32_t id = deck->ids[index];

M dsm9.h => dsm9.h +4 -4
@@ 47,14 47,14 @@ int dsm9load(char *basepath, dsm9deck *deck);
void dsm9unload(dsm9deck*);

/* Fetches the name of a card, by index */
char *dsm9cardname(dsm9deck*, uint32_t);
char *dsm9cardname(dsm9deck*, uint16_t);
/* Fetches the front of a card, by index */
char *dsm9cardfront(dsm9deck*, uint32_t);
char *dsm9cardfront(dsm9deck*, uint16_t);
/* Fetches the back of a card, by index */
char *dsm9cardback(dsm9deck*, uint32_t);
char *dsm9cardback(dsm9deck*, uint16_t);
/*
	Appends a log entry to the log file, updates the card information, and flushes the card information to the DSM accelerator file.
*/
int dsm9log(dsm9deck*, uint32_t index, uint8_t grade, uint32_t timestamp);
int dsm9log(dsm9deck*, uint16_t index, uint8_t grade, uint32_t timestamp);



M due.c => due.c +0 -1
@@ 10,7 10,6 @@
#endif
#include <stdio.h>

#include "sm2.h"
#include "dsm9.h"
#include "util.h"


M mkfile => mkfile +4 -4
@@ 4,19 4,19 @@ CFLAGS=$CFLAGS -p -D_POSIX_SOURCE -D_PLAN9_SOURCE -D_PLAN9_$objtype -D_PLAN9_NOA

all:V: due log cli

cli: cli.$O sm2.$O time.$O util.$O
cli: cli.$O time.$O util.$O dsm9.$O
	$LD $LDFLAGS -o $target $prereq

due: due.$O sm2.$O time.$O util.$O
due: due.$O time.$O util.$O dsm9.$O
	$LD $LDFLAGS -o $target $prereq

log: log.$O sm2.$O time.$O util.$O
log: log.$O time.$O util.$O dsm9.$O
	$LD $LDFLAGS -o $target $prereq

clean:
	rm -f due log cli *.$O

%.$O:	%.c sb.h dsm9.h sm2.h
%.$O:	%.c sb.h dsm9.h
	$CC $CFLAGS $stem.c

install:V: /$objtype/bin/dsm9/due /$objtype/bin/dsm9/log /$objtype/bin/dsm9/cli

D sm2.h => sm2.h +0 -47
@@ 1,47 0,0 @@
struct sm2_card {
	char *id;
	char *path;
	char **fields;
	unsigned long field_count;
	unsigned long next_due;
	uint8_t interval;
	uint32_t repetitions;
	float easiness;
};

struct sm2_instance {
	uint32_t card_count, card_cap;
	// heap-allocated array of size card_count. capacity is always equal to size.
	struct sm2_card *cards;
	char *delimiter;
};

/*
	Call this before calling any other sm2_* functions.
*/
extern void sm2_init(struct sm2_instance*);

/*
	Loads a deck into the instance. If overwrite is set, any cards with IDs
	matching already-loaded cards will replace the previously known cards.
	If overwrite is not set, then cards with duplicate IDs will be treated as distinct copies of the same card. In other words, log entries for one are considered log entries for BOTH. Behavior is not fully defined for this scenario.
	
	This *will* crash (or worse!) if the instance hasn't been initialized.
	This overwrites the provided string with undefined contents. The caller
	is responsible for duplicating the string if this is unwanted.
*/
extern int sm2_load_deck(struct sm2_instance*, char *, int overwrite, char *path);

/*
	Loads and processes logs. While logs can be loaded before decks, any cards not already
	loaded will be ignored entirely. This allows for deleting cards from a deck without touching
	the logs.
*/
extern int sm2_load_logs(struct sm2_instance*, char*);

/*
	Deallocate all resources used by the SM-2 instance
*/
extern void sm2_deinit(struct sm2_instance*);

extern int sm2_log(struct sm2_instance *instance, char *id, uint8_t grade, long timestamp);