void move_cursor_to_start_of_prompt(int erase) { int termwidth = winsize.ws_col; int promptlen_on_screen, number_of_lines_in_prompt, curpos, count; int cooked = (saved_rl_state.cooked_prompt != NULL); DPRINTF2(DEBUG_READLINE,"prompt_is_still_uncooked: %d, impatient_prompt: %d", prompt_is_still_uncooked, impatient_prompt); if (prompt_is_still_uncooked && ! impatient_prompt) return; /* @@@ is this necessary ?*/ promptlen_on_screen = colourless_strlen_unmarked(saved_rl_state.cooked_prompt ? saved_rl_state.cooked_prompt : saved_rl_state.raw_prompt, termwidth); curpos = (within_line_edit ? 1 : 0); /* if the user has pressed a key the cursor will be 1 past the current prompt */ assert(termwidth > 0); number_of_lines_in_prompt = 1 + ((promptlen_on_screen + curpos -1) / termwidth); /* integer arithmetic! (e.g. 171/80 = 2) */ cr(); for (count = 0; count < number_of_lines_in_prompt -1; count++) { if (erase) clear_line(); curs_up(); } clear_line(); DPRINTF4(DEBUG_READLINE,"moved cursor up %d lines (erase = %d, len=%d, cooked=%d)", number_of_lines_in_prompt - 1, erase, promptlen_on_screen, cooked); }
int colourless_strlen(const char *str, char **pcopy_without_ignore_markers, int termwidth) { int visible = TRUE; int column = 0; int length = strlen(str); const char *p; char *q, *copy_without_ignore_markers; assert(termwidth >= 0); q = copy_without_ignore_markers = mymalloc(length + 1); for(p = str; *p; p++) { assert (q < copy_without_ignore_markers + length); switch (*p) { case RL_PROMPT_START_IGNORE: visible = FALSE; continue; case RL_PROMPT_END_IGNORE: visible = TRUE; continue; case '\r': if (visible) { q -= column; column = 0; continue; } break; case '\b': if ((visible && q > copy_without_ignore_markers)) { q -= 1; column -= 1; if (termwidth && column < 0) column += termwidth; continue; } break; } if (visible) { *q++ = *p; column +=1; if (termwidth && column >= termwidth) column -= termwidth; } } *q = '\0'; DPRINTF4(DEBUG_READLINE, "colourless_strlen(\"%s\", 0x%lx, %d) = %ld", mangle_string_for_debug_log(str, MANGLE_LENGTH), (long) pcopy_without_ignore_markers, termwidth, q - copy_without_ignore_markers); if (pcopy_without_ignore_markers) *pcopy_without_ignore_markers = copy_without_ignore_markers; else free (copy_without_ignore_markers); return q - copy_without_ignore_markers; }
static void bindkey(int key, rl_command_func_t *function, const char *maplist) { char *mapnames[] = {"emacs-standard","emacs-ctlx","emacs-meta","vi-insert","vi-move","vi-command",NULL}; char **mapname; for (mapname = mapnames; *mapname; mapname++) if(strstr(maplist, *mapname)) { Keymap kmap = getmap(*mapname); DPRINTF4(DEBUG_READLINE,"Binding key %d (%s) in keymap '%s' to <0x%lx>", key, mangle_char_for_debug_log(key,TRUE), *mapname, (long) function); if (rl_bind_key_in_map(key, function, kmap)) myerror(FATAL|NOERRNO, "Could not bind key %d (%s) in keymap '%s'", key, mangle_char_for_debug_log(key,TRUE), *mapname); } }
void addSymbol(S_symbol *pp, S_symTab *tab) { /* a bug can produce, if you add a symbol into old table, and the same symbol exists in a newer one. Then it will be deleted from the newer one. All this story is about storing information in trail. It should containt, both table and pointer !!!! */ DPRINTF4("adding symbol %s: %s %s\n",pp->name, typesName[pp->b.symType], storagesName[pp->b.storage]); assert(pp->b.npointers==0); AddSymbolNoTrail(pp,tab); addToTrail(deleteSymDef, pp /* AND ALSO!!! , tab */ ); //if (WORK_NEST_LEVEL0()) {static int c=0;fprintf(dumpOut,"addsym0#%d\n",c++);} }
void cleanup_rlwrap_and_exit(int status) { unblock_all_signals(); DPRINTF0(DEBUG_TERMIO, "Cleaning up"); if (write_histfile && (histsize==0 || history_total_bytes() > 0)) /* avoid creating empty .speling_eror_history file after typo */ write_history(history_filename); /* ignore errors */ close_logfile(); DPRINTF4(DEBUG_SIGNALS, "command_pid: %d, commands_exit_status: %x, filter_pid: %d, filters_exit_status: %x", command_pid, commands_exit_status, filter_pid, filters_exit_status); mymicrosleep(10); /* we may have got an EOF or EPIPE because the filter or command died, but this doesn't mean that SIGCHLD has been caught already. Taking a little nap now improves the chance that we will catch it (no grave problem if we miss it, but diagnostics, exit status and transparent signal handling depend on it) */ if (filter_pid) kill_filter(); else if (filter_is_dead) { int filters_killer = killed_by(filters_exit_status); errno = 0; mywarn((filters_killer ? "filter was killed by signal %d (%s)": WEXITSTATUS(filters_exit_status) ? "filter died" : "filter exited"), filters_killer, signal_name(filters_killer)); } if (debug) debug_postmortem(); if (terminal_settings_saved) if (tcsetattr(STDIN_FILENO, TCSAFLUSH, &saved_terminal_settings) < 0) /* ignore errors (almost dead anyway) */ ; /* fprintf(stderr, "Arggh\n"); don't use myerror!!*/ if (!newline_came_last) /* print concluding newline, if necessary */ my_putstr("\n"); if (status != EXIT_SUCCESS) /* rlwrap itself has failed, rather than the wrapped command */ exit(status); else { int commands_killer = killed_by(commands_exit_status); if (commands_killer) suicide_by(commands_killer, commands_exit_status); /* command terminated by signal, make rlwrap's parent believe rlwrap was killed by it */ else exit(WEXITSTATUS(commands_exit_status)); /* propagate command's exit status */ } }
int adapt_tty_winsize(int from_fd, int to_fd) { int ret; struct winsize old_winsize = winsize; ret = ioctl(from_fd, TIOCGWINSZ, &winsize); DPRINTF1(DEBUG_SIGNALS, "ioctl (..., TIOCGWINSZ) = %d", ret); if (winsize.ws_col != old_winsize.ws_col || winsize.ws_row != old_winsize.ws_row) { DPRINTF4(DEBUG_SIGNALS, "ws.col: %d -> %d, ws.row: %d -> %d", old_winsize.ws_col, winsize.ws_col, old_winsize.ws_row, winsize.ws_row); if (always_readline &&!dont_wrap_command_waits()) /* if --always_readline option is set, the client will probably spew a */ deferred_adapt_commands_window_size = TRUE; /* volley of control chars at us when its terminal is resized. Hence we don't do it now */ else { ret = ioctl(to_fd, TIOCSWINSZ, &winsize); DPRINTF1(DEBUG_SIGNALS, "ioctl (..., TIOCSWINSZ) = %d", ret); } DPRINTF2(DEBUG_READLINE, "rl_prompt: %s, line_buffer: %s", mangle_string_for_debug_log(rl_prompt, 100), rl_line_buffer); rl_set_screen_size(winsize.ws_row, winsize.ws_col); /* this is safe: we know that right now rlwrap is not calling the readline library because we keep SIGWINCH blocked all the time */ if (!within_line_edit && !skip_rlwrap()) { wipe_textarea(&old_winsize); received_WINCH = TRUE; /* we can't start line edit in signal handler, so we only set a flag */ } else if (within_line_edit) { /* try to keep displayed line tidy */ wipe_textarea(&old_winsize); rl_on_new_line(); rl_redisplay(); } return (!always_readline || dont_wrap_command_waits()); /* pass the signal on (except when always_readline is set and command is not waiting) */ } else { /* window size has not changed */ return FALSE; } }
/* format line and add it to history list, avoiding duplicates if necessary */ static void my_add_history(char *line) { int lookback, count, here; char *new_entry, *filtered_line; filtered_line = pass_through_filter(TAG_HISTORY, line); switch (history_duplicate_avoidance_policy) { case KEEP_ALL_DOUBLES: lookback = 0; break; case ELIMINATE_SUCCESIVE_DOUBLES: lookback = 1; break; case ELIMINATE_ALL_DOUBLES: lookback = history_length; break; } new_entry = filtered_line; lookback = min(history_length, lookback); for (count = 0, here = history_length - 1; count < lookback ; count++, here--) { DPRINTF4(DEBUG_READLINE, "strcmping <%s> and <%s> (count = %d, here = %d)", line, history_get(history_base + here)->line ,count, here); if (strncmp(new_entry, history_get(history_base + here) -> line, 10000) == 0) { /* history_get uses the logical offset history_base .. */ HIST_ENTRY *entry = remove_history (here); /* .. but remove_history doesn't! */ DPRINTF2(DEBUG_READLINE, "removing duplicate entry #%d (%s)", here, entry->line); free_foreign(entry->line); free_foreign(entry); } } add_history(new_entry); free(new_entry); }
/* Given the actual data set <dataSet>, estimates the values for algorithm parameters that would give the optimal running time of a query. The set <sampleQueries> is a set with query sample points (R-NN DS's parameters are optimized for query points from the set <sampleQueries>). <sampleQueries> could be a sample of points from the actual query set or from the data set. When computing the estimated number of collisions of a sample query point <q> with the data set points, if there is a point in the data set with the same pointer with <q> (that is when <q> is a data set point), then the corresponding point (<q>) is not considered in the data set (for the purpose of computing the respective #collisions estimation). //如果查询样本点来自数据集,则为了考虑碰撞估计,不讲对应的样本查询点考虑进数据集合 The return value is the estimate of the optimal parameters. */ RNNParametersT computeOptimalParameters(RealT R, //半径 RealT successProbability, //成功率 IntT nPoints, //数据点的个数 IntT dimension, //维度 PPointT *dataSet, //数据集合 IntT nSampleQueries, //样本查询点的个数 PPointT *sampleQueries, //样本查询点的数据集 MemVarT memoryUpperBound){ //每个半径需要使用内存的上界,多个半径的时候Qboundry才会使用到,用一个半径的时候,是使用不到的。 ASSERT(nSampleQueries > 0); initializeLSHGlobal(); //设置了一个时间参数,把时间函数所花的时间求出来,得出精确操作所花费的时间 //用来评估算法效率 RNNParametersT optParameters; optParameters.successProbability = successProbability; optParameters.dimension = dimension; optParameters.parameterR = R; #ifdef USE_L1_DISTANCE optParameters.parameterR2 = R; //L1 norm下R2 与R相等, //R与R2的作用? #else optParameters.parameterR2 = SQR(R); //?R2的作用是什么 #endif optParameters.useUfunctions = TRUE; // TODO: could optimize here: // maybe sometimes, the old way // was better. optParameters.parameterW = PARAMETER_W_DEFAULT; //W用于计算L2 norm下的LSH函数的参数,4 optParameters.parameterT = nPoints; //数据点的个数 optParameters.typeHT = HT_HYBRID_CHAINS; //hashing表构建的类型 // Compute the run-time parameters (timings of different parts of the algorithm). IntT nReps = 10; // # number of repetitiions //重复的数量为10个 RealT lshPrecomp = 0, uhashOver = 0, distComp = 0; //LSH预处理的时间,从Hash表中取得桶的时间 ,计算两点距离的时间 for(IntT i = 0; i < nReps; i++){ //重复nReps次,计算LSH 3个阶段的时间,然后在计算出来的平均时间。 RealT lP, uO, dC; //并且进行了哈希表的构建,哈希函数的计算,等等 determineRTCoefficients(optParameters.parameterR, optParameters.successProbability, optParameters.useUfunctions, optParameters.typeHT, optParameters.dimension, nPoints, dataSet, lP, uO, dC); lshPrecomp += lP; //累计重复nReps次的时间。 因为随机数产生是不同的,所以每次处理的时间也不同 uhashOver += uO; distComp += dC; DPRINTF4("Coefs: lP = %0.9lf\tuO = %0.9lf\tdC = %0.9lf\n", lP, uO, dC); } lshPrecomp /= nReps; //平均处理的时间 uhashOver /= nReps; distComp /= nReps; DPRINTF("Coefs (final): lshPrecomp = %0.9lf\n", lshPrecomp); DPRINTF("Coefs (final): uhashOver = %0.9lf\n", uhashOver); DPRINTF("Coefs (final): distComp = %0.9lf\n", distComp); DPRINTF("Remaining memory: %lld\n", memoryUpperBound); // Try all possible <k>s and choose the one for which the time // estimate of a query is minimal. IntT k; RealT timeLSH, timeUH, timeCycling; //IntT queryIndex = genRandomInt(0, nPoints); //PPointT query = dataSet[queryIndex]; // query points = a random points from the data set. //查询点取为从数据集中取的随机点 IntT bestK = 0; RealT bestTime = 0; for(k = 2; ; k += 2){ //之前进行计算的时候,将k预设为16 DPRINTF("ST. k = %d\n", k); IntT m = computeMForULSH(k, successProbability); //通过k和p计算出m IntT L = m * (m-1) / 2; //DPRINTF("Available memory: %lld\n", getAvailableMemory()); if (L * nPoints > memoryUpperBound / 12){ //??为什么除以12呢?? 12是一个常数,用于对内存的估算 //4×tableSize+8×#buckets+8n<20n break; } timeLSH = m * k * lshPrecomp; //m次,k维预处理时间,之前有计算好平均每次处理的时间 timeUH = L * uhashOver; //L个hash表 //RealT nCollisions = estimateNCollisionsFromDSPoint(nPoints, dimension, dataSet, queryIndex, k, L, R); // Compute the mean number of collisions for the points from the sample query set. RealT nCollisions = 0; //计算平均冲突的点的数量 for(IntT i = 0; i < nSampleQueries; i++){ nCollisions += estimateNDistinctCollisions(nPoints, dimension, dataSet, sampleQueries[i], TRUE, k, m, R); } nCollisions /= nSampleQueries; //每个查询点能查询到近邻点的平均数量 timeCycling = nCollisions * distComp; //查询点查到所有近邻的时间?? DPRINTF3("ST.m=%d L=%d \n", m, L); DPRINTF("ST.Estimated # distinct collisions = %0.6lf\n", (double)nCollisions); DPRINTF("ST.TimeLSH = %0.6lf\n", timeLSH); DPRINTF("ST.TimeUH = %0.6lf\n", timeUH); DPRINTF("ST.TimeCycling = %0.6lf\n", timeCycling); DPRINTF("ST.Sum = %0.6lf\n", timeLSH + timeUH + timeCycling); if (bestK == 0 || (timeLSH + timeUH + timeCycling) < bestTime) { //第一轮循环k=0,此时bestTime是比较大的,所以随着k的增大,得到最优时间 bestK = k; bestTime = timeLSH + timeUH + timeCycling; } ASSERT(k < 100); // otherwise, k reached 100 -- which, from // experience, should never happen for reasonable // data set & available memory amount. } DPRINTF("STO.Optimal k = %d\n", bestK); IntT m = computeMForULSH(bestK, successProbability); //计算L2 norm下的m IntT L = m * (m-1) / 2; timeLSH = m * bestK * lshPrecomp; timeUH = L * uhashOver; // Compute the mean number of collisions for the points from the sample query set. RealT nCollisions = 0; for(IntT i = 0; i < nSampleQueries; i++){ nCollisions += estimateNDistinctCollisions(nPoints, dimension, dataSet, sampleQueries[i], TRUE, k, m, R); } nCollisions /= nSampleQueries; // timeCycling = estimateNCollisionsFromDSPoint(nPoints, dimension, dataSet, queryIndex, bestK, L, R) * distComp; timeCycling = nCollisions * distComp; DPRINTF("STO.TimeLSH = %0.6lf\n", timeLSH); DPRINTF("STO.TimeUH = %0.6lf\n", timeUH); DPRINTF("STO.TimeCycling = %0.6lf\n", timeCycling); DPRINTF("STO.Sum = %0.6lf\n", timeLSH + timeUH + timeCycling); optParameters.parameterK = bestK; optParameters.parameterM = m; optParameters.parameterL = L; return optParameters; }
/* Given the actual data set <dataSet>, estimates the values for algorithm parameters that would give the optimal running time of a query. The set <sampleQueries> is a set with query sample points (R-NN DS's parameters are optimized for query points from the set <sampleQueries>). <sampleQueries> could be a sample of points from the actual query set or from the data set. When computing the estimated number of collisions of a sample query point <q> with the data set points, if there is a point in the data set with the same pointer with <q> (that is when <q> is a data set point), then the corresponding point (<q>) is not considered in the data set (for the purpose of computing the respective #collisions estimation). The return value is the estimate of the optimal parameters. */ RNNParametersT computeOptimalParameters(RealT R, RealT successProbability, IntT nPoints, IntT dimension, PPointT *dataSet, IntT nSampleQueries, PPointT *sampleQueries, MemVarT memoryUpperBound){ ASSERT(nSampleQueries > 0); initializeLSHGlobal(); RNNParametersT optParameters; optParameters.successProbability = successProbability; optParameters.dimension = dimension; optParameters.parameterR = R; #ifdef USE_L1_DISTANCE optParameters.parameterR2 = R; #else optParameters.parameterR2 = SQR(R); #endif optParameters.useUfunctions = TRUE; // TODO: could optimize here: // maybe sometimes, the old way // was better. optParameters.parameterW = PARAMETER_W_DEFAULT; optParameters.parameterT = nPoints; optParameters.typeHT = HT_HYBRID_CHAINS; // Compute the run-time parameters (timings of different parts of the algorithm). IntT nReps = 10; // # number of repetions RealT lshPrecomp = 0, uhashOver = 0, distComp = 0; for(IntT i = 0; i < nReps; i++){ RealT lP, uO, dC; determineRTCoefficients(optParameters.parameterR, optParameters.successProbability, optParameters.useUfunctions, optParameters.typeHT, optParameters.dimension, nPoints, dataSet, lP, uO, dC); lshPrecomp += lP; uhashOver += uO; distComp += dC; DPRINTF4("Coefs: lP = %0.9lf\tuO = %0.9lf\tdC = %0.9lf\n", lP, uO, dC); } lshPrecomp /= nReps; uhashOver /= nReps; distComp /= nReps; DPRINTF("Coefs (final): lshPrecomp = %0.9lf\n", lshPrecomp); DPRINTF("Coefs (final): uhashOver = %0.9lf\n", uhashOver); DPRINTF("Coefs (final): distComp = %0.9lf\n", distComp); DPRINTF("Remaining memory: %lld\n", memoryUpperBound); // Try all possible <k>s and choose the one for which the time // estimate of a query is minimal. IntT k; RealT timeLSH, timeUH, timeCycling; //IntT queryIndex = genRandomInt(0, nPoints); //PPointT query = dataSet[queryIndex]; // query points = a random points from the data set. IntT bestK = 0; RealT bestTime = 0; for(k = 2; ; k += 2){ DPRINTF("ST. k = %d\n", k); IntT m = computeMForULSH(k, successProbability); IntT L = m * (m-1) / 2; //DPRINTF("Available memory: %lld\n", getAvailableMemory()); if (L * nPoints > memoryUpperBound / 12){ break; } timeLSH = m * k * lshPrecomp; timeUH = L * uhashOver; //RealT nCollisions = estimateNCollisionsFromDSPoint(nPoints, dimension, dataSet, queryIndex, k, L, R); // Compute the mean number of collisions for the points from the sample query set. RealT nCollisions = 0; for(IntT i = 0; i < nSampleQueries; i++){ nCollisions += estimateNDistinctCollisions(nPoints, dimension, dataSet, sampleQueries[i], TRUE, k, m, R); } nCollisions /= nSampleQueries; timeCycling = nCollisions * distComp; DPRINTF3("ST.m=%d L=%d \n", m, L); DPRINTF("ST.Estimated # distinct collisions = %0.6lf\n", (double)nCollisions); DPRINTF("ST.TimeLSH = %0.6lf\n", timeLSH); DPRINTF("ST.TimeUH = %0.6lf\n", timeUH); DPRINTF("ST.TimeCycling = %0.6lf\n", timeCycling); DPRINTF("ST.Sum = %0.6lf\n", timeLSH + timeUH + timeCycling); if (bestK == 0 || (timeLSH + timeUH + timeCycling) < bestTime) { bestK = k; bestTime = timeLSH + timeUH + timeCycling; } ASSERT(k < 100); // otherwise, k reached 100 -- which, from // experience, should never happen for reasonable // data set & available memory amount. } DPRINTF("STO.Optimal k = %d\n", bestK); IntT m = computeMForULSH(bestK, successProbability); IntT L = m * (m-1) / 2; timeLSH = m * bestK * lshPrecomp; timeUH = L * uhashOver; // Compute the mean number of collisions for the points from the sample query set. RealT nCollisions = 0; for(IntT i = 0; i < nSampleQueries; i++){ nCollisions += estimateNDistinctCollisions(nPoints, dimension, dataSet, sampleQueries[i], TRUE, k, m, R); } nCollisions /= nSampleQueries; // timeCycling = estimateNCollisionsFromDSPoint(nPoints, dimension, dataSet, queryIndex, bestK, L, R) * distComp; timeCycling = nCollisions * distComp; DPRINTF("STO.TimeLSH = %0.6lf\n", timeLSH); DPRINTF("STO.TimeUH = %0.6lf\n", timeUH); DPRINTF("STO.TimeCycling = %0.6lf\n", timeCycling); DPRINTF("STO.Sum = %0.6lf\n", timeLSH + timeUH + timeCycling); optParameters.parameterK = bestK; optParameters.parameterM = m; optParameters.parameterL = L; return optParameters; }