void add_order(timestamp_t timestamp, order_id_t id, side_t side, price_t price, int size) { assert(timestamp >= 0); assert(id >= 0); assert(price > (price_t) 0); assert(size >= 0); assert(side == m_side); // find() takes O(1) for hash tables order_table_iter_t pos = m_orders.find(id); if (pos != m_orders.end()) // order id already exists in the book { throw cant_add_order(timestamp, id, "order with the id already exists in the book"); } order_ptr neword(new order(timestamp, side, price, size)); m_orders[id] = neword; // operator [] takes O(1) amortized time for hash_table // O(N); N/2 on average insert_sorted(m_sorted_orders, neword); m_shares += size; m_last_timestamp = timestamp; if (m_orders.size() > m_max_orders) m_max_orders = m_orders.size(); if (m_sorted_orders.size() > m_max_sorted) m_max_sorted = m_sorted_orders.size(); }
int main() { size_t const U = 100; size_t const N = 100000; size_t const ITERATIONS = 1; size_t const SAMPLES = 5; // 0. Seed with a real random value, if available std::random_device rd; std::default_random_engine engine(rd()); // 1. Generate a random set of N unique values std::vector<int> unique_values; { std::uniform_int_distribution<int> uniform_dist(std::numeric_limits<int>::min(), std::numeric_limits<int>::max()); while (unique_values.size() < U) { insert_sorted(unique_values, uniform_dist(engine)); } } std::cout << "Generated " << unique_values.size() << " unique values in the range [" << unique_values.front() << ", " << unique_values.back() << "]\n"; // 2. From that set of unique values, generate a large collection of random numbers std::vector<int> random_stream; { random_stream.reserve(N); std::uniform_int_distribution<size_t> uniform_dist(0, unique_values.size()-1); for (size_t i = 0; i < N; ++i) { random_stream.push_back(unique_values.at(uniform_dist(engine))); } } std::cout << "Generated a stream of " << random_stream.size() << " values\n"; // 3. Time each solution double best_ures = std::numeric_limits<double>::max(); double best_uniq = std::numeric_limits<double>::max(); double best_sort = std::numeric_limits<double>::max(); for (size_t i = 0; i < SAMPLES; ++i) { size_t ures_result = 0; double ures = benchmark(VecUniquerReserver{ures_result, U, random_stream}, ITERATIONS); best_ures = std::min(best_ures, ures); std::cout << "Ures: " << ures_result << ": " << best_ures << "\n"; size_t uniq_result = 0; double uniq = benchmark(VecUniquer{uniq_result, random_stream}, ITERATIONS); best_uniq = std::min(best_uniq, uniq); std::cout << "Uniq: " << uniq_result << ": " << best_uniq << "\n"; size_t sort_result = 0; double sorting = benchmark(VecSorter{sort_result, random_stream}, ITERATIONS); best_sort = std::min(best_sort, sorting); std::cout << "Sort: " << sort_result << ": " << best_sort << "\n"; } std::cout << "Final: URES = " << best_ures << ", UNIQ = " << best_uniq << ", SORT = " << best_sort << "\n"; return 0; }
int batchMode(int argc, char** argv) { list *l = empty_list(); int value; for(int i = 1 ; i < argc ; ++i) if(!parse(argv[i], &value)) return printf("%s is not a number !\n", argv[i]) + 1; else insert_sorted(l, value); print_list(l); free_list(l); return 0; }
DexMethod* MethodCreator::make_static_from(DexString* name, DexProto* proto, DexMethod* meth, DexClass* target_cls) { assert(!(meth->get_access() & ACC_STATIC)); assert(!is_init(meth) && !is_clinit(meth)); auto smeth = DexMethod::make_method(target_cls->get_type(), name, proto); smeth->make_concrete( meth->get_access() | ACC_STATIC, meth->get_code(), false); insert_sorted(target_cls->get_dmethods(), smeth, compare_dexmethods); meth->set_code(nullptr); return smeth; }
void sort_list(node_t **list) { node_t *srtl = NULL; node_t *tmp = *list; node_t *n; while(tmp) { n = tmp; tmp = tmp->next; insert_sorted(&srtl, n); } *list = srtl; }
static void show_tweet_internal (CbTweetModel *self, guint index) { CbTweet *tweet = g_ptr_array_index (self->hidden_tweets, index); g_object_ref (tweet); g_ptr_array_remove_index (self->hidden_tweets, index); insert_sorted (self, tweet); g_object_unref (tweet); if (tweet->id > self->max_id) self->max_id = tweet->id; if (tweet->id < self->min_id) self->min_id = tweet->id; }
void interactiveMode() { char data[255]; int value = 0; list *l = empty_list(); for(;;) { scanf("%s", data); fseek(stdin, 0, SEEK_END); if(feof(stdin)) break; if(parse(data, &value)) insert_sorted(l, value); } print_list(l); free_list(l); }
void cb_tweet_model_add (CbTweetModel *self, CbTweet *tweet) { g_return_if_fail (CB_IS_TWEET_MODEL (self)); g_return_if_fail (CB_IS_TWEET (tweet)); if (cb_tweet_is_hidden (tweet)) { g_object_ref (tweet); g_ptr_array_add (self->hidden_tweets, tweet); } else { insert_sorted (self, tweet); if (tweet->id > self->max_id) self->max_id = tweet->id; if (tweet->id < self->min_id) self->min_id = tweet->id; } }
/** * Implements some simple test code for our singly-linked list. */ int main(void) { printf("Prepending ints 0-%d onto the list...", TEST_SIZE); for (int i = 0; i < TEST_SIZE; i++) { prepend(i); } printf("done!\n"); printf("Making sure that the list length is indeed %d...", TEST_SIZE); assert(length() == TEST_SIZE); printf("good!\n"); printf("Making sure that values are arranged in descending order..."); node* n = first; for (int i = 0; i < TEST_SIZE; i++) { assert(n != NULL); assert(n->i == TEST_SIZE - i - 1); n = n->next; } printf("good!\n"); printf("Freeing the list..."); while (first != NULL) { node* next = first->next; free(first); first = next; } printf("done!\n"); printf("Appending ints 0-%d to the list...", TEST_SIZE); for (int i = 0; i < TEST_SIZE; i++) { append(i); } printf("done!\n"); printf("Making sure that the list length is indeed %d...", TEST_SIZE); assert(length() == TEST_SIZE); printf("good!\n"); printf("Making sure that values are arranged in ascending order..."); n = first; for (int i = 0; i < TEST_SIZE; i++) { assert(n != NULL); assert(n->i == i); n = n->next; } printf("good!\n"); printf("Freeing the list..."); while (first != NULL) { node* next = first->next; free(first); first = next; } printf("done!\n"); printf("Inserting %d random ints to the list...", TEST_SIZE); for (int i = 0; i < TEST_SIZE; i++) { insert_sorted(rand() % TEST_SIZE); } printf("done!\n"); printf("Making sure that the list length is indeed %d...", TEST_SIZE); assert(length() == TEST_SIZE); printf("good!\n"); printf("Making sure that values are arranged in sorted order..."); n = first; int prev = 0; for (int i = 0; i < TEST_SIZE; i++) { assert(n != NULL); assert(n->i >= prev); prev = n->i; n = n->next; } printf("good!\n"); printf("Freeing the list..."); while (first != NULL) { node* next = first->next; free(first); first = next; } printf("done!\n"); printf("\n********\nSuccess!\n********\n"); return 0; }
int main (void) { system ("clear"); node_t *head = NULL; int choice = 0; int n; while (TRUE) { print_menu(); scanf("%d", &choice); switch(choice) { case 1: head = insert_rear(head); view_list(head); break; case 2: head = insert_front(head); view_list(head); break; case 3: head = delete_rear(head); view_list(head); break; case 4: head = delete_front(head); view_list(head); break; case 5: print_reverse(head); printf("\n"); break; // case 6: // link_sort1(head); // view_list(head); // break; case 7: head = reverse_list_iterative(head); view_list(head); break; case 8: head = reverse_list_recursive(head); view_list(head); break; case 9: head = swap_alternate(head); view_list(head); break; case 10: head = insert_sorted(head); view_list(head); break; case 11: printf ("Enter the Value of n: "); scanf ("%d", &n); head = reverse_n_list(head, n); view_list(head); break; case 100: view_list(head); break; default : printf("Invalid Choice\n"); } } return 0; }
void operator()() const { std::vector<int> uniq; for (int e: _stream) { insert_sorted(uniq, e); } _out = uniq.size(); }
void insert_sorted(std::vector<T>& vec, T const& e) { insert_sorted(vec, e, std::less<T>{}); }
/** Adds to waiting list edges from t to each minimal (resp. maximal) successor of t, where t belongs to P_O (resp. P_I) If t belongs to P_O: computes the minimal successors of t, sorts them and adds them to succ_to_visit[t]. The first successor passed in this list is then added to waiting and remove from succ_to_visit[t] If t belongs to P_I: computes the maximal successors of t, sorts them and adds them to waiting (some of them might then be skipped if enough information is known) **/ static GList* add_to_waiting(tuple *t, alphabet_info* alphabet, antichain* safety_game_PO, antichain* safety_game_PI, antichain* losing_PO, antichain* losing_PI, GList *waiting, GHashTable *succ_to_visit, GHashTable *passed) { char player = t->cf->player; int sigma_size; if(player == P_O) { sigma_size = alphabet->sigma_output_size; } else { sigma_size = alphabet->sigma_input_size; } // Compute the successors int i; GList *succ_list = NULL; safety_game_edge *cur_edge; for(i=0; i<sigma_size; i++) { cur_edge = (safety_game_edge*)malloc(sizeof(safety_game_edge)); cur_edge->from = t; cur_edge->to = tuple_succ(t, i, alphabet); cur_edge->label_index = i; if(player == P_O) { if(contains_element(safety_game_PI, cur_edge->to, (void*)compare_tuples) == TRUE) { // succ safe? if(is_losing(cur_edge->to, losing_PO, losing_PI) == FALSE) { // succ non losing? succ_list = scan_add_or_remove_and_free(succ_list, cur_edge, (void*)compare_safety_game_edges_reverse, (void*)free_safety_game_edge); } else { free_safety_game_edge(cur_edge); } } else { free_safety_game_edge(cur_edge); } } else { succ_list = scan_add_or_remove_and_free(succ_list, cur_edge, (void*)compare_safety_game_edges, (void*)free_safety_game_edge); } } GList *sorted_succ_list = NULL; GList *curlink = succ_list; if(player == P_O) { //t belongs to P_O -> sort the successors and add them to succ_to_visit while(curlink != NULL) { //sorted_succ_list = g_list_prepend(sorted_succ_list, curlink->data); sorted_succ_list = insert_sorted(sorted_succ_list, curlink->data, (void*)compare_safety_game_edges_counters_sum_reverse); // the smallest elements will be placed at the beginning of the linked list curlink = curlink->next; } hash_table_key* key = (hash_table_key*)malloc(sizeof(hash_table_key)); key->t = t; key->sigma_index = -1; g_hash_table_insert(succ_to_visit, (gconstpointer*)key, (gconstpointer*)sorted_succ_list); // Add the first successor passed to waiting waiting = g_list_append(waiting, get_first_successor_passed_non_losing(t, succ_to_visit, passed, losing_PO, losing_PI)); } else { //t belongs to P_I -> sort the successors and add them to waiting while(curlink != NULL) { //sorted_succ_list = g_list_prepend(sorted_succ_list, curlink->data); sorted_succ_list = insert_sorted(sorted_succ_list, curlink->data, (void*)compare_safety_game_edges_counters_sum_reverse); // the largest elements will be placed at the end of the linked list -> popped first curlink = curlink->next; } // Add the successors to waiting waiting = g_list_concat(waiting, sorted_succ_list); } g_list_free(succ_list); return waiting; }
/** getlinks() * Get the links in the files and update the relevant lists */ void *getlinks(void *arg) { off_t j; char *link_end; int state = START; int curr_thread = (int) arg; filelist_t *file = NULL; pthread_mutex_lock(&file_lock); file = currfile; if (currfile != NULL) currfile = currfile->next; pthread_mutex_unlock(&file_lock); // go through each file and look for links. //for (i=0; i<t_arg->num_files; i++) while(file != NULL) { assert(file); dprintf("Looking at file %s, size %d\n",file->name, (int)file->size); for (j=0; j<file->size; j++) { switch (state) { case START: if (file->data[j] == '<') state = IN_TAG; break; case IN_TAG: //dprintf("Found a tag\n"); if (file->data[j] == 'a') state = IN_ATAG; else if (file->data[j] == ' ') state = IN_TAG; else state = START; break; case IN_ATAG: //dprintf("Found <a\n"); if (file->data[j] == 'h') { if (strncmp(&file->data[j], "href", 4) == 0) { state = FOUND_HREF; j += 3; } else state = START; } else if (file->data[j] == ' ') state = IN_ATAG; else state = START; break; case FOUND_HREF: //dprintf("Found href\n"); if (file->data[j] == ' ') state = FOUND_HREF; else if (file->data[j] == '=') state = FOUND_HREF; else if (file->data[j] == '\"') state = START_LINK; else state = START; break; case START_LINK: //dprintf("Found a link\n"); link_end = NULL; link_end = strchr(&(file->data[j]), '\"'); if (link_end != NULL) { link_end[0] = 0; insert_sorted(&(file->data[j]), file->name, curr_thread); //dprintf("Found key %s in file %s\n", &(file->data[j]), file->name); j += strlen(&(file->data[j])); } state = START; break; } } pthread_mutex_lock(&file_lock); file = currfile; if (currfile != NULL) currfile = currfile->next; pthread_mutex_unlock(&file_lock); } return (void *)0; }
/** getlinks() * Get the links in the files and update the relevant lists */ void getlinks() { int i; off_t j; char *link_end; int state = START; links = (link_head_t*)calloc(START_ARRAY_SIZE,sizeof(link_head_t)); length = START_ARRAY_SIZE; use_len = 0; filelist_t *file = ri_data.filelist; // go through each file and look for links. for (i=0; i<ri_data.num_files; i++) { assert(file); dprintf("Looking at file %s, size %d\n",file->name, file->size); for (j=0; j<file->size; j++) { switch (state) { case START: if (file->data[j] == '<') state = IN_TAG; break; case IN_TAG: //dprintf("Found a tag\n"); if (file->data[j] == 'a') state = IN_ATAG; else if (file->data[j] == ' ') state = IN_TAG; else state = START; break; case IN_ATAG: //dprintf("Found <a\n"); if (file->data[j] == 'h') { if (strncmp(&file->data[j], "href", 4) == 0) { state = FOUND_HREF; j += 3; } else state = START; } else if (file->data[j] == ' ') state = IN_ATAG; else state = START; break; case FOUND_HREF: //dprintf("Found href\n"); if (file->data[j] == ' ') state = FOUND_HREF; else if (file->data[j] == '=') state = FOUND_HREF; else if (file->data[j] == '\"') state = START_LINK; else state = START; break; case START_LINK: //dprintf("Found a link\n"); link_end = NULL; link_end = strchr(&(file->data[j]), '\"'); if (link_end != NULL) { link_end[0] = 0; insert_sorted(&(file->data[j]), file->name); dprintf("Found key %s in file %s\n", &(file->data[j]), file->name); j += strlen(&(file->data[j])); } state = START; break; } } file = file->next; } }
bool operator() (const T& x, std::vector<T>& xs) { insert_sorted(x, xs); return std::is_sorted(xs.begin(), xs.end()); }
void PdfVecObjects::push_back( PdfObject* pObj ) { insert_sorted( pObj ); }
int gem_objects_update(struct gem_objects *obj) { char buf[8192], *b; struct gem_objects_comm *comm; struct gem_objects_comm *freed; int fd, len, ret; freed = obj->comm; obj->comm = NULL; sprintf(buf, "%s/i915_gem_objects", debugfs_dri_path); fd = open(buf, 0); if (fd < 0) { ret = errno; goto done; } len = read(fd, buf, sizeof(buf)-1); close(fd); if (len < 0) { ret = EIO; goto done; } buf[len] = '\0'; while (buf[--len] == '\n') buf[len] = '\0'; b = buf; sscanf(b, "%lu objects, %lu bytes", &obj->total_count, &obj->total_bytes); b = strchr(b, '\n'); sscanf(b, "%*d [%*d] objects, %lu [%lu] bytes in gtt", &obj->total_gtt, &obj->total_aperture); ret = 0; b = strchr(b, ':'); if (b == NULL) goto done; while (*b != '\n') b--; b++; do { char *eol, *colon; comm = freed; if (comm) freed = comm->next; else comm = malloc(sizeof(*comm)); if (comm == NULL) break; /* Xorg: 35 objects, 16347136 bytes (0 active, 12103680 inactive, 0 unbound) */ eol = strchr(b, '\n'); if (eol) { do { *eol++ = '\0'; } while (*eol == '\n'); } colon = strchr(b, ':'); memcpy(comm->name, b, colon-b+1); comm->name[colon-b+1] = '\0'; sscanf(colon + 1, "%lu objects, %lu bytes", &comm->count, &comm->bytes); insert_sorted(obj, comm); b = eol; } while (b != NULL); done: while (freed) { comm = freed; freed = comm->next; free(comm); } return ret; }