dict_t *insert_word( dict_t *d, char *word ) { // Insert word into dict or increment count if already there // return pointer to the updated dict dict_t *nd; dict_t *pd = NULL; // prior to insertion point dict_t *di = d; // following insertion point // Search down list to find if present or point of insertion while(di && ( strcmp(word, di->word ) >= 0) ) { if( strcmp( word, di->word ) == 0 ) { di->count++; // increment count return d; // return head } pd = di; // advance ptr pair di = di->next; } nd = make_dict(word); // not found, make entry nd->next = di; // entry bigger than word or tail if (pd) { pd->next = nd; return d; // insert beond head } return nd; }
static PyObject *set_union(PyObject *module, PyObject *args) { PyObject *a, *b, *item; if (!PyArg_ParseTuple(args, "OO:Union", &a, &b)) return NULL; if (PyObject_IsTrue(a) == 0) { /* empty set a, reuse b */ return PySequence_List(b); } else if (PyObject_IsTrue(b) == 0) { /* empty set b, reuse a */ return PySequence_List(a); } /* create a new set containing the union of a and b */ a = make_dict(a); if (a == NULL) return NULL; b = PyObject_GetIter(b); if (b == NULL) { Py_DECREF(a); return NULL; } while ((item = PyIter_Next(b)) != NULL) { if (PyDict_SetItem(a, item, Py_True) == -1) { Py_DECREF(item); Py_DECREF(b); Py_DECREF(a); return NULL; } Py_DECREF(item); } Py_DECREF(b); return make_ordered_set(a); }
void AttributeType::clone(const AttributeType *v) { attr_free(); if (v->is_string()) { this->make_string(v->to_string()); } else if (v->is_data()) { this->make_data(v->size(), v->data()); } else if (v->is_list()) { make_list(v->size()); for (unsigned i = 0; i < v->size(); i++ ) { u_.list[i].clone(v->list(i)); } } else if (v->is_dict()) { make_dict(); realloc_dict(v->size()); for (unsigned i = 0; i < v->size(); i++ ) { u_.dict[i].key_.make_string(v->dict_key(i)->to_string()); u_.dict[i].value_.clone(v->dict_value(i)); } } else { this->kind_ = v->kind_; this->u_ = v->u_; this->size_ = v->size_; } }
static void test_dict(void) { Dict *dict = make_dict(); assert_null(dict_get(dict, "abc")); dict_put(dict, "abc", (void *)50); dict_put(dict, "xyz", (void *)70); assert_int(50, (long)dict_get(dict, "abc")); assert_int(70, (long)dict_get(dict, "xyz")); assert_int(2, list_len(dict_keys(dict))); }
int main(int argc, char *argv[]) { char *outputdir = NULL, *bname, *dname, *dirc, *basec, *dslfilename, *dictfilename; int option_char; pcre_callout = callout; while (-1 != (option_char = getopt(argc, argv, "o:h"))) { switch (option_char) { case 'o': outputdir = strdup(optarg); break; case 'h': default: print_usage(); return 0; } } if (!argv[optind] || argv[optind+1]) { print_usage(); return 1; } dslfilename = argv[optind]; if ( !(outputdir) ) { char *suffpos; outputdir = dslfilename; if ( (suffpos = strstr(outputdir, ".dsl")) ) *suffpos = 0; } dirc = strdup(outputdir); basec = strdup(outputdir); dname = (char *)dirname(dirc); //Cast to avoid warning messages from compiler bname = (char *)basename(basec); //Cast to avoid warning messages from compiler if ( !(outputdir = (char *)malloc(strlen(dname)+strlen(bname)+3)) ) { fprintf(stderr, "In file %s, line %d ", __FILE__, __LINE__); perror("error allocate memory"); exit(errno); } sprintf(outputdir, "%s/%s/", dname, bname); if ( mkdir(outputdir, 0755) ) { perror("Error create directory"); return errno; } dictfilename = make_dict(dslfilename, outputdir, bname); make_idxifo(dictfilename, outputdir, bname); //FIXME: correctly release memory for compiled regex and pcre_extra() free(dictfilename); free(outputdir); return 0; }
static PyObject *set_unique(PyObject *module, PyObject *args) { PyObject *a; if (!PyArg_ParseTuple(args, "O:Unique", &a)) return NULL; if (PyObject_IsTrue(a) == 0) /* empty set, return new empty set */ return PyList_New(0); a = make_dict(a); if (a == NULL) return NULL; return make_ordered_set(a); }
static PyObject *set_intersection(PyObject *module, PyObject *args) { PyObject *a, *b, *set, *item; if (!PyArg_ParseTuple(args, "OO:Intersection", &a, &b)) return NULL; if (PyObject_IsTrue(a) == 0 || PyObject_IsTrue(b) == 0) /* either a or b are empty so the intersection is empty as well */ return PyList_New(0); set = PyDict_New(); if (set == NULL) return NULL; a = make_dict(a); if (a == NULL) { Py_DECREF(set); return NULL; } b = PyObject_GetIter(b); if (b == NULL) { Py_DECREF(a); Py_DECREF(set); return NULL; } while ((item = PyIter_Next(b)) != NULL) { if (PyDict_GetItem(a, item) != NULL) { if (PyDict_SetItem(set, item, Py_True) == -1) { Py_DECREF(item); Py_DECREF(b); Py_DECREF(a); Py_DECREF(set); return NULL; } } Py_DECREF(item); } Py_DECREF(b); Py_DECREF(a); return make_ordered_set(set); }
static PyObject *set_not(PyObject *module, PyObject *args) { PyObject *a, *b, *item; if (!PyArg_ParseTuple(args, "OO:Not", &a, &b)) return NULL; if (PyObject_IsTrue(a) == 0) { /* nothing to subtract from, return empty set */ return PyList_New(0); } else if (PyObject_IsTrue(b) == 0) { /* nothing to subtract, use original set */ return PySequence_List(a); } /* subtract set b from set a */ a = make_dict(a); if (a == NULL) return NULL; b = PyObject_GetIter(b); if (b == NULL) { Py_DECREF(a); return NULL; } while ((item = PyIter_Next(b)) != NULL) { if (PyDict_DelItem(a, item) == -1) { if (PyErr_ExceptionMatches(PyExc_KeyError)) { PyErr_Clear(); } else { Py_DECREF(item); Py_DECREF(b); Py_DECREF(a); return NULL; } } Py_DECREF(item); } Py_DECREF(b); return make_ordered_set(a); }
dict_t * insert_word( dict_t *d, char *word ) { dict_t *nd; dict_t *pd = NULL; dict_t *di = d; while(di && ( strcmp(word, di->word ) >= 0) ) { if( strcmp( word, di->word ) == 0 ) { di->count++; return d; } pd = di; di = di->next; } nd = make_dict(word); nd->next = di; if (pd) { pd->next = nd; return d; } return nd; }
void LiftingLrDpleInternal::init() { form_ = getOptionEnumValue("form"); // Initialize the base classes LrDpleInternal::init(); casadi_assert_message(!pos_def_, "pos_def option set to True: Solver only handles the indefinite case."); casadi_assert_message(const_dim_, "const_dim option set to False: Solver only handles the True case."); // We will construct an MXFunction to facilitate the calculation of derivatives MX As = MX::sym("As", input(LR_DLE_A).sparsity()); MX Vs = MX::sym("Vs", input(LR_DLE_V).sparsity()); MX Cs = MX::sym("Cs", input(LR_DLE_C).sparsity()); MX Hs = MX::sym("Hs", input(LR_DLE_H).sparsity()); n_ = A_[0].size1(); // Chop-up the arguments std::vector<MX> As_ = horzsplit(As, n_); std::vector<MX> Vs_ = horzsplit(Vs, V_[0].size2()); std::vector<MX> Cs_ = horzsplit(Cs, V_[0].size2()); std::vector<MX> Hs_; if (with_H_) { Hs_ = horzsplit(Hs, Hsi_); } MX A; if (K_==1) { A = As; } else { if (form_==0) { MX AL = diagcat(vector_slice(As_, range(As_.size()-1))); MX AL2 = horzcat(AL, MX(AL.size1(), As_[0].size2())); MX AT = horzcat(MX(As_[0].size1(), AL.size2()), As_.back()); A = vertcat(AT, AL2); } else { MX AL = diagcat(reverse(vector_slice(As_, range(As_.size()-1)))); MX AL2 = horzcat(MX(AL.size1(), As_[0].size2()), AL); MX AT = horzcat(As_.back(), MX(As_[0].size1(), AL.size2())); A = vertcat(AL2, AT); } } MX V; MX C; MX H; if (form_==0) { V = diagcat(Vs_.back(), diagcat(vector_slice(Vs_, range(Vs_.size()-1)))); if (with_C_) { C = diagcat(Cs_.back(), diagcat(vector_slice(Cs_, range(Cs_.size()-1)))); } } else { V = diagcat(diagcat(reverse(vector_slice(Vs_, range(Vs_.size()-1)))), Vs_.back()); if (with_C_) { C = diagcat(diagcat(reverse(vector_slice(Cs_, range(Cs_.size()-1)))), Cs_.back()); } } if (with_H_) { H = diagcat(form_==0? Hs_ : reverse(Hs_)); } // QP solver options Dict options; if (hasSetOption(optionsname())) { options = getOption(optionsname()); } options["Hs"] = Hss_; // Create an LrDleSolver instance solver_ = LrDleSolver("solver", getOption(solvername()), make_map("a", A.sparsity(), "v", V.sparsity(), "c", C.sparsity(), "h", H.sparsity()), options); std::vector<MX> v_in(LR_DPLE_NUM_IN); v_in[LR_DLE_A] = As; v_in[LR_DLE_V] = Vs; if (with_C_) { v_in[LR_DLE_C] = Cs; } if (with_H_) { v_in[LR_DLE_H] = Hs; } MX Pf = solver_(make_map("a", A, "v", V, "c", C, "h", H)).at(solver_.outputName(0)); std::vector<MX> Ps = with_H_ ? diagsplit(Pf, Hsi_) : diagsplit(Pf, n_); if (form_==1) { Ps = reverse(Ps); } f_ = MXFunction(name_, v_in, dpleOut("p", horzcat(Ps)), make_dict("input_scheme", IOScheme(SCHEME_LR_DPLEInput), "output_scheme", IOScheme(SCHEME_LR_DPLEOutput))); Wrapper<LiftingLrDpleInternal>::checkDimensions(); }