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 ****************************************************************************/
35 /******************************************************************************
38 hardscroll.c -- hardware-scrolling optimization for ncurses
41 void _nc_scroll_optimize(void)
46 This algorithm for computes optimum hardware scrolling to transform an
47 old screen (curscr) into a new screen (newscr) via vertical line moves.
49 Because the screen has a `grain' (there are insert/delete/scroll line
50 operations but no insert/delete/scroll column operations), it is efficient
51 break the update algorithm into two pieces: a first stage that does only line
52 moves, optimizing the end product of user-invoked insertions, deletions, and
53 scrolls; and a second phase (corresponding to the present doupdate code in
54 ncurses) that does only line transformations.
56 The common case we want hardware scrolling for is to handle line insertions
57 and deletions in screen-oriented text-editors. This two-stage approach will
58 accomplish that at a low computation and code-size cost.
62 Now, to a discussion of the line-move computation.
64 For expository purposes, consider the screen lines to be represented by
65 integers 0..23 (with the understanding that the value of 23 may vary).
66 Let a new line introduced by insertion, scrolling, or at the bottom of
67 the screen following a line delete be given the index -1.
69 Assume that the real screen starts with lines 0..23. Now, we have
70 the following possible line-oriented operations on the screen:
72 Insertion: inserts a line at a given screen row, forcing all lines below
73 to scroll forward. The last screen line is lost. For example, an insertion
74 at line 5 would produce: 0..4 -1 5..23.
76 Deletion: deletes a line at a given screen row, forcing all lines below
77 to scroll forward. The last screen line is made new. For example, a deletion
78 at line 7 would produce: 0..6 8..23 -1.
80 Scroll up: move a range of lines up 1. The bottom line of the range
81 becomes new. For example, scrolling up the region from 9 to 14 will
82 produce 0..8 10..14 -1 15..23.
84 Scroll down: move a range of lines down 1. The top line of the range
85 becomes new. For example, scrolling down the region from 12 to 16 will produce
86 0..11 -1 12..15 17..23.
88 Now, an obvious property of all these operations is that they preserve the
89 order of old lines, though not their position in the sequence.
91 The key trick of this algorithm is that the original line indices described
92 above are actually maintained as _line[].oldindex fields in the window
93 structure, and stick to each line through scroll and insert/delete operations.
95 Thus, it is possible at update time to look at the oldnum fields and compute
96 an optimal set of il/dl/scroll operations that will take the real screen
97 lines to the virtual screen lines. Once these vertical moves have been done,
98 we can hand off to the second stage of the update algorithm, which does line
101 Note that the move computation does not need to have the full generality
102 of a diff algorithm (which it superficially resembles) because lines cannot
103 be moved out of order.
107 The scrolling is done in two passes. The first pass is from top to bottom
108 scroling hunks UP. The second one is from bottom to top scrolling hunks DOWN.
109 Obviously enough, no lines to be scrolled will be destroyed. (lav)
113 Use the following production:
115 hardscroll: hardscroll.c
116 $(CC) -g -DSCROLLDEBUG hardscroll.c -o hardscroll
118 Then just type scramble vectors and watch. The following test loads are
119 a representative sample of cases:
121 ----------------------------- CUT HERE ------------------------------------
123 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23
126 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 -1
129 -1 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22
131 # An insertion (after line 12)
132 0 1 2 3 4 5 6 7 8 9 10 11 12 -1 13 14 15 16 17 18 19 20 21 22
134 # A simple deletion (line 10)
135 0 1 2 3 4 5 6 7 8 9 11 12 13 14 15 16 17 18 19 20 21 22 23 -1
137 # A more complex case
138 -1 -1 -1 -1 -1 3 4 5 6 7 -1 -1 8 9 10 11 12 13 14 15 16 17 -1 -1
139 ----------------------------- CUT HERE ------------------------------------
142 Eric S. Raymond <esr@snark.thyrsus.com>, November 1994
143 New algorithm by Alexander V. Lukyanov <lav@yars.free.net>, Aug 1997
145 *****************************************************************************/
147 #include <curses.priv.h>
149 MODULE_ID("$Id: hardscroll.c,v 1.33 1999/02/27 20:01:29 tom Exp $")
151 #if defined(SCROLLDEBUG) || defined(HASHDEBUG)
154 # define screen_lines MAXLINES
155 int oldnums[MAXLINES];
156 # define OLDNUM(n) oldnums[n]
157 # define _tracef printf
159 # define TR(n, a) if (_nc_tracing & (n)) { _tracef a ; putchar('\n'); }
163 /* OLDNUM(n) indicates which line will be shifted to the position n.
164 if OLDNUM(n) == _NEWINDEX, then the line n in new, not shifted from
167 int *_nc_oldnums = 0;
168 static int oldnums_allocated = 0;
169 # define oldnums _nc_oldnums
170 # define OLDNUM(n) oldnums[n]
171 # else /* !USE_HASHMAP */
172 # define OLDNUM(n) newscr->_line[n].oldindex
173 # endif /* !USE_HASHMAP */
175 #endif /* defined(SCROLLDEBUG) || defined(HASHDEBUG) */
178 void _nc_scroll_optimize(void)
179 /* scroll optimization to transform curscr to newscr */
182 int start, end, shift;
184 TR(TRACE_ICALLS, ("_nc_scroll_optimize() begins"));
186 #if !defined(SCROLLDEBUG) && !defined(HASHDEBUG)
188 /* get enough storage */
189 if (oldnums_allocated < screen_lines)
191 int *new_oldnums = typeRealloc(int, screen_lines, oldnums);
194 oldnums = new_oldnums;
195 oldnums_allocated = screen_lines;
197 /* calculate the indices */
200 #endif /* !defined(SCROLLDEBUG) && !defined(HASHDEBUG) */
203 if (_nc_tracing & (TRACE_UPDATE | TRACE_MOVE))
207 /* pass 1 - from top to bottom scrolling up */
208 for (i = 0; i < screen_lines; )
210 while (i < screen_lines && (OLDNUM(i) == _NEWINDEX || OLDNUM(i) <= i))
212 if (i >= screen_lines)
215 shift = OLDNUM(i) - i; /* shift > 0 */
219 while (i < screen_lines && OLDNUM(i) != _NEWINDEX && OLDNUM(i) - i == shift)
223 TR(TRACE_UPDATE | TRACE_MOVE, ("scroll [%d, %d] by %d", start, end, shift));
224 #if !defined(SCROLLDEBUG) && !defined(HASHDEBUG)
225 if (_nc_scrolln(shift, start, end, screen_lines - 1) == ERR)
227 TR(TRACE_UPDATE | TRACE_MOVE, ("unable to scroll"));
230 #endif /* !defined(SCROLLDEBUG) && !defined(HASHDEBUG) */
233 /* pass 2 - from bottom to top scrolling down */
234 for (i = screen_lines-1; i >= 0; )
236 while (i >= 0 && (OLDNUM(i) == _NEWINDEX || OLDNUM(i) >= i))
241 shift = OLDNUM(i) - i; /* shift < 0 */
245 while (i >= 0 && OLDNUM(i) != _NEWINDEX && OLDNUM(i) - i == shift)
247 start = i+1 - (-shift);
249 TR(TRACE_UPDATE | TRACE_MOVE, ("scroll [%d, %d] by %d", start, end, shift));
250 #if !defined(SCROLLDEBUG) && !defined(HASHDEBUG)
251 if (_nc_scrolln(shift, start, end, screen_lines - 1) == ERR)
253 TR(TRACE_UPDATE | TRACE_MOVE, ("unable to scroll"));
256 #endif /* !defined(SCROLLDEBUG) && !defined(HASHDEBUG) */
260 #if defined(TRACE) || defined(SCROLLDEBUG) || defined(HASHDEBUG)
261 void _nc_linedump(void)
262 /* dump the state of the real and virtual oldnum fields */
268 size_t want = (screen_lines + 1) * 4;
271 buf = typeMalloc(char, have = want);
273 (void) strcpy(buf, "virt");
274 for (n = 0; n < screen_lines; n++)
275 (void) sprintf(buf + strlen(buf), " %02d", OLDNUM(n));
276 TR(TRACE_UPDATE | TRACE_MOVE, (buf));
282 #endif /* defined(TRACE) || defined(SCROLLDEBUG) */
287 main(int argc GCC_UNUSED, char *argv[] GCC_UNUSED)
289 char line[BUFSIZ], *st;
292 _nc_tracing = TRACE_MOVE;
298 for (n = 0; n < screen_lines; n++)
299 oldnums[n] = _NEWINDEX;
301 /* grab the test vector */
302 if (fgets(line, sizeof(line), stdin) == (char *)NULL)
309 (void) fputs(line, stderr);
312 st = strtok(line, " ");
314 oldnums[n++] = atoi(st);
316 ((st = strtok((char *)NULL, " ")) != 0);
319 (void) fputs("Initial input:\n", stderr);
322 _nc_scroll_optimize();
326 #endif /* SCROLLDEBUG */
328 /* hardscroll.c ends here */