int naiHash_tryset(naRef hash, naRef key, naRef val) { HashRec* hr = REC(hash); if(hr) { int ent, cell = findcell(hr, key, refhash(key)); if((ent = TAB(hr)[cell]) >= 0) { ENTS(hr)[ent].val = val; return 1; } } return 0; }
int naHash_get(naRef hash, naRef key, naRef* out) { HashRec* hr = REC(hash); if(hr) { int ent, cell = findcell(hr, key, refhash(key)); if((ent = TAB(hr)[cell]) < 0) return 0; *out = ENTS(hr)[ent].val; return 1; } return 0; }
/* * Here when we get a keypress in a window. */ static void dokey(GR_EVENT_KEYSTROKE *kp) { if ((kp->wid != boardwid) || !playing) return; switch (kp->ch) { case ' ': /* remember or forget mine */ togglecell(findcell(kp->x, kp->y)); break; } }
static void hashset(HashRec* hr, naRef key, naRef val) { int ent, cell = findcell(hr, key, refhash(key)); if((ent = TAB(hr)[cell]) == ENT_EMPTY) { ent = hr->next++; if(ent >= NCELLS(hr)) return; /* race protection, don't overrun */ TAB(hr)[cell] = ent; hr->size++; ENTS(hr)[ent].key = key; } ENTS(hr)[ent].val = val; }
void naHash_delete(naRef hash, naRef key) { HashRec* hr = REC(hash); if(hr) { int cell = findcell(hr, key, refhash(key)); if(TAB(hr)[cell] >= 0) { TAB(hr)[cell] = ENT_DELETED; if(--hr->size < POW2(hr->lgsz-1)) resize(PTR(hash).hash); } } }
/* * Here when we get a button down event. */ static void dobutton(GR_EVENT_BUTTON *bp) { if (bp->wid == boardwid) { movetopos(findcell(bp->x, bp->y)); return; } if (bp->wid == quitwid) { GrFillRect(quitwid, xorgc, 0, 0, BUTTONWIDTH, BUTTONHEIGHT); GrFlush(); if (savefile) writegame(savefile); GrClose(); exit(0); } if (bp->wid == savewid) { GrFillRect(savewid, xorgc, 0, 0, BUTTONWIDTH, BUTTONHEIGHT); GrFlush(); if (savefile == NULL) savefile = SAVEFILE; if (writegame(savefile)) write(1, "\007", 1); else delay(); GrFillRect(savewid, xorgc, 0, 0, BUTTONWIDTH, BUTTONHEIGHT); } if (bp->wid == newgamewid) { GrFillRect(newgamewid, xorgc, 0, 0, BUTTONWIDTH, BUTTONHEIGHT); GrFlush(); /*if (playing) write(1, "\007", 1); else {*/ newgame(); delay(); /*}*/ GrFillRect(newgamewid, xorgc, 0, 0, BUTTONWIDTH, BUTTONHEIGHT); } }