Skip to content

Commit 29eaa13

Browse files
committed
Modifications to NamedRow
1 parent 6292382 commit 29eaa13

3 files changed

Lines changed: 230 additions & 3 deletions

File tree

Source/Core/src/ca/uqac/lif/spreadsheet/functions/Merge.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
/*
22
A provenance-aware spreadsheet library
3-
Copyright (C) 2021 Sylvain Hallé
3+
Copyright (C) 2021-2022 Sylvain Hallé
44
55
This program is free software: you can redistribute it and/or modify
66
it under the terms of the GNU Lesser General Public License as published by
Lines changed: 110 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,110 @@
1+
/*
2+
A provenance-aware spreadsheet library
3+
Copyright (C) 2021-2022 Sylvain Hallé
4+
5+
This program is free software: you can redistribute it and/or modify
6+
it under the terms of the GNU Lesser General Public License as published by
7+
the Free Software Foundation, either version 3 of the License, or
8+
(at your option) any later version.
9+
10+
This program is distributed in the hope that it will be useful,
11+
but WITHOUT ANY WARRANTY; without even the implied warranty of
12+
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13+
GNU General Public License for more details.
14+
15+
You should have received a copy of the GNU Lesser General Public License
16+
along with this program. If not, see <http://www.gnu.org/licenses/>.
17+
*/
18+
package ca.uqac.lif.spreadsheet.relation;
19+
20+
import java.util.List;
21+
22+
import ca.uqac.lif.dag.LabelledNode;
23+
import ca.uqac.lif.petitpoucet.NodeFactory;
24+
import ca.uqac.lif.petitpoucet.function.AtomicFunction;
25+
import ca.uqac.lif.petitpoucet.function.InvalidArgumentTypeException;
26+
import ca.uqac.lif.petitpoucet.function.InvalidNumberOfArgumentsException;
27+
import ca.uqac.lif.spreadsheet.Spreadsheet;
28+
import ca.uqac.lif.spreadsheet.functions.Merge;
29+
30+
public class Join extends RelationalOperator
31+
{
32+
/**
33+
* The names of the columns on which to apply the join condition.
34+
*/
35+
protected final Object[] m_columnNames;
36+
37+
/**
38+
* The indices in the first spreadsheet of the columns on which to
39+
* perform the join.
40+
*/
41+
protected int[] m_firstJoinIndices;
42+
43+
/**
44+
* The indices in the second spreadsheet of the columns on which to
45+
* perform the join.
46+
*/
47+
protected int[] m_secondJoinIndices;
48+
49+
/**
50+
* Creates a new instance of the join operator.
51+
* @param columns The names of the columns on which to apply the join
52+
* condition
53+
*/
54+
public Join(boolean sort_output, Object ... columns)
55+
{
56+
super(2);
57+
m_sortOutput = sort_output;
58+
m_columnNames = columns;
59+
}
60+
61+
@Override
62+
protected Object[] getValue(Object... inputs) throws InvalidNumberOfArgumentsException
63+
{
64+
if (!(inputs[0] instanceof Spreadsheet) || !(inputs[1] instanceof Spreadsheet))
65+
{
66+
throw new InvalidArgumentTypeException("One of the arguments is not a spreadsheet");
67+
}
68+
Spreadsheet s1 = (Spreadsheet) inputs[0];
69+
Spreadsheet s2 = (Spreadsheet) inputs[0];
70+
m_firstJoinIndices = NamedRow.getColumnIndices(s1, m_columnNames);
71+
m_secondJoinIndices = NamedRow.getColumnIndices(s2, m_columnNames);
72+
for (int row_1 = 1; row_1 < s1.getHeight(); row_1++)
73+
{
74+
NamedRow nr = new NamedRow(m_columnNames, m_firstJoinIndices, row_1, s1);
75+
List<Integer> indices = nr.indicesOf(s2, m_secondJoinIndices);
76+
}
77+
return null;
78+
}
79+
80+
@Override
81+
public String toString()
82+
{
83+
StringBuilder out = new StringBuilder();
84+
out.append("\u2a1d ["); // bowtie
85+
for (int i = 0; i < m_columnNames.length; i++)
86+
{
87+
if (i > 0)
88+
{
89+
out.append(",");
90+
}
91+
out.append(m_columnNames[i]);
92+
}
93+
out.append("]");
94+
return out.toString();
95+
}
96+
97+
@Override
98+
protected LabelledNode getConnectorNode(NodeFactory f)
99+
{
100+
// TODO Auto-generated method stub
101+
return null;
102+
}
103+
104+
@Override
105+
public AtomicFunction duplicate(boolean with_state)
106+
{
107+
// TODO Auto-generated method stub
108+
return null;
109+
}
110+
}

Source/Relations/src/ca/uqac/lif/spreadsheet/relation/NamedRow.java

Lines changed: 119 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,9 @@
1717
*/
1818
package ca.uqac.lif.spreadsheet.relation;
1919

20+
import java.util.ArrayList;
21+
import java.util.List;
22+
2023
import 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

Comments
 (0)