Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 528ff28

Browse files
committed
Extended index types to include temporal columns: Date, Time, and DateTime.
Minor fixes for LongIndex
1 parent 4bd9073 commit 528ff28

File tree

8 files changed

+228
-5
lines changed

8 files changed

+228
-5
lines changed

dependency-reduced-pom.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
<groupId>com.github.lwhite1</groupId>
55
<artifactId>tablesaw</artifactId>
66
<name>Tablesaw</name>
7-
<version>0.7.6.4</version>
7+
<version>0.7.6.5-SNAPSHOT</version>
88
<description>High-performance Java Dataframe with integrated columnar storage</description>
99
<url>https://jtablesaw.wordpress.com</url>
1010
<developers>

src/main/java/com/github/lwhite1/tablesaw/columns/DateColumnUtils.java

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package com.github.lwhite1.tablesaw.columns;
22

33
import com.github.lwhite1.tablesaw.api.DateColumn;
4+
import com.github.lwhite1.tablesaw.filtering.IntBiPredicate;
45
import com.github.lwhite1.tablesaw.filtering.IntPredicate;
56
import it.unimi.dsi.fastutil.ints.IntArrayList;
67

@@ -16,4 +17,13 @@ public interface DateColumnUtils extends Column, Iterable<LocalDate> {
1617
IntPredicate isMissing = i -> i == DateColumn.MISSING_VALUE;
1718

1819
IntPredicate isNotMissing = i -> i != DateColumn.MISSING_VALUE;
20+
21+
IntBiPredicate isGreaterThan = (valueToTest, valueToCompareAgainst) -> valueToTest > valueToCompareAgainst;
22+
IntBiPredicate isGreaterThanOrEqualTo = (valueToTest, valueToCompareAgainst) -> valueToTest >= valueToCompareAgainst;
23+
24+
IntBiPredicate isLessThan = (valueToTest, valueToCompareAgainst) -> valueToTest < valueToCompareAgainst;
25+
IntBiPredicate isLessThanOrEqualTo = (valueToTest, valueToCompareAgainst) -> valueToTest <= valueToCompareAgainst;
26+
27+
IntBiPredicate isEqualTo = (valueToTest, valueToCompareAgainst) -> valueToTest == valueToCompareAgainst;
28+
1929
}
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
package com.github.lwhite1.tablesaw.index;
2+
3+
import com.github.lwhite1.tablesaw.api.DateColumn;
4+
import com.github.lwhite1.tablesaw.columns.packeddata.PackedLocalDate;
5+
import com.github.lwhite1.tablesaw.util.Selection;
6+
7+
import java.time.LocalDate;
8+
9+
/**
10+
* An index for four-byte integer and Date columns
11+
*/
12+
public class DateIndex {
13+
14+
private final IntIndex index;
15+
16+
public DateIndex(DateColumn column) {
17+
index = new IntIndex(column);
18+
}
19+
20+
/**
21+
* Returns a bitmap containing row numbers of all cells matching the given int
22+
*
23+
* @param value This is a 'key' from the index perspective, meaning it is a value from the standpoint of the column
24+
*/
25+
public Selection get(LocalDate value) {
26+
return index.get(PackedLocalDate.pack(value));
27+
}
28+
29+
public Selection atLeast(LocalDate value) {
30+
return index.atLeast(PackedLocalDate.pack(value));
31+
}
32+
33+
public Selection greaterThan(LocalDate value) {
34+
return index.greaterThan(PackedLocalDate.pack(value));
35+
}
36+
37+
public Selection atMost(LocalDate value) {
38+
return index.atMost(PackedLocalDate.pack(value));
39+
}
40+
41+
public Selection lessThan(LocalDate value) {
42+
return index.lessThan(PackedLocalDate.pack(value));
43+
}
44+
}
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
package com.github.lwhite1.tablesaw.index;
2+
3+
import com.github.lwhite1.tablesaw.api.DateTimeColumn;
4+
import com.github.lwhite1.tablesaw.columns.packeddata.PackedLocalDateTime;
5+
import com.github.lwhite1.tablesaw.util.Selection;
6+
7+
import java.time.LocalDateTime;
8+
9+
/**
10+
* An index for four-byte integer and Date columns
11+
*/
12+
public class DateTimeIndex {
13+
14+
private final LongIndex index;
15+
16+
public DateTimeIndex(DateTimeColumn column) {
17+
index = new LongIndex(column);
18+
}
19+
20+
/**
21+
* Returns a bitmap containing row numbers of all cells matching the given int
22+
*
23+
* @param value This is a 'key' from the index perspective, meaning it is a value from the standpoint of the column
24+
*/
25+
public Selection get(LocalDateTime value) {
26+
return index.get(PackedLocalDateTime.pack(value));
27+
}
28+
29+
public Selection atLeast(LocalDateTime value) {
30+
return index.atLeast(PackedLocalDateTime.pack(value));
31+
}
32+
33+
public Selection greaterThan(LocalDateTime value) {
34+
return index.greaterThan(PackedLocalDateTime.pack(value));
35+
}
36+
37+
public Selection atMost(LocalDateTime value) {
38+
return index.atMost(PackedLocalDateTime.pack(value));
39+
}
40+
41+
public Selection lessThan(LocalDateTime value) {
42+
return index.lessThan(PackedLocalDateTime.pack(value));
43+
}
44+
}

src/main/java/com/github/lwhite1/tablesaw/index/IntIndex.java

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
package com.github.lwhite1.tablesaw.index;
22

3+
import com.github.lwhite1.tablesaw.api.DateColumn;
34
import com.github.lwhite1.tablesaw.api.IntColumn;
5+
import com.github.lwhite1.tablesaw.api.TimeColumn;
46
import com.github.lwhite1.tablesaw.util.BitmapBackedSelection;
57
import com.github.lwhite1.tablesaw.util.Selection;
68
import it.unimi.dsi.fastutil.ints.Int2ObjectAVLTreeMap;
@@ -35,6 +37,42 @@ public IntIndex(IntColumn column) {
3537
index = new Int2ObjectAVLTreeMap<>(tempMap);
3638
}
3739

40+
public IntIndex(DateColumn column) {
41+
int sizeEstimate = Integer.min(1_000_000, column.size() / 100);
42+
Int2ObjectOpenHashMap<IntArrayList> tempMap = new Int2ObjectOpenHashMap<>(sizeEstimate);
43+
for (int i = 0; i < column.size(); i++) {
44+
int value = column.getInt(i);
45+
IntArrayList recordIds = tempMap.get(value);
46+
if (recordIds == null) {
47+
recordIds = new IntArrayList();
48+
recordIds.add(i);
49+
tempMap.trim();
50+
tempMap.put(value, recordIds);
51+
} else {
52+
recordIds.add(i);
53+
}
54+
}
55+
index = new Int2ObjectAVLTreeMap<>(tempMap);
56+
}
57+
58+
public IntIndex(TimeColumn column) {
59+
int sizeEstimate = Integer.min(1_000_000, column.size() / 100);
60+
Int2ObjectOpenHashMap<IntArrayList> tempMap = new Int2ObjectOpenHashMap<>(sizeEstimate);
61+
for (int i = 0; i < column.size(); i++) {
62+
int value = column.getInt(i);
63+
IntArrayList recordIds = tempMap.get(value);
64+
if (recordIds == null) {
65+
recordIds = new IntArrayList();
66+
recordIds.add(i);
67+
tempMap.trim();
68+
tempMap.put(value, recordIds);
69+
} else {
70+
recordIds.add(i);
71+
}
72+
}
73+
index = new Int2ObjectAVLTreeMap<>(tempMap);
74+
}
75+
3876
private final static Comparator<int[]> intArrayComparator = new Comparator<int[]>() {
3977
public int compare(int[] a, int[] b) {
4078
return Integer.compare(a[1], b[1]);

src/main/java/com/github/lwhite1/tablesaw/index/LongIndex.java

Lines changed: 23 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package com.github.lwhite1.tablesaw.index;
22

3+
import com.github.lwhite1.tablesaw.api.DateTimeColumn;
34
import com.github.lwhite1.tablesaw.api.LongColumn;
45
import com.github.lwhite1.tablesaw.util.BitmapBackedSelection;
56
import com.github.lwhite1.tablesaw.util.Selection;
@@ -34,6 +35,24 @@ public LongIndex(LongColumn column) {
3435
index = new Long2ObjectAVLTreeMap<>(tempMap);
3536
}
3637

38+
public LongIndex(DateTimeColumn column) {
39+
int sizeEstimate = Integer.min(1_000_000, column.size() / 100);
40+
Long2ObjectOpenHashMap<IntArrayList> tempMap = new Long2ObjectOpenHashMap<>(sizeEstimate);
41+
for (int i = 0; i < column.size(); i++) {
42+
long value = column.getLong(i);
43+
IntArrayList recordIds = tempMap.get(value);
44+
if (recordIds == null) {
45+
recordIds = new IntArrayList();
46+
recordIds.add(i);
47+
tempMap.trim();
48+
tempMap.put(value, recordIds);
49+
} else {
50+
recordIds.add(i);
51+
}
52+
}
53+
index = new Long2ObjectAVLTreeMap<>(tempMap);
54+
}
55+
3756
/**
3857
* Returns a bitmap containing row numbers of all cells matching the given long
3958
*
@@ -46,7 +65,7 @@ public Selection get(long value) {
4665
return selection;
4766
}
4867

49-
public Selection atLeast(int value) {
68+
public Selection atLeast(long value) {
5069
Selection selection = new BitmapBackedSelection();
5170
Long2ObjectSortedMap<IntArrayList> tail = index.tailMap(value);
5271
for (IntArrayList keys : tail.values()) {
@@ -55,7 +74,7 @@ public Selection atLeast(int value) {
5574
return selection;
5675
}
5776

58-
public Selection greaterThan(int value) {
77+
public Selection greaterThan(long value) {
5978
Selection selection = new BitmapBackedSelection();
6079
Long2ObjectSortedMap<IntArrayList> tail = index.tailMap(value + 1);
6180
for (IntArrayList keys : tail.values()) {
@@ -64,7 +83,7 @@ public Selection greaterThan(int value) {
6483
return selection;
6584
}
6685

67-
public Selection atMost(int value) {
86+
public Selection atMost(long value) {
6887
Selection selection = new BitmapBackedSelection();
6988
Long2ObjectSortedMap<IntArrayList> head = index.headMap(value + 1); // we add 1 to get values equal to the arg
7089
for (IntArrayList keys : head.values()) {
@@ -73,7 +92,7 @@ public Selection atMost(int value) {
7392
return selection;
7493
}
7594

76-
public Selection lessThan(int value) {
95+
public Selection lessThan(long value) {
7796
Selection selection = new BitmapBackedSelection();
7897
Long2ObjectSortedMap<IntArrayList> head = index.headMap(value); // we add 1 to get values equal to the arg
7998
for (IntArrayList keys : head.values()) {
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
package com.github.lwhite1.tablesaw.index;
2+
3+
import com.github.lwhite1.tablesaw.api.TimeColumn;
4+
import com.github.lwhite1.tablesaw.columns.packeddata.PackedLocalTime;
5+
import com.github.lwhite1.tablesaw.util.Selection;
6+
7+
import java.time.LocalTime;
8+
9+
/**
10+
* An index for four-byte integer and Date columns
11+
*/
12+
public class TimeIndex {
13+
14+
private final IntIndex index;
15+
16+
public TimeIndex(TimeColumn column) {
17+
index = new IntIndex(column);
18+
}
19+
20+
/**
21+
* Returns a bitmap containing row numbers of all cells matching the given int
22+
*
23+
* @param value This is a 'key' from the index perspective, meaning it is a value from the standpoint of the column
24+
*/
25+
public Selection get(LocalTime value) {
26+
return index.get(PackedLocalTime.pack(value));
27+
}
28+
29+
public Selection atLeast(LocalTime value) {
30+
return index.atLeast(PackedLocalTime.pack(value));
31+
}
32+
33+
public Selection greaterThan(LocalTime value) {
34+
return index.greaterThan(PackedLocalTime.pack(value));
35+
}
36+
37+
public Selection atMost(LocalTime value) {
38+
return index.atMost(PackedLocalTime.pack(value));
39+
}
40+
41+
public Selection lessThan(LocalTime value) {
42+
return index.lessThan(PackedLocalTime.pack(value));
43+
}
44+
}

src/test/java/com/github/lwhite1/tablesaw/index/IntIndexTest.java

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,17 @@
22

33
import com.github.lwhite1.tablesaw.api.ColumnType;
44
import com.github.lwhite1.tablesaw.api.Table;
5+
import com.github.lwhite1.tablesaw.columns.DateColumnUtils;
56
import com.github.lwhite1.tablesaw.columns.IntColumnUtils;
7+
import com.github.lwhite1.tablesaw.columns.packeddata.PackedLocalDate;
68
import com.github.lwhite1.tablesaw.io.csv.CsvReader;
79
import com.github.lwhite1.tablesaw.util.Selection;
810
import com.google.common.base.Stopwatch;
911
import org.junit.Before;
1012
import org.junit.Test;
1113

14+
import java.time.LocalDate;
15+
1216
import static com.github.lwhite1.tablesaw.api.ColumnType.*;
1317
import static org.junit.Assert.assertEquals;
1418

@@ -24,13 +28,15 @@ public class IntIndexTest {
2428
};
2529

2630
private IntIndex index;
31+
private DateIndex dateIndex;
2732
private Table table;
2833

2934
@Before
3035
public void setUp() throws Exception {
3136
Stopwatch stopwatch = Stopwatch.createStarted();
3237
table = CsvReader.read(types, "data/BushApproval.csv");
3338
index = new IntIndex(table.intColumn("approval"));
39+
dateIndex = new DateIndex(table.dateColumn("date"));
3440
}
3541

3642
@Test
@@ -40,13 +46,31 @@ public void testGet() {
4046
assertEquals(fromCol, fromIdx);
4147
}
4248

49+
@Test
50+
public void testGet2() {
51+
LocalDate date = LocalDate.of(2001, 12, 12);
52+
int packedDate = PackedLocalDate.pack(date);
53+
Selection fromCol = table.dateColumn("date").select(DateColumnUtils.isEqualTo, packedDate);
54+
Selection fromIdx = dateIndex.get(date);
55+
assertEquals(fromCol, fromIdx);
56+
}
57+
4358
@Test
4459
public void testGTE() {
4560
Selection fromCol = table.intColumn("approval").select(IntColumnUtils.isGreaterThanOrEqualTo, 71);
4661
Selection fromIdx = index.atLeast(71);
4762
assertEquals(fromCol, fromIdx);
4863
}
4964

65+
@Test
66+
public void testGTE2() {
67+
LocalDate date = LocalDate.of(2001, 12, 12);
68+
int packedDate = PackedLocalDate.pack(date);
69+
Selection fromCol = table.dateColumn("date").select(DateColumnUtils.isGreaterThanOrEqualTo, packedDate);
70+
Selection fromIdx = dateIndex.atLeast(date);
71+
assertEquals(fromCol, fromIdx);
72+
}
73+
5074
@Test
5175
public void testLTE() {
5276
Selection fromCol = table.intColumn("approval").select(IntColumnUtils.isLessThanOrEqualTo, 71);

0 commit comments

Comments
 (0)