/* PUBLIC */ BOOL sos_keep1(Topform c, Clist sos, Prover_options opt) { Plist pickers = pickers_for_clause(c); if (pickers == NULL) return FALSE; /* no picker will accept the clause */ else { int limit = parm(opt->sos_limit); /* printf("sos_keep, sos=%d, c=%d:", clist_length(sos), c->weight); */ if (limit == -1) { zap_plist(pickers); return TRUE; /* some picker wants the clause, and there is no limit */ } else { double fullness_of_sos = clist_length(sos) / (double) limit; double x = .5; /* when sos is at (x * limit), start being selective */ if (fullness_of_sos < x) { zap_plist(pickers); return TRUE; /* there is a limit, but we don't yet consider it */ } else { Picker p; pickers = get_sos_limit_pickers(pickers); p = largest_picker(pickers); zap_plist(pickers); if (p == NULL) { return FALSE; /* all pickers complete, and we're getting full */ } else { /* keep if wt < than clause at ref_point */ double ref_point = x + (1 - fullness_of_sos); /* fullness_of_sos >= x, x <= ref_point <= 1. if sos is full: ref_point=x if sos is at x: ref_point=1 */ int n = avl_size(p->idx) * ref_point; Topform d = avl_nth_item(p->idx, n); if (d == NULL) { return TRUE; /* the picker is empty */ } else { if (c->weight >= d->weight) { if (c->weight < p->low_delete) { printf("\nLow Water (keep, %s): wt=%d\n", p->name, c->weight); fflush(stdout); p->low_delete = c->weight; } p->number_deleted++; return FALSE; } else { /* keep the clause (smaller than clause at reference point) */ return TRUE; } } } } } } } /* sos_keep1 */
static void nc_conn_build_iov(clist *write_q, unsigned int partial, struct iovec **iov_, unsigned int *iov_len_) { *iov_ = NULL; *iov_len_ = 0; unsigned int i, iov_len = clist_length(write_q); struct iovec *iov = calloc(iov_len, sizeof(struct iovec)); clist *tmp = write_q; i = 0; while (tmp) { struct buffer *buf = tmp->data; iov[i].iov_base = buf->p; iov[i].iov_len = buf->len; if (i == 0) { iov[0].iov_base += partial; iov[0].iov_len -= partial; } tmp = tmp->next; i++; } *iov_ = iov; *iov_len_ = iov_len; }
struct peer_manager *peerman_seed(bool use_dns) { struct peer_manager *peers; peers = peerman_new(); if (!peers) return NULL; /* make DNS query for seed data */ clist *tmp, *seedlist = NULL; if (use_dns) seedlist = bu_dns_seed_addrs(); log_debug("peerman: DNS returned %zu addresses", clist_length(seedlist)); clist_shuffle(seedlist); /* import seed data into peerman */ tmp = seedlist; while (tmp) { struct bp_address *addr = tmp->data; tmp = tmp->next; peerman_add_addr(peers, addr, true); free(addr); } clist_free(seedlist); return peers; }
static void init_peers(struct net_child_info *nci) { /* * read network peers */ struct peer_manager *peers; peers = peerman_read(); if (!peers) { fprintf(plog, "net: initializing empty peer list\n"); peers = peerman_seed(setting("no_dns") == NULL ? true : false); if (!peerman_write(peers)) { fprintf(plog, "net: failed to write peer list\n"); exit(1); } } char *addnode = setting("addnode"); if (addnode) peerman_addstr(peers, addnode); peerman_sort(peers); if (debugging) fprintf(plog, "net: have %u/%zu peers\n", bp_hashtab_size(peers->map_addr), clist_length(peers->addrlist)); nci->peers = peers; }
int main(void) { link cursor = NULL; int i = 0; clist_print(cursor, print_int_data); for (i = 0; i < 100; i++) { int *p = make_data(i+1); link item = make_node(p); cursor = clist_insert_after(cursor, item); } cursor = cursor->next; clist_print(cursor, print_int_data); printf("ring list length = %d\n", clist_length(cursor)); int step = 0; while (cursor != NULL) { print_int_data(cursor->data); step++; if (step == 3) { printf("-> %d out\n", *(int *)(cursor->data)); cursor = clist_delete(cursor, cursor); printf("length = %d\n", clist_length(cursor)); step = 0; } else cursor = cursor->next; //getchar(); //sleep(1); } return 0; }
void peerman_addstr(struct peer_manager *peers, const char *addr_str) { char hoststr[64] = {}; char portstr[16] = {}; char *space = strchr(addr_str, ' '); int port; if (space) { unsigned int hlen = (space - addr_str); if (hlen > (sizeof(hoststr) - 1)) hlen = sizeof(hoststr) - 1; memcpy(hoststr, addr_str, hlen); hoststr[hlen] = 0; strncpy(portstr, space + 1, sizeof(portstr) - 1); } else { strncpy(hoststr, addr_str, sizeof(hoststr) - 1); strcpy(portstr, "8333"); } port = atoi(portstr); if (port < 1 || port > 65535) port = 8333; clist *tmp, *seedlist = bu_dns_lookup(NULL, hoststr, port); if (debugging) fprintf(stderr, "peerman: DNS lookup '%s' returned %zu addresses\n", addr_str, clist_length(seedlist)); /* import seed data into peerman */ tmp = seedlist; while (tmp) { struct bp_address *addr = tmp->data; tmp = tmp->next; peerman_add_addr(peers, addr, true); } clist_free(seedlist); }
static bool ser_peerman(struct peer_manager *peers, int fd, const struct chain_info *chain) { /* write "magic number" (constant first file record) */ cstring *rec = message_str(chain->netmagic, "magic.peers", NULL, 0); unsigned int rec_len = rec->len; ssize_t wrc = write(fd, rec->str, rec_len); cstr_free(rec, true); if (wrc != rec_len) return false; if (debugging) fprintf(stderr, "peerman: %zu peers to write\n", clist_length(peers->addrlist)); /* write peer list */ clist *tmp = peers->addrlist; while (tmp) { struct peer *peer; peer = tmp->data; tmp = tmp->next; cstring *msg_data = cstr_new_sz(sizeof(struct peer)); ser_peer(msg_data, CADDR_TIME_VERSION, peer); rec = message_str(chain->netmagic, "peer", msg_data->str, msg_data->len); rec_len = rec->len; wrc = write(fd, rec->str, rec_len); cstr_free(rec, true); cstr_free(msg_data, true); if (wrc != rec_len) return false; } return true; }
void show (hand *h, int reveal) { card *c; if (clist_length(h->cards) == 0) { printf("[no cards]\n"); return; } else { if (reveal) { printf(" %s\n", card_name(*h->cards.first)); } else { printf(" one face down card\n"); } } for (c=h->cards.first->next; c!=NULL; c=c->next) printf(" %s\n", card_name(*c)); showvalue(h, reveal); if (h->bet > 0) printf ("Bet: $%0.2f\n", h->bet); }
static void nc_conns_gc(struct net_child_info *nci, bool free_all) { clist *dead = NULL; unsigned int n_gc = 0; /*nr of dead connections*/ size_t nr_dead = 0; /* build list of dead connections */ unsigned int i; for (i = 0; i < nci->conns->len; i++) { struct nc_conn *conn = parr_idx(nci->conns, i); if (free_all || conn->dead) dead = clist_prepend(dead, conn); } nr_dead = clist_length( dead ); if (nr_dead){ LOG("Nr of dead connections:%d\n", nr_dead); } /* remove and free dead connections */ clist *tmp = dead; while (tmp) { struct nc_conn *conn = tmp->data; tmp = tmp->next; parr_remove(nci->conns, conn); nc_conn_free(conn); n_gc++; } clist_free(dead); if (debugging) fprintf(plog, "net: gc'd %u connections\n", n_gc); }
void clist_shuffle(clist *l) { unsigned int len = (int32_t)clist_length(l); unsigned int idx = 0; clist *tmp = l; while (tmp) { unsigned int ridx = random_int_range(0, len - 1); if (ridx != idx) { clist *swap = clist_nth(l, ridx); void *tmp_data = tmp->data; void *swap_data = swap->data; tmp->data = swap_data; swap->data = tmp_data; } tmp = tmp->next; idx++; } }
static void shutdown_daemon(struct net_child_info *nci) { LOG_BEGIN; bool rc = peerman_write(nci->peers); fprintf(plog, "net: %s %u/%zu peers\n", rc ? "wrote" : "failed to write", bp_hashtab_size(nci->peers->map_addr), clist_length(nci->peers->addrlist)); if (plog != stdout && plog != stderr) { fclose(plog); plog = NULL; } if (setting("free")) { shutdown_nci(nci); bp_hashtab_unref(orphans); bp_hashtab_unref(settings); blkdb_free(&db); bp_utxo_set_free(&uset); } LOG_END; }
/* PUBLIC */ int redundant_hints(void) { return clist_length(Redundant_hints); } /* redundant_hints */
int blackjack (hand *h) { return (clist_length(h->cards) == 2) && (handvalue(h) == 21); }