ndbreorder.c 1.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  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 <u.h>
  10. #include <libc.h>
  11. #include <bio.h>
  12. #include <ndb.h>
  13. /*
  14. * reorder the tuple to put x's line first in the entry and x fitst in its line
  15. */
  16. Ndbtuple*
  17. ndbreorder(Ndbtuple *t, Ndbtuple *x)
  18. {
  19. Ndbtuple *nt;
  20. Ndbtuple *last, *prev;
  21. /* if x is first, we're done */
  22. if(x == t)
  23. return t;
  24. /* find end of x's line */
  25. for(last = x; last->line == last->entry; last = last->line)
  26. ;
  27. /* rotate to make this line first */
  28. if(last->line != t){
  29. /* detach this line and everything after it from the entry */
  30. for(nt = t; nt->entry != last->line; nt = nt->entry)
  31. ;
  32. nt->entry = nil;
  33. /* switch */
  34. for(nt = last; nt->entry != nil; nt = nt->entry)
  35. ;
  36. nt->entry = t;
  37. }
  38. /* rotate line to make x first */
  39. if(x != last->line){
  40. /* find entry before x */
  41. for(prev = last; prev->line != x; prev = prev->line)
  42. ;
  43. /* detach line */
  44. nt = last->entry;
  45. last->entry = last->line;
  46. /* reattach */
  47. prev->entry = nt;
  48. }
  49. return x;
  50. }