1717 */
1818package ca .uqac .lif .spreadsheet .relation ;
1919
20+ import java .util .ArrayList ;
21+ import java .util .List ;
22+
2023import ca .uqac .lif .spreadsheet .Spreadsheet ;
2124
2225/**
@@ -29,7 +32,39 @@ public class NamedRow extends Row
2932 * The name of each column.
3033 */
3134 /*@ non_null @*/ protected final Object [] m_columnNames ;
32-
35+
36+ /**
37+ * Gets the numerical column indices in a spreadsheet corresponding to each
38+ * column name present in an array.
39+ * @param s The spreadsheet to look into
40+ * @param col_names A list of column names
41+ * @return An array of non-negative indices, or <tt>null</tt> if at least one
42+ * column name could not be found in the input spreadsheet
43+ */
44+ /*@ null @*/ public static int [] getColumnIndices (Spreadsheet s , Object ... col_names )
45+ {
46+ int [] indices = new int [col_names .length ];
47+ for (int n_col = 0 ; n_col < col_names .length ; n_col ++)
48+ {
49+ boolean found = false ;
50+ for (int col = 0 ; col < s .getWidth (); col ++)
51+ {
52+ Object name = s .get (col , 0 );
53+ if (Spreadsheet .same (name , col_names [n_col ]))
54+ {
55+ indices [n_col ] = col ;
56+ found = true ;
57+ break ;
58+ }
59+ }
60+ if (!found )
61+ {
62+ return null ;
63+ }
64+ }
65+ return indices ;
66+ }
67+
3368 /**
3469 * Creates a new named row.
3570 * @param col_names The name of each attribute
@@ -40,7 +75,13 @@ public NamedRow(Object[] col_names, Object[] values)
4075 super (values );
4176 m_columnNames = col_names ;
4277 }
43-
78+
79+ public NamedRow (Object [] col_names , int [] col_indices , int row , Spreadsheet s )
80+ {
81+ super (getValues (col_indices , row , s ));
82+ m_columnNames = col_names ;
83+ }
84+
4485 /**
4586 * Gets the value associated to a column name.
4687 * @param col_name The name of the column
@@ -58,4 +99,80 @@ public Object valueOf(Object col_name)
5899 }
59100 return null ;
60101 }
102+
103+ /**
104+ * Gets the row indices in a spreadsheet whose cells have the same values as
105+ * those in the named row.
106+ * @param s The spreadsheet to look into
107+ * @param col_indices The indices of the columns in s corresponding to each
108+ * column mentioned in this named row
109+ * @return A list of row indices in the spreadsheet with the same value
110+ */
111+ public List <Integer > indicesOf (Spreadsheet s , int [] col_indices )
112+ {
113+ List <Integer > indices = new ArrayList <Integer >();
114+ if (col_indices == null )
115+ {
116+ return indices ;
117+ }
118+ for (int row = 1 ; row < s .getHeight (); row ++)
119+ {
120+ Object [] row_o = s .getRow (row );
121+ boolean matches = true ;
122+ for (int col = 0 ; col < col_indices .length && matches ; col ++)
123+ {
124+ if (!Spreadsheet .same (m_contents [col ], row_o [col_indices [col ]]))
125+ {
126+ matches = false ;
127+ }
128+ }
129+ if (matches )
130+ {
131+ indices .add (row );
132+ }
133+ }
134+ return indices ;
135+ }
136+
137+ /**
138+ * Gets the row indices in a spreadsheet whose cells have the same values as
139+ * those in the named row.
140+ * @param s The spreadsheet to look into
141+ * @return A list of row indices in the spreadsheet with the same value
142+ */
143+ /*@ non_null @*/ public List <Integer > indicesOf (Spreadsheet s )
144+ {
145+ return indicesOf (s , getColumnIndices (s ));
146+ }
147+
148+ /**
149+ * Gets the numerical column indices in a spreadsheet corresponding to each
150+ * column name present in this named row.
151+ * @param s The spreadsheet to look into
152+ * @return An array of non-negative indices, or <tt>null</tt> if at least one
153+ * column name could not be found in the input spreadsheet
154+ */
155+ /*@ null @*/ protected int [] getColumnIndices (Spreadsheet s )
156+ {
157+ return getColumnIndices (s , m_columnNames );
158+ }
159+
160+ /**
161+ * Fetches a subset of the values in a row of a spreadsheet.
162+ * @param col_indices The indices of the columns to fetch
163+ * @param row The index of the row in the spreadsheet
164+ * @param s The spreadsheet to fetch the values from
165+ * @return The array of values at the corresponding indices in that
166+ * spreadsheet's row
167+ */
168+ /*@ non_null @*/ protected static Object [] getValues (int [] col_indices , int row , Spreadsheet s )
169+ {
170+ Object [] values = new Object [col_indices .length ];
171+ Object [] s_row = s .getRow (row );
172+ for (int i = 0 ; i < col_indices .length ; i ++)
173+ {
174+ values [i ] = s_row [col_indices [i ]];
175+ }
176+ return values ;
177+ }
61178}
0 commit comments