TPoss tpossIntersect(TPoss S, TPoss T) { TFormList LS, LT, l = 0; if (S == NULL || T == NULL) return NULL; /* If T is free of duplicates, then the result will also be. */ for (LT = T->possl; LT; LT = cdr(LT)) { car(LT) = tfFollowOnly(car(LT)); for (LS = S->possl; LS; LS = cdr(LS)) { car(LS) = tfFollowOnly(car(LS)); if (tfSatisfies(car(LS), car(LT))) { l = listCons(TForm)(car(LT), l); break; } if (tfSatisfies(car(LT), car(LS))) { if (!listMember(TForm)(l, car(LS), tfEqual)) l = listCons(TForm)(car(LS), l); } } } l = listNReverse(TForm)(l); return tpossFrTheList(l); }
ListPtr listAdjoin(char *car, ListPtr cdr) { if(listMember(car, cdr)) { free(car); return cdr; } return listCons(car, cdr); }
int listMember(struct linked_list *lst, int x, int y){ if (lst == NULL) { return FALSE; } if ((*lst).x == x && (*lst).y == y) { return TRUE; } else { return listMember((*lst).next, x, y); } }
Bool tpossHas(TPoss tp, TForm t) { if (tp == NULL) return false; if (listMemq(TForm)(tp->possl, t)) return true; return listMember(TForm)(tp->possl, t, tfEqual); }
int evaluateLiberties(char *board, int x, int y, struct linked_list *waiting, struct linked_list *evaled, struct linked_list *nolibs, char col, int turn){ BOOLEAN first = (waiting == NULL); if ((x > width) || (y > length) || (x <= 0) || (y <= 0)) { return UNDECIDED; } else if (board[toIndex(x,y)] == SPACE) { return TRUE; } else if (board[toIndex(x,y)] != col) { return FALSE; } else if (listMember(waiting, x, y) == TRUE) { return UNDECIDED; } else if (listMember(evaled, x, y) == TRUE) { return TRUE; } else if (listMember(nolibs, x, y) == TRUE) { return FALSE; } else { waiting = listAdd(waiting, x, y); if (evaluateLiberties(board, x+1, y, waiting, evaled, nolibs, col, turn) == TRUE || evaluateLiberties(board, x, y+1, waiting, evaled, nolibs, col, turn) == TRUE || evaluateLiberties(board, x, y-1, waiting, evaled, nolibs, col, turn) == TRUE || evaluateLiberties(board, x-1, y, waiting, evaled, nolibs, col, turn) == TRUE) { listAdd(evaled, x, y); waiting = listRemove(waiting, x, y); return TRUE; } else { if (first == TRUE) { while ((*waiting).next != NULL) { listAdd(nolibs, (*(*waiting).next).x, (*(*waiting).next).y); (*waiting).next = listRemove((*waiting).next, (*(*waiting).next).x, (*(*waiting).next).y); } listAdd(nolibs, (*waiting).x, (*waiting).y); SafeFree(waiting); return FALSE; } else { return UNDECIDED; } } } }
void do_ar(int argc, const char ** argv) { const char * options; const char * archiveName; BOOL doExtract; BOOL doTable; BOOL doPrint; BOOL verbose; Archive arch; verbose = FALSE; doExtract = FALSE; doTable = FALSE; doPrint = FALSE; if (argc < 3) { fprintf(stderr, "Too few arguments for ar\n"); return; } /* * Get the option string and archive file name. */ options = argv[1]; archiveName = argv[2]; /* * Advance the arguments to the list of file names (if any). */ argc -= 3; argv += 3; /* * Parse the option characters. */ for (; *options; options++) { switch (*options) { case 't': doTable = TRUE; break; case 'x': doExtract = TRUE; break; case 'p': doPrint = TRUE; break; case 'v': verbose = TRUE; break; case 'd': case 'm': case 'q': case 'r': fprintf(stderr, "Writing ar files is not supported\n"); return; default: fprintf(stderr, "Unknown ar flag: %c\n", *options); return; } } if (doExtract + doTable + doPrint != 1) { fprintf(stderr, "Exactly one of 'x', 'p' or 't' must be specified\n"); return; } /* * Open the archive file. */ initArchive(&arch); if (!openArchive(archiveName, &arch)) return; /* * Read the first special member of the archive. */ if (!readSpecialMember(&arch)) return; /* * Read all of the normal members of the archive. */ while (readNormalMember(&arch)) { /* * If this file is not wanted then skip it. */ if (!wantMember(&arch, argc, argv)) { if (!skipMember(&arch)) break; continue; } /* * This file is wanted. */ if (doTable) { if (verbose) listMember(&arch); else puts(arch.name); if (!skipMember(&arch)) break; } else if (doPrint) { if (verbose) { /* * The verbose format makes me gag, * but 4.4BSD, GNU and even V7 all * have the same lossage. */ printf("\n<%s>\n\n", arch.name); fflush(stdout); } if (!writeFile(&arch, STDOUT)) break; } else if (doExtract) { int outfd; BOOL success; if (verbose) printf("x - %s\n", arch.name); outfd = createFile(&arch); if (outfd == -1) break; success = writeFile(&arch, outfd); if (close(outfd) == -1) { fprintf(stderr, "Can't close %s: %s\n", arch.name, strerror(errno)); break; } if (!success) break; } else { fprintf(stderr, "Oops -- I don't know what to do\n"); break; } } closeArchive(&arch); }