/* Parse a HTTP server response; bail at first sign of an invalid response */ int parse_server_response(char *header_line) { char *http_version, *status_code, *reason_phrase; #ifdef DEBUG ASSERT(header_line); ASSERT(strlen(header_line) > 0); #endif http_version = header_line; if ((status_code = strchr(http_version, ' ')) == NULL) return 1; *status_code++ = '\0'; while (isspace(*status_code)) status_code++; if ((reason_phrase = strchr(status_code, ' ')) == NULL) return 1; *reason_phrase++ = '\0'; while (isspace(*reason_phrase)) reason_phrase++; insert_value("http-version", http_version); insert_value("status-code", status_code); insert_value("reason-phrase", reason_phrase); insert_value("direction", "<"); return 0; }
void Store::insert_value(Songptr root, string name, string artist, string album, string genre, double price, int year) { if (root->songName == name) { return; } else if (name < root->songName) { if (root->left == NULL) { root->left = new Song(name, artist, album, genre, price, year); } else { insert_value(root->left, name, artist, album, genre, price, year); } } else { if (root->right == NULL) { root->right = new Song(name, artist, album, genre, price, year); } else { insert_value(root->right, name, artist, album, genre, price, year); } } }
/* Parse a HTTP client request; bail at first sign of an invalid request */ int parse_client_request(char *header_line) { char *method, *request_uri, *http_version; #ifdef DEBUG ASSERT(header_line); ASSERT(strlen(header_line) > 0); #endif method = header_line; if ((request_uri = strchr(method, ' ')) == NULL) return 1; *request_uri++ = '\0'; while (isspace(*request_uri)) request_uri++; if ((http_version = strchr(request_uri, ' ')) != NULL) { *http_version++ = '\0'; while (isspace(*http_version)) http_version++; if (strncmp(http_version, HTTP_STRING, strlen(HTTP_STRING)) != 0) return 1; insert_value("http-version", http_version); } insert_value("method", method); insert_value("request-uri", request_uri); insert_value("direction", ">"); return 0; }
void insert_value(memory_arena *Arena, node **Node, char *Key, void *Value) { if(!(*Node)) { *Node = (node *)PushStruct(Arena, node); (*Node)->Value = Value; strcpy((*Node)->Key, Key); (*Node)->Left = 0; (*Node)->Right = 0; } else if(strcmp((*Node)->Key, Key) < 0) { insert_value(Arena, &((*Node)->Left), Key, Value); } else if(strcmp((*Node)->Key, Key) > 0) { insert_value(Arena, &((*Node)->Right), Key, Value); } else { (*Node)->Value = Value; } return; }
//This is the Vardi algorithm coded in C void vardir(double *x, double *y, int *m, int *n, double *tj, int *h, int *maxiter, double *tol, double *pvec, int *iter, double *xu, int *mx, double *yu, int *ny){ int i,j, finished=0; double pold[h[0]],pnew[*h],val=0.0; double xcounts[mx[0]], ycounts[ny[0]],txcounts[h[0]], tycounts[h[0]]; double tcounts[h[0]], tpk[h[0]],temp,tmp; for (i=0;i<*mx;i++) { xcounts[i]=0.0; } for (i=0;i<*ny;i++) { ycounts[i]=0.0; } for (i=0;i<*h;i++) { txcounts[i]=0.0; tycounts[i]=0.0; } /*Initializing xcounts, ycounts, tcounts, pold */ countsorted(x,m,xcounts); countsorted(y,n,ycounts); iter[0]=0; insert_value(&val,tj,h,xu,mx,xcounts,txcounts); insert_value(&val,tj,h,yu,ny,ycounts,tycounts); for (i=0;i<*h;i++) { tcounts[i]=txcounts[i]+tycounts[i]; pold[i]=tcounts[i]/h[0]; } while ((iter[0]<maxiter[0]) && (!finished)) { temp=0.0; for (j=h[0];j>0;j--) { temp+=pold[j-1]/tj[j-1]; tpk[j-1]=tycounts[j-1]/temp; } //for j in h:1 temp=0.0; for (j=0;j<*h;j++) { temp+=tpk[j]; pnew[j]=(1.0/(m[0]+n[0]))*(txcounts[j]+(1.0/tj[j])*pold[j]*temp); } tmp=0.0; for (j=0;j<*h;j++) { if (pold[j]<pnew[j]) tmp+=pnew[j]-pold[j]; else tmp+=pold[j]-pnew[j]; } if (tmp<*tol) finished=1; for (j=0;j<*h;j++) { pold[j]=pnew[j]; } iter[0]++; } //while for (j=0;j<h[0];j++) pvec[j]=pnew[j]; }
//Функция, добавляющая новый элемент в наш сет std::shared_ptr<persistent_set::node> persistent_set::insert_value(std::shared_ptr<persistent_set::node> v, std::shared_ptr<persistent_set::node> add_node) { if (v->get_value() > add_node->get_value()) { if (!v->left) { return std::shared_ptr<node>(new node(v->get_value(), add_node, v->right)); } return std::shared_ptr<node>(new node(v->get_value(), insert_value(v->left, add_node), v->right)); } if (!v->right) { return std::shared_ptr<node>(new node(v->get_value(), v->left, add_node)); } return std::shared_ptr<node>(new node(v->get_value(), v->left, insert_value(v->right, add_node))); };
non_owning_record::non_owning_record( const Record& r, std::vector<size_t> col_indexes ) { for (auto i : col_indexes) { insert_value(r.at(i)); } }
int insert_value(BTreeNode **root, int value) { if (*root == NULL) { *root = (BTreeNode*)malloc(sizeof(BTreeNode)); if (*root == NULL) printf("%s : malloc failed\n", __func__); (*root)->value = value; (*root)->left = NULL; (*root)->right = NULL; return 0; } if (value < (*root)->value) { insert_value(&((*root)->left), value); } else { insert_value(&((*root)->right), value); } return 0; }
void Store::insert(string name, string artist, string album, string genre, double price, int year) { if (root == NULL) { root = new Song(name, artist, album, genre, price, year); } else { insert_value(root, name, artist, album, genre, price, year); } }
gboolean gupnp_dlna_info_set_add_unsupported_bool (GUPnPDLNAInfoSet *info_set, const gchar *name) { g_return_val_if_fail (info_set != NULL, FALSE); g_return_val_if_fail (name != NULL, FALSE); return insert_value (info_set, name, gupnp_dlna_info_value_new_unsupported_bool ()); }
void *hello(void *tid){ int num = rand() % MAX_ALEAT; int id = *((int *)tid); do{ if( (i == 0 && num % 2 == 0) || ( num % 2 == 0 && (num == vet[i-1]+2)) ){ printf(" "); insert_value(num, id,0); } if(vet[i-1] == MAX_ALEAT-1){ insert_value(0,id,1); printf(" "); } num = rand() % MAX_ALEAT; }while(i<TAM); pthread_exit(NULL); }
/** * mnt_optstr_set_option: * @optstr: string with a comma separated list of options * @name: requested option * @value: new value or NULL * * Set or unset the option @value. * * Returns: 0 on success, 1 when not found the @name or negative number in case * of error. */ int mnt_optstr_set_option(char **optstr, const char *name, const char *value) { struct libmnt_optloc ol; char *nameend; int rc = 1; assert(optstr); assert(name); if (!optstr) return -EINVAL; mnt_init_optloc(&ol); if (*optstr) rc = mnt_optstr_locate_option(*optstr, name, &ol); if (rc < 0) return rc; /* parse error */ if (rc == 1) return mnt_optstr_append_option(optstr, name, value); /* not found */ nameend = ol.begin + ol.namesz; if (value == NULL && ol.value && ol.valsz) /* remove unwanted "=value" */ mnt_optstr_remove_option_at(optstr, nameend, ol.end); else if (value && ol.value == NULL) /* insert "=value" */ rc = insert_value(optstr, nameend, value, NULL); else if (value && ol.value && strlen(value) == ol.valsz) /* simply replace =value */ memcpy(ol.value, value, ol.valsz); else if (value && ol.value) { mnt_optstr_remove_option_at(optstr, nameend, ol.end); rc = insert_value(optstr, nameend, value, NULL); } return rc; }
int main() { int i; int n, k; int v; int unique; List * list = NULL; Snode * head = NULL; Qnode * qnode = NULL; list = make_list(list); printf("Enter n, k: "); scanf("%d %d", &n, &k); for (i=0; i<k; i++) { // initializing scanf("%d", &v); list = push_back(make_Qnode(v), list); head = insert_value(v, head); } unique = search_unique_value(head); if (unique!=MAX_INT) { printf("%d\n", unique); } else { printf("Nothing\n"); } for (i=0; i<n-k; i++) { scanf("%d", &v); list = push_back(make_Qnode(v), list); head = insert_value(v, head); qnode = pop_front(list); head = reduce_value(qnode->data, head); free(qnode); unique = search_unique_value(head); if (unique!=MAX_INT) { printf("%d\n", unique); } else { printf("Nothing\n"); } } //print_list(list); return 0; }
gboolean gupnp_dlna_info_set_add_string (GUPnPDLNAInfoSet *info_set, const gchar *name, const gchar *value) { g_return_val_if_fail (info_set != NULL, FALSE); g_return_val_if_fail (name != NULL, FALSE); g_return_val_if_fail (value != NULL, FALSE); return insert_value (info_set, name, gupnp_dlna_info_value_new_string (value)); }
// Вставка элемента. // 1. Если такой ключ уже присутствует, вставка не производиться, возвращается итератор // на уже присутствующий элемент и false. // 2. Если такого ключа ещё нет, производиться вставка, возвращается итератор на созданный // элемент и true. // Инвалидирует все итераторы, принадлежащие persistent_set'у this, включая end(). std::pair<persistent_set::iterator, bool> persistent_set::insert(value_type val) { assert(root); iterator f = find(val); if (f.value != root) { return std::make_pair(f, false); } std::shared_ptr<node> add_node = std::shared_ptr<node>(new node(val)); root->valid = false; old_root.push_back(root); root = insert_value(root, add_node); iterator ans(add_node, root); return std::make_pair(ans, true); };
/// Inserts a row in the truth table. bool MainWindow::insert_row(unsigned int row, FunctionParser* const parser) { for(int i = parser->size()-1; i>=0; --i) insert_value(getbit(i, row), row, (parser->size()-1) - i); try { insert_function(parser->eval(row), row); } catch(FunctionParser::FunctionParserException& e) { MessageBox(e.what(), "Function Evaluation Error", MB_OK | MB_ICONERROR); return false; } return true; }
void PB_DS_CLASS_C_DEC:: copy_from_range(It first_it, It last_it) { while (first_it != last_it) { insert_value(*first_it, s_no_throw_copies_ind); ++first_it; } std::make_heap(m_a_entries, m_a_entries + m_size, static_cast<entry_cmp& >(*this)); _GLIBCXX_DEBUG_ONLY(assert_valid();) }
gboolean gupnp_dlna_info_set_add_fraction (GUPnPDLNAInfoSet *info_set, const gchar *name, gint numerator, gint denominator) { g_return_val_if_fail (info_set != NULL, FALSE); g_return_val_if_fail (name != NULL, FALSE); return insert_value (info_set, name, gupnp_dlna_info_value_new_fraction (numerator, denominator)); }
int main(int argc, char **argv) { struct TreeNode *tr = NULL; int min; int i; int val[10] = {49, 15, 86, 79, 93, 35, 12, 92, 21, 77}; tr = NULL; for(i = 0; i < 10; i++) { tr = insert_value(tr, val[i]); } print_sorted_tree(tr); printf("\n"); min = minDepth(tr); printf("min depth = %d\n", min); return 0; }
int main(int argc, const char *argv[]) { BTreeNode *root = NULL; int operation = 0; int value = 0; while (operation != -1) { printf("operations\n"); printf("1 : insert a value\n"); printf("2 : show the tree\n"); printf("3 : breadth_first_traversal\n"); printf("0 : exit\n"); scanf("%d", &operation); switch (operation) { case 1: printf("input a value to insert:"); scanf("%d", &value); insert_value(&root, value); break; case 2: { printf("do preorder_traversal\n"); preorder_traversal(root); printf("end preorder_traversal\n"); break; } case 3: printf("do bfs\n"); breadth_first_traversal(root); printf("end do bfs\n"); break; default: operation = -1; break; } } return 0; }
BOOL generate(unit_box boxes[],INDEX index, INDEX r, INDEX c, BOOL *is_going_down) { UNIT val = 0; UNIT k = 0; if(index >= TOTAL) { if(*is_going_down == TRUE) { *is_going_down = FALSE; return FALSE; } return TRUE; } if(debug) printf("\nPROCESSING (%d,%d,%d)", index, r, c); k = val = boxes[index].value[r][c]; if(k == 0) { *is_going_down = FALSE; k = 1; } for ( ; k <= TOTAL ; k++) { INDEX newIndex = index, newr = r, newc = c+1; delete_value(boxes, index, r, c); if(debug) printf("\nINSERTING (%d,%d,%d) : %d", index, r, c, k); if(insert_value(boxes, index, k, r, c)) { if(newc >= ROW){ newr++; newc = 0;} if(newr >= COLUMN) {newIndex++; newr = 0;} if(generate(boxes, newIndex, newr, newc, is_going_down)) return TRUE; } } delete_value(boxes, index, r, c); return FALSE; }
static handle_type insert(value_type& x){return insert_value(x);}
int main(int argc, char **argv) { const char *b, *e, *p; const char *output_file = NULL; int f, tot, last, linenum, err, parse_err; struct timer *t = NULL, *t2; struct eb32_node *n; int val, test; int array[5]; int filter_acc_delay = 0, filter_acc_count = 0; int filter_time_resp = 0; int skip_fields = 1; argc--; argv++; while (argc > 0) { if (*argv[0] != '-') break; if (strcmp(argv[0], "-ad") == 0) { if (argc < 2) die("missing option for -ad"); argc--; argv++; filter |= FILT_ACC_DELAY; filter_acc_delay = atol(*argv); } else if (strcmp(argv[0], "-ac") == 0) { if (argc < 2) die("missing option for -ac"); argc--; argv++; filter |= FILT_ACC_COUNT; filter_acc_count = atol(*argv); } else if (strcmp(argv[0], "-rt") == 0) { if (argc < 2) die("missing option for -rt"); argc--; argv++; filter |= FILT_TIME_RESP; filter_time_resp = atol(*argv); } else if (strcmp(argv[0], "-RT") == 0) { if (argc < 2) die("missing option for -RT"); argc--; argv++; filter |= FILT_TIME_RESP | FILT_INVERT_TIME_RESP; filter_time_resp = atol(*argv); } else if (strcmp(argv[0], "-s") == 0) { if (argc < 2) die("missing option for -s"); argc--; argv++; skip_fields = atol(*argv); } else if (strcmp(argv[0], "-e") == 0) filter |= FILT_ERRORS_ONLY; else if (strcmp(argv[0], "-E") == 0) filter |= FILT_ERRORS_ONLY | FILT_INVERT_ERRORS; else if (strcmp(argv[0], "-c") == 0) filter |= FILT_COUNT_ONLY; else if (strcmp(argv[0], "-q") == 0) filter |= FILT_QUIET; else if (strcmp(argv[0], "-v") == 0) filter_invert = !filter_invert; else if (strcmp(argv[0], "-gt") == 0) filter |= FILT_GRAPH_TIMERS; else if (strcmp(argv[0], "-pct") == 0) filter |= FILT_PERCENTILE; else if (strcmp(argv[0], "-o") == 0) { if (output_file) die("Fatal: output file name already specified.\n"); if (argc < 2) die("Fatal: missing output file name.\n"); output_file = argv[1]; } argc--; argv++; } if (!filter) die("No action specified.\n"); if (filter & FILT_ACC_COUNT && !filter_acc_count) filter_acc_count=1; if (filter & FILT_ACC_DELAY && !filter_acc_delay) filter_acc_delay = 1; linenum = 0; tot = 0; parse_err = 0; while ((line = fgets2(stdin)) != NULL) { linenum++; test = 1; if (filter & FILT_TIME_RESP) { int tps; /* only report lines with response times larger than filter_time_resp */ b = field_start(line, TIME_FIELD + skip_fields); if (!*b) { truncated_line(linenum, line); continue; } e = field_stop(b + 1); /* we have field TIME_FIELD in [b]..[e-1] */ p = b; err = 0; for (f = 0; f < 4 && *p; f++) { tps = str2ic(p); if (tps < 0) { tps = -1; err = 1; } SKIP_CHAR(p, '/'); } if (f < 4) { parse_err++; continue; } test &= (tps >= filter_time_resp) ^ !!(filter & FILT_INVERT_TIME_RESP); } if (filter & FILT_ERRORS_ONLY) { /* only report erroneous status codes */ b = field_start(line, STATUS_FIELD + skip_fields); if (!*b) { truncated_line(linenum, line); continue; } if (*b == '-') { test &= !!(filter & FILT_INVERT_ERRORS); } else { val = strl2ui(b, 3); test &= (val >= 500 && val <= 599) ^ !!(filter & FILT_INVERT_ERRORS); } } if (filter & (FILT_ACC_COUNT|FILT_ACC_DELAY)) { b = field_start(line, ACCEPT_FIELD + skip_fields); if (!*b) { truncated_line(linenum, line); continue; } tot++; val = convert_date(b); //printf("date=%s => %d\n", b, val); if (val < 0) { parse_err++; continue; } t2 = insert_value(&timers[0], &t, val); t2->count++; continue; } if (filter & (FILT_GRAPH_TIMERS|FILT_PERCENTILE)) { int f; b = field_start(line, TIME_FIELD + skip_fields); if (!*b) { truncated_line(linenum, line); continue; } e = field_stop(b + 1); /* we have field TIME_FIELD in [b]..[e-1] */ p = b; err = 0; for (f = 0; f < 5 && *p; f++) { array[f] = str2ic(p); if (array[f] < 0) { array[f] = -1; err = 1; } SKIP_CHAR(p, '/'); } if (f < 5) { parse_err++; continue; } /* if we find at least one negative time, we count one error * with a time equal to the total session time. This will * emphasize quantum timing effects associated to known * timeouts. Note that on some buggy machines, it is possible * that the total time is negative, hence the reason to reset * it. */ if (filter & FILT_GRAPH_TIMERS) { if (err) { if (array[4] < 0) array[4] = -1; t2 = insert_timer(&timers[0], &t, array[4]); // total time t2->count++; } else { int v; t2 = insert_timer(&timers[1], &t, array[0]); t2->count++; // req t2 = insert_timer(&timers[2], &t, array[2]); t2->count++; // conn t2 = insert_timer(&timers[3], &t, array[3]); t2->count++; // resp v = array[4] - array[0] - array[1] - array[2] - array[3]; // data time if (v < 0 && !(filter & FILT_QUIET)) fprintf(stderr, "ERR: %s (%d %d %d %d %d => %d)\n", line, array[0], array[1], array[2], array[3], array[4], v); t2 = insert_timer(&timers[4], &t, v); t2->count++; tot++; } } else { /* percentile */ if (err) { if (array[4] < 0) array[4] = -1; t2 = insert_value(&timers[0], &t, array[4]); // total time t2->count++; } else { int v; t2 = insert_value(&timers[1], &t, array[0]); t2->count++; // req t2 = insert_value(&timers[2], &t, array[2]); t2->count++; // conn t2 = insert_value(&timers[3], &t, array[3]); t2->count++; // resp v = array[4] - array[0] - array[1] - array[2] - array[3]; // data time if (v < 0 && !(filter & FILT_QUIET)) fprintf(stderr, "ERR: %s (%d %d %d %d %d => %d)\n", line, array[0], array[1], array[2], array[3], array[4], v); t2 = insert_value(&timers[4], &t, v); t2->count++; tot++; } } continue; } test ^= filter_invert; if (!test) continue; /* all other cases mean we just want to count lines */ tot++; if (!(filter & FILT_COUNT_ONLY)) puts(line); } if (t) free(t); if (filter & FILT_COUNT_ONLY) { printf("%d\n", tot); exit(0); } if (filter & FILT_ERRORS_ONLY) exit(0); if (filter & (FILT_ACC_COUNT|FILT_ACC_DELAY)) { /* sort and count all timers. Output will look like this : * <accept_date> <delta_ms from previous one> <nb entries> */ n = eb32_first(&timers[0]); if (n) last = n->key; while (n) { unsigned int d, h, m, s, ms; t = container_of(n, struct timer, node); h = n->key; d = h - last; last = h; if (d >= filter_acc_delay && t->count >= filter_acc_count) { ms = h % 1000; h = h / 1000; s = h % 60; h = h / 60; m = h % 60; h = h / 60; tot++; printf("%02d:%02d:%02d.%03d %d %d %d\n", h, m, s, ms, last, d, t->count); } n = eb32_next(n); } }
int main(void) { clock_t begin, end; double time_spent; begin = clock(); matrix* a = create_matrix(4, 4); value temp_a[16] = { 18, 60, 57, 96, 41, 24, 99, 58, 14, 30, 97, 66, 51, 13, 19, 85 }; insert_array(temp_a, a); matrix* b = create_matrix(4, 4); assert(insert_array(temp_a, b)); //tests check_boundaries assert(check_boundaries(1,1,a)); assert(check_boundaries(4,4,a)); assert(!check_boundaries(4,5,a)); assert(!check_boundaries(5,4,a)); assert(!check_boundaries(0,1,a)); assert(!check_boundaries(1,0,a)); assert(!check_boundaries(-1,1,a)); assert(!check_boundaries(1,-1,a)); //tests compare_matrices,insert_value and get_value assert(compare_matrices(a,b)); assert(insert_value(10,1,1,b)); assert(!compare_matrices(a,b)); assert(get_value(1,1,b)==10); assert(insert_value(18,1,1,b)); assert(compare_matrices(a,b)); //tests is_matrix matrix* c=a; assert(compare_matrices(a,c)); assert(!is_matrix(a,b)); assert(is_matrix(a,c)); //tests insert_value by trying to go outside the matrix assert(insert_value(1,1,1,c)); assert(insert_value(2,2,2,c)); assert(insert_value(3,3,3,c)); assert(insert_value(4,4,4,c)); assert(!insert_value(5,5,5,c)); assert(!insert_value(-1,-1,-1,c)); assert(!insert_value(-1,-1,1,c)); assert(!insert_value(-1,1,-1,c)); //test get_value assert(get_value(1,1,c)==1); assert(get_value(2,2,c)==2); assert(get_value(3,3,c)==3); assert(get_value(4,4,c)==4); assert(get_value(0,0,c)==0); assert(get_value(1,-1,c)==0); assert(get_value(-1,1,c)==0); assert(get_value(5,5,c)==0); //tests insert and get without boundary checks insert_value_without_check(4,1,1,c); insert_value_without_check(3,2,2,c); insert_value_without_check(2,3,3,c); insert_value_without_check(1,4,4,c); assert(get_value_without_check(1,1,c)==4); assert(get_value_without_check(2,2,c)==3); assert(get_value_without_check(3,3,c)==2); assert(get_value_without_check(4,4,c)==1); //tests add_matrices value temp_b[16]={ 36,120,114,192, 82,48,198,116, 28, 60, 194,132, 102,26,38,170}; assert(insert_array(temp_b,a)); matrix* d = create_matrix(4, 4); assert(add_matrices(b,b,d)); assert(compare_matrices(d,a)); //tests subtract_matrices value temp_c[16]={ 0,0,0,0, 0,0,0,0, 0, 0, 0,0, 0,0,0,0}; assert(insert_array(temp_c,a)); assert(subtract_matrices(b,b,d)); assert(compare_matrices(d,a)); //tests sum_of_row assert(insert_array(temp_a,a)); assert(sum_of_row(1,a)==231); assert(sum_of_row(4,a)==168); assert(sum_of_row(0,a)==0); assert(sum_of_row(5,a)==0); //tests sum_of_column assert(sum_of_column(1,a)==124); assert(sum_of_column(4,a)==305); assert(sum_of_column(0,a)==0); assert(sum_of_column(5,a)==0); //tests get_row_vector matrix* e = create_matrix(1, 4); value temp_d[4] = { 18, 60, 57, 96}; assert(insert_array(temp_d,e)); matrix* f = create_matrix(1, 4); assert(!get_row_vector(0,a,f)); assert(!get_row_vector(5,a,f)); assert(get_row_vector(1,a,f)); assert(compare_matrices(e,f)); //tests get_column_vector matrix* g = create_matrix(4, 1); assert(insert_array(temp_d,e)); matrix* h = create_matrix(1, 4); assert(!get_row_vector(0,a,h)); assert(!get_row_vector(5,a,h)); assert(get_row_vector(1,a,h)); assert(compare_matrices(e,h)); //tests mulitply_matrices assert(multiply_matrices(a,a,b)); value temp_f[16]={8478,5478,14319,17130, 6066,6760,15418,16792, 6206,5328,14431,15096, 6052,5047,7652,14129.00}; assert(insert_array(temp_f,d)); assert(compare_matrices(b,d)); assert(!multiply_matrices(a,h,b)); assert(!multiply_matrices(a,a,h)); //tests transpose_matrix value temp_g[16]={18,41,14,51, 60,24,30,13, 57,99,97,19, 96,58,66,85}; assert(insert_array(temp_g,d)); assert(transpose_matrix(a,b)); assert(compare_matrices(b,d)); assert(!transpose_matrix(e,b)); assert(!transpose_matrix(a,e)); //tests multiply_matrix_with_scalar value temp_h[16] = { 36, 120, 114, 192, 82, 48, 198, 116, 28, 60, 194, 132, 102, 26, 38, 170 }; assert(insert_array(temp_h,b)); multiply_matrix_with_scalar(2,a); assert(compare_matrices(a,b)); //test get_sub_matrix matrix* i=create_matrix(2,2); assert(insert_array(temp_a,a)); assert(get_sub_matrix(1,2,1,2,a,i)); matrix* j=create_matrix(2,2); value temp_i[4] = { 18, 60, 41, 24}; assert(insert_array(temp_i,j)); assert(compare_matrices(j,i)); value temp_j[4] = { 97, 66, 19, 85}; assert(insert_array(temp_j,j)); assert(get_sub_matrix(3,4,3,4,a,i)); assert(compare_matrices(j,i)); assert(!get_sub_matrix(2,4,3,4,a,i)); assert(!get_sub_matrix(3,4,2,4,a,i)); assert(!get_sub_matrix(4,5,4,5,a,i)); assert(!get_sub_matrix(0,1,0,1,a,i)); //test insert_row_vector assert(insert_array(temp_a,a)); value temp_k[16] = { 18, 60, 57, 96, 18, 60, 57, 96, 14, 30, 97, 66, 51, 13, 19, 85 }; assert(insert_array(temp_k,b)); assert(insert_array(temp_d,e)); assert(insert_row_vector(2,e,a)); assert(compare_matrices(a,b)); end = clock(); time_spent = (double)(end - begin) / CLOCKS_PER_SEC; printf("time taken was: %f \n",time_spent); free_matrix(a); free_matrix(b); free_matrix(d); free_matrix(e); free_matrix(f); free_matrix(g); free_matrix(h); free_matrix(i); free_matrix(j); return 0; }
int extract_points(int z_flag) { struct line_pnts *points = Vect_new_line_struct(); CELL *cellbuf; FCELL *fcellbuf; DCELL *dcellbuf; int row, col; double x, y; int count; switch (data_type) { case CELL_TYPE: cellbuf = Rast_allocate_c_buf(); break; case FCELL_TYPE: fcellbuf = Rast_allocate_f_buf(); break; case DCELL_TYPE: dcellbuf = Rast_allocate_d_buf(); break; } G_message(_("Extracting points...")); count = 1; for (row = 0; row < cell_head.rows; row++) { G_percent(row, n_rows, 2); y = Rast_row_to_northing((double)(row + .5), &cell_head); switch (data_type) { case CELL_TYPE: Rast_get_c_row(input_fd, cellbuf, row); break; case FCELL_TYPE: Rast_get_f_row(input_fd, fcellbuf, row); break; case DCELL_TYPE: Rast_get_d_row(input_fd, dcellbuf, row); break; } for (col = 0; col < cell_head.cols; col++) { int cat, val; double dval; x = Rast_col_to_easting((double)(col + .5), &cell_head); switch (data_type) { case CELL_TYPE: if (Rast_is_c_null_value(cellbuf + col)) continue; val = cellbuf[col]; dval = val; break; case FCELL_TYPE: if (Rast_is_f_null_value(fcellbuf + col)) continue; dval = fcellbuf[col]; break; case DCELL_TYPE: if (Rast_is_d_null_value(dcellbuf + col)) continue; dval = dcellbuf[col]; break; } /* value_flag is used only for CELL type */ cat = (value_flag) ? val : count; Vect_reset_line(points); Vect_reset_cats(Cats); Vect_cat_set(Cats, 1, cat); Vect_append_point(points, x, y, dval); Vect_write_line(&Map, GV_POINT, points, Cats); if ((driver != NULL) && !value_flag) { insert_value(cat, val, dval); } count++; } } G_percent(row, n_rows, 2); switch (data_type) { case CELL_TYPE: G_free(cellbuf); break; case FCELL_TYPE: G_free(fcellbuf); break; case DCELL_TYPE: G_free(dcellbuf); break; } Vect_destroy_line_struct(points); return (1); }
/* Process each packet that passes the capture filter */ void parse_http_packet(u_char *args, const struct pcap_pkthdr *header, const u_char *pkt) { struct tm *pkt_time; char *header_line, *req_value; char saddr[INET6_ADDRSTRLEN], daddr[INET6_ADDRSTRLEN]; char sport[PORTSTRLEN], dport[PORTSTRLEN]; char ts[MAX_TIME_LEN]; int is_request = 0, is_response = 0; unsigned int eth_type = 0, offset; const struct eth_header *eth; const struct ip_header *ip; const struct ip6_header *ip6; const struct tcp_header *tcp; const char *data; int size_ip, size_tcp, size_data, family; /* Check the ethernet type and insert a VLAN offset if necessary */ eth = (struct eth_header *) pkt; eth_type = ntohs(eth->ether_type); if (eth_type == ETHER_TYPE_VLAN) { offset = link_offset + 4; } else { offset = link_offset; } offset += eth_skip_bits; /* Position pointers within packet stream and do sanity checks */ ip = (struct ip_header *) (pkt + offset); ip6 = (struct ip6_header *) (pkt + offset); switch (IP_V(ip)) { case 4: family = AF_INET; break; case 6: family = AF_INET6; break; default: return; } if (family == AF_INET) { size_ip = IP_HL(ip) * 4; if (size_ip < 20) return; if (ip->ip_p != IPPROTO_TCP) return; } else { /* AF_INET6 */ size_ip = sizeof(struct ip6_header); if (ip6->ip6_nh != IPPROTO_TCP) size_ip = process_ip6_nh(pkt, size_ip, header->caplen, offset); if (size_ip < 40) return; } tcp = (struct tcp_header *) (pkt + offset + size_ip); size_tcp = TH_OFF(tcp) * 4; if (size_tcp < 20) return; data = (char *) (pkt + offset + size_ip + size_tcp); size_data = (header->caplen - (offset + size_ip + size_tcp)); if (size_data <= 0) return; /* Check if we appear to have a valid request or response */ if (is_request_method(data)) { is_request = 1; } else if (strncmp(data, HTTP_STRING, strlen(HTTP_STRING)) == 0) { is_response = 1; } else { return; } /* Copy packet data to editable buffer that was created in main() */ if (size_data > BUFSIZ) size_data = BUFSIZ; memcpy(buf, data, size_data); buf[size_data] = '\0'; /* Parse header line, bail if malformed */ if ((header_line = parse_header_line(buf)) == NULL) return; if (is_request) { if (parse_client_request(header_line)) return; } else if (is_response) { if (parse_server_response(header_line)) return; } /* Iterate through request/entity header fields */ while ((header_line = parse_header_line(NULL)) != NULL) { if ((req_value = strchr(header_line, ':')) == NULL) continue; *req_value++ = '\0'; while (isspace(*req_value)) req_value++; insert_value(header_line, req_value); } /* Grab source/destination IP addresses */ if (family == AF_INET) { inet_ntop(family, &ip->ip_src, saddr, sizeof(saddr)); inet_ntop(family, &ip->ip_dst, daddr, sizeof(daddr)); } else { /* AF_INET6 */ inet_ntop(family, &ip6->ip_src, saddr, sizeof(saddr)); inet_ntop(family, &ip6->ip_dst, daddr, sizeof(daddr)); } insert_value("source-ip", saddr); insert_value("dest-ip", daddr); /* Grab source/destination ports */ snprintf(sport, PORTSTRLEN, "%d", ntohs(tcp->th_sport)); snprintf(dport, PORTSTRLEN, "%d", ntohs(tcp->th_dport)); insert_value("source-port", sport); insert_value("dest-port", dport); /* Extract packet capture time */ pkt_time = localtime((time_t *) &header->ts.tv_sec); strftime(ts, MAX_TIME_LEN, "%Y-%m-%d %H:%M:%S", pkt_time); insert_value("timestamp", ts); if (rate_stats) { update_host_stats(get_value("host"), header->ts.tv_sec); clear_values(); } else { print_format_values(); } if (dumpfile) pcap_dump((unsigned char *) dumpfile, header, pkt); num_parsed++; if (parse_count && (num_parsed >= parse_count)) pcap_breakloop(pcap_hnd); return; }
/* Process each packet that passes the capture filter */ void parse_http_packet(u_char *args, const struct pcap_pkthdr *header, const u_char *pkt) { struct tm *pkt_time; char *header_line, *req_value; char saddr[INET_ADDRSTRLEN], daddr[INET_ADDRSTRLEN]; char sport[PORTSTRLEN], dport[PORTSTRLEN]; char ts[MAX_TIME_LEN]; int is_request = 0, is_response = 0; const struct ip_header *ip; const struct tcp_header *tcp; const char *data; int size_ip, size_tcp, size_data; /* Position pointers within packet stream and do sanity checks */ ip = (struct ip_header *) (pkt + header_offset); size_ip = IP_HL(ip) * 4; if (size_ip < 20) return; if (ip->ip_p != IPPROTO_TCP) return; tcp = (struct tcp_header *) (pkt + header_offset + size_ip); size_tcp = TH_OFF(tcp) * 4; if (size_tcp < 20) return; data = (char *) (pkt + header_offset + size_ip + size_tcp); size_data = (header->caplen - (header_offset + size_ip + size_tcp)); if (size_data <= 0) return; /* Check if we appear to have a valid request or response */ if (is_request_method(data)) { is_request = 1; } else if (strncmp(data, HTTP_STRING, strlen(HTTP_STRING)) == 0) { is_response = 1; } else { return; } /* Copy packet data to editable buffer that was created in main() */ if (size_data > BUFSIZ) size_data = BUFSIZ; strncpy(buf, data, size_data); buf[size_data] = '\0'; /* Parse header line, bail if malformed */ if ((header_line = parse_header_line(buf)) == NULL) return; if (is_request) { if (parse_client_request(header_line)) return; } else if (is_response) { if (parse_server_response(header_line)) return; } /* Iterate through request/entity header fields */ while ((header_line = parse_header_line(NULL)) != NULL) { if ((req_value = strchr(header_line, ':')) == NULL) continue; *req_value++ = '\0'; while (isspace(*req_value)) req_value++; insert_value(header_line, req_value); } /* Grab source/destination IP addresses */ strncpy(saddr, (char *) inet_ntoa(ip->ip_src), INET_ADDRSTRLEN); strncpy(daddr, (char *) inet_ntoa(ip->ip_dst), INET_ADDRSTRLEN); insert_value("source-ip", saddr); insert_value("dest-ip", daddr); /* Grab source/destination ports */ sprintf(sport, "%d", ntohs(tcp->th_sport)); sprintf(dport, "%d", ntohs(tcp->th_dport)); insert_value("source-port", sport); insert_value("dest-port", dport); /* Extract packet capture time */ pkt_time = localtime((time_t *) &header->ts.tv_sec); strftime(ts, MAX_TIME_LEN, "%Y-%m-%d %H:%M:%S", pkt_time); insert_value("timestamp", ts); print_format_values(); if (dumpfile) pcap_dump((unsigned char *) dumpfile, header, pkt); num_parsed++; if (parse_count && (num_parsed >= parse_count)) pcap_breakloop(pcap_hnd); return; }
/** This functions creates an interface between MATLAB and the solver together with the matrixlibrary. It also converts MATLAB structured matrices into the matrixlibrary structure. */ void mexFunction(int nlhs, mxArray* plhs[], int nrhs, const mxArray* prhs[]){ /* Declare variables */ int nr_of_matrices = nrhs-2; /* Number of matrices */ mxArray* mat_matrix; /* Used to store the incoming matrices from MATLAB when converting to Matlib matrices. */ double* out_matrix; /* Used to return the result back to MATLAB. */ matrix* lib_matrix; /* Used to temporarily store the Matlib matrix that is created when converting the MATLAB matrix. */ matrix* result_matrix; /* The Matlib matrix containing the result returned from the solver. */ matrix* lib_matrices[nr_of_matrices]; /* An array of all the matrices that should be sent to the solver. */ /* Check for proper number of arguments */ if(nrhs != 9){ mexErrMsgIdAndTxt("MyToolbox:quadopt:nrhs","Nine inputs required."); } /* Convert MATLAB matrises to library matrices */ int i; for(i = 0; i < nr_of_matrices; i++){ mat_matrix = prhs[i]; /* If matrix is not empty, create_matrix else set it to NULL */ if(!mxIsEmpty(mat_matrix)){ int rows = (int)mxGetM(mat_matrix); int columns = (int)mxGetN(mat_matrix); lib_matrix = create_matrix(rows, columns); double* element_ptr = mxGetPr(mat_matrix); int x; for(x = 0; x < columns; x++){ int y; for(y = 0; y < rows; y++){ insert_value(*element_ptr, y+1, x+1, lib_matrix); element_ptr++; } } lib_matrices[i] = lib_matrix; }else{ lib_matrices[i] = NULL; } } /* Convert max_iter and max_time to integers */ int max_iter = (int)(*mxGetPr(prhs[7])); int max_time = (int)(*mxGetPr(prhs[8])); /* Create problem from solver.h with library matrices */ problem* problem = create_problem(lib_matrices[0], lib_matrices[1], lib_matrices[2], lib_matrices[3], lib_matrices[4], lib_matrices[5], lib_matrices[6], max_iter, max_time); /* Solve problem */ quadopt_solver(problem); /* Get the solution from the problem struct */ result_matrix = problem->solution; /* Convert resulting matrix to MATLAB matrix and set output matrix */ plhs[0] = mxCreateDoubleMatrix(result_matrix->rows, result_matrix->columns, mxREAL); out_matrix = mxGetPr(plhs[0]); int x; for(x = 0; x < result_matrix->columns; x++){ int y; for(y = 0; y < result_matrix->rows; y++){ *out_matrix = get_value(y+1, x+1, result_matrix); out_matrix++; } } /* Free memory */ for(i = 0; i < nr_of_matrices; i++){ if(lib_matrices[i] != NULL){ free_matrix(lib_matrices[i]); } } free_matrix(result_matrix); }
/** * Needs brief description here. * * Substitutes variables from string: * * - The leading "~" is replaced by the home directory. * - Variables like "$PATH" or "${PATH}" are replaced by their value, as * fetched from the environment, or the empty string if not found. * * If given a NULL input, we return NULL. * * @return string constant, which is not meant to be freed until exit time. */ const char * eval_subst(const char *str) { char buf[MAX_STRING]; char *end = &buf[sizeof(buf)]; char *p; size_t len; char c; if (str == NULL) return NULL; len = g_strlcpy(buf, str, sizeof buf); if (len >= sizeof buf) { g_warning("%s(): string too large for substitution (%zu bytes)", G_STRFUNC, len); return constant_str(str); } if (common_dbg > 3) g_debug("%s: on entry: \"%s\"", G_STRFUNC, buf); for (p = buf, c = *p++; c; c = *p++) { const char *val = NULL; char *start = p - 1; switch (c) { case '~': if (start == buf && ('\0' == buf[1] || '/' == buf[1])) { /* Leading ~ only */ val = gethomedir(); g_assert(val); memmove(start, &start[1], len - (start - buf)); len--; g_assert(size_is_non_negative(len)); } break; case '$': { const char *after; val = get_variable(p, &after); g_assert(val); memmove(start, after, len + 1 - (after - buf)); len -= after - start; /* Also removing leading '$' */ g_assert(size_is_non_negative(len)); } break; } if (val != NULL) { char *next; next = insert_value(val, start, start - buf, len, sizeof buf - 1); len += next - start; p = next; g_assert(len < sizeof buf); g_assert(p < end); } g_assert(p <= &buf[len]); } if (common_dbg > 3) g_debug("%s: on exit: \"%s\"", G_STRFUNC, buf); g_assert(len == strlen(buf)); return constant_str(buf); }
int main(void) { //printf("sucess 1 !\n"); int i = 0; head = (struct mylink*)malloc(sizeof(struct mylink)); //合并用的链表 headA = (struct mylink*)malloc(sizeof(struct mylink)); headB = (struct mylink*)malloc(sizeof(struct mylink)); if (head == NULL || headA==NULL || headB == NULL){ printf("malloc error!\n"); return -1; } //printf("sucess 2 !\n"); head->value = 0; head->next =NULL; headA->value = 0; headA->next =NULL; headB->value = 0; headB->next =NULL; for (i = 0; i < 10;i++){ insert_value(head,i+2); } for (i = 1;i < 10;i++){ insert_value(headA,i*2-1); insert_value(headB,i*2); } printf("=================Test 1&2 ======!\n"); #if 1 //------------------测试1&2 printf("------------creat show--------\n"); show_link(head); //打印创建好的链表 printf("----------reverst show----------\n"); reverse_link(head); //翻转链表 show_link(head); //打印翻转后的链表 printf("-------------- kill 4 show------\n"); delete_link(head,4); //删除链表中的某块数据 show_link(head); //打印删除后的链表 #endif //-------------测试 3 printf("=================Test 3 ======!\n"); printf("-------------- headA befor show------\n"); reverse_link(headA);show_link(headA); //打印A 链表 printf("-------------- headB befor show------\n"); reverse_link(headB);show_link(headB); //打印B 链表 headA = mergelink(headB,headA); //合并 A B 链表 printf("-------------- headAB befor show------\n"); show_link(headA);//打印合并后的链表 printf("=================Test 4 ======!\n"); josephus(8,4,3);//问题4 printf("sucess !\n"); return 0; }