PtSet SetCreateFile (char *pnomef) { PtSet Set; FILE *PtF; unsigned int Cardinal, I; char Elemento; /* abertura com validacao do ficheiro para leitura - opening the text file for reading */ if ( (PtF = fopen (pnomef, "r")) == NULL) { Error = NO_FILE; return NULL; } /* leitura do cardinal do conjunto do ficheiro - reading the set's cardinal from the text file */ fscanf (PtF, "%u", &Cardinal); fscanf (PtF, "%*c"); /* criação do set vazio - creating an empty set */ if ((Set = SetCreate ()) == NULL) { fclose (PtF); return NULL; } /* leitura dos elementos do conjunto do ficheiro - reading the set's elements from the text file */ for (I = 0; I < Cardinal ; I++) { fscanf (PtF, "%c", &Elemento); fscanf (PtF, "%*c"); if (!SetInsertElement (Set, Elemento)) { SetDestroy (&Set); fclose (PtF); return NULL; } } fclose (PtF); /* fecho do ficheiro - closing the text file */ Error = OK; return Set; /* devolve o set criado - returning the new set */ }
PtSet SetIntersection (PtSet pset1, PtSet pset2) { PtSet intersection; unsigned int i; PtNode currentNode; /* Verify sets */ if (!ValidSets(pset1, pset2)) return NULL; /* Calls the constructor */ if ((intersection = SetCreate()) == NULL) return NULL; /* Verifies the existance of the elements of pset1 in pset2 */ currentNode = pset1->Head; for (i = 0; i < pset1->Cardinal; i++) { if (SetSearchElement(pset2, *(currentNode->PtElem))) { if (SetInsertElement(intersection, *(currentNode->PtElem)) == 0 && Error == NO_MEM) { SetDestroy(&intersection); return NULL; } } currentNode = currentNode->PtNext; } Error = OK; return intersection; }
PtSet SetCopy (PtSet pset) { PtSet copy; unsigned int i; PtNode currentNode; /* Verifies if set exists */ if (pset == NULL) { Error = NO_SET; return NULL; } /* Call the constructor to create a new set */ if ((copy = SetCreate()) == NULL) return NULL; /* Copy all nodes and cardinal info */ currentNode = pset->Head; for (i = 0; i < pset->Cardinal; i++) { if (SetInsertElement(copy, *(currentNode->PtElem)) == 0 && Error == NO_MEM) { SetDestroy(©); break; } currentNode = currentNode->PtNext; } Error = OK; return copy; }
Stav* ProblemStavCreate(Problem* prb, int v, Set* m) { Stav* s = malloc(sizeof(Stav)); if(m) s->m = SetCopy(m); else s->m = SetCreate(prb->g->n); s->v = v; return s; }
static Stacklet_t* Stacklet_Alloc(StackChain_t* stackChain) { int i; Stacklet_t *res = NULL; /* Each stacklet contains the primary and replica. Each one starts with a guard page, a C area, and then an ML area. */ int size = (GuardStackletSize + MLStackletSize + CStackletSize) * kilobyte; /* for just one of the two: primary and replica */ assert(stackletOffset == size); for (i=0; i<NumStacklet; i++) if (CompareAndSwap(&Stacklets[i].count, 0, 1) == 0) { res = &Stacklets[i]; break; } if (res == NULL) DIE("out of stack space"); res->parent = stackChain; res->state = Inconsistent; if (!res->mapped) { mem_t start = my_mmap(2 * size, PROT_READ | PROT_WRITE); mem_t middle = start + size / (sizeof (val_t)); res->baseExtendedBottom = start + (GuardStackletSize * kilobyte) / (sizeof (val_t)); res->baseBottom = res->baseExtendedBottom + (CStackletSize * kilobyte) / (sizeof (val_t)); res->baseTop = res->baseBottom + (MLStackletSize * kilobyte) / (sizeof (val_t)); assert(res->baseTop == middle); /* Get some initial room in multiples of 64 bytes; Sparc requires at least 68 byte for the save area. */ res->baseTop -= (128 / sizeof(val_t)); my_mprotect(0, (caddr_t) start, GuardStackletSize * kilobyte, PROT_NONE); /* Guard page at bottom of primary */ my_mprotect(1, (caddr_t) middle, GuardStackletSize * kilobyte, PROT_NONE); /* Guard page at bottom of replica */ res->callinfoStack = SetCreate(size / (32 * sizeof (val_t))); res->mapped = 1; } res->baseCursor = res->baseTop; for (i=0; i<32; i++) res->bottomBaseRegs[i] = 0; SetReset(res->callinfoStack); return res; }
PtSet SetSymmetricDifference (PtSet pset1, PtSet pset2) { PtSet diff; unsigned int i; PtNode currentNode; /* Verify sets */ if (!ValidSets(pset1, pset2)) return NULL; /* Calls the constructor */ if ((diff = SetCreate()) == NULL) return NULL; /* Verifies the existance of the elements of pset1 in pset2 (and vice-versa) and save those who aren't commun */ currentNode = pset1->Head; for (i = 0; i < pset1->Cardinal; i++) { if (!SetSearchElement(pset2, *(currentNode->PtElem))) { if (SetInsertElement(diff, *(currentNode->PtElem)) == 0 && Error == NO_MEM) { SetDestroy(&diff); return NULL; } } currentNode = currentNode->PtNext; } currentNode = pset2->Head; for (i = 0; i < pset2->Cardinal; i++) { if (!SetSearchElement(pset1, *(currentNode->PtElem))) { if (SetInsertElement(diff, *(currentNode->PtElem)) == 0 && Error == NO_MEM) { SetDestroy(&diff); return NULL; } } currentNode = currentNode->PtNext; } Error = OK; return diff; }
PtSet SetReunion (PtSet pset1, PtSet pset2) { PtSet reunion; unsigned int i; PtNode currentNode; /* Verify sets */ if (!ValidSets(pset1, pset2)) return NULL; /* Calls the constructor */ if ((reunion = SetCreate()) == NULL) return NULL; /* Copy all nodes and cardinal info */ currentNode = pset1->Head; for (i = 0; i < pset1->Cardinal; i++) { if (SetInsertElement(reunion, *(currentNode->PtElem)) == 0) { if (Error == NO_MEM) { SetDestroy(&reunion); return NULL; } } currentNode = currentNode->PtNext; } currentNode = pset2->Head; for (i = 0; i < pset2->Cardinal; i++) { if (SetInsertElement(reunion, *(currentNode->PtElem)) == 0) { if (Error == NO_MEM) { SetDestroy(&reunion); return NULL; } } currentNode = currentNode->PtNext; } Error = OK; return reunion; }
int main (void) { PtSet set1 = NULL, set2 = NULL, set3 = NULL; char filename[21], car; int st; system ("clear"); printf ("\nLer conjunto do ficheiro - Read set from a text file\n"); do { printf ("Nome do ficheiro (Filename) -> "); st = scanf ("%20[^\n]", filename); scanf ("%*[^\n]"); scanf ("%*c"); } while (st == 0); set1 = SetCreateFile (filename); printf ("\nConjunto lido do ficheiro - Set acquired from text file %s\n", filename); WriteSet (set1); printf ("\nLer conjunto do teclado [# para terminar] - Read set from keyboard [# to stop]\n"); set2 = SetCreate (); do { do { printf ("Elemento do conjunto (Set's element) ? "); st = scanf ("%c", &car); scanf ("%*[^\n]"); scanf ("%*c"); } while (st == 0); car = toupper (car); if (isupper(car)) SetInsertElement (set2, car); } while (car != '#'); printf ("\nConjunto lido do teclado - Set acquired from keyboard\n"); WriteSet (set2); printf ("\nEscrever conjunto no ficheiro - Storing the set in a text file\n"); do { printf ("Nome do ficheiro (Filename) -> "); st = scanf ("%20[^\n]", filename); scanf ("%*[^\n]"); scanf ("%*c"); } while (st == 0); SetStoreFile (set2, filename); set3 = SetReunion (set1, set2); printf ("\nConjunto Reuniao - Union set\n"); WriteSet (set3); SetDestroy (&set3); set3 = SetIntersection (set1, set2); printf ("\nConjunto Interseccao - Intersection set\n"); WriteSet (set3); SetDestroy (&set3); set3 = SetSymmetricDifference (set1, set2); printf ("\nConjunto Diferença - Symmetric Difference set\n"); WriteSet (set3); printf ("\nDestruir os conjuntos - Releasing the sets\n"); SetDestroy (&set1); SetDestroy (&set2); SetDestroy (&set3); WriteSet (set3); return 0; }