blob: e50b63847b99c14a6b47935d26ded98d32f2d712 [file] [log] [blame]
Amit Daniel Kachhape6a01f52011-07-20 11:45:59 +05301/****************************************************************************
micky3879b9f5e72025-07-08 18:04:53 -04002 * Copyright 2019-2020,2023 Thomas E. Dickey *
3 * Copyright 1998-2015,2016 Free Software Foundation, Inc. *
Amit Daniel Kachhape6a01f52011-07-20 11:45:59 +05304 * *
5 * Permission is hereby granted, free of charge, to any person obtaining a *
6 * copy of this software and associated documentation files (the *
7 * "Software"), to deal in the Software without restriction, including *
8 * without limitation the rights to use, copy, modify, merge, publish, *
9 * distribute, distribute with modifications, sublicense, and/or sell *
10 * copies of the Software, and to permit persons to whom the Software is *
11 * furnished to do so, subject to the following conditions: *
12 * *
13 * The above copyright notice and this permission notice shall be included *
14 * in all copies or substantial portions of the Software. *
15 * *
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS *
17 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF *
18 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. *
19 * IN NO EVENT SHALL THE ABOVE COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, *
20 * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR *
21 * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR *
22 * THE USE OR OTHER DEALINGS IN THE SOFTWARE. *
23 * *
24 * Except as contained in this notice, the name(s) of the above copyright *
25 * holders shall not be used in advertising or otherwise to promote the *
26 * sale, use or other dealings in this Software without prior written *
27 * authorization. *
28 ****************************************************************************/
29
30/****************************************************************************
31 * Author: Zeyd M. Ben-Halim <zmbenhal@netcom.com> 1992,1995 *
32 * and: Eric S. Raymond <esr@snark.thyrsus.com> *
33 ****************************************************************************/
34
35/******************************************************************************
36
37NAME
38 hashmap.c -- fill in scramble vector based on text hashes
39
40SYNOPSIS
41 void _nc_hash_map(void)
42
43DESCRIPTION:
44 This code attempts to recognize pairs of old and new lines in the physical
45and virtual screens. When a line pair is recognized, the old line index is
46placed in the oldindex member of the virtual screen line, to be used by the
47vertical-motion optimizer portion of the update logic (see hardscroll.c).
48
49 Line pairs are recognized by applying a modified Heckel's algorithm,
50sped up by hashing. If a line hash is unique in both screens, those
51lines must be a pair. Then if the lines just before or after the pair
52are the same or similar, they are a pair too.
53
54 We don't worry about false pairs produced by hash collisions, on the
55assumption that such cases are rare and will only make the latter stages
56of update less efficient, not introduce errors.
57
58HOW TO TEST THIS:
59
60Use the following production:
61
62hashmap: hashmap.c
63 $(CC) -g -DHASHDEBUG hashmap.c hardscroll.c ../objects/lib_trace.o -o hashmap
64
65AUTHOR
66 Eric S. Raymond <esr@snark.thyrsus.com>, May 1996
67 Bug fixes and improvements by Alexander V. Lukyanov <lav@yars.free.net>, 1997
68
69*****************************************************************************/
70
71#include <curses.priv.h>
Amit Daniel Kachhape6a01f52011-07-20 11:45:59 +053072
Steve Kondikae271bc2015-11-15 02:50:53 +010073#ifndef CUR
74#define CUR SP_TERMTYPE
75#endif
76
micky3879b9f5e72025-07-08 18:04:53 -040077MODULE_ID("$Id: hashmap.c,v 1.71 2023/09/16 16:28:53 tom Exp $")
Amit Daniel Kachhape6a01f52011-07-20 11:45:59 +053078
79#ifdef HASHDEBUG
80
81# define _tracef printf
82# undef TR
Steve Kondikae271bc2015-11-15 02:50:53 +010083# ifdef TRACE
Amit Daniel Kachhape6a01f52011-07-20 11:45:59 +053084# define TR(n, a) if (_nc_tracing & (n)) { _tracef a ; putchar('\n'); }
Steve Kondikae271bc2015-11-15 02:50:53 +010085# else
86# define TR(n, a) { _tracef a ; putchar('\n'); }
87# endif
Amit Daniel Kachhape6a01f52011-07-20 11:45:59 +053088# undef screen_lines
Steve Kondikae271bc2015-11-15 02:50:53 +010089# define screen_lines(sp) MAXLINES
90# define TEXTWIDTH(sp) 1
micky3879b9f5e72025-07-08 18:04:53 -040091static int oldnums[MAXLINES], reallines[MAXLINES];
Steve Kondikae271bc2015-11-15 02:50:53 +010092static NCURSES_CH_T oldtext[MAXLINES][TEXTWIDTH(sp)];
93static NCURSES_CH_T newtext[MAXLINES][TEXTWIDTH(sp)];
94# define OLDNUM(sp,n) oldnums[n]
95# define OLDTEXT(sp,n) oldtext[n]
96# define NEWTEXT(sp,m) newtext[m]
97# define PENDING(sp,n) 1
Amit Daniel Kachhape6a01f52011-07-20 11:45:59 +053098
99#else /* !HASHDEBUG */
100
Steve Kondikae271bc2015-11-15 02:50:53 +0100101# define OLDNUM(sp,n) (sp)->_oldnum_list[n]
102# define OLDTEXT(sp,n) CurScreen(sp)->_line[n].text
103# define NEWTEXT(sp,m) NewScreen(sp)->_line[m].text
104# define TEXTWIDTH(sp) (CurScreen(sp)->_maxx + 1)
105# define PENDING(sp,n) (NewScreen(sp)->_line[n].firstchar != _NOCHANGE)
Amit Daniel Kachhape6a01f52011-07-20 11:45:59 +0530106
107#endif /* !HASHDEBUG */
108
Steve Kondikae271bc2015-11-15 02:50:53 +0100109#define oldhash(sp) ((sp)->oldhash)
110#define newhash(sp) ((sp)->newhash)
111#define hashtab(sp) ((sp)->hashtab)
112#define lines_alloc(sp) ((sp)->hashtab_len)
Amit Daniel Kachhape6a01f52011-07-20 11:45:59 +0530113
114#if USE_WIDEC_SUPPORT
115#define HASH_VAL(ch) (ch.chars[0])
116#else
117#define HASH_VAL(ch) (ch)
118#endif
119
120static const NCURSES_CH_T blankchar = NewChar(BLANK_TEXT);
121
122static NCURSES_INLINE unsigned long
micky3879b9f5e72025-07-08 18:04:53 -0400123hash(SCREEN *sp, NCURSES_CH_T *text)
Amit Daniel Kachhape6a01f52011-07-20 11:45:59 +0530124{
125 int i;
126 NCURSES_CH_T ch;
127 unsigned long result = 0;
Steve Kondikae271bc2015-11-15 02:50:53 +0100128 (void) sp;
129
130 for (i = TEXTWIDTH(sp); i > 0; i--) {
Amit Daniel Kachhape6a01f52011-07-20 11:45:59 +0530131 ch = *text++;
Steve Kondikae271bc2015-11-15 02:50:53 +0100132 result += (result << 5) + (unsigned long) HASH_VAL(ch);
Amit Daniel Kachhape6a01f52011-07-20 11:45:59 +0530133 }
134 return result;
135}
136
137/* approximate update cost */
138static int
micky3879b9f5e72025-07-08 18:04:53 -0400139update_cost(SCREEN *sp, NCURSES_CH_T *from, NCURSES_CH_T *to)
Amit Daniel Kachhape6a01f52011-07-20 11:45:59 +0530140{
141 int cost = 0;
142 int i;
Steve Kondikae271bc2015-11-15 02:50:53 +0100143 (void) sp;
Amit Daniel Kachhape6a01f52011-07-20 11:45:59 +0530144
Steve Kondikae271bc2015-11-15 02:50:53 +0100145 for (i = TEXTWIDTH(sp); i > 0; i--, from++, to++)
Amit Daniel Kachhape6a01f52011-07-20 11:45:59 +0530146 if (!(CharEq(*from, *to)))
147 cost++;
148
149 return cost;
150}
151
152static int
micky3879b9f5e72025-07-08 18:04:53 -0400153update_cost_from_blank(SCREEN *sp, NCURSES_CH_T *to)
Amit Daniel Kachhape6a01f52011-07-20 11:45:59 +0530154{
155 int cost = 0;
156 int i;
157 NCURSES_CH_T blank = blankchar;
Steve Kondikae271bc2015-11-15 02:50:53 +0100158 (void) sp;
Amit Daniel Kachhape6a01f52011-07-20 11:45:59 +0530159
160 if (back_color_erase)
161 SetPair(blank, GetPair(stdscr->_nc_bkgd));
162
Steve Kondikae271bc2015-11-15 02:50:53 +0100163 for (i = TEXTWIDTH(sp); i > 0; i--, to++)
Amit Daniel Kachhape6a01f52011-07-20 11:45:59 +0530164 if (!(CharEq(blank, *to)))
165 cost++;
166
167 return cost;
168}
169
170/*
171 * Returns true when moving line 'from' to line 'to' seems to be cost
172 * effective. 'blank' indicates whether the line 'to' would become blank.
173 */
174static NCURSES_INLINE bool
Steve Kondikae271bc2015-11-15 02:50:53 +0100175cost_effective(SCREEN *sp, const int from, const int to, const int blank)
Amit Daniel Kachhape6a01f52011-07-20 11:45:59 +0530176{
177 int new_from;
178
179 if (from == to)
180 return FALSE;
181
Steve Kondikae271bc2015-11-15 02:50:53 +0100182 new_from = OLDNUM(sp, from);
Amit Daniel Kachhape6a01f52011-07-20 11:45:59 +0530183 if (new_from == _NEWINDEX)
184 new_from = from;
185
186 /*
187 * On the left side of >= is the cost before moving;
188 * on the right side -- cost after moving.
189 */
Steve Kondikae271bc2015-11-15 02:50:53 +0100190 return (((blank ? update_cost_from_blank(sp, NEWTEXT(sp, to))
191 : update_cost(sp, OLDTEXT(sp, to), NEWTEXT(sp, to)))
192 + update_cost(sp, OLDTEXT(sp, new_from), NEWTEXT(sp, from)))
193 >= ((new_from == from ? update_cost_from_blank(sp, NEWTEXT(sp, from))
194 : update_cost(sp, OLDTEXT(sp, new_from), NEWTEXT(sp, from)))
195 + update_cost(sp, OLDTEXT(sp, from), NEWTEXT(sp, to))))
196 ? TRUE : FALSE;
Amit Daniel Kachhape6a01f52011-07-20 11:45:59 +0530197}
198
199static void
Steve Kondikae271bc2015-11-15 02:50:53 +0100200grow_hunks(SCREEN *sp)
Amit Daniel Kachhape6a01f52011-07-20 11:45:59 +0530201{
micky3879b9f5e72025-07-08 18:04:53 -0400202 int back_limit; /* limits for cells to fill */
203 int back_ref_limit; /* limit for references */
Amit Daniel Kachhape6a01f52011-07-20 11:45:59 +0530204 int i;
205 int next_hunk;
206
207 /*
208 * This is tricky part. We have unique pairs to use as anchors.
209 * Use these to deduce the presence of spans of identical lines.
210 */
211 back_limit = 0;
212 back_ref_limit = 0;
213
214 i = 0;
Steve Kondikae271bc2015-11-15 02:50:53 +0100215 while (i < screen_lines(sp) && OLDNUM(sp, i) == _NEWINDEX)
Amit Daniel Kachhape6a01f52011-07-20 11:45:59 +0530216 i++;
Steve Kondikae271bc2015-11-15 02:50:53 +0100217 for (; i < screen_lines(sp); i = next_hunk) {
micky3879b9f5e72025-07-08 18:04:53 -0400218 int forward_limit;
219 int forward_ref_limit;
220 int end;
221 int start = i;
222 int shift = OLDNUM(sp, i) - i;
Amit Daniel Kachhape6a01f52011-07-20 11:45:59 +0530223
224 /* get forward limit */
225 i = start + 1;
Steve Kondikae271bc2015-11-15 02:50:53 +0100226 while (i < screen_lines(sp)
227 && OLDNUM(sp, i) != _NEWINDEX
228 && OLDNUM(sp, i) - i == shift)
Amit Daniel Kachhape6a01f52011-07-20 11:45:59 +0530229 i++;
230 end = i;
Steve Kondikae271bc2015-11-15 02:50:53 +0100231 while (i < screen_lines(sp) && OLDNUM(sp, i) == _NEWINDEX)
Amit Daniel Kachhape6a01f52011-07-20 11:45:59 +0530232 i++;
233 next_hunk = i;
234 forward_limit = i;
Steve Kondikae271bc2015-11-15 02:50:53 +0100235 if (i >= screen_lines(sp) || OLDNUM(sp, i) >= i)
Amit Daniel Kachhape6a01f52011-07-20 11:45:59 +0530236 forward_ref_limit = i;
237 else
Steve Kondikae271bc2015-11-15 02:50:53 +0100238 forward_ref_limit = OLDNUM(sp, i);
Amit Daniel Kachhape6a01f52011-07-20 11:45:59 +0530239
240 i = start - 1;
241 /* grow back */
242 if (shift < 0)
243 back_limit = back_ref_limit + (-shift);
244 while (i >= back_limit) {
Steve Kondikae271bc2015-11-15 02:50:53 +0100245 if (newhash(sp)[i] == oldhash(sp)[i + shift]
246 || cost_effective(sp, i + shift, i, shift < 0)) {
247 OLDNUM(sp, i) = i + shift;
Amit Daniel Kachhape6a01f52011-07-20 11:45:59 +0530248 TR(TRACE_UPDATE | TRACE_MOVE,
249 ("connected new line %d to old line %d (backward continuation)",
250 i, i + shift));
251 } else {
252 TR(TRACE_UPDATE | TRACE_MOVE,
253 ("not connecting new line %d to old line %d (backward continuation)",
254 i, i + shift));
255 break;
256 }
257 i--;
258 }
259
260 i = end;
261 /* grow forward */
262 if (shift > 0)
263 forward_limit = forward_ref_limit - shift;
264 while (i < forward_limit) {
Steve Kondikae271bc2015-11-15 02:50:53 +0100265 if (newhash(sp)[i] == oldhash(sp)[i + shift]
266 || cost_effective(sp, i + shift, i, shift > 0)) {
267 OLDNUM(sp, i) = i + shift;
Amit Daniel Kachhape6a01f52011-07-20 11:45:59 +0530268 TR(TRACE_UPDATE | TRACE_MOVE,
269 ("connected new line %d to old line %d (forward continuation)",
270 i, i + shift));
271 } else {
272 TR(TRACE_UPDATE | TRACE_MOVE,
273 ("not connecting new line %d to old line %d (forward continuation)",
274 i, i + shift));
275 break;
276 }
277 i++;
278 }
279
280 back_ref_limit = back_limit = i;
281 if (shift > 0)
282 back_ref_limit += shift;
283 }
284}
285
286NCURSES_EXPORT(void)
Steve Kondikae271bc2015-11-15 02:50:53 +0100287NCURSES_SP_NAME(_nc_hash_map) (NCURSES_SP_DCL0)
Amit Daniel Kachhape6a01f52011-07-20 11:45:59 +0530288{
Steve Kondikae271bc2015-11-15 02:50:53 +0100289 HASHMAP *hsp;
Amit Daniel Kachhape6a01f52011-07-20 11:45:59 +0530290 register int i;
Amit Daniel Kachhape6a01f52011-07-20 11:45:59 +0530291
Steve Kondikae271bc2015-11-15 02:50:53 +0100292 if (screen_lines(SP_PARM) > lines_alloc(SP_PARM)) {
293 if (hashtab(SP_PARM))
294 free(hashtab(SP_PARM));
295 hashtab(SP_PARM) = typeMalloc(HASHMAP,
296 ((size_t) screen_lines(SP_PARM) + 1) * 2);
297 if (!hashtab(SP_PARM)) {
298 if (oldhash(SP_PARM)) {
299 FreeAndNull(oldhash(SP_PARM));
Amit Daniel Kachhape6a01f52011-07-20 11:45:59 +0530300 }
Steve Kondikae271bc2015-11-15 02:50:53 +0100301 lines_alloc(SP_PARM) = 0;
Amit Daniel Kachhape6a01f52011-07-20 11:45:59 +0530302 return;
303 }
Steve Kondikae271bc2015-11-15 02:50:53 +0100304 lines_alloc(SP_PARM) = screen_lines(SP_PARM);
Amit Daniel Kachhape6a01f52011-07-20 11:45:59 +0530305 }
306
Steve Kondikae271bc2015-11-15 02:50:53 +0100307 if (oldhash(SP_PARM) && newhash(SP_PARM)) {
Amit Daniel Kachhape6a01f52011-07-20 11:45:59 +0530308 /* re-hash only changed lines */
Steve Kondikae271bc2015-11-15 02:50:53 +0100309 for (i = 0; i < screen_lines(SP_PARM); i++) {
310 if (PENDING(SP_PARM, i))
311 newhash(SP_PARM)[i] = hash(SP_PARM, NEWTEXT(SP_PARM, i));
Amit Daniel Kachhape6a01f52011-07-20 11:45:59 +0530312 }
313 } else {
314 /* re-hash all */
Steve Kondikae271bc2015-11-15 02:50:53 +0100315 if (oldhash(SP_PARM) == 0)
316 oldhash(SP_PARM) = typeCalloc(unsigned long,
317 (size_t) screen_lines(SP_PARM));
318 if (newhash(SP_PARM) == 0)
319 newhash(SP_PARM) = typeCalloc(unsigned long,
320 (size_t) screen_lines(SP_PARM));
micky3879b9f5e72025-07-08 18:04:53 -0400321 if (!oldhash(SP_PARM) || !newhash(SP_PARM)) {
322 FreeAndNull(oldhash(SP_PARM));
323 FreeAndNull(newhash(SP_PARM));
Amit Daniel Kachhape6a01f52011-07-20 11:45:59 +0530324 return; /* malloc failure */
micky3879b9f5e72025-07-08 18:04:53 -0400325 }
Steve Kondikae271bc2015-11-15 02:50:53 +0100326 for (i = 0; i < screen_lines(SP_PARM); i++) {
327 newhash(SP_PARM)[i] = hash(SP_PARM, NEWTEXT(SP_PARM, i));
328 oldhash(SP_PARM)[i] = hash(SP_PARM, OLDTEXT(SP_PARM, i));
Amit Daniel Kachhape6a01f52011-07-20 11:45:59 +0530329 }
330 }
331
332#ifdef HASH_VERIFY
Steve Kondikae271bc2015-11-15 02:50:53 +0100333 for (i = 0; i < screen_lines(SP_PARM); i++) {
334 if (newhash(SP_PARM)[i] != hash(SP_PARM, NEWTEXT(SP_PARM, i)))
Amit Daniel Kachhape6a01f52011-07-20 11:45:59 +0530335 fprintf(stderr, "error in newhash[%d]\n", i);
Steve Kondikae271bc2015-11-15 02:50:53 +0100336 if (oldhash(SP_PARM)[i] != hash(SP_PARM, OLDTEXT(SP_PARM, i)))
Amit Daniel Kachhape6a01f52011-07-20 11:45:59 +0530337 fprintf(stderr, "error in oldhash[%d]\n", i);
338 }
339#endif
340
341 /*
342 * Set up and count line-hash values.
343 */
Steve Kondikae271bc2015-11-15 02:50:53 +0100344 memset(hashtab(SP_PARM), '\0',
345 sizeof(*(hashtab(SP_PARM)))
346 * ((size_t) screen_lines(SP_PARM) + 1) * 2);
347 for (i = 0; i < screen_lines(SP_PARM); i++) {
348 unsigned long hashval = oldhash(SP_PARM)[i];
Amit Daniel Kachhape6a01f52011-07-20 11:45:59 +0530349
Steve Kondikae271bc2015-11-15 02:50:53 +0100350 for (hsp = hashtab(SP_PARM); hsp->hashval; hsp++)
351 if (hsp->hashval == hashval)
Amit Daniel Kachhape6a01f52011-07-20 11:45:59 +0530352 break;
Steve Kondikae271bc2015-11-15 02:50:53 +0100353 hsp->hashval = hashval; /* in case this is a new entry */
354 hsp->oldcount++;
355 hsp->oldindex = i;
Amit Daniel Kachhape6a01f52011-07-20 11:45:59 +0530356 }
Steve Kondikae271bc2015-11-15 02:50:53 +0100357 for (i = 0; i < screen_lines(SP_PARM); i++) {
358 unsigned long hashval = newhash(SP_PARM)[i];
Amit Daniel Kachhape6a01f52011-07-20 11:45:59 +0530359
Steve Kondikae271bc2015-11-15 02:50:53 +0100360 for (hsp = hashtab(SP_PARM); hsp->hashval; hsp++)
361 if (hsp->hashval == hashval)
Amit Daniel Kachhape6a01f52011-07-20 11:45:59 +0530362 break;
Steve Kondikae271bc2015-11-15 02:50:53 +0100363 hsp->hashval = hashval; /* in case this is a new entry */
364 hsp->newcount++;
365 hsp->newindex = i;
Amit Daniel Kachhape6a01f52011-07-20 11:45:59 +0530366
Steve Kondikae271bc2015-11-15 02:50:53 +0100367 OLDNUM(SP_PARM, i) = _NEWINDEX; /* initialize old indices array */
Amit Daniel Kachhape6a01f52011-07-20 11:45:59 +0530368 }
369
370 /*
371 * Mark line pairs corresponding to unique hash pairs.
372 *
373 * We don't mark lines with offset 0, because it can make fail
374 * extending hunks by cost_effective. Otherwise, it does not
375 * have any side effects.
376 */
Steve Kondikae271bc2015-11-15 02:50:53 +0100377 for (hsp = hashtab(SP_PARM); hsp->hashval; hsp++)
378 if (hsp->oldcount == 1 && hsp->newcount == 1
379 && hsp->oldindex != hsp->newindex) {
Amit Daniel Kachhape6a01f52011-07-20 11:45:59 +0530380 TR(TRACE_UPDATE | TRACE_MOVE,
381 ("new line %d is hash-identical to old line %d (unique)",
Steve Kondikae271bc2015-11-15 02:50:53 +0100382 hsp->newindex, hsp->oldindex));
383 OLDNUM(SP_PARM, hsp->newindex) = hsp->oldindex;
Amit Daniel Kachhape6a01f52011-07-20 11:45:59 +0530384 }
385
Steve Kondikae271bc2015-11-15 02:50:53 +0100386 grow_hunks(SP_PARM);
Amit Daniel Kachhape6a01f52011-07-20 11:45:59 +0530387
388 /*
389 * Eliminate bad or impossible shifts -- this includes removing
390 * those hunks which could not grow because of conflicts, as well
391 * those which are to be moved too far, they are likely to destroy
392 * more than carry.
393 */
Steve Kondikae271bc2015-11-15 02:50:53 +0100394 for (i = 0; i < screen_lines(SP_PARM);) {
micky3879b9f5e72025-07-08 18:04:53 -0400395 int start, shift, size;
396
Steve Kondikae271bc2015-11-15 02:50:53 +0100397 while (i < screen_lines(SP_PARM) && OLDNUM(SP_PARM, i) == _NEWINDEX)
Amit Daniel Kachhape6a01f52011-07-20 11:45:59 +0530398 i++;
Steve Kondikae271bc2015-11-15 02:50:53 +0100399 if (i >= screen_lines(SP_PARM))
Amit Daniel Kachhape6a01f52011-07-20 11:45:59 +0530400 break;
401 start = i;
Steve Kondikae271bc2015-11-15 02:50:53 +0100402 shift = OLDNUM(SP_PARM, i) - i;
Amit Daniel Kachhape6a01f52011-07-20 11:45:59 +0530403 i++;
Steve Kondikae271bc2015-11-15 02:50:53 +0100404 while (i < screen_lines(SP_PARM)
405 && OLDNUM(SP_PARM, i) != _NEWINDEX
406 && OLDNUM(SP_PARM, i) - i == shift)
Amit Daniel Kachhape6a01f52011-07-20 11:45:59 +0530407 i++;
408 size = i - start;
micky3879b9f5e72025-07-08 18:04:53 -0400409 if (size < 3 || size + Min(size / 8, 2) < abs(shift)) {
Amit Daniel Kachhape6a01f52011-07-20 11:45:59 +0530410 while (start < i) {
Steve Kondikae271bc2015-11-15 02:50:53 +0100411 OLDNUM(SP_PARM, start) = _NEWINDEX;
Amit Daniel Kachhape6a01f52011-07-20 11:45:59 +0530412 start++;
413 }
414 }
415 }
416
417 /* After clearing invalid hunks, try grow the rest. */
Steve Kondikae271bc2015-11-15 02:50:53 +0100418 grow_hunks(SP_PARM);
Amit Daniel Kachhape6a01f52011-07-20 11:45:59 +0530419}
420
Steve Kondikae271bc2015-11-15 02:50:53 +0100421#if NCURSES_SP_FUNCS
422NCURSES_EXPORT(void)
423_nc_hash_map(void)
424{
425 NCURSES_SP_NAME(_nc_hash_map) (CURRENT_SCREEN);
426}
427#endif
428
429NCURSES_EXPORT(void)
430NCURSES_SP_NAME(_nc_make_oldhash) (NCURSES_SP_DCLx int i)
431{
432 if (oldhash(SP_PARM))
433 oldhash(SP_PARM)[i] = hash(SP_PARM, OLDTEXT(SP_PARM, i));
434}
435
436#if NCURSES_SP_FUNCS
Amit Daniel Kachhape6a01f52011-07-20 11:45:59 +0530437NCURSES_EXPORT(void)
438_nc_make_oldhash(int i)
439{
Steve Kondikae271bc2015-11-15 02:50:53 +0100440 NCURSES_SP_NAME(_nc_make_oldhash) (CURRENT_SCREEN, i);
Amit Daniel Kachhape6a01f52011-07-20 11:45:59 +0530441}
Steve Kondikae271bc2015-11-15 02:50:53 +0100442#endif
Amit Daniel Kachhape6a01f52011-07-20 11:45:59 +0530443
444NCURSES_EXPORT(void)
Steve Kondikae271bc2015-11-15 02:50:53 +0100445NCURSES_SP_NAME(_nc_scroll_oldhash) (NCURSES_SP_DCLx int n, int top, int bot)
Amit Daniel Kachhape6a01f52011-07-20 11:45:59 +0530446{
447 size_t size;
448 int i;
449
Steve Kondikae271bc2015-11-15 02:50:53 +0100450 if (!oldhash(SP_PARM))
Amit Daniel Kachhape6a01f52011-07-20 11:45:59 +0530451 return;
452
Steve Kondikae271bc2015-11-15 02:50:53 +0100453 size = sizeof(*(oldhash(SP_PARM))) * (size_t) (bot - top + 1 - abs(n));
Amit Daniel Kachhape6a01f52011-07-20 11:45:59 +0530454 if (n > 0) {
Steve Kondikae271bc2015-11-15 02:50:53 +0100455 memmove(oldhash(SP_PARM) + top, oldhash(SP_PARM) + top + n, size);
Amit Daniel Kachhape6a01f52011-07-20 11:45:59 +0530456 for (i = bot; i > bot - n; i--)
Steve Kondikae271bc2015-11-15 02:50:53 +0100457 oldhash(SP_PARM)[i] = hash(SP_PARM, OLDTEXT(SP_PARM, i));
Amit Daniel Kachhape6a01f52011-07-20 11:45:59 +0530458 } else {
Steve Kondikae271bc2015-11-15 02:50:53 +0100459 memmove(oldhash(SP_PARM) + top - n, oldhash(SP_PARM) + top, size);
Amit Daniel Kachhape6a01f52011-07-20 11:45:59 +0530460 for (i = top; i < top - n; i++)
Steve Kondikae271bc2015-11-15 02:50:53 +0100461 oldhash(SP_PARM)[i] = hash(SP_PARM, OLDTEXT(SP_PARM, i));
Amit Daniel Kachhape6a01f52011-07-20 11:45:59 +0530462 }
463}
464
Steve Kondikae271bc2015-11-15 02:50:53 +0100465#if NCURSES_SP_FUNCS
466NCURSES_EXPORT(void)
467_nc_scroll_oldhash(int n, int top, int bot)
468{
469 NCURSES_SP_NAME(_nc_scroll_oldhash) (CURRENT_SCREEN, n, top, bot);
470}
471#endif
472
Amit Daniel Kachhape6a01f52011-07-20 11:45:59 +0530473#ifdef HASHDEBUG
474static void
475usage(void)
476{
477 static const char *table[] =
478 {
479 "hashmap test-driver",
480 "",
481 "# comment",
482 "l get initial line number vector",
483 "n use following letters as text of new lines",
484 "o use following letters as text of old lines",
485 "d dump state of test arrays",
486 "h apply hash mapper and see scroll optimization",
487 "? this message"
488 };
489 size_t n;
490 for (n = 0; n < sizeof(table) / sizeof(table[0]); n++)
491 fprintf(stderr, "%s\n", table[n]);
492}
493
494int
495main(int argc GCC_UNUSED, char *argv[]GCC_UNUSED)
496{
497 char line[BUFSIZ], *st;
498 int n;
499
500 if (setupterm(NULL, fileno(stdout), (int *) 0) == ERR)
501 return EXIT_FAILURE;
502 (void) _nc_alloc_screen();
503
Steve Kondikae271bc2015-11-15 02:50:53 +0100504 for (n = 0; n < screen_lines(sp); n++) {
Amit Daniel Kachhape6a01f52011-07-20 11:45:59 +0530505 reallines[n] = n;
506 oldnums[n] = _NEWINDEX;
507 CharOf(oldtext[n][0]) = CharOf(newtext[n][0]) = '.';
508 }
509
Steve Kondikae271bc2015-11-15 02:50:53 +0100510 if (NC_ISATTY(fileno(stdin)))
Amit Daniel Kachhape6a01f52011-07-20 11:45:59 +0530511 usage();
512
513#ifdef TRACE
514 _nc_tracing = TRACE_MOVE;
515#endif
516 for (;;) {
517 /* grab a test command */
518 if (fgets(line, sizeof(line), stdin) == (char *) NULL)
519 break;
520
521 switch (line[0]) {
522 case '#': /* comment */
523 (void) fputs(line, stderr);
524 break;
525
526 case 'l': /* get initial line number vector */
Steve Kondikae271bc2015-11-15 02:50:53 +0100527 for (n = 0; n < screen_lines(sp); n++) {
Amit Daniel Kachhape6a01f52011-07-20 11:45:59 +0530528 reallines[n] = n;
529 oldnums[n] = _NEWINDEX;
530 }
531 n = 0;
532 st = strtok(line, " ");
533 do {
534 oldnums[n++] = atoi(st);
535 } while
536 ((st = strtok((char *) NULL, " ")) != 0);
537 break;
538
539 case 'n': /* use following letters as text of new lines */
Steve Kondikae271bc2015-11-15 02:50:53 +0100540 for (n = 0; n < screen_lines(sp); n++)
Amit Daniel Kachhape6a01f52011-07-20 11:45:59 +0530541 CharOf(newtext[n][0]) = '.';
Steve Kondikae271bc2015-11-15 02:50:53 +0100542 for (n = 0; n < screen_lines(sp); n++)
Amit Daniel Kachhape6a01f52011-07-20 11:45:59 +0530543 if (line[n + 1] == '\n')
544 break;
545 else
546 CharOf(newtext[n][0]) = line[n + 1];
547 break;
548
549 case 'o': /* use following letters as text of old lines */
Steve Kondikae271bc2015-11-15 02:50:53 +0100550 for (n = 0; n < screen_lines(sp); n++)
Amit Daniel Kachhape6a01f52011-07-20 11:45:59 +0530551 CharOf(oldtext[n][0]) = '.';
Steve Kondikae271bc2015-11-15 02:50:53 +0100552 for (n = 0; n < screen_lines(sp); n++)
Amit Daniel Kachhape6a01f52011-07-20 11:45:59 +0530553 if (line[n + 1] == '\n')
554 break;
555 else
556 CharOf(oldtext[n][0]) = line[n + 1];
557 break;
558
559 case 'd': /* dump state of test arrays */
560#ifdef TRACE
561 _nc_linedump();
562#endif
563 (void) fputs("Old lines: [", stdout);
Steve Kondikae271bc2015-11-15 02:50:53 +0100564 for (n = 0; n < screen_lines(sp); n++)
Amit Daniel Kachhape6a01f52011-07-20 11:45:59 +0530565 putchar(CharOf(oldtext[n][0]));
566 putchar(']');
567 putchar('\n');
568 (void) fputs("New lines: [", stdout);
Steve Kondikae271bc2015-11-15 02:50:53 +0100569 for (n = 0; n < screen_lines(sp); n++)
Amit Daniel Kachhape6a01f52011-07-20 11:45:59 +0530570 putchar(CharOf(newtext[n][0]));
571 putchar(']');
572 putchar('\n');
573 break;
574
575 case 'h': /* apply hash mapper and see scroll optimization */
576 _nc_hash_map();
577 (void) fputs("Result:\n", stderr);
578#ifdef TRACE
579 _nc_linedump();
580#endif
581 _nc_scroll_optimize();
582 (void) fputs("Done.\n", stderr);
583 break;
584 default:
585 case '?':
586 usage();
587 break;
588 }
589 }
micky3879b9f5e72025-07-08 18:04:53 -0400590 exit_curses(EXIT_SUCCESS);
Amit Daniel Kachhape6a01f52011-07-20 11:45:59 +0530591}
592
593#endif /* HASHDEBUG */
594
595/* hashmap.c ends here */