void UniquePrimeFactors (void) // Version 2 (DP approach). Parameter is left void as the target struct is defined statically in the heap (not the stack). Simplified version of the main function in Problem 70. { int i, j; void intcat(int *, int *); int isPrime (long, int); for (i = 0; i < MAXNUM; i ++) { numlist[i].number = i; numlist[i].DistinctPrimes = NULL; if (isPrime(i, 5)) { numlist[i].DistinctPrimes = malloc(sizeof(int)); *numlist[i].DistinctPrimes = i; } else { for (j = 0; prime[j] <= sqrt(i); j++) { if (!(i % prime[j])) { if (search(prime[j], numlist[i/prime[j]].DistinctPrimes)) numlist[i].DistinctPrimes = numlist[i/prime[j]].DistinctPrimes; else { numlist[i].DistinctPrimes = malloc(MAXFACTOR*sizeof(int)); *numlist[i].DistinctPrimes = prime[j]; intcat (numlist[i].DistinctPrimes, numlist[i/prime[j]].DistinctPrimes); } break; } else continue; } } } }
static PyObject *parseFasta(PyObject *self, PyObject *args) { /* Parse sequences from *filename* into the memory pointed by the Numpy array passed as Python object. */ char *filename; PyArrayObject *msa; PyObject *labels = PyList_New(0); PyObject *mapping = PyDict_New(); if (!PyArg_ParseTuple(args, "sO", &filename, &msa)) return NULL; if (!labels || !mapping) return PyErr_NoMemory(); char *line = malloc((FASTALINELEN) * sizeof(char)); if (!line) return PyErr_NoMemory(); char *data = (char *) PyArray_DATA(msa); int aligned = 1; char ch, errmsg[LENLABEL] = "failed to parse FASTA file at line "; long index = 0, count = 0; long iline = 0, i, seqlen = 0, curlen = 0; FILE *file = fopen(filename, "rb"); while (fgets(line, FASTALINELEN, file) != NULL) { iline++; if (line[0] == '>') { if (seqlen != curlen) { if (seqlen) { aligned = 0; free(line); free(data); fclose(file); PyErr_SetString(PyExc_IOError, intcat(errmsg, iline)); return NULL; } else seqlen = curlen; } // `line + 1` is to omit `>` character count += parseLabel(labels, mapping, line + 1, FASTALINELEN); curlen = 0; } else { for (i = 0; i < FASTALINELEN; i++) { ch = line[i]; if (ch < 32) break; else { data[index++] = ch; curlen++; } } } } fclose(file); free(line); if (aligned && seqlen != curlen) { PyErr_SetString(PyExc_IOError, intcat(errmsg, iline)); return NULL; } npy_intp dims[2] = {index / seqlen, seqlen}; PyArray_Dims arr_dims; arr_dims.ptr = dims; arr_dims.len = 2; PyArray_Resize(msa, &arr_dims, 0, NPY_CORDER); PyObject *result = Py_BuildValue("(OOOi)", msa, labels, mapping, count); Py_DECREF(labels); Py_DECREF(mapping); return result; }
Token *build_type (typeID t, Token o, Token ret[]) { /* XXX: elliptics */ if (is_reference (t)) t = ptrdown (dereference (t)); Token tmp [100], *dcls = &tmp [20], *dcle = dcls; Token *st = open_typeID (t); int i = 1, b = 0; if (o) { *(++dcle) = -1; *dcls-- = o; } else *dcls-- = -1; for (;;i++) { switch (st [i]) { case '*': *dcls-- = '*'; b = 1; continue; case '[': if (b) *dcls-- = '(', *dcle++ = ')', b = 0; *dcle++ = '['; *dcle++ = ']'; continue; case '(': if (b) *dcls-- = '(', *dcle++ = ')', b = 0; *dcle++ = '('; for (i++;;) if (st [i] == B_ELLIPSIS) { *dcle++ = ELLIPSIS; break; } else { if (st [i] == INTERNAL_ARGEND) break; Token arg [50]; intcpy (dcle, build_type (st [i++], 0, arg)); dcle += intlen (dcle); *dcle++ = ','; } if (dcle [-1] == ',') --dcle; *dcle++ = ')'; continue; case -1: break; default: PRINTF ("UNKNWOWN %i\n", st [i]); } break; } *dcle = -1; if (st [0] >= 0) if (ISSYMBOL (st [0])) sintprintf (ret, st [0], -1); else sintprintf (ret, isunion (st [0]) ? RESERVED_union : iRESERVED_struct (st [0]), name_of_struct (st [0]), -1); else switch (st [0]) { case B_UCHAR: sintprintf (ret, RESERVED_unsigned, RESERVED_char, -1); ncase B_SCHAR: sintprintf (ret, RESERVED_char, -1); ncase B_USINT: sintprintf (ret, RESERVED_unsigned, RESERVED_short, RESERVED_int, -1); ncase B_SSINT: sintprintf (ret, RESERVED_short, RESERVED_int, -1); ncase B_UINT: sintprintf (ret, RESERVED_unsigned, RESERVED_int, -1); ncase B_SINT: sintprintf (ret, RESERVED_int, -1); ncase B_ULONG: sintprintf (ret, RESERVED_unsigned, RESERVED_long, -1); ncase B_SLONG: sintprintf (ret, RESERVED_long, -1); ncase B_ULLONG: sintprintf (ret, RESERVED_unsigned, RESERVED_long, RESERVED_long, -1); ncase B_SLLONG: sintprintf (ret, RESERVED_long, RESERVED_long, -1); ncase B_FLOAT: sintprintf (ret, RESERVED_float, -1); ncase B_DOUBLE: sintprintf (ret, RESERVED_double, -1); ncase B_VOID: sintprintf (ret, RESERVED_void, -1); } intcat (ret, dcls + 1); return ret; }
static PyObject *parseSelex(PyObject *self, PyObject *args) { /* Parse sequences from *filename* into the the memory pointed by the Numpy array passed as Python object. */ char *filename; PyArrayObject *msa; if (!PyArg_ParseTuple(args, "sO", &filename, &msa)) return NULL; long i = 0, beg = 0, end = 0; long size = SELEXLINELEN + 1, iline = 0, seqlen = 0; char errmsg[LENLABEL] = "failed to parse SELEX/Stockholm file at line "; PyObject *labels = PyList_New(0), *mapping = PyDict_New(); if (!labels || !mapping) return PyErr_NoMemory(); char *line = malloc(size * sizeof(char)); if (!line) return PyErr_NoMemory(); char *data = (char *) PyArray_DATA(msa); /* figure out where the sequence starts and ends in a line*/ FILE *file = fopen(filename, "rb"); while (fgets(line, size, file) != NULL) { iline++; if (line[0] == '#' || line[0] == '/' || line[0] == '%') continue; for (i = 0; i < size; i++) if (line[i] == ' ') break; for (; i < size; i++) if (line[i] != ' ') break; beg = i; for (; i < size; i++) if (line[i] < 32) break; end = i; seqlen = end - beg; break; } iline--; fseek(file, - strlen(line), SEEK_CUR); long index = 0, count = 0; int space = beg - 1; /* index of space character before sequence */ while (fgets(line, size, file) != NULL) { iline++; if (line[0] == '#' || line[0] == '/' || line[0] == '%') continue; if (line[space] != ' ') { free(line); fclose(file); PyErr_SetString(PyExc_IOError, intcat(errmsg, iline)); return NULL; } count += parseLabel(labels, mapping, line, space); for (i = beg; i < end; i++) data[index++] = line[i]; } fclose(file); free(line); npy_intp dims[2] = {index / seqlen, seqlen}; PyArray_Dims arr_dims; arr_dims.ptr = dims; arr_dims.len = 2; PyArray_Resize(msa, &arr_dims, 0, NPY_CORDER); PyObject *result = Py_BuildValue("(OOOi)", msa, labels, mapping, count); Py_DECREF(labels); Py_DECREF(mapping); return result; }