Annotation of 43BSD/contrib/icon/rt/cplist.c, revision 1.1.1.1

1.1       root        1: #include "../h/rt.h"
                      2: 
                      3: /*
                      4:  * cplist(d1,d2,i,j) - copy sublist d1[i:j] into d2.
                      5:  */
                      6: 
                      7: cplist(d1, d2, i, j)
                      8: struct descrip *d1, *d2;
                      9: int i, j;
                     10:    {
                     11:    register struct descrip *dp;
                     12:    int size, nelem;
                     13:    struct b_list *lp1, *lp2;
                     14:    struct b_lelem *bp1, *bp2;
                     15: 
                     16:    /*
                     17:     * Calculate the size of the sublist and fail if it's less than 0.
                     18:     *  Also round nelem up to the minimum list block size.
                     19:     */
                     20:    size = nelem = j - i;
                     21:    if (size < 0)
                     22:       fail();
                     23:    if (nelem < LISTBLKSIZE)
                     24:       nelem = LISTBLKSIZE;
                     25: 
                     26:    /*
                     27:     * Get pointers to the list and list elements for the source list
                     28:     *  (bp1, lp1) and the sublist (bp2, lp2).
                     29:     */
                     30:    hneed(sizeof(struct b_list) + sizeof(struct b_lelem) +
                     31:          nelem * sizeof(struct descrip));
                     32:    lp1 = (struct b_list *) BLKLOC(*d1);
                     33:    bp1 = (struct b_lelem *) BLKLOC(lp1->listhead);
                     34:    lp2 = (struct b_list *) alclist(size);
                     35:    bp2 = (struct b_lelem *) alclstb(nelem, 0, size);
                     36:    lp2->listhead.type = lp2->listtail.type = D_LELEM;
                     37:    BLKLOC(lp2->listhead) = BLKLOC(lp2->listtail) = (union block *) bp2;
                     38:    dp = bp2->lslots;
                     39: 
                     40:    /*
                     41:     * Locate the block containing element i in the source list.
                     42:     */
                     43:    if (size > 0) {
                     44:       while (i > bp1->nused) {
                     45:          i -= bp1->nused;
                     46:          bp1 = (struct b_lelem *) BLKLOC(bp1->listnext);
                     47:          }
                     48:       }
                     49: 
                     50:    /*
                     51:     * Copy elements from the source list into the sublist, moving to
                     52:     *  the next list block in the source list when all elements in a
                     53:     *  block have been copied.
                     54:     */
                     55:    while (size > 0) {
                     56:       j = bp1->first + i - 1;
                     57:       if (j >= bp1->nelem)
                     58:          j -= bp1->nelem;
                     59:       *dp++ = bp1->lslots[j];
                     60:       if (++i > bp1->nused) {
                     61:          i = 1;
                     62:          bp1 = (struct b_lelem *) BLKLOC(bp1->listnext);
                     63:          }
                     64:       size--;
                     65:       }
                     66: 
                     67:    /*
                     68:     * Fix type and location fields for the new list.
                     69:     */
                     70:    d2->type = D_LIST;
                     71:    BLKLOC(*d2) = (union block *) lp2;
                     72:    }

unix.superglobalmegacorp.com

This archive runs on limited infrastructure. Preserving old code on modern bandwidth. Automated agents are requested to crawl responsibly.