inprogress
[jalview.git] / forester / ruby / evoruby / lib / evo / table / basic_table.rb
1 # = lib/evo/table/basic_table.rb - BasicTable class
2 #
3 # Copyright::  Copyright (C) 2006-2007 Christian M. Zmasek
4 # License::    GNU Lesser General Public License (LGPL)'s
5 #
6 # $Id: basic_table.rb,v 1.3 2007/09/28 03:12:10 cmzmasek Exp $
7 #
8 # last modified: 05/16/2007
9
10 #require 'lib/evo/util/constants'
11
12 module Evoruby
13
14   class BasicTable
15
16         def initialize()
17             @rows = Hash.new
18             @max_row = 0
19             @max_col = 0
20         end
21
22         # raises ArgumentError
23         def set_value( row, col, value )
24             if ( ( row < 0 ) || ( col < 0 ) )
25                 raise( ArgumentError, "attempt to use negative values for row or column" )
26             end
27             if ( row > get_max_row() )
28                 set_max_row( row )
29             end
30             if ( col > get_max_col() )
31                 set_max_col( col )
32             end
33             row_map = nil
34             if ( @rows.has_key?( row ) )
35                 row_map = @rows[ row ]
36             else
37                 row_map = Hash.new
38                 @rows[ row ] = row_map
39             end
40             row_map[ col ] = value
41         end
42
43         # raises ArgumentError
44         def get_value_as_string( row, col )
45             return ( get_value( row, col ) ).to_s
46         end
47
48         # raises ArgumentError
49         def get_value( row, col )
50             if ( ( row > get_max_row() ) || ( row < 0 ) )
51                 raise( ArgumentError, "value for row (" + row.to_s +
52                          ") is out of range [max row: " + get_max_row().to_s + "]" )
53             elsif ( ( col > get_max_col() ) || ( row < 0 ) )
54                 raise( ArgumentError, "value for column (" + col.to_s +
55                          ") is out of range [max column: " + get_max_col().to_s + "]" )
56             end
57             row_map = @rows[ row ]
58             if ( ( row_map == nil ) || ( row_map.length < 1 ) )
59                 return nil
60             end
61             return row_map[ col ]
62         end
63
64         def get_max_col()
65             return @max_col
66         end
67
68         def get_max_row()
69             return @max_row
70         end
71
72         # raises ArgumentError
73         def get_columns_as_map( key_col, value_col )
74             map = Hash.new
75             for row in 0 .. get_max_row
76                 key = get_value( row, key_col )
77                 value = get_value( row, value_col )
78                 if ( ( key != nil ) && ( value != nil ) )
79                     if ( map.has_key?( key ) )
80                         raise( ArgumentError, "attempt to use non-unique table value as key [" +
81                                         + key + "]" )
82                     end
83                     map[ key ] = value
84                 end
85             end
86             return map
87         end
88
89         def to_s
90             str = String.new
91             for row in 0 .. get_max_row
92                for col in 0 .. get_max_col
93                    str << col.to_s << " "
94                end
95                str << LEvoruby::Constants::LINE_DELIMITER
96                for col in 0 .. get_max_col
97                    str << row.to_s << ": "
98                    str << get_value( row, col ) << " "
99                end
100                str << Evoruby::Constants::LINE_DELIMITER
101             end
102             return str
103         end
104
105
106         private
107
108         def get_row( row )
109             return @rows[ row ]
110         end
111
112         def set_max_col( max_col )
113             @max_col = max_col
114         end
115
116         def set_max_row( max_row )
117             @max_row = max_row
118         end
119
120     end # class BasicTable
121
122 end # module Evoruby