int search(const word_t *pre_words, const char **pat_words, int pat_count, int **documents) { // find common words word_t **words; int num_of_words = match_words(pre_words, pat_words, pat_count, &words); // find priorities of these words double *priorities = NULL; get_priorities((const word_t **)words, num_of_words, &priorities); // get used documents int num_of_doc = get_documents((const word_t **)words, num_of_words, documents); int *measures = (int *)malloc(sizeof(int) * num_of_doc); int *step_words = NULL; // i.e. initializing with 0s int *step_info_position = (int *)calloc(sizeof(int), num_of_words); int cur_doc_num = -1; int cur_doc = -1; int num_step_words = 0; while((num_step_words = get_step_words((const word_t **)words, num_of_words, cur_doc, step_info_position, &step_words)) != -1) { int measure = get_measure((const word_t **)words, step_info_position, priorities, step_words, num_step_words); ++cur_doc_num; cur_doc = (*documents)[cur_doc_num]; for(int i = 0; i < num_of_doc; i++) if(cur_doc == (*documents)[i]) { measures[i] = measure; break; } if(cur_doc_num == num_of_doc - 1) break; } for(int i = 0; i < num_of_doc - 1; i++) { int max = 0; for(int j = 1; j < num_of_doc - i; j++) { if(measures[j] < measures[max]) { max = j; } } int temp = measures[num_of_doc - i - 1]; measures[num_of_doc - i - 1] = measures[max]; measures[max] = temp; temp = (*documents)[num_of_doc - i - 1]; (*documents)[num_of_doc - i - 1] = (*documents)[max]; (*documents)[max] = temp; } free(step_info_position); free(priorities); free(step_words); for(int i = 0; i < num_of_doc; i++) printf("%d ", measures[i]); printf("\n"); free (measures); free(words); return num_of_doc; }
DEFINEFN void psyco_stats_reset(void) { /* reset all stats */ int i = 0; PyObject *key, *value, *d; stats_printf(("stats: reset\n")); /* reset the charge of all PyCodeStats, keep only the used ones */ RECLIMIT_SAFE_ENTER(); d = PyDict_New(); if (d == NULL) OUT_OF_MEMORY(); while (PyDict_Next(codestats_dict, &i, &key, &value)) { PyCodeStats* cs = (PyCodeStats*) key; if (cs->st_mergepoints) { /* clear the charge and keep alive */ cs->st_charge = 0.0f; if (PyDict_SetItem(d, key, value)) OUT_OF_MEMORY(); } } RECLIMIT_SAFE_LEAVE(); Py_DECREF(codestats_dict); codestats_dict = d; charge_total = 0.0; charge_prelimit = 0.0f; /* reset the time measure in all threads */ { #if MEASURE_ALL_THREADS PyInterpreterState* istate = PyThreadState_Get()->interp; PyThreadState* tstate; for (tstate=istate->tstate_head; tstate; tstate=tstate->next) { (void) get_measure(tstate); } #else (void) get_measure(NULL); #endif } }
DEFINEFN void psyco_stats_append(PyThreadState* tstate, PyFrameObject* f) { double charge; float cs_charge; int bits; time_measure_t numticks; if (!measuring_state(tstate)) return; numticks = get_measure(tstate); if (measure_is_zero(numticks) || f == NULL) return; /* f==NULL must still make a get_measure() call */ charge = ((double) charge_unit) * numticks; bits = c_random(); while (1) { PyCodeStats* cs = PyCodeStats_Get(f->f_code); cs_charge = (float)(cs->st_charge + charge); cs->st_charge = cs_charge; charge_total += charge; if (cs_charge > charge_prelimit && charge_callback) { /* update charge_prelimit */ charge_prelimit = (float)(charge_total * charge_watermark); if (cs_charge > charge_prelimit) { /* still over the up-to-date limit */ cs->st_charge = 0.0f; break; } } if (bits >= 0) return; /* triggers in about 50% of the cases */ bits <<= 1; f = f->f_back; if (!f) return; charge *= charge_parent2; } /* charge limit reached, invoke callback */ { PyObject* r; r = PyObject_CallFunction(charge_callback, "Of", f, cs_charge); if (r == NULL) { PyErr_WriteUnraisable((PyObject*) f); } else { Py_DECREF(r); } } }