akaros/user/ndblib/ndbreorder.c
<<
>>
Prefs
   1/* 
   2 * This file is part of the UCB release of Plan 9. It is subject to the license
   3 * terms in the LICENSE file found in the top-level directory of this
   4 * distribution and at http://akaros.cs.berkeley.edu/files/Plan9License. No
   5 * part of the UCB release of Plan 9, including this file, may be copied,
   6 * modified, propagated, or distributed except according to the terms contained
   7 * in the LICENSE file.
   8 */
   9#include <stdlib.h>
  10
  11#include <stdio.h>
  12#include <parlib/parlib.h>
  13#include <unistd.h>
  14#include <signal.h>
  15#include <fcntl.h>
  16#include <iplib/iplib.h>
  17#include <ndblib/ndb.h>
  18
  19/*
  20 *  reorder the tuple to put x's line first in the entry and x fitst in its line
  21 */
  22struct ndbtuple*
  23ndbreorder(struct ndbtuple *t, struct ndbtuple *x)
  24{
  25        struct ndbtuple *nt;
  26        struct ndbtuple *last, *prev;
  27
  28        /* if x is first, we're done */
  29        if(x == t)
  30                return t;
  31
  32        /* find end of x's line */
  33        for(last = x; last->line == last->entry; last = last->line)
  34                ;
  35
  36        /* rotate to make this line first */
  37        if(last->line != t){
  38
  39                /* detach this line and everything after it from the entry */
  40                for(nt = t; nt->entry != last->line; nt = nt->entry)
  41                        ;
  42                nt->entry = NULL;
  43        
  44                /* switch */
  45                for(nt = last; nt->entry != NULL; nt = nt->entry)
  46                        ;
  47                nt->entry = t;
  48        }
  49
  50        /* rotate line to make x first */
  51        if(x != last->line){
  52
  53                /* find entry before x */
  54                for(prev = last; prev->line != x; prev = prev->line);
  55                        ;
  56
  57                /* detach line */
  58                nt = last->entry;
  59                last->entry = last->line;
  60
  61                /* reattach */
  62                prev->entry = nt;
  63        }
  64
  65        return x;
  66}
  67