in reply to 'ls -C' column style

This one loops over the list of items once (at most). It computes the widths for all possible numbers of columns as it goes, eliminating cases of too-many columns when it determines that they would be too wide.

Fully tested and now it does not waste time on column counts over about @items/2 and (update 3) chooses single-row output correctly.

#!/usr/bin/perl -w use strict; my( $maxWid )= ( @ARGV, 79 ); my @items = qw( B ByteLoader Cwd DB_File Data Devel Digest DynaLoader Encode Errno Fcntl File Filter GDBM_File I18N IO IPC List MIME NDBM_File ODBM_File Opcode POSIX PerlIO SDBM_File Safe Socket Storable Sys Thread Time Unicode XS attrs re threads util ); print '='x$maxWid, $/; print Columns( $maxWid, \@items ); exit( 0 ); sub Columns { my( $maxWid, $avItems )= @_; my $maxCols= 1 + $#$avItems/2; my @height= ( 0, map 1+int($#$avItems/$_), 1..$maxCols ); my @total= ( $#$avItems, 0 .. ($maxCols-1) ); my @width; for my $i ( 0..$#$avItems ) { my $len = length( $avItems->[$i] ); $total[0] += $len; for my $cols ( 1 .. $maxCols ) { for( $width[ $cols ][ $i/$height[$cols] ] ) { $_ ||= 0; if( $_ <= $len ) { $total[$cols] += $len - $_; if( $maxWid < $total[$cols] ) { $maxCols= $cols - 1; } $_= $len; } } last if $maxCols < $cols; } last if $maxCols < 2; } $maxCols ||= 1; my $height= $height[$maxCols]; @width= @{ $width[$maxCols] }; if( $total[0] <= $maxWid ) { $maxCols= @$avItems; $height= 1; @width= (0) x $maxCols; } my $text= ''; for my $l ( 1 .. $height ) { my $i= $l - 1; my $c= 0; while( $i < @$avItems ) { my $item= $avItems->[$i]; $i += $height; if( $i < @$avItems ) { $text .= sprintf "%-$width[$c++]s ", $item; } else { $text .= $item . $/; } } } return $text; }

- tye        

Replies are listed 'Best First'.
Re^2: 'ls -C' column style (sideways)
by BrowserUk (Patriarch) on Nov 04, 2004 at 23:08 UTC
    This one loops over the list of items once (at most).

    Intriguing. Both your original version and the updated one do much better than mine at small widths (25), but then get progressively worse, where mine gets better.

    I haven't read through your algorithm properly yet, but we do get the same results (except I used 2 spaces not 1 between the columns).

    [22:49:46.54] P:\test>405274 >nul 100 trials of Buk:25 ( 190.051ms total), 1.901ms/trial 100 trials of Buk:50 ( 113.853ms total), 1.139ms/trial 100 trials of Buk:75 ( 88.451ms total), 884us/trial 100 trials of Buk:100 ( 70.376ms total), 703us/trial 100 trials of Buk:125 ( 38.077ms total), 380us/trial 100 trials of Buk:150 ( 46.875ms total), 468us/trial 100 trials of Buk:175 ( 46.875ms total), 468us/trial 100 trials of Buk:200 ( 46.875ms total), 468us/trial [22:50:02.71] P:\test>405274-tye1 >nul 100 trials of Tye1:25 ( 93.742ms total), 937us/trial 100 trials of Tye1:50 ( 190.402ms total), 1.904ms/trial 100 trials of Tye1:75 ( 265.551ms total), 2.656ms/trial 100 trials of Tye1:100 ( 343.750ms total), 3.438ms/tria +l 100 trials of Tye1:125 ( 421.875ms total), 4.219ms/tria +l 100 trials of Tye1:150 ( 453.125ms total), 4.531ms/tria +l 100 trials of Tye1:175 ( 468.750ms total), 4.688ms/tria +l 100 trials of Tye1:200 ( 484.375ms total), 4.844ms/tria +l [22:50:08.43] P:\test>405274-tye2 >nul 100 trials of Tye2:25 ( 107.743ms total), 1.077ms/trial 100 trials of Tye2:50 ( 154.991ms total), 1.550ms/trial 100 trials of Tye2:75 ( 200.225ms total), 2.002ms/trial 100 trials of Tye2:100 ( 227.510ms total), 2.275ms/tria +l 100 trials of Tye2:125 ( 250ms total), 2.500ms/tria +l 100 trials of Tye2:150 ( 234.375ms total), 2.344ms/tria +l 100 trials of Tye2:175 ( 234.375ms total), 2.344ms/tria +l 100 trials of Tye2:200 ( 250ms total), 2.500ms/tria +l

    Examine what is said, not who speaks.
    "Efficiency is intelligent laziness." -David Dunham
    "Think for yourself!" - Abigail
    "Memory, processor, disk in that order on the hardware side. Algorithm, algorithm, algorithm on the code side." - tachyon

      I didn't write it this way for performance reasons. But this technique should have good performance when sorting items across instead of down.

      You can also make it faster with a bit of complication by teaching it to consider about 2N^.5 column counts rather than about N/2 of them.

      - tye        

        Neither did I, I was just curious about your "This one loops over the list of items once (at most)." claim. Knowing mine makes several passes, I wondered how they compared. I (think) I see that you actually do make 2 passes: 1 to determine the no. of columns; 1 to do the formatting.

        I'm not sure what you mean by the 2N^.5 bit? No matter how I bracket that, it always comes out to 74 compared to 37/2 = 18 or 19.

        The problem with limiting the columns in that way is that you miss the edge case where the entire list will format into 1 row--and split it across 2.

        The reason mine arrives at the answer quicker in all but the smallest widths case, is that I come at from the other end. I iterate the rows 1 .. N, and calculate the columns.

        I noticed that in the general case increasing the number of rows until it fit arrived more quickly than increasing the number of columns as it considered less duplicate cases. For best performance, you could probably determine some formula that would indicate which end to start from, but my math ain't that strong.


        Examine what is said, not who speaks.
        "Efficiency is intelligent laziness." -David Dunham
        "Think for yourself!" - Abigail
        "Memory, processor, disk in that order on the hardware side. Algorithm, algorithm, algorithm on the code side." - tachyon

      I suspect the most efficient way would be to do a binary chop on the number of rows, with an initial min/max of 1, @items.

      Hugo

Re^2: 'ls -C' column style (sideways)
by hv (Prior) on Nov 05, 2004 at 11:07 UTC

    This is quite nice, if a bit opaque; I fiddled with it to get a 2-char spacer - as well as the sprintf, I modified the initialisation of @total so:

    my @total= ( $#$avItems, map $_ * 2, 0 .. ($maxCols-1) );
    which seemed to do the right thing, but there may be edge cases I missed.

    Hugo