ndbreorder.c 966 B

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