forked from paulfitz/daff
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathIndex.hx
104 lines (95 loc) · 2.98 KB
/
Index.hx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
// -*- mode:java; tab-width:4; c-basic-offset:4; indent-tabs-mode:nil -*-
#if !TOPLEVEL
package coopy;
#end
@:noDoc
class Index {
public var items : Map<String,IndexItem>;
public var keys : Array<String>;
public var top_freq : Int;
public var height : Int;
private var cols : Array<Int>;
private var v : View;
private var indexed_table : Table;
private var hdr : Int;
private var ignore_whitespace : Bool;
private var ignore_case : Bool;
public function new(flags : CompareFlags) : Void {
items = new Map<String,IndexItem>();
cols = new Array<Int>();
keys = new Array<String>();
top_freq = 0;
height = 0;
hdr = 0;
ignore_whitespace = false;
ignore_case = false;
if (flags!=null) {
ignore_whitespace = flags.ignore_whitespace;
ignore_case = flags.ignore_case;
}
}
public function addColumn(i: Int) : Void {
cols.push(i);
}
public function indexTable(t: Table, hdr: Int) : Void {
indexed_table = t;
this.hdr = hdr;
if (keys.length!=t.height && t.height>0) {
// preallocate array, helpful for php
keys[t.height-1] = null;
}
for (i in 0...t.height) {
var key : String = keys[i];
if (key==null) {
key = toKey(t,i);
keys[i] = key;
}
var item : IndexItem = items.get(key);
if (item==null) {
item = new IndexItem();
items.set(key,item);
}
var ct : Int = item.add(i);
if (ct>top_freq) top_freq = ct;
}
height = t.height;
}
public function toKey(t: Table,
i: Int) : String {
var wide : String = (i<hdr)?"_":"";
if (v==null) v = t.getCellView();
for (k in 0...cols.length) {
var d : Dynamic = t.getCell(cols[k],i);
var txt : String = v.toString(d);
if (ignore_whitespace) {
txt = StringTools.trim(txt);
}
if (ignore_case) {
txt = txt.toLowerCase();
}
if (k>0) wide += " // ";
if (txt==null || txt=="" || txt=="null" || txt=="undefined") continue;
wide += txt;
}
return wide;
}
public function toKeyByContent(row: Row) : String {
var wide : String = row.isPreamble()?"_":"";
for (k in 0...cols.length) {
var txt : String = row.getRowString(cols[k]);
if (ignore_whitespace) {
txt = StringTools.trim(txt);
}
if (ignore_case) {
txt = txt.toLowerCase();
}
if (k>0) wide += " // ";
if (txt==null || txt=="" || txt=="null" || txt=="undefined") continue;
wide += txt;
}
return wide;
}
public function getTable() : Table {
return indexed_table;
}
}