/********************************************************************** * * firstpos * * Fills firstpos-field in syntax tree. Here we suppose, that all * nodes below current node are already visited (depth-first traversal). * Firstpos is a set of positions that can match the first symbol of a * string generated by the subexpression rooted by node. */ static void firstpos(REG1 int node, REG2 node_t* tnode) { switch (tnode->type) { case EMPTY_ID: set_clear(tnode->firstpos, rbuf.setsize); break; case CLASS_ID: case ID: add_set(tnode->firstpos, node); break; case OR: set_union(tnode->firstpos, rbuf.tree[tnode->val.next.left].firstpos, rbuf.tree[tnode->val.next.right].firstpos, rbuf.setsize); break; case CAT: if (rbuf.tree[tnode->val.next.left].nullable) set_union(tnode->firstpos, rbuf.tree[tnode->val.next.left].firstpos, rbuf.tree[tnode->val.next.right].firstpos, rbuf.setsize); else set_copy(tnode->firstpos, rbuf.tree[tnode->val.next.left].firstpos, rbuf.setsize); break; case CLOSURE: set_copy(tnode->firstpos, rbuf.tree[tnode->val.next.left].firstpos, rbuf.setsize); break; } }
/* ------------------------------------------------------------------------- * --- set_new * ------------------------------------------------------------------------- */ Set* set_prod_new(const Set* a, const Set* b) { Set* set; assert(set_is_valid(a)); assert(set_is_valid(b)); assert(a->head.type != SET_PSEUDO); assert(b->head.type != SET_PSEUDO); if (a->head.type == SET_EMPTY || b->head.type == SET_EMPTY) return set_empty_new(a->head.dim + b->head.dim); set = calloc(1, sizeof(*set)); assert(set != NULL); set->head.refc = 1; set->head.dim = a->head.dim + b->head.dim; set->head.members = a->head.members * b->head.members; set->head.type = SET_PROD; set->prod.set_a = set_copy(a); set->prod.set_b = set_copy(b); SID_set2(set->prod, SET_PROD_SID); assert(set_prod_is_valid(set)); return set; }
void ddf_InitialDataSetup(ddf_ConePtr cone) { long j, r; ddf_rowset ZSet; static ddf_Arow Vector1,Vector2; static ddf_colrange last_d=0; if (last_d < cone->d){ if (last_d>0) { for (j=0; j<last_d; j++){ ddf_clear(Vector1[j]); ddf_clear(Vector2[j]); } free(Vector1); free(Vector2); } Vector1=(myfloat*)calloc(cone->d,sizeof(myfloat)); Vector2=(myfloat*)calloc(cone->d,sizeof(myfloat)); for (j=0; j<cone->d; j++){ ddf_init(Vector1[j]); ddf_init(Vector2[j]); } last_d=cone->d; } cone->RecomputeRowOrder=ddf_FALSE; cone->ArtificialRay = NULL; cone->FirstRay = NULL; cone->LastRay = NULL; set_initialize(&ZSet,cone->m); ddf_AddArtificialRay(cone); set_copy(cone->AddedHalfspaces, cone->InitialHalfspaces); set_copy(cone->WeaklyAddedHalfspaces, cone->InitialHalfspaces); ddf_UpdateRowOrderVector(cone, cone->InitialHalfspaces); for (r = 1; r <= cone->d; r++) { for (j = 0; j < cone->d; j++){ ddf_set(Vector1[j], cone->B[j][r-1]); ddf_neg(Vector2[j], cone->B[j][r-1]); } ddf_Normalize(cone->d, Vector1); ddf_Normalize(cone->d, Vector2); ddf_ZeroIndexSet(cone->m, cone->d, cone->A, Vector1, ZSet); if (set_subset(cone->EqualitySet, ZSet)){ if (ddf_debug) { fprintf(stderr,"add an initial ray with zero set:"); set_fwrite(stderr,ZSet); } ddf_AddRay(cone, Vector1); if (cone->InitialRayIndex[r]==0) { ddf_AddRay(cone, Vector2); if (ddf_debug) { fprintf(stderr,"and add its negative also.\n"); } } } } ddf_CreateInitialEdges(cone); cone->Iteration = cone->d + 1; if (cone->Iteration > cone->m) cone->CompStatus=ddf_AllFound; /* 0.94b */ set_free(ZSet);
/* ------------------------------------------------------------------------- * --- copy * ------------------------------------------------------------------------- */ static Set* set_prod_copy(const Set* source) { Set* set = (Set*)source; set->head.refc++; (void)set_copy(set->prod.set_a); (void)set_copy(set->prod.set_b); return set; }
void expand1_gasp(pset_family F, pset_family D, pset_family R, pset_family Foriginal, int c1index, pset_family *G) { register int c2index; register pset p, last, c2under; pset RAISE, FREESET, temp, *FD, c2essential; pset_family F1; if (debug & 0x0008) { printf("\nEXPAND1_GASP: \t%s\n", pc1(((F)->data + (F)->wsize * ( c1index)))); } RAISE = set_clear(((unsigned int *) malloc(sizeof(unsigned int) * ( ((cube.size) <= 32 ? 2 : (((((cube.size)-1) >> 5) + 1) + 1))))), cube.size); FREESET = set_clear(((unsigned int *) malloc(sizeof(unsigned int) * ( ((cube.size) <= 32 ? 2 : (((((cube.size)-1) >> 5) + 1) + 1))))), cube.size); temp = set_clear(((unsigned int *) malloc(sizeof(unsigned int) * ( ((cube.size) <= 32 ? 2 : (((((cube.size)-1) >> 5) + 1) + 1))))), cube.size); R->active_count = R->count; for( p=R->data, last= p+R->count*R->wsize; p< last; p+=R->wsize) { (p[0] |= ( 0x2000)); } F->active_count = F->count; for( c2under=F->data, c2index=0; c2index<F->count; c2under+=F->wsize, c2index++) { if (c1index == c2index || (c2under[0] & ( 0x8000))) { F->active_count--; (c2under[0] &= ~ ( 0x2000)); } else { (c2under[0] |= ( 0x2000)); } } (void) set_copy(RAISE, ((F)->data + (F)->wsize * ( c1index))); (void) set_diff(FREESET, cube.fullset, RAISE); essen_parts(R, F, RAISE, FREESET); essen_raising(R, RAISE, FREESET); for( c2under=F->data, c2index=0; c2index<F->count; c2under+=F->wsize, c2index++) { if ((c2under[0] & ( 0x2000))) { if (setp_implies(c2under, RAISE) || feasibly_covered(R, c2under, RAISE, temp)) { F1 = sf_save(Foriginal); (void) set_copy(((F1)->data + (F1)->wsize * ( c1index)), ((F)->data + (F)->wsize * ( c1index))); FD = cube2list(F1, D); c2essential = reduce_cube(FD, ((F1)->data + (F1)->wsize * ( c2index))); ((FD[0]) ? (free((char *) (FD[0])), (FD[0]) = 0) : 0); ((FD) ? (free((char *) (FD)), (FD) = 0) : 0);; sf_free(F1); if (feasibly_covered(R, c2essential, RAISE, temp)) { (void) set_or(temp, RAISE, c2essential); (temp[0] &= ~ ( 0x8000)); *G = sf_addset(*G, temp); } ((c2essential) ? (free((char *) (c2essential)), (c2essential) = 0) : 0); } } } ((RAISE) ? (free((char *) (RAISE)), (RAISE) = 0) : 0); ((FREESET) ? (free((char *) (FREESET)), (FREESET) = 0) : 0); ((temp) ? (free((char *) (temp)), (temp) = 0) : 0); }
STATIC mp_obj_t set_diff_int(int n_args, const mp_obj_t *args, bool update) { assert(n_args > 0); assert(MP_OBJ_IS_TYPE(args[0], &mp_type_set)); mp_obj_set_t *self; if (update) { self = args[0]; } else { self = set_copy(args[0]); } for (int i = 1; i < n_args; i++) { mp_obj_t other = args[i]; if (self == other) { set_clear(self); } else { mp_obj_t iter = mp_getiter(other); mp_obj_t next; while ((next = mp_iternext(iter)) != MP_OBJ_STOP_ITERATION) { set_discard(self, next); } } } return self; }
STATIC mp_obj_t set_intersect_int(mp_obj_t self_in, mp_obj_t other, bool update) { assert(MP_OBJ_IS_TYPE(self_in, &mp_type_set)); if (self_in == other) { return update ? mp_const_none : set_copy(self_in); } mp_obj_set_t *self = self_in; mp_obj_set_t *out = mp_obj_new_set(0, NULL); mp_obj_t iter = mp_getiter(other); mp_obj_t next; while ((next = mp_iternext(iter)) != MP_OBJ_STOP_ITERATION) { if (mp_set_lookup(&self->set, next, MP_MAP_LOOKUP)) { set_add(out, next); } } if (update) { m_del(mp_obj_t, self->set.table, self->set.alloc); self->set.alloc = out->set.alloc; self->set.used = out->set.used; self->set.table = out->set.table; } return update ? mp_const_none : out; }
pcover reduce(pset_family F, pset_family D) { register pcube last, p, cunder, *FD; /* Order the cubes */ if (use_random_order) F = random_order(F); else { F = toggle ? sort_reduce(F) : mini_sort(F, (qsort_compare_func) descend); toggle = ! toggle; } /* Try to reduce each cube */ FD = cube2list(F, D); foreach_set(F, last, p) { cunder = reduce_cube(FD, p); /* reduce the cube */ if (setp_equal(cunder, p)) { /* see if it actually did */ SET(p, ACTIVE); /* cube remains active */ SET(p, PRIME); /* cube remains prime ? */ } else { if (debug & REDUCE) { printf("REDUCE: %s to %s %s\n", pc1(p), pc2(cunder), print_time(ptime())); } set_copy(p, cunder); /* save reduced version */ RESET(p, PRIME); /* cube is no longer prime */ if (setp_empty(cunder)) RESET(p, ACTIVE); /* if null, kill the cube */ else SET(p, ACTIVE); /* cube is active */ } free_cube(cunder); }
/*@ set_union - find the union of two sets Input arguments: + s - The first set - t - The second set Output arguments: None Returns: A new set that is the union of the two input sets if successful, NULL otherwise. Notes: s and t must contain the same types of elements @*/ struct set *set_union(struct set *s, struct set *t) { void * e; struct set *tmp, *u, *v; /* Try to make sure that these two sets contain the same types */ if (s->cmp != t->cmp || s->elemsize != t->elemsize) return NULL; /* Whichever set we iterate over should be smaller because iteration is O(n) */ /* while bsearch is O(log n) */ if (s->size < t->size) u = s, v = t; else u = t, v = s; tmp = set_copy(v); if (tmp == NULL) return NULL; /* Insert all the elements of u not in v */ for (set_reset(u), e = set_next(u); e != NULL; e = set_next(u)){ if (!set_exists(v, e)) set_insert(tmp, e); } return tmp; }
STATIC mp_obj_t set_intersect_int(mp_obj_t self_in, mp_obj_t other, bool update) { if (update) { check_set(self_in); } else { check_set_or_frozenset(self_in); } if (self_in == other) { return update ? mp_const_none : set_copy(self_in); } mp_obj_set_t *self = MP_OBJ_TO_PTR(self_in); mp_obj_set_t *out = MP_OBJ_TO_PTR(mp_obj_new_set(0, NULL)); mp_obj_t iter = mp_getiter(other, NULL); mp_obj_t next; while ((next = mp_iternext(iter)) != MP_OBJ_STOP_ITERATION) { if (mp_set_lookup(&self->set, next, MP_MAP_LOOKUP)) { set_add(MP_OBJ_FROM_PTR(out), next); } } if (update) { m_del(mp_obj_t, self->set.table, self->set.alloc); self->set.alloc = out->set.alloc; self->set.used = out->set.used; self->set.table = out->set.table; } return update ? mp_const_none : MP_OBJ_FROM_PTR(out); }
STATIC mp_obj_t set_diff_int(size_t n_args, const mp_obj_t *args, bool update) { mp_obj_t self; if (update) { check_set(args[0]); self = args[0]; } else { self = set_copy(args[0]); } for (size_t i = 1; i < n_args; i++) { mp_obj_t other = args[i]; if (self == other) { set_clear(self); } else { mp_set_t *self_set = &((mp_obj_set_t*)MP_OBJ_TO_PTR(self))->set; mp_obj_t iter = mp_getiter(other, NULL); mp_obj_t next; while ((next = mp_iternext(iter)) != MP_OBJ_STOP_ITERATION) { mp_set_lookup(self_set, next, MP_MAP_LOOKUP_REMOVE_IF_FOUND); } } } return self; }
/**Function************************************************************* Synopsis [Converts SOP in ABC into SOP representation in Espresso.] Description [] SideEffects [] SeeAlso [] ***********************************************************************/ pset_family Abc_SopToEspresso( char * pSop ) { char * pCube; pset_family Cover; pset set; int nCubes, nVars, Value, v; if ( pSop == NULL ) return NULL; nVars = Abc_SopGetVarNum(pSop); nCubes = Abc_SopGetCubeNum(pSop); assert( cube.size == 2 * nVars ); if ( Abc_SopIsConst0(pSop) ) { Cover = sf_new(0, cube.size); return Cover; } if ( Abc_SopIsConst1(pSop) ) { Cover = sf_new(1, cube.size); set = GETSET(Cover, Cover->count++); set_copy( set, cube.fullset ); return Cover; } // create the cover Cover = sf_new(nCubes, cube.size); // fill in the cubes Abc_SopForEachCube( pSop, nVars, pCube ) { set = GETSET(Cover, Cover->count++); set_copy( set, cube.fullset ); Abc_CubeForEachVar( pCube, Value, v ) { if ( Value == '0' ) set_remove(set, 2*v+1); else if ( Value == '1' ) set_remove(set, 2*v); } }
set *set_union(set *s1, set *s2) { set_iter itr; char *str; set *s = set_copy(s1); set_iter_init(s2, &itr); while((str = set_iterate(s2, &itr)) != NULL) set_add(s, str); return s; }
/* * db_seq_copy() - This function makes a copy of the given source. The copied * sequence will be a free set that is not owned by an object and will not * be persistent in the database. * return : a new sequence * source(in): a sequence to copy * * note : To make the sequence persistent, you must assign it as the value of * an object attribute. If you do not assign the sequence to an attribute, * it must be freed manually with db_seq_free function */ DB_SEQ * db_seq_copy (DB_SEQ * source) { DB_SEQ *new_ = NULL; CHECK_CONNECT_NULL (); if (source != NULL) { new_ = set_copy (source); } return (new_); }
/* else return FALSE. */ bool Is_Any_Connect(pcover WSS, pcover P) { register int i, ind; register pset q, x=new_cube(), r=new_cube(); int pos_p; foreachi_set(P, i, q){ ind = Get_Var_Ind(q, 0); set_copy(x, GETSET(WSS, ind)); set_diff(r, x, q); if (!setp_empty(r)) return TRUE; }
/* * db_col_copy() - This function returns a copy of the given collection. The * new collection has the identical domain and element values as the source * collection. * return : new collection (NULL if error) * col(in): collection to copy */ DB_COLLECTION * db_col_copy (DB_COLLECTION * col) { DB_COLLECTION *new_ = NULL; CHECK_CONNECT_NULL (); if (col != NULL) { new_ = set_copy (col); } return new_; }
mxArray * ZH_set_Vlist(const dd_SetFamilyPtr Inc, const dd_SetFamilyPtr Adj) { mxArray *A, *row; double *r; dd_bigrange i, j; /* unsigned long int */ dd_bigrange cur; set_type s, adj; int count, total; int num; if(Inc){ num = (int) Inc->famsize; A = mxCreateCellMatrix(num,1); for (i=0;i<Inc->famsize;i++){ set_initialize(&s, Inc->set[i][0]); set_copy(s, Inc->set[i]); count=0; if (set_card(s)>0) { /*get the first element in the set*/ for (j=1;j<=s[0];j++){ if (set_member(j,s)) break; } cur = j; total = set_card(s); row = mxCreateDoubleMatrix(1,total,mxREAL); r = mxGetPr(row); while (count <= total){ /* write to the output */ set_delelem(s, cur); r[count++]=(double)cur; /* find its neighbor in list */ adj = Adj->set[cur-1]; for (j=1; j<=s[0]; j++){ if ((set_member(j,adj)==1 )&&(set_member(j,s)==1)) { break; } } if (j<=s[0]) cur = j; else break; } mxSetCell(A, i, row); } } return A; } return 0; }
void set_test2(SET_TYPE& set_obj) { if (set_obj.size() < 1) { typename SET_TYPE::const_iterator s_beg = set_obj.begin(), s_end = set_obj.end(); if (s_beg != s_end) { throw std::runtime_error("failed test2 1"); } } else set_obj.clear(); set_obj.insert2(5); set_obj.insert2(4); set_obj.insert2(7); set_obj.insert2(6); set_obj.insert2(2); set_obj.insert2(3); SET_TYPE set_copy(set_obj); if (set_copy.size() != set_obj.size()) { throw std::runtime_error("failed test2 2"); } typename SET_TYPE::const_iterator s_iter = set_obj.begin(), s_end = set_obj.end(); typename SET_TYPE::const_iterator c_iter = set_copy.begin(), c_end = set_copy.end(); for(; s_iter != s_end; ++s_iter) { if (*s_iter != *c_iter) { throw std::runtime_error("failed test2 3"); } ++c_iter; } if (c_iter != c_end) { throw std::runtime_error("failed test2 4"); } }
/* Try to expand each nonprime and noncovered cube */ foreach_set(F, last, p) { /* do not expand if PRIME or if covered by previous expansion */ if (! TESTP(p, PRIME) && ! TESTP(p, COVERED)) { /* expand the cube p, result is RAISE */ expand1(R, F, RAISE, FREESET, OVEREXPANDED_CUBE, SUPER_CUBE, INIT_LOWER, &num_covered, p); if (debug & EXPAND) printf("EXPAND: %s (covered %d)\n", pc1(p), num_covered); (void) set_copy(p, RAISE); SET(p, PRIME); RESET(p, COVERED); /* not really necessary */ /* See if we generated an inessential prime */ if (num_covered == 0 && ! setp_equal(p, OVEREXPANDED_CUBE)) { SET(p, NONESSEN); } } }
Symbol* symbol_new( const char* name, SymbolType type, const Set* set, int estimated_size, const Entry* deflt) { Symbol* sym; assert(name != NULL); assert(strlen(name) > 0); assert(set != NULL); assert(estimated_size >= 0); sym = calloc(1, sizeof(*sym)); assert(sym != NULL); sym->name = name; sym->type = type; sym->size = 1; sym->used = 0; sym->extend = SYMBOL_EXTEND_SIZE; sym->set = set_copy(set); sym->hash = hash_new(HASH_ENTRY, estimated_size); sym->entry = calloc(1, sizeof(*sym->entry)); sym->deflt = (deflt != ENTRY_NULL) ? entry_copy(deflt) : ENTRY_NULL; sym->next = anchor; anchor = sym; assert(sym->entry != NULL); SID_set(sym, SYMBOL_SID); assert(symbol_is_valid(sym)); return sym; }
dd_MatrixPtr dd_BlockElimination(dd_MatrixPtr M, dd_colset delset, dd_ErrorType *error) /* Eliminate the variables (columns) delset by the Block Elimination with dd_DoubleDescription algorithm. Given (where y is to be eliminated): c1 + A1 x + B1 y >= 0 c2 + A2 x + B2 y = 0 1. First construct the dual system: z1^T B1 + z2^T B2 = 0, z1 >= 0. 2. Compute the generators of the dual. 3. Then take the linear combination of the original system with each generator. 4. Remove redundant inequalies. */ { dd_MatrixPtr Mdual=NULL, Mproj=NULL, Gdual=NULL; dd_rowrange i,h,m,mproj,mdual,linsize; dd_colrange j,k,d,dproj,ddual,delsize; dd_colindex delindex; mytype temp,prod; dd_PolyhedraPtr dualpoly; dd_ErrorType err=dd_NoError; dd_boolean localdebug=dd_FALSE; *error=dd_NoError; m= M->rowsize; d= M->colsize; delindex=(long*)calloc(d+1,sizeof(long)); dd_init(temp); dd_init(prod); k=0; delsize=0; for (j=1; j<=d; j++){ if (set_member(j, delset)){ k++; delsize++; delindex[k]=j; /* stores the kth deletion column index */ } } if (localdebug) dd_WriteMatrix(stdout, M); linsize=set_card(M->linset); ddual=m+1; mdual=delsize + m - linsize; /* #equalitions + dimension of z1 */ /* setup the dual matrix */ Mdual=dd_CreateMatrix(mdual, ddual); Mdual->representation=dd_Inequality; for (i = 1; i <= delsize; i++){ set_addelem(Mdual->linset,i); /* equality */ for (j = 1; j <= m; j++) { dd_set(Mdual->matrix[i-1][j], M->matrix[j-1][delindex[i]-1]); } } k=0; for (i = 1; i <= m; i++){ if (!set_member(i, M->linset)){ /* set nonnegativity for the dual variable associated with each non-linearity inequality. */ k++; dd_set(Mdual->matrix[delsize+k-1][i], dd_one); } } /* 2. Compute the generators of the dual system. */ dualpoly=dd_DDMatrix2Poly(Mdual, &err); Gdual=dd_CopyGenerators(dualpoly); /* 3. Take the linear combination of the original system with each generator. */ dproj=d-delsize; mproj=Gdual->rowsize; Mproj=dd_CreateMatrix(mproj, dproj); Mproj->representation=dd_Inequality; set_copy(Mproj->linset, Gdual->linset); for (i=1; i<=mproj; i++){ k=0; for (j=1; j<=d; j++){ if (!set_member(j, delset)){ k++; /* new index of the variable x_j */ dd_set(prod, dd_purezero); for (h = 1; h <= m; h++){ dd_mul(temp,M->matrix[h-1][j-1],Gdual->matrix[i-1][h]); dd_add(prod,prod,temp); } dd_set(Mproj->matrix[i-1][k-1],prod); } } } if (localdebug) printf("Size of the projection system: %ld x %ld\n", mproj, dproj); dd_FreePolyhedra(dualpoly); free(delindex); dd_clear(temp); dd_clear(prod); dd_FreeMatrix(Mdual); dd_FreeMatrix(Gdual); return Mproj; }
/* * Yes, I know this routine is a mess */ void read_cube(register FILE *fp, pPLA PLA) { register int var, i; pcube cf = cube.temp[0], cr = cube.temp[1], cd = cube.temp[2]; bool savef = FALSE, saved = FALSE, saver = FALSE; char token[256]; /* for kiss read hack */ int varx, first, last, offset; /* for kiss read hack */ set_clear(cf, cube.size); /* Loop and read binary variables */ for(var = 0; var < cube.num_binary_vars; var++) switch(getc(fp)) { case EOF: goto bad_char; case '\n': if (! line_length_error) fprintf(stderr, "product term(s) %s\n", "span more than one line (warning only)"); line_length_error = TRUE; lineno++; var--; break; case ' ': case '|': case '\t': var--; break; case '2': case '-': set_insert(cf, var*2+1); case '0': set_insert(cf, var*2); break; case '1': set_insert(cf, var*2+1); break; case '?': break; default: goto bad_char; } /* Loop for the all but one of the multiple-valued variables */ for(var = cube.num_binary_vars; var < cube.num_vars-1; var++) /* Read a symbolic multiple-valued variable */ if (cube.part_size[var] < 0) { (void) fscanf(fp, "%s", token); if (equal(token, "-") || equal(token, "ANY")) { if (kiss && var == cube.num_vars - 2) { /* leave it empty */ } else { /* make it full */ set_or(cf, cf, cube.var_mask[var]); } } else if (equal(token, "~")) { ; /* leave it empty ... (?) */ } else { if (kiss && var == cube.num_vars - 2) varx = var - 1, offset = ABS(cube.part_size[var-1]); else varx = var, offset = 0; /* Find the symbolic label in the label table */ first = cube.first_part[varx]; last = cube.last_part[varx]; for(i = first; i <= last; i++) if (PLA->label[i] == (char *) NULL) { PLA->label[i] = strdup(token); /* add new label */ set_insert(cf, i+offset); break; } else if (equal(PLA->label[i], token)) { set_insert(cf, i+offset); /* use column i */ break; } if (i > last) { fprintf(stderr, "declared size of variable %d (counting from variable 0) is too small\n", var); exit(-1); } } } else for(i = cube.first_part[var]; i <= cube.last_part[var]; i++) switch (getc(fp)) { case EOF: goto bad_char; case '\n': if (! line_length_error) fprintf(stderr, "product term(s) %s\n", "span more than one line (warning only)"); line_length_error = TRUE; lineno++; i--; break; case ' ': case '|': case '\t': i--; break; case '1': set_insert(cf, i); case '0': break; default: goto bad_char; } /* Loop for last multiple-valued variable */ if (kiss) { saver = savef = TRUE; (void) set_xor(cr, cf, cube.var_mask[cube.num_vars - 2]); } else set_copy(cr, cf); set_copy(cd, cf); for(i = cube.first_part[var]; i <= cube.last_part[var]; i++) switch (getc(fp)) { case EOF: goto bad_char; case '\n': if (! line_length_error) fprintf(stderr, "product term(s) %s\n", "span more than one line (warning only)"); line_length_error = TRUE; lineno++; i--; break; case ' ': case '|': case '\t': i--; break; case '4': case '1': if (PLA->pla_type & F_type) set_insert(cf, i), savef = TRUE; break; case '3': case '0': if (PLA->pla_type & R_type) set_insert(cr, i), saver = TRUE; break; case '2': case '-': if (PLA->pla_type & D_type) set_insert(cd, i), saved = TRUE; case '~': break; default: goto bad_char; } if (savef) PLA->F = sf_addset(PLA->F, cf); if (saved) PLA->D = sf_addset(PLA->D, cd); if (saver) PLA->R = sf_addset(PLA->R, cr); return; bad_char: fprintf(stderr, "(warning): input line #%d ignored\n", lineno); skip_line(fp, stdout, TRUE); return; }
void set_test1(SET_TYPE& set_obj) { if (set_obj.size() < 1) { typename SET_TYPE::const_iterator s_beg = set_obj.begin(), s_end = set_obj.end(); if (s_beg != s_end) { throw std::runtime_error("failed test 1"); } } else set_obj.clear(); std::pair<typename SET_TYPE::const_iterator,bool> result = set_obj.insert(5); if (!result.second) { throw std::runtime_error("failed test 2"); } result = set_obj.insert(4); if (!result.second) { throw std::runtime_error("failed test 3"); } result = set_obj.insert(7); ++(result.first); if (result.first != set_obj.end()) { throw std::runtime_error("failed test 4"); } result = set_obj.insert(6); if (!result.second) { throw std::runtime_error("failed test 5"); } ++(result.first); if (*(result.first) != 7) { throw std::runtime_error("failed test 6"); } ++(result.first); if (result.first != set_obj.end()) { throw std::runtime_error("failed test 7"); } result = set_obj.insert(2); result = set_obj.insert(3); ++(result.first); if (*(result.first) != 4) { throw std::runtime_error("failed test 8"); } SET_TYPE set_copy(set_obj); if (set_copy.size() != set_obj.size()) { throw std::runtime_error("failed test 9"); } typename SET_TYPE::const_iterator s_iter = set_obj.begin(), s_end = set_obj.end(); typename SET_TYPE::const_iterator c_iter = set_copy.begin(), c_end = set_copy.end(); for(; s_iter != s_end; ++s_iter) { if (*s_iter != *c_iter) { throw std::runtime_error("failed test 10"); } ++c_iter; } if (c_iter != c_end) { throw std::runtime_error("failed test 11"); } }
// try to find a case of xx^* and convert it to x^+ void CommutativeRExp::optimize_starplus() { // we have to be careful, we will modify the set_copy! // loop over all elements in the set and find the stared elements std::shared_ptr<std::multiset<CommutativeRExp>> set_copy(new std::multiset<CommutativeRExp>()); *set_copy = *this->setm; bool changed = false; for(auto it = this->setm->begin(); it != this->setm->end(); ++it) { if(it->type == Star) // we found a stared element { // distinguish on the nested type switch(it->rexp->type) { case Element: // easy, just try to find this element on the outer set, fall through case case Addition: // addition behaves like an element in the original multiplication set { auto elem = set_copy->find(*it->rexp); if(elem != set_copy->end()) { // create x^+ auto plus_elem = CommutativeRExp(Plus, it->rexp); // delete both x and x^* set_copy->erase(*elem); set_copy->erase(*it); // then insert x^+ set_copy->insert(plus_elem); changed = true; } break; } case Multiplication: // more tricky case, because the inner stared elements are distributed in the original multiplication set { bool found_all_elements = true; // for every element in the starred multiplication for(auto it2 = it->rexp->setm->begin(); it2 != it->rexp->setm->end(); ++it2) { auto elem = set_copy->find(*it2); if(elem == set_copy->end()) { found_all_elements = false; break; } } if(found_all_elements) { // create x^+ auto plus_elem = CommutativeRExp(Plus, it->rexp); // delete both x and x^* for(auto it2 = it->rexp->setm->begin(); it2 != it->rexp->setm->end(); ++it2) set_copy->erase(*it2); set_copy->erase(*it); // then insert x^+ set_copy->insert(plus_elem); changed = true; } break; } case Star: // should not happen, fall through case Plus: // no idea, what to do, fall through case Empty: // should not happen assert(false); // we should have been optimizing this, debug more! break; } } } if(changed) this->setm = set_copy; }
STATIC mp_obj_t set_symmetric_difference(mp_obj_t self_in, mp_obj_t other_in) { assert(MP_OBJ_IS_TYPE(self_in, &mp_type_set)); self_in = set_copy(self_in); set_symmetric_difference_update(self_in, other_in); return self_in; }
static dd_ErrorType FaceEnumHelper(dd_MatrixPtr M, dd_rowset R, dd_rowset S) { dd_ErrorType err; dd_rowset LL, ImL, RR, SS, Lbasis; dd_rowrange iprev = 0; dd_colrange dim; dd_LPSolutionPtr lps = NULL; set_initialize(&LL, M->rowsize); set_initialize(&RR, M->rowsize); set_initialize(&SS, M->rowsize); set_copy(LL, M->linset); set_copy(RR, R); set_copy(SS, S); /* note actual type of "value" is mpq_t (defined in cddmp.h) */ mytype value; dd_init(value); err = dd_NoError; dd_boolean foo = dd_ExistsRestrictedFace(M, R, S, &err); if (err != dd_NoError) { #ifdef MOO fprintf(stderr, "err from dd_ExistsRestrictedFace\n"); fprintf(stderr, "err = %d\n", err); #endif /* MOO */ set_free(LL); set_free(RR); set_free(SS); dd_clear(value); return err; } if (foo) { set_uni(M->linset, M->linset, R); err = dd_NoError; dd_FindRelativeInterior(M, &ImL, &Lbasis, &lps, &err); if (err != dd_NoError) { #ifdef MOO fprintf(stderr, "err from dd_FindRelativeInterior\n"); fprintf(stderr, "err = %d\n", err); #endif /* MOO */ dd_FreeLPSolution(lps); set_free(ImL); set_free(Lbasis); set_free(LL); set_free(RR); set_free(SS); dd_clear(value); return err; } dim = M->colsize - set_card(Lbasis) - 1; set_uni(M->linset, M->linset, ImL); SEXP mydim, myactive, myrip; PROTECT(mydim = ScalarInteger(dim)); PROTECT(myactive = rr_set_fwrite(M->linset)); int myd = (lps->d) - 2; PROTECT(myrip = allocVector(STRSXP, myd)); for (int j = 1; j <= myd; j++) { dd_set(value, lps->sol[j]); char *zstr = NULL; zstr = mpq_get_str(zstr, 10, value); SET_STRING_ELT(myrip, j - 1, mkChar(zstr)); free(zstr); } REPROTECT(dimlist = CONS(mydim, dimlist), dimidx); REPROTECT(riplist = CONS(myrip, riplist), ripidx); REPROTECT(activelist = CONS(myactive, activelist), activeidx); UNPROTECT(3); dd_FreeLPSolution(lps); set_free(ImL); set_free(Lbasis); if (dim > 0) { for (int i = 1; i <= M->rowsize; i++) { if ((! set_member(i, M->linset)) && (! set_member(i, S))) { set_addelem(RR, i); if (iprev) { set_delelem(RR, iprev); set_delelem(M->linset, iprev); set_addelem(SS, iprev); } iprev = i; err = FaceEnumHelper(M, RR, SS); if (err != dd_NoError) { #ifdef MOO fprintf(stderr, "err from FaceEnumHelper\n"); fprintf(stderr, "err = %d\n", err); #endif /* MOO */ set_copy(M->linset, LL); set_free(LL); set_free(RR); set_free(SS); dd_clear(value); return err; } } } } } set_copy(M->linset, LL); set_free(LL); set_free(RR); set_free(SS); dd_clear(value); return dd_NoError; }
STATIC mp_obj_t set_union(mp_obj_t self_in, mp_obj_t other_in) { check_set_or_frozenset(self_in); mp_obj_set_t *self = set_copy(self_in); set_update_int(self, other_in); return self; }
STATIC mp_obj_t set_union(mp_obj_t self_in, mp_obj_t other_in) { check_set_or_frozenset(self_in); mp_obj_t self = set_copy(self_in); set_update_int(MP_OBJ_TO_PTR(self), other_in); return self; }
// the plus argument indicates, whether there is the SR-One element in the set or not void CommutativeRExp::optimize(bool one) { std::shared_ptr<std::set<CommutativeRExp>> set_copy(new std::set<CommutativeRExp>()); *set_copy = *this->seta; bool changed = false; bool used_one = false; for(auto it = this->seta->begin(); it != this->seta->end(); ++it) { if(one && it->type == Plus) // 1 + x^+ = 1 + x^* // remove the one later { auto elem = set_copy->find(*it); if(elem != set_copy->end()) { auto tmp = *elem; tmp.type = Star; set_copy->erase(*elem); set_copy->insert(tmp); changed = true; used_one = true; } } if(it->type == Star) // we found a stared element { // distinguish on the nested type switch(it->rexp->type) { case Element: // easy, just try to find this element on the outer set, fall through case case Multiplication: // multiplication behaves like an element in the original addition set { auto elem = set_copy->find(*it->rexp); if(elem != set_copy->end()) { // delete x set_copy->erase(*elem); changed = true; } break; } case Addition: // more tricky case, because the inner stared elements are distributed in the original addition set { bool found_all_elements = true; // for every element in the starred multiplication for(auto it2 = it->rexp->seta->begin(); it2 != it->rexp->seta->end(); ++it2) { auto elem = set_copy->find(*it2); if(elem == set_copy->end()) { found_all_elements = false; break; } } if(found_all_elements) { // delete all the elements in x* in the original addition set for(auto it2 = it->rexp->seta->begin(); it2 != it->rexp->seta->end(); ++it2) set_copy->erase(*it2); changed = true; } break; } case Star: // should not happen, fall through case Plus: // no idea, what to do, fall through case Empty: // should not happen assert(false); // we should have been optimizing this, debug more! break; } } } if(changed) { this->seta = set_copy; if(used_one) { auto o = this->seta->find(this->one()); if(o == this->seta->end()) assert(false); // this should not happen this->seta->erase(*o); // delete the one element } } }
STATIC mp_obj_t set_union(mp_obj_t self_in, mp_obj_t other_in) { assert(MP_OBJ_IS_TYPE(self_in, &mp_type_set)); mp_obj_set_t *self = set_copy(self_in); set_update_int(self, other_in); return self; }