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
67 *****************************************************************************/
69 #include <curses.priv.h>
71 MODULE_ID("$Id: hashmap.c,v 1.24 1998/02/11 12:13:55 tom Exp $")
75 int oldnums[MAXLINES], reallines[MAXLINES];
76 static chtype oldtext[MAXLINES][TEXTWIDTH], newtext[MAXLINES][TEXTWIDTH];
77 #define OLDNUM(n) oldnums[n]
78 #define REAL(m) reallines[m]
79 #define OLDTEXT(n) oldtext[n]
80 #define NEWTEXT(m) newtext[m]
82 #define T(x) (void) printf x ; (void) putchar('\n');
85 #define OLDNUM(n) newscr->_line[n].oldindex
86 #define REAL(m) curscr->_line[m].oldindex
87 #define OLDTEXT(n) curscr->_line[n].text
88 #define NEWTEXT(m) newscr->_line[m].text
89 #define TEXTWIDTH (curscr->_maxx+1)
92 #endif /* _NEWINDEX */
93 #endif /* HASHDEBUG */
95 static inline unsigned long hash(chtype *text)
99 unsigned long result = 0;
100 for (i = TEXTWIDTH; i>0; i--)
103 result += (result<<5) + ch + (ch>>16);
108 /* approximate update cost */
109 static int update_cost(chtype *from,chtype *to)
114 for (i=TEXTWIDTH; i>0; i--)
115 if (*from++ != *to++)
120 static int update_cost_from_blank(chtype *to)
125 /* FIXME: ClrBlank should be used */
126 for (i=TEXTWIDTH; i>0; i--)
134 * Returns true when moving line 'from' to line 'to' seems to be cost
135 * effective. 'blank' indicates whether the line 'to' would become blank.
137 static inline bool cost_effective(const int from, const int to, const bool blank)
144 new_from = OLDNUM(from);
145 if (new_from == _NEWINDEX)
149 * On the left side of >= is the cost before moving;
150 * on the right side -- cost after moving.
152 return (((blank ? update_cost_from_blank(NEWTEXT(to))
153 : update_cost(OLDTEXT(to),NEWTEXT(to)))
154 + update_cost(OLDTEXT(new_from),NEWTEXT(from)))
155 >= ((new_from==from ? update_cost_from_blank(NEWTEXT(from))
156 : update_cost(OLDTEXT(new_from),NEWTEXT(from)))
157 + update_cost(OLDTEXT(from),NEWTEXT(to)))) ? TRUE : FALSE;
163 unsigned long hashval;
164 int oldcount, newcount;
165 int oldindex, newindex;
169 static sym *hashtab=0;
170 static int lines_alloc=0;
171 static long *oldhash=0;
172 static long *newhash=0;
174 static void grow_hunks(void)
176 int start, end, shift;
177 int back_limit, forward_limit; /* limits for cells to fill */
178 int back_ref_limit, forward_ref_limit; /* limits for refrences */
183 * This is tricky part. We have unique pairs to use as anchors.
184 * Use these to deduce the presence of spans of identical lines.
190 while (i < screen_lines && OLDNUM(i) == _NEWINDEX)
192 for ( ; i < screen_lines; i=next_hunk)
195 shift = OLDNUM(i) - i;
197 /* get forward limit */
199 while (i < screen_lines && OLDNUM(i) != _NEWINDEX && OLDNUM(i) - i == shift)
202 while (i < screen_lines && OLDNUM(i) == _NEWINDEX)
206 if (i >= screen_lines || OLDNUM(i) >= i)
207 forward_ref_limit = i;
209 forward_ref_limit = OLDNUM(i);
214 back_limit = back_ref_limit + (-shift);
215 while (i >= back_limit)
217 if(newhash[i] == oldhash[i+shift]
218 || cost_effective(i+shift, i, shift<0))
221 TR(TRACE_UPDATE | TRACE_MOVE,
222 ("connected new line %d to old line %d (backward continuation)",
227 TR(TRACE_UPDATE | TRACE_MOVE,
228 ("not connecting new line %d to old line %d (backward continuation)",
238 forward_limit = forward_ref_limit - shift;
239 while (i < forward_limit)
241 if(newhash[i] == oldhash[i+shift]
242 || cost_effective(i+shift, i, shift>0))
245 TR(TRACE_UPDATE | TRACE_MOVE,
246 ("connected new line %d to old line %d (forward continuation)",
251 TR(TRACE_UPDATE | TRACE_MOVE,
252 ("not connecting new line %d to old line %d (forward continuation)",
259 back_ref_limit = back_limit = i;
261 back_ref_limit += shift;
265 void _nc_hash_map(void)
269 int start, shift, size;
272 if (screen_lines > lines_alloc)
276 hashtab = malloc (sizeof(*hashtab)*(screen_lines+1)*2);
280 FreeAndNull(oldhash);
287 oldhash = malloc (sizeof(*oldhash)*screen_lines*2);
291 FreeAndNull(hashtab);
296 lines_alloc = screen_lines;
298 newhash = oldhash + screen_lines; /* two arrays in the same memory block */
301 * Set up and count line-hash values.
303 memset(hashtab, '\0', sizeof(*hashtab)*(screen_lines+1)*2);
304 for (i = 0; i < screen_lines; i++)
306 unsigned long hashval = hash(OLDTEXT(i));
308 for (sp = hashtab; sp->hashval; sp++)
309 if (sp->hashval == hashval)
311 sp->hashval = hashval; /* in case this is a new entry */
312 oldhash[i] = hashval;
316 for (i = 0; i < screen_lines; i++)
318 unsigned long hashval = hash(NEWTEXT(i));
320 for (sp = hashtab; sp->hashval; sp++)
321 if (sp->hashval == hashval)
323 sp->hashval = hashval; /* in case this is a new entry */
324 newhash[i] = hashval;
328 OLDNUM(i) = _NEWINDEX;
332 * Mark line pairs corresponding to unique hash pairs.
334 * We don't mark lines with offset 0, because it can make fail
335 * extending hunks by cost_effective. Otherwise, it does not
336 * have any side effects.
338 for (sp = hashtab; sp->hashval; sp++)
339 if (sp->oldcount == 1 && sp->newcount == 1
340 && sp->oldindex != sp->newindex)
342 TR(TRACE_UPDATE | TRACE_MOVE,
343 ("new line %d is hash-identical to old line %d (unique)",
344 sp->newindex, sp->oldindex));
345 OLDNUM(sp->newindex) = sp->oldindex;
351 * Eliminate bad or impossible shifts -- this includes removing
352 * those hunks which could not grow because of conflicts, as well
353 * those which are to be moved too far, they are likely to destroy
356 for (i = 0; i < screen_lines; )
358 while (i < screen_lines && OLDNUM(i) == _NEWINDEX)
360 if (i >= screen_lines)
363 shift = OLDNUM(i) - i;
365 while (i < screen_lines && OLDNUM(i) != _NEWINDEX && OLDNUM(i) - i == shift)
368 if (size <= abs(shift))
372 OLDNUM(start) = _NEWINDEX;
378 /* After clearing invalid hunks, try grow the rest. */
382 FreeAndNull(hashtab);
383 FreeAndNull(oldhash);
391 main(int argc GCC_UNUSED, char *argv[] GCC_UNUSED)
393 char line[BUFSIZ], *st;
396 for (n = 0; n < screen_lines; n++)
399 oldnums[n] = _NEWINDEX;
400 oldtext[n][0] = newtext[n][0] = '.';
404 _nc_tracing = TRACE_MOVE;
408 /* grab a test command */
409 if (fgets(line, sizeof(line), stdin) == (char *)NULL)
414 case '#': /* comment */
415 (void) fputs(line, stderr);
418 case 'l': /* get initial line number vector */
419 for (n = 0; n < screen_lines; n++)
422 oldnums[n] = _NEWINDEX;
425 st = strtok(line, " ");
427 oldnums[n++] = atoi(st);
429 ((st = strtok((char *)NULL, " ")) != 0);
432 case 'n': /* use following letters as text of new lines */
433 for (n = 0; n < screen_lines; n++)
435 for (n = 0; n < screen_lines; n++)
436 if (line[n+1] == '\n')
439 newtext[n][0] = line[n+1];
442 case 'o': /* use following letters as text of old lines */
443 for (n = 0; n < screen_lines; n++)
445 for (n = 0; n < screen_lines; n++)
446 if (line[n+1] == '\n')
449 oldtext[n][0] = line[n+1];
452 case 'd': /* dump state of test arrays */
456 (void) fputs("Old lines: [", stdout);
457 for (n = 0; n < screen_lines; n++)
458 putchar(oldtext[n][0]);
461 (void) fputs("New lines: [", stdout);
462 for (n = 0; n < screen_lines; n++)
463 putchar(newtext[n][0]);
468 case 'h': /* apply hash mapper and see scroll optimization */
470 (void) fputs("Result:\n", stderr);
474 _nc_scroll_optimize();
475 (void) fputs("Done.\n", stderr);
482 #endif /* HASHDEBUG */
484 /* hashmap.c ends here */