void kmeansModule::initClustersLabeled(float** data, vector<int>& labels,int M, vector<int>& pts) { int N = pts.size(); vector<int> lset = determineLabelSet(pts, labels); int KK = lset.size(); vector <float> counts (KK, 0); vector<vector<float> > classMeans(KK, vector<float> (M, 0)); for (int i = 0 ; i < N; i++) { int l = labels[pts[i]]; int k = vec_find(lset, l); counts[k]++; for (int j = 0; j < M; j++) if (weights[j]) classMeans[k][j] += data[pts[i]][j]; } for (int k = 0 ; k < KK; k++) for (int j = 0; j < M; j++) if (weights[j]) classMeans[k][j] /= counts[k]; if (K >= KK) { K = KK; means = classMeans; return; } vector<int> clustLbls = simpleMeans(classMeans, K); counts = vector<float>(K, 0); for (int i = 0; i < pts.size(); i++) { int l = labels[pts[i]]; int k = vec_find(lset, l); for (int j = 0; j < M; j++) if (weights[j]) means[clustLbls[k]][j] += data[pts[i]][j]; counts[clustLbls[k]]++; } for (int k = 0 ; k < K; k++) for (int j = 0; j < M; j++) if (weights[j]) means[k][j] /= counts[k]; }
void LLCCEP_debugger::debugCore::addBreakpoint(::std::string name, ::std::string fname, size_t lineOf) { DEBUG_CORE_OK; auto files = softcore::reader->getProgramData().fnames; if (vec_find(files, fname) == files.end()) { ::std::cerr << "No such file '" << fname << "' to add breakpoint" << ::std::endl; return; } for (const auto &i: breakpoints) { if (i.name == name) { ::std::cerr << "There's already a breakpoint with '" << name << "' name" << ::std::endl; return; } if (i.fname == fname && i.line == lineOf) { ::std::cerr << "There's already a breakpoint at '" << fname << "' file at" << lineOf << " line" << ::std::endl; return; } } LLCCEP_debugger::debugCore::breakpointInfo info{ name, fname, lineOf, true }; breakpoints.push_back(info); DEBUG_CORE_OK; }
struct membuf *get_named_buffer(const char *name) { int pos; struct sbe e[1]; struct sbe *ep; struct membuf *mp; /* name is already strdup:ped */ e->name = name; pos = vec_find(s_sbe_table, sbe_cmp, e); if(pos >= 0) { /* found */ ep = vec_get(s_sbe_table, pos); } else { membuf_init(e->mb); read_file(name, e->mb); ep = vec_insert(s_sbe_table, -(pos + 2), e); } mp = ep->mb; return mp; }
void LLCCEP_ASM::analyzer::analyze(::std::vector<LLCCEP_ASM::lexem> lex) { auto argumentsMismatch = [this](LLCCEP_ASM::lexem lexemData, LLCCEP_ASM::lex_t t1) { ::std::map<LLCCEP_ASM::lex_t, ::std::vector<LLCCEP_ASM::lex_t> > supportedMap = { {LLCCEP_ASM::LEX_T_REG, {LLCCEP_ASM::LEX_T_REG}}, {LLCCEP_ASM::LEX_T_MEM, {LLCCEP_ASM::LEX_T_REG, LLCCEP_ASM::LEX_T_MEM}}, {LLCCEP_ASM::LEX_T_VAL, {LLCCEP_ASM::LEX_T_REG, LLCCEP_ASM::LEX_T_MEM, LLCCEP_ASM::LEX_T_VAL}}, {LLCCEP_ASM::LEX_T_COND, {LLCCEP_ASM::LEX_T_COND}}, {LLCCEP_ASM::LEX_T_NO, {LLCCEP_ASM::LEX_T_NO}} }; auto found = supportedMap.find(t1); if (found == supportedMap.end()) { analyzerIssue("undefined", 0, "Lexem of type '%s' shouldn't appear on analyzis step!", LLCCEP_ASM::getLexemTypename(lexemData.type).c_str()); } auto data = supportedMap[t1]; return (vec_find(data, lexemData.type) == data.end()); }; if (!lex.size()) return; if (lex.size() > 4) { analyzerIssue(lex[0].pos.file, lex[0].pos.line, "Too many lexems!\n" "At least an instruction name and three arguments."); } int64_t instructionFound = LLCCEP_ASM::isInstruction(lex[0].val); if (instructionFound == -1) { analyzerIssue(lex[0].pos.file, lex[0].pos.line, "No such instruction: '%s'!", lex[0].val.c_str()); } for (unsigned i = 1; i < lex.size(); i++) { if (argumentsMismatch(lex[i], LLCCEP_ASM::INSTRUCTIONS[instructionFound].types[i - 1])) { analyzerIssue(lex[i].pos.file, lex[i].pos.line, "Conflicting type of %u argument of " "'%s' instruction.\n" "'%s' is required, instead of '%s'.\n", i, lex[0].val.c_str(), LLCCEP_ASM::getLexemTypename( LLCCEP_ASM::INSTRUCTIONS[instructionFound].types[i - 1]).c_str(), LLCCEP_ASM::getLexemTypename(lex[i].type).c_str()); } } }
struct membuf *new_named_buffer(const char *name) { int pos; struct sbe e[1]; struct sbe *ep; struct membuf *mp; /* name is already strdup:ped */ e->name = name; pos = vec_find(s_sbe_table, sbe_cmp, e); if(pos >= 0) { /* found */ LOG(LOG_ERROR, ("buffer already exists.\n")); exit(-1); } membuf_init(e->mb); ep = vec_insert(s_sbe_table, -(pos + 2), e); mp = ep->mb; return mp; }
bool opt_set(const wxString& name, const wxString& val) { const opt_desc dummy = new_opt_desc(name); const opt_desc* opt = std::lower_bound(&opts[0], &opts[num_opts], dummy, opt_lt); if (!wxStrcmp(name, opt->opt)) { if (opt->stropt) *opt->stropt = wxString(val); else if (opt->boolopt) { if (!(val == wxT('0') || val == wxT('1'))) wxLogWarning(_("Invalid flag option %s - %s ignored"), name.c_str(), val.c_str()); else *opt->boolopt = val == wxT('1'); } else if (!opt->enumvals.empty()) { wxString s = val; s.MakeLower(); wxString ev = opt->enumvals; ev.MakeLower(); auto enum_opts = str_split(ev, wxT("|")); const std::size_t found_pos = vec_find(enum_opts, s); const bool matched = ((int)found_pos != wxNOT_FOUND); if (!matched) { const wxString evx = wxGetTranslation(opt->enumvals); bool isx = wxStrcmp(opt->enumvals, evx) != 0; // technically, the translation for this string could incorproate // the equals sign if necessary instead of doing it this way wxLogWarning(_("Invalid value %s for option %s; valid values are %s%s%s"), s.c_str(), opt->opt.c_str(), opt->enumvals.c_str(), isx ? wxT(" = ") : wxEmptyString, isx ? evx.c_str() : wxEmptyString); } else { *opt->intopt = found_pos; } } else if (opt->intopt) { const wxString s(val); long ival; if (!s.ToLong(&ival) || ival < opt->min || ival > opt->max) wxLogWarning(_("Invalid value %d for option %s; valid values are %d - %d"), ival, name.c_str(), opt->min, opt->max); else *opt->intopt = ival; } else if (opt->doubleopt) { const wxString s(val); double dval; if (!s.ToDouble(&dval) || dval < opt->min || dval > opt->max) wxLogWarning(_("Invalid value %f for option %s; valid values are %f - %f"), dval, name.c_str(), opt->min, opt->max); else *opt->doubleopt = dval; } else if (opt->uintopt) { const wxString s(val); unsigned long uival; if (!s.ToULong(&uival) || uival < opt->min || uival > opt->max) wxLogWarning(_("Invalid value %f for option %s; valid values are %f - %f"), uival, name.c_str(), opt->min, opt->max); else *opt->uintopt = (uint32_t)uival; } else { // GB/Palette[0-2] is virtual for (int i = 0; i < 3; i++) { wxString optn; optn.Printf(wxT("GB/Palette%d"), i); if (optn != name) continue; wxString vals(val); for (size_t j = 0, cpos = 0; j < 8; j++) { int start = cpos; cpos = vals.find(wxT(','), cpos); if (cpos == wxString::npos) cpos = vals.size(); long ival; // ignoring errors; if the value is bad, palette will be corrupt // -- tough. // stupid wxString.ToLong doesn't support start @ offset wxString entry = vals.substr(start, cpos - start); entry.ToLong(&ival, 16); systemGbPalette[i * 8 + j] = ival; if (cpos != vals.size()) cpos++; } } } return true; } else { if (name.Find(wxT('/')) == wxNOT_FOUND) return false; auto parts = str_split(name, wxT("/")); if (parts[0] != wxT("Keyboard")) { cmditem* cmd = std::lower_bound(&cmdtab[0], &cmdtab[ncmds], cmditem{parts[1],wxString(),0,0,NULL}, cmditem_lt); if (cmd == &cmdtab[ncmds] || wxStrcmp(parts[1], cmd->cmd)) return false; for (auto i = gopts.accels.begin(); i < gopts.accels.end(); ++i) if (i->GetCommand() == cmd->cmd_id) { auto j = i; for (; j < gopts.accels.end(); ++j) if (j->GetCommand() != cmd->cmd_id) break; gopts.accels.erase(i, j); break; } if (!val.empty()) { auto aval = wxKeyTextCtrl::FromString(val); for (size_t i = 0; i < aval.size(); i++) aval[i].Set(aval[i].GetFlags(), aval[i].GetKeyCode(), cmd->cmd_id); if (!aval.size()) wxLogWarning(_("Invalid key binding %s for %s"), val.c_str(), name.c_str()); else gopts.accels.insert(gopts.accels.end(), aval.begin(), aval.end()); } return true; } else if (!wxStrncmp(name, wxT("Joypad"), wxStrlen(wxT("Joypad")))) { if (parts[1] < wxT('1') || parts[1] > wxT('4') || parts.size() < 3) return false; int jno = parts[1][0] - wxT('1'); int kno; for (kno = 0; kno < NUM_KEYS; kno++) if (!wxStrcmp(joynames[kno], parts[2])) break; if (kno == NUM_KEYS) return false; if (val.empty()) gopts.joykey_bindings[jno][kno].clear(); else { auto b = wxJoyKeyTextCtrl::FromString(val); if (!b.size()) wxLogWarning(_("Invalid key binding %s for %s"), val.c_str(), name.c_str()); else gopts.joykey_bindings[jno][kno] = b; } return true; } else return false; } }
// FIXME: simulate MakeInstanceFilename(vbam.ini) using subkeys (Slave%d/*) void load_opts() { // just for sanity... bool did_init = false; if (did_init) return; did_init = true; // Translations can't be initialized in static structures (before locale // is initialized), so do them now for (int i = 0; i < num_opts; i++) opts[i].desc = wxGetTranslation(opts[i].desc); // enumvals should not be translated, since they would cause config file // change after lang change // instead, translate when presented to user wxFileConfig* cfg = wxGetApp().cfg; cfg->SetPath(wxT("/")); // enure there are no unknown options present // note that items cannot be deleted until after loop or loop will fail wxArrayString item_del, grp_del; long grp_idx; wxString s; bool cont; for (cont = cfg->GetFirstEntry(s, grp_idx); cont; cont = cfg->GetNextEntry(s, grp_idx)) { //wxLogWarning(_("Invalid option %s present; removing if possible"), s.c_str()); item_del.push_back(s); } // Date of last online update check; gopts.last_update = cfg->Read(wxT("General/LastUpdated"), (long)0); cfg->Read(wxT("General/LastUpdatedFileName"), &gopts.last_updated_filename); std::sort(&opts[0], &opts[num_opts], opt_lt); for (cont = cfg->GetFirstGroup(s, grp_idx); cont; cont = cfg->GetNextGroup(s, grp_idx)) { // ignore wxWidgets-managed global library settings if (s == wxT("wxWindows")) continue; // ignore file history if (s == wxT("Recent")) continue; cfg->SetPath(s); int poff = s.size(); long entry_idx; wxString e; for (cont = cfg->GetFirstGroup(e, entry_idx); cont; cont = cfg->GetNextGroup(e, entry_idx)) { // the only one with subgroups if (s == wxT("Joypad") && e.size() == 1 && e[0] >= wxT('1') && e[0] <= wxT('4')) { s.append(wxT('/')); s.append(e); s.append(wxT('/')); int poff2 = s.size(); cfg->SetPath(e); long key_idx; for (cont = cfg->GetFirstGroup(e, key_idx); cont; cont = cfg->GetNextGroup(e, key_idx)) { s.append(e); //wxLogWarning(_("Invalid option group %s present; removing if possible"), s.c_str()); grp_del.push_back(s); s.resize(poff2); } for (cont = cfg->GetFirstEntry(e, key_idx); cont; cont = cfg->GetNextEntry(e, key_idx)) { int i; for (i = 0; i < NUM_KEYS; i++) if (e == joynames[i]) break; if (i == NUM_KEYS) { s.append(e); //wxLogWarning(_("Invalid option %s present; removing if possible"), s.c_str()); item_del.push_back(s); s.resize(poff2); } } s.resize(poff); cfg->SetPath(wxT("/")); cfg->SetPath(s); } else { s.append(wxT('/')); s.append(e); //wxLogWarning(_("Invalid option group %s present; removing if possible"), s.c_str()); grp_del.push_back(s); s.resize(poff); } } for (cont = cfg->GetFirstEntry(e, entry_idx); cont; cont = cfg->GetNextEntry(e, entry_idx)) { // kb options come from a different list if (s == wxT("Keyboard")) { const cmditem dummy = new_cmditem(e); if (!std::binary_search(&cmdtab[0], &cmdtab[ncmds], dummy, cmditem_lt)) { s.append(wxT('/')); s.append(e); //wxLogWarning(_("Invalid option %s present; removing if possible"), s.c_str()); item_del.push_back(s); s.resize(poff); } } else { s.append(wxT('/')); s.append(e); opt_desc dummy = new_opt_desc(s); wxString opt_name(dummy.opt); if (!std::binary_search(&opts[0], &opts[num_opts], dummy, opt_lt) && opt_name != wxT("General/LastUpdated") && opt_name != wxT("General/LastUpdatedFileName")) { //wxLogWarning(_("Invalid option %s present; removing if possible"), s.c_str()); item_del.push_back(s); } s.resize(poff); } } cfg->SetPath(wxT("/")); } for (size_t i = 0; i < item_del.size(); i++) cfg->DeleteEntry(item_del[i]); for (size_t i = 0; i < grp_del.size(); i++) cfg->DeleteGroup(grp_del[i]); // now read actual values and set to default if unset // config file will be updated with unset options cfg->SetRecordDefaults(); for (int i = 0; i < num_opts; i++) { opt_desc& opt = opts[i]; if (opt.stropt) { //Fix provided by nhdailey cfg->Read(opt.opt, opt.stropt, *opt.stropt); opt.curstr = *opt.stropt; } else if (!opt.enumvals.empty()) { auto enum_opts = str_split(opt.enumvals.MakeLower(), wxT("|")); opt.curint = *opt.intopt; bool gotit = cfg->Read(opt.opt, &s); s.MakeLower(); if (gotit && !s.empty()) { const auto found_pos = vec_find(enum_opts, s); const bool matched = ((int)found_pos != wxNOT_FOUND); if (!matched) { opt.curint = 0; const wxString ev = opt.enumvals; const wxString evx = wxGetTranslation(ev); bool isx = wxStrcmp(ev, evx) != 0; // technically, the translation for this string could incorproate // the equals sign if necessary instead of doing it this way wxLogWarning(_("Invalid value %s for option %s; valid values are %s%s%s"), s.c_str(), opt.opt.c_str(), ev.c_str(), isx ? wxT(" = ") : wxEmptyString, isx ? evx.c_str() : wxEmptyString); // write first option cfg->Write(opt.opt, enum_opts[0]); } else opt.curint = found_pos; *opt.intopt = opt.curint; } else { cfg->Write(opt.opt, enum_opts[opt.curint]); } } else if (opt.intopt) { cfg->Read(opt.opt, &opt.curint, *opt.intopt); if (opt.curint < opt.min || opt.curint > opt.max) { wxLogWarning(_("Invalid value %d for option %s; valid values are %d - %d"), opt.curint, opt.opt.c_str(), opt.min, opt.max); } else *opt.intopt = opt.curint; } else if (opt.doubleopt) { cfg->Read(opt.opt, &opt.curdouble, *opt.doubleopt); if (opt.curdouble < opt.min || opt.curdouble > opt.max) { wxLogWarning(_("Invalid value %f for option %s; valid values are %f - %f"), opt.curdouble, opt.opt.c_str(), opt.min, opt.max); } else *opt.doubleopt = opt.curdouble; } else if (opt.uintopt) { int val; cfg->Read(opt.opt, &val, *opt.uintopt); opt.curuint = val; if (opt.curuint < opt.min || opt.curuint > opt.max) { wxLogWarning(_("Invalid value %f for option %s; valid values are %f - %f"), opt.curuint, opt.opt.c_str(), opt.min, opt.max); } else *opt.uintopt = opt.curuint; } else if (opt.boolopt) { cfg->Read(opt.opt, opt.boolopt, *opt.boolopt); opt.curbool = *opt.boolopt; } } // GB/Palette[0-2] is special for (int i = 0; i < 3; i++) { wxString optn; optn.Printf(wxT("GB/Palette%d"), i); wxString val; const opt_desc dummy = new_opt_desc(optn); opt_desc* opt = std::lower_bound(&opts[0], &opts[num_opts], dummy, opt_lt); wxString entry; for (int j = 0; j < 8; j++) { // stupid wxString.Printf doesn't support printing at offset entry.Printf(wxT("%04X,"), (int)systemGbPalette[i * 8 + j]); val.append(entry); } val.resize(val.size() - 1); cfg->Read(optn, &val, val); opt->curstr = val; for (int j = 0, cpos = 0; j < 8; j++) { int start = cpos; cpos = val.find(wxT(','), cpos); if ((size_t)cpos == wxString::npos) cpos = val.size(); long ival; // ignoring errors; if the value is bad, palette will be corrupt // -- tough. // stupid wxString.ToLong doesn't support start @ offset entry = val.substr(start, cpos - start); entry.ToLong(&ival, 16); systemGbPalette[i * 8 + j] = ival; if ((size_t)cpos != val.size()) cpos++; } } // joypad is special for (int i = 0; i < 4; i++) { for (int j = 0; j < NUM_KEYS; j++) { wxString optname; optname.Printf(wxT("Joypad/%d/%s"), i + 1, joynames[j].c_str()); bool gotit = cfg->Read(optname, &s); if (gotit) { gopts.joykey_bindings[i][j] = wxJoyKeyTextCtrl::FromString(s); if (s.size() && !gopts.joykey_bindings[i][j].size()) wxLogWarning(_("Invalid key binding %s for %s"), s.c_str(), optname.c_str()); } else { s = wxJoyKeyTextCtrl::ToString(gopts.joykey_bindings[i][j]); cfg->Write(optname, s); } } } // keyboard is special // Keyboard does not get written with defaults wxString kbopt(wxT("Keyboard/")); int kboff = kbopt.size(); for (int i = 0; i < ncmds; i++) { kbopt.resize(kboff); kbopt.append(cmdtab[i].cmd); if (cfg->Read(kbopt, &s) && s.size()) { wxAcceleratorEntry_v val = wxKeyTextCtrl::FromString(s); if (!val.size()) wxLogWarning(_("Invalid key binding %s for %s"), s.c_str(), kbopt.c_str()); else { for (size_t j = 0; j < val.size(); j++) val[j].Set(val[j].GetFlags(), val[j].GetKeyCode(), cmdtab[i].cmd_id); gopts.accels.insert(gopts.accels.end(), val.begin(), val.end()); } } } // recent is special // Recent does not get written with defaults cfg->SetPath(wxT("/Recent")); gopts.recent->Load(*cfg); cfg->SetPath(wxT("/")); cfg->Flush(); }