/
StringDataFrameColumn.cs
520 lines (471 loc) · 21.4 KB
/
StringDataFrameColumn.cs
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
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.
using System;
using System.Collections;
using System.Collections.Generic;
using System.Diagnostics;
using Microsoft.ML;
using Microsoft.ML.Data;
namespace Microsoft.Data.Analysis
{
/// <summary>
/// A mutable column to hold strings
/// </summary>
/// <remarks> Is NOT Arrow compatible </remarks>
public partial class StringDataFrameColumn : DataFrameColumn, IEnumerable<string>
{
private List<List<string>> _stringBuffers = new List<List<string>>(); // To store more than intMax number of strings
public StringDataFrameColumn(string name, long length = 0) : base(name, length, typeof(string))
{
int numberOfBuffersRequired = Math.Max((int)(length / int.MaxValue), 1);
for (int i = 0; i < numberOfBuffersRequired; i++)
{
long bufferLen = length - _stringBuffers.Count * int.MaxValue;
List<string> buffer = new List<string>((int)Math.Min(int.MaxValue, bufferLen));
_stringBuffers.Add(buffer);
for (int j = 0; j < bufferLen; j++)
{
buffer.Add(default);
}
}
}
public StringDataFrameColumn(string name, IEnumerable<string> values) : base(name, 0, typeof(string))
{
values = values ?? throw new ArgumentNullException(nameof(values));
if (_stringBuffers.Count == 0)
{
_stringBuffers.Add(new List<string>());
}
foreach (var value in values)
{
Append(value);
}
}
private long _nullCount;
public override long NullCount => _nullCount;
protected internal override void Resize(long length)
{
if (length < Length)
throw new ArgumentException(Strings.CannotResizeDown, nameof(length));
for (long i = Length; i < length; i++)
{
Append(null);
}
}
public void Append(string value)
{
List<string> lastBuffer = _stringBuffers[_stringBuffers.Count - 1];
if (lastBuffer.Count == int.MaxValue)
{
lastBuffer = new List<string>();
_stringBuffers.Add(lastBuffer);
}
lastBuffer.Add(value);
if (value == null)
_nullCount++;
Length++;
}
private int GetBufferIndexContainingRowIndex(ref long rowIndex)
{
if (rowIndex > Length)
{
throw new ArgumentOutOfRangeException(Strings.ColumnIndexOutOfRange, nameof(rowIndex));
}
return (int)(rowIndex / int.MaxValue);
}
protected override object GetValue(long rowIndex)
{
int bufferIndex = GetBufferIndexContainingRowIndex(ref rowIndex);
return _stringBuffers[bufferIndex][(int)rowIndex];
}
protected override IReadOnlyList<object> GetValues(long startIndex, int length)
{
var ret = new List<object>();
int bufferIndex = GetBufferIndexContainingRowIndex(ref startIndex);
while (ret.Count < length && bufferIndex < _stringBuffers.Count)
{
for (int i = (int)startIndex; ret.Count < length && i < _stringBuffers[bufferIndex].Count; i++)
{
ret.Add(_stringBuffers[bufferIndex][i]);
}
bufferIndex++;
startIndex = 0;
}
return ret;
}
protected override void SetValue(long rowIndex, object value)
{
if (value == null || value is string)
{
int bufferIndex = GetBufferIndexContainingRowIndex(ref rowIndex);
var oldValue = this[rowIndex];
_stringBuffers[bufferIndex][(int)rowIndex] = (string)value;
if (oldValue != (string)value)
{
if (value == null)
_nullCount++;
if (oldValue == null && _nullCount > 0)
_nullCount--;
}
}
else
{
throw new ArgumentException(string.Format(Strings.MismatchedValueType, typeof(string)), nameof(value));
}
}
public new string this[long rowIndex]
{
get => (string)GetValue(rowIndex);
set => SetValue(rowIndex, value);
}
public new List<string> this[long startIndex, int length]
{
get
{
var ret = new List<string>();
int bufferIndex = GetBufferIndexContainingRowIndex(ref startIndex);
while (ret.Count < length && bufferIndex < _stringBuffers.Count)
{
for (int i = (int)startIndex; ret.Count < length && i < _stringBuffers[bufferIndex].Count; i++)
{
ret.Add(_stringBuffers[bufferIndex][i]);
}
bufferIndex++;
startIndex = 0;
}
return ret;
}
}
public IEnumerator<string> GetEnumerator()
{
foreach (List<string> buffer in _stringBuffers)
{
foreach (string value in buffer)
{
yield return value;
}
}
}
protected override IEnumerator GetEnumeratorCore() => GetEnumerator();
public override DataFrameColumn Clamp<U>(U min, U max, bool inPlace = false) => throw new NotSupportedException();
public override DataFrameColumn Filter<U>(U min, U max) => throw new NotSupportedException();
public new StringDataFrameColumn Sort(bool ascending = true)
{
PrimitiveDataFrameColumn<long> columnSortIndices = GetAscendingSortIndices(out Int64DataFrameColumn _);
return Clone(columnSortIndices, !ascending, NullCount);
}
internal override PrimitiveDataFrameColumn<long> GetAscendingSortIndices(out Int64DataFrameColumn nullIndices)
{
PrimitiveDataFrameColumn<long> columnSortIndices = GetSortIndices(Comparer<string>.Default, out nullIndices);
return columnSortIndices;
}
private PrimitiveDataFrameColumn<long> GetSortIndices(Comparer<string> comparer, out Int64DataFrameColumn columnNullIndices)
{
List<int[]> bufferSortIndices = new List<int[]>(_stringBuffers.Count);
columnNullIndices = new Int64DataFrameColumn("NullIndices", NullCount);
long nullIndicesSlot = 0;
foreach (List<string> buffer in _stringBuffers)
{
var sortIndices = new int[buffer.Count];
for (int i = 0; i < buffer.Count; i++)
{
sortIndices[i] = i;
if (buffer[i] == null)
{
columnNullIndices[nullIndicesSlot] = i + bufferSortIndices.Count * int.MaxValue;
nullIndicesSlot++;
}
}
// TODO: Refactor the sort routine to also work with IList?
string[] array = buffer.ToArray();
IntrospectiveSort(array, array.Length, sortIndices, comparer);
bufferSortIndices.Add(sortIndices);
}
// Simple merge sort to build the full column's sort indices
ValueTuple<string, int> GetFirstNonNullValueStartingAtIndex(int stringBufferIndex, int startIndex)
{
string value = _stringBuffers[stringBufferIndex][bufferSortIndices[stringBufferIndex][startIndex]];
while (value == null && ++startIndex < bufferSortIndices[stringBufferIndex].Length)
{
value = _stringBuffers[stringBufferIndex][bufferSortIndices[stringBufferIndex][startIndex]];
}
return (value, startIndex);
}
SortedDictionary<string, List<ValueTuple<int, int>>> heapOfValueAndListOfTupleOfSortAndBufferIndex = new SortedDictionary<string, List<ValueTuple<int, int>>>(comparer);
List<List<string>> buffers = _stringBuffers;
for (int i = 0; i < buffers.Count; i++)
{
List<string> buffer = buffers[i];
ValueTuple<string, int> valueAndBufferSortIndex = GetFirstNonNullValueStartingAtIndex(i, 0);
if (valueAndBufferSortIndex.Item1 == null)
{
// All nulls
continue;
}
if (heapOfValueAndListOfTupleOfSortAndBufferIndex.ContainsKey(valueAndBufferSortIndex.Item1))
{
heapOfValueAndListOfTupleOfSortAndBufferIndex[valueAndBufferSortIndex.Item1].Add((valueAndBufferSortIndex.Item2, i));
}
else
{
heapOfValueAndListOfTupleOfSortAndBufferIndex.Add(valueAndBufferSortIndex.Item1, new List<ValueTuple<int, int>>() { (valueAndBufferSortIndex.Item2, i) });
}
}
PrimitiveDataFrameColumn<long> columnSortIndices = new PrimitiveDataFrameColumn<long>("SortIndices");
GetBufferSortIndex getBufferSortIndex = new GetBufferSortIndex((int bufferIndex, int sortIndex) => (bufferSortIndices[bufferIndex][sortIndex]) + bufferIndex * bufferSortIndices[0].Length);
GetValueAndBufferSortIndexAtBuffer<string> getValueAtBuffer = new GetValueAndBufferSortIndexAtBuffer<string>((int bufferIndex, int sortIndex) => GetFirstNonNullValueStartingAtIndex(bufferIndex, sortIndex));
GetBufferLengthAtIndex getBufferLengthAtIndex = new GetBufferLengthAtIndex((int bufferIndex) => bufferSortIndices[bufferIndex].Length);
PopulateColumnSortIndicesWithHeap(heapOfValueAndListOfTupleOfSortAndBufferIndex, columnSortIndices, getBufferSortIndex, getValueAtBuffer, getBufferLengthAtIndex);
return columnSortIndices;
}
public new StringDataFrameColumn Clone(DataFrameColumn mapIndices, bool invertMapIndices, long numberOfNullsToAppend)
{
StringDataFrameColumn clone;
if (!(mapIndices is null))
{
Type dataType = mapIndices.DataType;
if (dataType != typeof(long) && dataType != typeof(int) && dataType != typeof(bool))
throw new ArgumentException(String.Format(Strings.MultipleMismatchedValueType, typeof(long), typeof(int), typeof(bool)), nameof(mapIndices));
if (mapIndices.DataType == typeof(long))
clone = Clone(mapIndices as PrimitiveDataFrameColumn<long>, invertMapIndices);
else if (dataType == typeof(int))
clone = Clone(mapIndices as PrimitiveDataFrameColumn<int>, invertMapIndices);
else
clone = Clone(mapIndices as PrimitiveDataFrameColumn<bool>);
}
else
{
clone = Clone();
}
for (long i = 0; i < numberOfNullsToAppend; i++)
{
clone.Append(null);
}
return clone;
}
protected override DataFrameColumn CloneImplementation(DataFrameColumn mapIndices = null, bool invertMapIndices = false, long numberOfNullsToAppend = 0)
{
return Clone(mapIndices, invertMapIndices, numberOfNullsToAppend);
}
private StringDataFrameColumn Clone(PrimitiveDataFrameColumn<bool> boolColumn)
{
if (boolColumn.Length > Length)
throw new ArgumentException(Strings.MapIndicesExceedsColumnLenth, nameof(boolColumn));
StringDataFrameColumn ret = new StringDataFrameColumn(Name, 0);
for (long i = 0; i < boolColumn.Length; i++)
{
bool? value = boolColumn[i];
if (value.HasValue && value.Value == true)
ret.Append(this[i]);
}
return ret;
}
private StringDataFrameColumn CloneImplementation<U>(PrimitiveDataFrameColumn<U> mapIndices, bool invertMapIndices = false)
where U : unmanaged
{
mapIndices = mapIndices ?? throw new ArgumentNullException(nameof(mapIndices));
StringDataFrameColumn ret = new StringDataFrameColumn(Name, mapIndices.Length);
List<string> setBuffer = ret._stringBuffers[0];
long setBufferMinRange = 0;
long setBufferMaxRange = int.MaxValue;
List<string> getBuffer = _stringBuffers[0];
long getBufferMinRange = 0;
long getBufferMaxRange = int.MaxValue;
long maxCapacity = int.MaxValue;
if (mapIndices.DataType == typeof(long))
{
PrimitiveDataFrameColumn<long> longMapIndices = mapIndices as PrimitiveDataFrameColumn<long>;
longMapIndices.ApplyElementwise((long? mapIndex, long rowIndex) =>
{
long index = rowIndex;
if (invertMapIndices)
index = longMapIndices.Length - 1 - index;
if (index < setBufferMinRange || index >= setBufferMaxRange)
{
int bufferIndex = (int)(index / maxCapacity);
setBuffer = ret._stringBuffers[bufferIndex];
setBufferMinRange = bufferIndex * maxCapacity;
setBufferMaxRange = (bufferIndex + 1) * maxCapacity;
}
index -= setBufferMinRange;
if (mapIndex == null)
{
setBuffer[(int)index] = null;
ret._nullCount++;
return mapIndex;
}
if (mapIndex.Value < getBufferMinRange || mapIndex.Value >= getBufferMaxRange)
{
int bufferIndex = (int)(mapIndex.Value / maxCapacity);
getBuffer = _stringBuffers[bufferIndex];
getBufferMinRange = bufferIndex * maxCapacity;
getBufferMaxRange = (bufferIndex + 1) * maxCapacity;
}
int bufferLocalMapIndex = (int)(mapIndex - getBufferMinRange);
string value = getBuffer[bufferLocalMapIndex];
setBuffer[(int)index] = value;
if (value == null)
ret._nullCount++;
return mapIndex;
});
}
else if (mapIndices.DataType == typeof(int))
{
PrimitiveDataFrameColumn<int> intMapIndices = mapIndices as PrimitiveDataFrameColumn<int>;
intMapIndices.ApplyElementwise((int? mapIndex, long rowIndex) =>
{
long index = rowIndex;
if (invertMapIndices)
index = intMapIndices.Length - 1 - index;
if (mapIndex == null)
{
setBuffer[(int)index] = null;
ret._nullCount++;
return mapIndex;
}
string value = getBuffer[mapIndex.Value];
setBuffer[(int)index] = value;
if (value == null)
ret._nullCount++;
return mapIndex;
});
}
else
{
Debug.Assert(false, nameof(mapIndices.DataType));
}
return ret;
}
private StringDataFrameColumn Clone(PrimitiveDataFrameColumn<long> mapIndices = null, bool invertMapIndex = false)
{
if (mapIndices is null)
{
StringDataFrameColumn ret = new StringDataFrameColumn(Name, mapIndices is null ? Length : mapIndices.Length);
for (long i = 0; i < Length; i++)
{
ret[i] = this[i];
}
return ret;
}
else
{
return CloneImplementation(mapIndices, invertMapIndex);
}
}
private StringDataFrameColumn Clone(PrimitiveDataFrameColumn<int> mapIndices, bool invertMapIndex = false)
{
return CloneImplementation(mapIndices, invertMapIndex);
}
internal static DataFrame ValueCountsImplementation(Dictionary<string, ICollection<long>> groupedValues)
{
StringDataFrameColumn keys = new StringDataFrameColumn("Values", 0);
PrimitiveDataFrameColumn<long> counts = new PrimitiveDataFrameColumn<long>("Counts");
foreach (KeyValuePair<string, ICollection<long>> keyValuePair in groupedValues)
{
keys.Append(keyValuePair.Key);
counts.Append(keyValuePair.Value.Count);
}
return new DataFrame(new List<DataFrameColumn> { keys, counts });
}
public override DataFrame ValueCounts()
{
Dictionary<string, ICollection<long>> groupedValues = GroupColumnValues<string>(out HashSet<long> _);
return ValueCountsImplementation(groupedValues);
}
public override GroupBy GroupBy(int columnIndex, DataFrame parent)
{
Dictionary<string, ICollection<long>> dictionary = GroupColumnValues<string>(out HashSet<long> _);
return new GroupBy<string>(parent, columnIndex, dictionary);
}
public override Dictionary<TKey, ICollection<long>> GroupColumnValues<TKey>(out HashSet<long> nullIndices)
{
if (typeof(TKey) == typeof(string))
{
Dictionary<string, ICollection<long>> multimap = new Dictionary<string, ICollection<long>>(EqualityComparer<string>.Default);
nullIndices = new HashSet<long>();
for (long i = 0; i < Length; i++)
{
string str = this[i];
if (str != null)
{
bool containsKey = multimap.TryGetValue(str, out ICollection<long> values);
if (containsKey)
{
values.Add(i);
}
else
{
multimap.Add(str, new List<long>() { i });
}
}
else
{
nullIndices.Add(i);
}
}
return multimap as Dictionary<TKey, ICollection<long>>;
}
else
{
throw new NotImplementedException(nameof(TKey));
}
}
public StringDataFrameColumn FillNulls(string value, bool inPlace = false)
{
if (value == null)
throw new ArgumentNullException(nameof(value));
StringDataFrameColumn column = inPlace ? this : Clone();
for (long i = 0; i < column.Length; i++)
{
if (column[i] == null)
column[i] = value;
}
return column;
}
protected override DataFrameColumn FillNullsImplementation(object value, bool inPlace)
{
if (value is string valueString)
return FillNulls(valueString, inPlace);
else
throw new ArgumentException(String.Format(Strings.MismatchedValueType, typeof(string)), nameof(value));
}
protected internal override void AddDataViewColumn(DataViewSchema.Builder builder)
{
builder.AddColumn(Name, TextDataViewType.Instance);
}
protected internal override Delegate GetDataViewGetter(DataViewRowCursor cursor)
{
return CreateValueGetterDelegate(cursor);
}
private ValueGetter<ReadOnlyMemory<char>> CreateValueGetterDelegate(DataViewRowCursor cursor) =>
(ref ReadOnlyMemory<char> value) => value = this[cursor.Position].AsMemory();
protected internal override void AddValueUsingCursor(DataViewRowCursor cursor, Delegate getter)
{
long row = cursor.Position;
ReadOnlyMemory<char> value = default;
Debug.Assert(getter != null, "Excepted getter to be valid");
(getter as ValueGetter<ReadOnlyMemory<char>>)(ref value);
if (Length > row)
{
this[row] = value.ToString();
}
else if (Length == row)
{
Append(value.ToString());
}
else
{
throw new IndexOutOfRangeException(nameof(row));
}
}
protected internal override Delegate GetValueGetterUsingCursor(DataViewRowCursor cursor, DataViewSchema.Column schemaColumn)
{
return cursor.GetGetter<ReadOnlyMemory<char>>(schemaColumn);
}
public override Dictionary<long, ICollection<long>> GetGroupedOccurrences(DataFrameColumn other, out HashSet<long> otherColumnNullIndices)
{
return GetGroupedOccurrences<string>(other, out otherColumnNullIndices);
}
}
}