int main() { std::vector<MyClass> values { MyClass { "Dagobart", 25 }, MyClass { "Dagobart", 26 }, MyClass { "Dagobart", 27 }, MyClass { "John", 0 }, MyClass { "John", 10 }, MyClass { "John", 15 }, MyClass { "John", 5 }, MyClass { "Mike", 0 }, MyClass { "Mike", 25 }, MyClass { "Mike", 25 }, MyClass { "Mike", 27 } }; HashTable::bucket_type buckets[100]; HashTable hashtable(values.begin(), values.end(), HashTable::bucket_traits(buckets, 100)); std::cout << "\nDebugging buckets of hashtable\n"; std::multimap<size_t, MyClass const*> debug_map; std::transform(hashtable.begin(), hashtable.end(), std::inserter(debug_map, debug_map.end()), [&](MyClass const& mc) { return std::make_pair(hashtable.bucket(mc), &mc); } ); for (auto& entry : debug_map) std::cout << "Debug bucket: " << entry.first << " -> " << *entry.second << "\n"; }
int main() { std::vector<MyClass> values { MyClass { "Dagobart", 25 }, MyClass { "Dagobart", 26 }, MyClass { "Dagobart", 27 }, MyClass { "John", 0 }, MyClass { "John", 10 }, MyClass { "John", 15 }, MyClass { "John", 5 }, MyClass { "Mike", 0 }, MyClass { "Mike", 25 }, MyClass { "Mike", 25 }, MyClass { "Mike", 27 } }; HashTable::bucket_type buckets[100]; HashTable hashtable(values.begin(), values.end(), HashTable::bucket_traits(buckets, 100)); debug_buckets(hashtable); std::cout << "Removing all Mikes\n"; values.erase( std::remove_if(values.begin(), values.end(), [](MyClass const& mc) { return mc.name == "Mike"; }), values.end()); debug_buckets(hashtable); }
static void WriteHashtable(const char* map_out_path, MFRNodeArray &nodes, MFREdgeArray &edges, MFRQuadTree &quadTree) { int tablesize = TableSize(nodes.nrnodes); HashTable hashtable(map_out_path, tablesize); int i; for (i=0;i<nodes.nrnodes;i++) { int bin = hashtable.Hash(nodes.nodes[i].nodeidp); if (!hashtable.first[bin]) { fprintf(hashtable.Bucket(bin),",\n"); } hashtable.first[bin] = 0; fprintf(hashtable.Bucket(bin),"\"%s\" : [%f, %f, \"%s\" ]\n",nodes.nodes[i].nodeidp, nodes.nodes[i].x, nodes.nodes[i].y, nodes.nodes[i].quadNode->quadid); } }
int main() { std::array<Element, 256> storage; // reserved 256 entries std::array<Elements::bucket_type, 100> buckets; // buckets for the hashtable Elements hashtable(Elements::bucket_traits(buckets.data(), buckets.size())); storage[0] = { "one", "Eins" }; storage[1] = { "two", "Zwei" }; storage[2] = { "three", "Drei" }; storage[3] = { "four", "Vier" }; storage[4] = { "five", "Fuenf" }; hashtable.insert(storage.data(), storage.data() + 5); for (auto& e : hashtable) { std::cout << "Hash entry: " << e.key << " -> " << e.value << "\n"; } std::cout << "hashtable[\"three\"] -> " << hashtable.find({"three"})->value << "\n"; }
/* * Reads each line from the input file, then malloc()s and populates main * data structures X, T, Y */ void compile(FILE *fin, t_clause ***X, t_clause ***T, t_clause ***Y, int *params, char *xs) { int r, value; int count = 0; t_fsm *fsmp = fsmparser(); char line[LINLEN]; char copy[LINLEN]; char *token; char *op; char c; t_clause *cl; // used for keeping track of input and output symbols t_hashtable *inputs = hashtable(IN_HASHSIZE); t_hashtable *outputs = hashtable(OUT_HASHSIZE); t_hashtable *temps = hashtable(TMP_HASHSIZE); /* check hashtable pointers */ if (!inputs || !outputs || !temps) { fprintf(stderr, "-- compile() : could not init hashtables\n"); return; } /* process each line at a time */ while (fgets(line, LINLEN, fin)) { int l = lincpy(line, copy, "\n\t\r;"); if (l > 1) { /* initialize data structures */ if (isdigit((c=copy[0]))) { token = strtok(copy, " "); value = atoi(token); token = strtok(NULL, " "); if (streq(token, "gates") || streq(token, "gate")) { *T = malloc(value*sizeof(t_clause*)); params[2]=value; r = next(fsmp, LEN); } else if (streq(token, "inputs")) { *X = malloc(value*sizeof(t_clause*)); params[0]=value; r = next(fsmp, IN); } else if (streq(token, "outputs")) { *Y = malloc(value*sizeof(t_clause*)); params[1]=value; r = next(fsmp, OUT); } else r = next(fsmp, ERR); } /* line == 'begin' or line == 'end' */ else if (streq(copy, "begin")) r = next(fsmp, PROG); else if (streq(copy, "end")) r = next(fsmp, END); /* list of input symbols */ else if (getstate(fsmp) == IN) { token=strtok(copy, " "); while (token) { cl = clause(NULL, NULL, NULL, token, count); printf("0x%x @ %s ", cl, get_clause_name(cl)); if (xs) { int xval = -48; bool b; xval += (count < strlen(xs)) ? xs[count] : 48; b = (!xval) ? FALSE : TRUE; set_value(cl, b); printf("= %d", b); } (*X)[count++] = cl; put_clause(inputs, cl); printf("\n"); token = strtok(NULL, " "); } count=0; } /* list of output symbols */ else if (getstate(fsmp) == OUT) { token = strtok(copy, " "); while (token) { cl = clause(NULL, NULL, NULL, token, count++); put_clause(outputs, cl); printf("0x%x @ %s\n", cl, get_clause_name(cl)); token=strtok(NULL, " "); } count=0; } /* parse clauses */ else if (getstate(fsmp) == PROG) { token = strtok(copy, "= "); t_clause *target = find_clause(outputs, token); char *lcname, *rcname, *op; t_clause *lcl, *rcl; lcname = strtok(NULL, "= "); op = strtok(NULL, "= "); rcname = strtok(NULL, "= "); /* composite output clause */ if (op && lcname && rcname && target) { // find left and right operands lcl = lookup(lcname, inputs, outputs, temps); rcl = lookup(rcname, inputs, outputs, temps); if (!lcl || !rcl) { r = next(fsmp, ERR); continue; } cl = clause(lcl, rcl, op, token, count); printf("0x%x @ %s = %s %s %s\n", target, get_clause_name(target), get_clause_name(lcl), op, get_clause_name(rcl)); int num = enumerate(target); clause_copy(cl, target); (*Y)[num] = target; } /* simple output clause */ else if (target && lcname) { lcl = lookup(lcname, inputs, outputs, temps); if (!lcl) { r = next(fsmp, ERR); continue; } printf("0x%x @ %s = %s\n", target, get_clause_name(target), get_clause_name(lcl)); int num = enumerate(target); clause_copy(lcl, target); (*Y)[num] = target; } /* just a temp clause ?! */ else { lcl = lookup(lcname, inputs, outputs, temps); rcl = lookup(rcname, inputs, outputs, temps); if (!lcl || !rcl) { r = next(fsmp, ERR); continue; } cl = clause(lcl, rcl, op, token, count); put_clause(temps, cl); (*T)[count++]=cl; printf("0x%x @ %s\n", cl, get_clause_name(cl)); } } else { /* invalid line */ r = next(fsmp, ERR); } } else { /* l <= 1 means line is comment or blank */ r = next(fsmp, IGN); } /* valid transaction? */ if (!r || getstate(fsmp) == ERR) { fprintf(stderr, "compile() :: parsing error (r=%d\ts=%d)\n", r, getstate(fsmp)); if (*T) free(*T); if (*Y) free(*Y); if (*X) free(*X); exit(1); } } // get rid of unnecessary hashtables // note that this won't affect clauses, which // remain available through X, Y, and T wipe_hashtable(inputs, FALSE); wipe_hashtable(outputs, FALSE); wipe_hashtable(temps, FALSE); }