defrag

Algoritmus:

  1. vyplnim vsechny clustery, ktere jsou prazdne a maji byt plne
  2. pak uz existuji jen "cykly", ktere spotrebuji o jeden krok vic nez je delka cyklu

/* fill the unused cluster which has to be filled;
   if new one is freed, fill it too, recursively */
void fill_one (int idx)
{
    int n;
    while ( idx < cntuse && belongs[idx] == UNUSED )
    {
        n = whereis[idx];
        move_op (n, idx);
        idx = n;
    }
}


/* perform the defragmentation */
void defragment (void)
{
    int i;

    /* fill all of the unused clusters which are to be filled */
    for ( i = 0;  i < cntuse;  ++i )
        fill_one (i);

    /* now move away any cluster which is in a wrong place */
    for ( i = 0;  i < cntuse;  ++i )
        if ( i != belongs[i] )
        {
            move_op (i, cntuse);
            fill_one (i);
        }
}