1 /****************************************************************************
2 * Copyright (c) 1998 Free Software Foundation, Inc. *
4 * Permission is hereby granted, free of charge, to any person obtaining a *
5 * copy of this software and associated documentation files (the *
6 * "Software"), to deal in the Software without restriction, including *
7 * without limitation the rights to use, copy, modify, merge, publish, *
8 * distribute, distribute with modifications, sublicense, and/or sell *
9 * copies of the Software, and to permit persons to whom the Software is *
10 * furnished to do so, subject to the following conditions: *
12 * The above copyright notice and this permission notice shall be included *
13 * in all copies or substantial portions of the Software. *
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS *
16 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF *
17 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. *
18 * IN NO EVENT SHALL THE ABOVE COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, *
19 * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR *
20 * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR *
21 * THE USE OR OTHER DEALINGS IN THE SOFTWARE. *
23 * Except as contained in this notice, the name(s) of the above copyright *
24 * holders shall not be used in advertising or otherwise to promote the *
25 * sale, use or other dealings in this Software without prior written *
27 ****************************************************************************/
29 /****************************************************************************
30 * Author: Zeyd M. Ben-Halim <zmbenhal@netcom.com> 1992,1995 *
31 * and: Eric S. Raymond <esr@snark.thyrsus.com> *
32 ****************************************************************************/
34 /******************************************************************************
37 hashmap.c -- fill in scramble vector based on text hashes
40 void _nc_hash_map(void)
43 This code attempts to recognize pairs of old and new lines in the physical
44 and virtual screens. When a line pair is recognized, the old line index is
45 placed in the oldindex member of the virtual screen line, to be used by the
46 vertical-motion optimizer portion of the update logic (see hardscroll.c).
48 Line pairs are recognized by applying a modified Heckel's algorithm,
49 sped up by hashing. If a line hash is unique in both screens, those
50 lines must be a pair. Then if the lines just before or after the pair
51 are the same or similar, they are a pair too.
53 We don't worry about false pairs produced by hash collisions, on the
54 assumption that such cases are rare and will only make the latter stages
55 of update less efficient, not introduce errors.
59 Use the following production:
62 $(CC) -g -DHASHDEBUG hashmap.c hardscroll.c ../objects/lib_trace.o -o hashmap
65 Eric S. Raymond <esr@snark.thyrsus.com>, May 1996
66 Bug fixes and improvements by Alexander V. Lukyanov <lav@yars.free.net>, 1997
68 *****************************************************************************/
70 #include <curses.priv.h>
71 #include <term.h> /* for back_color_erase */
73 MODULE_ID("$Id: hashmap.c,v 1.34 1999/11/28 00:10:57 tom Exp $")
77 # define _tracef printf
79 # define TR(n, a) if (_nc_tracing & (n)) { _tracef a ; putchar('\n'); }
81 # define screen_lines MAXLINES
83 int oldnums[MAXLINES], reallines[MAXLINES];
84 static chtype oldtext[MAXLINES][TEXTWIDTH], newtext[MAXLINES][TEXTWIDTH];
85 # define OLDNUM(n) oldnums[n]
86 # define OLDTEXT(n) oldtext[n]
87 # define NEWTEXT(m) newtext[m]
90 #else /* !HASHDEBUG */
92 # define OLDNUM(n) _nc_oldnums[n]
93 # define OLDTEXT(n) curscr->_line[n].text
94 # define NEWTEXT(m) newscr->_line[m].text
95 # define TEXTWIDTH (curscr->_maxx+1)
96 # define PENDING(n) (newscr->_line[n].firstchar != _NOCHANGE)
98 #endif /* !HASHDEBUG */
100 #define oldhash (SP->oldhash)
101 #define newhash (SP->newhash)
103 static inline unsigned long hash(chtype *text)
107 unsigned long result = 0;
108 for (i = TEXTWIDTH; i>0; i--)
111 result += (result<<5) + ch;
116 /* approximate update cost */
117 static int update_cost(chtype *from,chtype *to)
122 for (i=TEXTWIDTH; i>0; i--)
123 if (*from++ != *to++)
128 static int update_cost_from_blank(chtype *to)
132 chtype blank = BLANK;
134 if (back_color_erase)
135 blank |= (stdscr->_bkgd & A_COLOR);
137 for (i=TEXTWIDTH; i>0; i--)
145 * Returns true when moving line 'from' to line 'to' seems to be cost
146 * effective. 'blank' indicates whether the line 'to' would become blank.
148 static inline bool cost_effective(const int from, const int to, const bool blank)
155 new_from = OLDNUM(from);
156 if (new_from == _NEWINDEX)
160 * On the left side of >= is the cost before moving;
161 * on the right side -- cost after moving.
163 return (((blank ? update_cost_from_blank(NEWTEXT(to))
164 : update_cost(OLDTEXT(to),NEWTEXT(to)))
165 + update_cost(OLDTEXT(new_from),NEWTEXT(from)))
166 >= ((new_from==from ? update_cost_from_blank(NEWTEXT(from))
167 : update_cost(OLDTEXT(new_from),NEWTEXT(from)))
168 + update_cost(OLDTEXT(from),NEWTEXT(to)))) ? TRUE : FALSE;
174 unsigned long hashval;
175 int oldcount, newcount;
176 int oldindex, newindex;
180 static sym *hashtab=0;
181 static int lines_alloc=0;
183 static void grow_hunks(void)
185 int start, end, shift;
186 int back_limit, forward_limit; /* limits for cells to fill */
187 int back_ref_limit, forward_ref_limit; /* limits for refrences */
192 * This is tricky part. We have unique pairs to use as anchors.
193 * Use these to deduce the presence of spans of identical lines.
199 while (i < screen_lines && OLDNUM(i) == _NEWINDEX)
201 for ( ; i < screen_lines; i=next_hunk)
204 shift = OLDNUM(i) - i;
206 /* get forward limit */
208 while (i < screen_lines && OLDNUM(i) != _NEWINDEX && OLDNUM(i) - i == shift)
211 while (i < screen_lines && OLDNUM(i) == _NEWINDEX)
215 if (i >= screen_lines || OLDNUM(i) >= i)
216 forward_ref_limit = i;
218 forward_ref_limit = OLDNUM(i);
223 back_limit = back_ref_limit + (-shift);
224 while (i >= back_limit)
226 if(newhash[i] == oldhash[i+shift]
227 || cost_effective(i+shift, i, shift<0))
230 TR(TRACE_UPDATE | TRACE_MOVE,
231 ("connected new line %d to old line %d (backward continuation)",
236 TR(TRACE_UPDATE | TRACE_MOVE,
237 ("not connecting new line %d to old line %d (backward continuation)",
247 forward_limit = forward_ref_limit - shift;
248 while (i < forward_limit)
250 if(newhash[i] == oldhash[i+shift]
251 || cost_effective(i+shift, i, shift>0))
254 TR(TRACE_UPDATE | TRACE_MOVE,
255 ("connected new line %d to old line %d (forward continuation)",
260 TR(TRACE_UPDATE | TRACE_MOVE,
261 ("not connecting new line %d to old line %d (forward continuation)",
268 back_ref_limit = back_limit = i;
270 back_ref_limit += shift;
274 void _nc_hash_map(void)
278 int start, shift, size;
281 if (screen_lines > lines_alloc)
285 hashtab = typeMalloc(sym, (screen_lines+1)*2);
290 FreeAndNull(oldhash);
295 lines_alloc = screen_lines;
298 if (oldhash && newhash)
300 /* re-hash only changed lines */
301 for (i = 0; i < screen_lines; i++)
304 newhash[i] = hash(NEWTEXT(i));
311 oldhash = typeCalloc (unsigned long, screen_lines);
313 newhash = typeCalloc (unsigned long, screen_lines);
314 if (!oldhash || !newhash)
315 return; /* malloc failure */
316 for (i = 0; i < screen_lines; i++)
318 newhash[i] = hash(NEWTEXT(i));
319 oldhash[i] = hash(OLDTEXT(i));
324 for (i = 0; i < screen_lines; i++)
326 if(newhash[i] != hash(NEWTEXT(i)))
327 fprintf(stderr,"error in newhash[%d]\n",i);
328 if(oldhash[i] != hash(OLDTEXT(i)))
329 fprintf(stderr,"error in oldhash[%d]\n",i);
334 * Set up and count line-hash values.
336 memset(hashtab, '\0', sizeof(*hashtab)*(screen_lines+1)*2);
337 for (i = 0; i < screen_lines; i++)
339 unsigned long hashval = oldhash[i];
341 for (sp = hashtab; sp->hashval; sp++)
342 if (sp->hashval == hashval)
344 sp->hashval = hashval; /* in case this is a new entry */
348 for (i = 0; i < screen_lines; i++)
350 unsigned long hashval = newhash[i];
352 for (sp = hashtab; sp->hashval; sp++)
353 if (sp->hashval == hashval)
355 sp->hashval = hashval; /* in case this is a new entry */
359 OLDNUM(i) = _NEWINDEX; /* initialize old indices array */
363 * Mark line pairs corresponding to unique hash pairs.
365 * We don't mark lines with offset 0, because it can make fail
366 * extending hunks by cost_effective. Otherwise, it does not
367 * have any side effects.
369 for (sp = hashtab; sp->hashval; sp++)
370 if (sp->oldcount == 1 && sp->newcount == 1
371 && sp->oldindex != sp->newindex)
373 TR(TRACE_UPDATE | TRACE_MOVE,
374 ("new line %d is hash-identical to old line %d (unique)",
375 sp->newindex, sp->oldindex));
376 OLDNUM(sp->newindex) = sp->oldindex;
382 * Eliminate bad or impossible shifts -- this includes removing
383 * those hunks which could not grow because of conflicts, as well
384 * those which are to be moved too far, they are likely to destroy
387 for (i = 0; i < screen_lines; )
389 while (i < screen_lines && OLDNUM(i) == _NEWINDEX)
391 if (i >= screen_lines)
394 shift = OLDNUM(i) - i;
396 while (i < screen_lines && OLDNUM(i) != _NEWINDEX && OLDNUM(i) - i == shift)
399 if (size < 3 || size+min(size/8,2) < abs(shift))
403 OLDNUM(start) = _NEWINDEX;
409 /* After clearing invalid hunks, try grow the rest. */
413 FreeAndNull(hashtab);
418 void _nc_make_oldhash(int i)
421 oldhash[i] = hash(OLDTEXT(i));
424 void _nc_scroll_oldhash(int n, int top, int bot)
432 size = sizeof(*oldhash) * (bot-top+1-abs(n));
435 memmove (oldhash+top, oldhash+top+n, size);
436 for (i = bot; i > bot-n; i--)
437 oldhash[i] = hash(OLDTEXT(i));
441 memmove (oldhash+top-n, oldhash+top, size);
442 for (i = top; i < top-n; i++)
443 oldhash[i] = hash(OLDTEXT(i));
452 static const char *table[] = {
453 "hashmap test-driver",
456 "l get initial line number vector",
457 "n use following letters as text of new lines",
458 "o use following letters as text of old lines",
459 "d dump state of test arrays",
460 "h apply hash mapper and see scroll optimization",
464 for (n = 0; n < sizeof(table)/sizeof(table[0]); n++)
465 fprintf(stderr, "%s\n", table[n]);
469 main(int argc GCC_UNUSED, char *argv[] GCC_UNUSED)
471 char line[BUFSIZ], *st;
474 SP = typeCalloc(SCREEN,1);
475 for (n = 0; n < screen_lines; n++)
478 oldnums[n] = _NEWINDEX;
479 oldtext[n][0] = newtext[n][0] = '.';
482 if (isatty(fileno(stdin)))
486 _nc_tracing = TRACE_MOVE;
490 /* grab a test command */
491 if (fgets(line, sizeof(line), stdin) == (char *)NULL)
496 case '#': /* comment */
497 (void) fputs(line, stderr);
500 case 'l': /* get initial line number vector */
501 for (n = 0; n < screen_lines; n++)
504 oldnums[n] = _NEWINDEX;
507 st = strtok(line, " ");
509 oldnums[n++] = atoi(st);
511 ((st = strtok((char *)NULL, " ")) != 0);
514 case 'n': /* use following letters as text of new lines */
515 for (n = 0; n < screen_lines; n++)
517 for (n = 0; n < screen_lines; n++)
518 if (line[n+1] == '\n')
521 newtext[n][0] = line[n+1];
524 case 'o': /* use following letters as text of old lines */
525 for (n = 0; n < screen_lines; n++)
527 for (n = 0; n < screen_lines; n++)
528 if (line[n+1] == '\n')
531 oldtext[n][0] = line[n+1];
534 case 'd': /* dump state of test arrays */
538 (void) fputs("Old lines: [", stdout);
539 for (n = 0; n < screen_lines; n++)
540 putchar(oldtext[n][0]);
543 (void) fputs("New lines: [", stdout);
544 for (n = 0; n < screen_lines; n++)
545 putchar(newtext[n][0]);
550 case 'h': /* apply hash mapper and see scroll optimization */
552 (void) fputs("Result:\n", stderr);
556 _nc_scroll_optimize();
557 (void) fputs("Done.\n", stderr);
567 #endif /* HASHDEBUG */
569 /* hashmap.c ends here */