Back to index

tetex-bin  3.0
hardscroll.c
Go to the documentation of this file.
00001 /****************************************************************************
00002  * Copyright (c) 1998,2000 Free Software Foundation, Inc.                   *
00003  *                                                                          *
00004  * Permission is hereby granted, free of charge, to any person obtaining a  *
00005  * copy of this software and associated documentation files (the            *
00006  * "Software"), to deal in the Software without restriction, including      *
00007  * without limitation the rights to use, copy, modify, merge, publish,      *
00008  * distribute, distribute with modifications, sublicense, and/or sell       *
00009  * copies of the Software, and to permit persons to whom the Software is    *
00010  * furnished to do so, subject to the following conditions:                 *
00011  *                                                                          *
00012  * The above copyright notice and this permission notice shall be included  *
00013  * in all copies or substantial portions of the Software.                   *
00014  *                                                                          *
00015  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS  *
00016  * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF               *
00017  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.   *
00018  * IN NO EVENT SHALL THE ABOVE COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,   *
00019  * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR    *
00020  * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR    *
00021  * THE USE OR OTHER DEALINGS IN THE SOFTWARE.                               *
00022  *                                                                          *
00023  * Except as contained in this notice, the name(s) of the above copyright   *
00024  * holders shall not be used in advertising or otherwise to promote the     *
00025  * sale, use or other dealings in this Software without prior written       *
00026  * authorization.                                                           *
00027  ****************************************************************************/
00028 
00029 /****************************************************************************
00030  *  Author: Zeyd M. Ben-Halim <zmbenhal@netcom.com> 1992,1995               *
00031  *     and: Eric S. Raymond <esr@snark.thyrsus.com>                         *
00032  ****************************************************************************/
00033 
00034 /******************************************************************************
00035 
00036 NAME
00037    hardscroll.c -- hardware-scrolling optimization for ncurses
00038 
00039 SYNOPSIS
00040    void _nc_scroll_optimize(void)
00041 
00042 DESCRIPTION
00043                      OVERVIEW
00044 
00045 This algorithm for computes optimum hardware scrolling to transform an
00046 old screen (curscr) into a new screen (newscr) via vertical line moves.
00047 
00048 Because the screen has a `grain' (there are insert/delete/scroll line
00049 operations but no insert/delete/scroll column operations), it is efficient
00050 break the update algorithm into two pieces: a first stage that does only line
00051 moves, optimizing the end product of user-invoked insertions, deletions, and
00052 scrolls; and a second phase (corresponding to the present doupdate code in
00053 ncurses) that does only line transformations.
00054 
00055 The common case we want hardware scrolling for is to handle line insertions
00056 and deletions in screen-oriented text-editors.  This two-stage approach will
00057 accomplish that at a low computation and code-size cost.
00058 
00059                      LINE-MOVE COMPUTATION
00060 
00061 Now, to a discussion of the line-move computation.
00062 
00063 For expository purposes, consider the screen lines to be represented by
00064 integers 0..23 (with the understanding that the value of 23 may vary).
00065 Let a new line introduced by insertion, scrolling, or at the bottom of
00066 the screen following a line delete be given the index -1.
00067 
00068 Assume that the real screen starts with lines 0..23.  Now, we have
00069 the following possible line-oriented operations on the screen:
00070 
00071 Insertion: inserts a line at a given screen row, forcing all lines below
00072 to scroll forward.  The last screen line is lost.  For example, an insertion
00073 at line 5 would produce: 0..4 -1 5..23.
00074 
00075 Deletion: deletes a line at a given screen row, forcing all lines below
00076 to scroll forward.  The last screen line is made new.  For example, a deletion
00077 at line 7 would produce: 0..6 8..23 -1.
00078 
00079 Scroll up: move a range of lines up 1.  The bottom line of the range
00080 becomes new.  For example, scrolling up the region from 9 to 14 will
00081 produce 0..8 10..14 -1 15..23.
00082 
00083 Scroll down: move a range of lines down 1.  The top line of the range
00084 becomes new.  For example, scrolling down the region from 12 to 16 will produce
00085 0..11 -1 12..15 17..23.
00086 
00087 Now, an obvious property of all these operations is that they preserve the
00088 order of old lines, though not their position in the sequence.
00089 
00090 The key trick of this algorithm is that the original line indices described
00091 above are actually maintained as _line[].oldindex fields in the window
00092 structure, and stick to each line through scroll and insert/delete operations.
00093 
00094 Thus, it is possible at update time to look at the oldnum fields and compute
00095 an optimal set of il/dl/scroll operations that will take the real screen
00096 lines to the virtual screen lines.  Once these vertical moves have been done,
00097 we can hand off to the second stage of the update algorithm, which does line
00098 transformations.
00099 
00100 Note that the move computation does not need to have the full generality
00101 of a diff algorithm (which it superficially resembles) because lines cannot
00102 be moved out of order.
00103 
00104                      THE ALGORITHM
00105 
00106 The scrolling is done in two passes. The first pass is from top to bottom
00107 scroling hunks UP. The second one is from bottom to top scrolling hunks DOWN.
00108 Obviously enough, no lines to be scrolled will be destroyed. (lav)
00109 
00110 HOW TO TEST THIS:
00111 
00112 Use the following production:
00113 
00114 hardscroll: hardscroll.c
00115        $(CC) -g -DSCROLLDEBUG hardscroll.c -o hardscroll
00116 
00117 Then just type scramble vectors and watch.  The following test loads are
00118 a representative sample of cases:
00119 
00120 -----------------------------  CUT HERE ------------------------------------
00121 # No lines moved
00122  0  1  2  3  4  5  6  7  8  9 10 11 12 13 14 15 16 17 18 19 20 21 22 23
00123 #
00124 # A scroll up
00125  1  2  3  4  5  6  7  8  9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 -1
00126 #
00127 # A scroll down
00128 -1  0  1  2  3  4  5  6  7  8  9 10 11 12 13 14 15 16 17 18 19 20 21 22
00129 #
00130 # An insertion (after line 12)
00131  0  1  2  3  4  5  6  7  8  9 10 11 12 -1 13 14 15 16 17 18 19 20 21 22
00132 #
00133 # A simple deletion (line 10)
00134  0  1  2  3  4  5  6  7  8  9  11 12 13 14 15 16 17 18 19 20 21 22 23 -1
00135 #
00136 # A more complex case
00137 -1 -1 -1 -1 -1  3  4  5  6  7  -1 -1  8  9 10 11 12 13 14 15 16 17 -1 -1
00138 -----------------------------  CUT HERE ------------------------------------
00139 
00140 AUTHOR
00141     Eric S. Raymond <esr@snark.thyrsus.com>, November 1994
00142     New algorithm by Alexander V. Lukyanov <lav@yars.free.net>, Aug 1997
00143 
00144 *****************************************************************************/
00145 
00146 #include <curses.priv.h>
00147 
00148 MODULE_ID("$Id: hardscroll.c,v 1.36 2001/01/14 00:17:28 tom Exp $")
00149 
00150 #if defined(SCROLLDEBUG) || defined(HASHDEBUG)
00151 
00152 # undef screen_lines
00153 # define screen_lines MAXLINES
00154 NCURSES_EXPORT_VAR(int)
00155 oldnums[MAXLINES];
00156 # define OLDNUM(n)   oldnums[n]
00157 # define _tracef     printf
00158 # undef TR
00159 # define TR(n, a)    if (_nc_tracing & (n)) { _tracef a ; putchar('\n'); }
00160 
00161 #else /* no debug */
00162 
00163 /* OLDNUM(n) indicates which line will be shifted to the position n.
00164    if OLDNUM(n) == _NEWINDEX, then the line n in new, not shifted from
00165    somewhere. */
00166 NCURSES_EXPORT_VAR(int *)
00167 _nc_oldnums = 0;
00168 
00169 # if USE_HASHMAP
00170      static int oldnums_allocated = 0;
00171 #  define oldnums       _nc_oldnums
00172 #  define OLDNUM(n)  oldnums[n]
00173 # else                      /* !USE_HASHMAP */
00174 #  define OLDNUM(n)  newscr->_line[n].oldindex
00175 # endif                            /* !USE_HASHMAP */
00176 
00177 #endif /* defined(SCROLLDEBUG) || defined(HASHDEBUG) */
00178 
00179 NCURSES_EXPORT(void)
00180 _nc_scroll_optimize(void)
00181 /* scroll optimization to transform curscr to newscr */
00182 {
00183     int i;
00184     int start, end, shift;
00185 
00186     TR(TRACE_ICALLS, ("_nc_scroll_optimize() begins"));
00187 
00188 #if !defined(SCROLLDEBUG) && !defined(HASHDEBUG)
00189 #if USE_HASHMAP
00190     /* get enough storage */
00191     if (oldnums_allocated < screen_lines) {
00192        int *new_oldnums = typeRealloc(int, screen_lines, oldnums);
00193        if (!new_oldnums)
00194            return;
00195        oldnums = new_oldnums;
00196        oldnums_allocated = screen_lines;
00197     }
00198     /* calculate the indices */
00199     _nc_hash_map();
00200 #endif
00201 #endif /* !defined(SCROLLDEBUG) && !defined(HASHDEBUG) */
00202 
00203 #ifdef TRACE
00204     if (_nc_tracing & (TRACE_UPDATE | TRACE_MOVE))
00205        _nc_linedump();
00206 #endif /* TRACE */
00207 
00208     /* pass 1 - from top to bottom scrolling up */
00209     for (i = 0; i < screen_lines;) {
00210        while (i < screen_lines && (OLDNUM(i) == _NEWINDEX || OLDNUM(i) <= i))
00211            i++;
00212        if (i >= screen_lines)
00213            break;
00214 
00215        shift = OLDNUM(i) - i;      /* shift > 0 */
00216        start = i;
00217 
00218        i++;
00219        while (i < screen_lines && OLDNUM(i) != _NEWINDEX && OLDNUM(i) - i
00220               == shift)
00221            i++;
00222        end = i - 1 + shift;
00223 
00224        TR(TRACE_UPDATE | TRACE_MOVE, ("scroll [%d, %d] by %d", start, end, shift));
00225 #if !defined(SCROLLDEBUG) && !defined(HASHDEBUG)
00226        if (_nc_scrolln(shift, start, end, screen_lines - 1) == ERR) {
00227            TR(TRACE_UPDATE | TRACE_MOVE, ("unable to scroll"));
00228            continue;
00229        }
00230 #endif /* !defined(SCROLLDEBUG) && !defined(HASHDEBUG) */
00231     }
00232 
00233     /* pass 2 - from bottom to top scrolling down */
00234     for (i = screen_lines - 1; i >= 0;) {
00235        while (i >= 0 && (OLDNUM(i) == _NEWINDEX || OLDNUM(i) >= i))
00236            i--;
00237        if (i < 0)
00238            break;
00239 
00240        shift = OLDNUM(i) - i;      /* shift < 0 */
00241        end = i;
00242 
00243        i--;
00244        while (i >= 0 && OLDNUM(i) != _NEWINDEX && OLDNUM(i) - i == shift)
00245            i--;
00246        start = i + 1 - (-shift);
00247 
00248        TR(TRACE_UPDATE | TRACE_MOVE, ("scroll [%d, %d] by %d", start, end, shift));
00249 #if !defined(SCROLLDEBUG) && !defined(HASHDEBUG)
00250        if (_nc_scrolln(shift, start, end, screen_lines - 1) == ERR) {
00251            TR(TRACE_UPDATE | TRACE_MOVE, ("unable to scroll"));
00252            continue;
00253        }
00254 #endif /* !defined(SCROLLDEBUG) && !defined(HASHDEBUG) */
00255     }
00256 }
00257 
00258 #if defined(TRACE) || defined(SCROLLDEBUG) || defined(HASHDEBUG)
00259 NCURSES_EXPORT(void)
00260 _nc_linedump(void)
00261 /* dump the state of the real and virtual oldnum fields */
00262 {
00263     static size_t have;
00264     static char *buf;
00265 
00266     int n;
00267     size_t want = (screen_lines + 1) * 4;
00268 
00269     if (have < want)
00270        buf = typeMalloc(char, have = want);
00271 
00272     (void) strcpy(buf, "virt");
00273     for (n = 0; n < screen_lines; n++)
00274        (void) sprintf(buf + strlen(buf), " %02d", OLDNUM(n));
00275     TR(TRACE_UPDATE | TRACE_MOVE, (buf));
00276 #if NO_LEAKS
00277     free(buf);
00278     have = 0;
00279 #endif
00280 }
00281 #endif /* defined(TRACE) || defined(SCROLLDEBUG) */
00282 
00283 #ifdef SCROLLDEBUG
00284 
00285 int
00286 main(int argc GCC_UNUSED, char *argv[]GCC_UNUSED)
00287 {
00288     char line[BUFSIZ], *st;
00289 
00290 #ifdef TRACE
00291     _nc_tracing = TRACE_MOVE;
00292 #endif
00293     for (;;) {
00294        int n;
00295 
00296        for (n = 0; n < screen_lines; n++)
00297            oldnums[n] = _NEWINDEX;
00298 
00299        /* grab the test vector */
00300        if (fgets(line, sizeof(line), stdin) == (char *) NULL)
00301            exit(EXIT_SUCCESS);
00302 
00303        /* parse it */
00304        n = 0;
00305        if (line[0] == '#') {
00306            (void) fputs(line, stderr);
00307            continue;
00308        }
00309        st = strtok(line, " ");
00310        do {
00311            oldnums[n++] = atoi(st);
00312        } while
00313            ((st = strtok((char *) NULL, " ")) != 0);
00314 
00315        /* display it */
00316        (void) fputs("Initial input:\n", stderr);
00317        _nc_linedump();
00318 
00319        _nc_scroll_optimize();
00320     }
00321 }
00322 
00323 #endif /* SCROLLDEBUG */
00324 
00325 /* hardscroll.c ends here */