Re: FW: [css3-flexbox] flex distribution

On Mon, Jan 30, 2012 at 9:49 PM, Alex Mogilevsky <alexmog@microsoft.com> wrote:

> I wonder if there will be cases when at different iterations free space will
> change sign. I think it is possible, and it can be nasty.
>

Indeed, your algorithm in the way it is explained tends to oscillate I
think (didn't do excessive
analysis though).

It should look like this:

0.      Let TOTAL_FLEXES be a sum of all flexes. Let TOTAL_SPACE be an
available space.
         Let SET be a list of flexible items.

1.      From all flexible items in the SET reset sizes to their preferred value.

2.      If either
            a.  TOTAL_SPACE is zero or
            b.  TOTAL_FLEXES is zero or
            c.  SET is empty.
            we are done - exit.

3.      Find free space by subtracting sum of item sizes from available space.

4.      Distribute free space proportional to flex.

5.      Fix min/max violations:
     a.      If size of flex item is less than min value set the size
to that min value and
              exclude (subtract) flex value of the item from
TOTAL_FLEXES and the size from
              TOTAL_SPACE. Exclude the item from the SET. Go to step 1.
     b.      If size of flex item is greater than max value set the
size to that max value and
              exclude (subtract) flex value of the item from
TOTAL_FLEXES and the size from
              TOTAL_SPACE. Exclude the item from the SET. Go to step 1.

By its nature this algorithm will do max of sizeof(SET) iterations so is stable.

One of possible implementations is here:
http://www.terrainformatica.com/w3/flex-layout/spring-engine.h

-- 
Andrew Fedoniouk.

http://terrainformatica.com

Received on Wednesday, 1 February 2012 03:49:08 UTC