Skip to content

Rename RefPredicate to InPredicate and change ref to in. #1405

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Oct 29, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,5 @@

namespace Microsoft.ML.Runtime.Data
{
public delegate bool RefPredicate<T>(ref T value);
public delegate bool InPredicate<T>(in T value);
}
6 changes: 3 additions & 3 deletions src/Microsoft.ML.Core/Utilities/VBufferUtils.cs
Original file line number Diff line number Diff line change
Expand Up @@ -492,7 +492,7 @@ public static void DensifyFirst<T>(ref VBuffer<T> dst, int denseCount)
/// Creates a maybe sparse copy of a VBuffer.
/// Whether the created copy is sparse or not is determined by the proportion of non-default entries compared to the sparsity parameter.
/// </summary>
public static void CreateMaybeSparseCopy<T>(ref VBuffer<T> src, ref VBuffer<T> dst, RefPredicate<T> isDefaultPredicate, float sparsityThreshold = SparsityThreshold)
public static void CreateMaybeSparseCopy<T>(ref VBuffer<T> src, ref VBuffer<T> dst, InPredicate<T> isDefaultPredicate, float sparsityThreshold = SparsityThreshold)
{
Contracts.CheckParam(0 < sparsityThreshold && sparsityThreshold < 1, nameof(sparsityThreshold));
if (!src.IsDense || src.Length < 20)
Expand All @@ -505,7 +505,7 @@ public static void CreateMaybeSparseCopy<T>(ref VBuffer<T> src, ref VBuffer<T> d
var sparseCountThreshold = (int)(src.Length * sparsityThreshold);
for (int i = 0; i < src.Length; i++)
{
if (!isDefaultPredicate(ref src.Values[i]))
if (!isDefaultPredicate(in src.Values[i]))
sparseCount++;

if (sparseCount > sparseCountThreshold)
Expand All @@ -527,7 +527,7 @@ public static void CreateMaybeSparseCopy<T>(ref VBuffer<T> src, ref VBuffer<T> d
int j = 0;
for (int i = 0; i < src.Length; i++)
{
if (!isDefaultPredicate(ref src.Values[i]))
if (!isDefaultPredicate(in src.Values[i]))
{
Contracts.Assert(j < sparseCount);
indices[j] = i;
Expand Down
194 changes: 97 additions & 97 deletions src/Microsoft.ML.Data/Data/Conversion.cs

Large diffs are not rendered by default.

4 changes: 2 additions & 2 deletions src/Microsoft.ML.Data/DataLoadSave/Text/TextLoaderParser.cs
Original file line number Diff line number Diff line change
Expand Up @@ -258,7 +258,7 @@ public override bool Consume(int irow, int index, ref ReadOnlyMemory<char> text)
{
Contracts.Assert(0 <= irow && irow < _values.Length);
Contracts.Assert(index == 0);
return _conv(ref text, out _values[irow]);
return _conv(in text, out _values[irow]);
}

public void Get(ref TResult value)
Expand Down Expand Up @@ -337,7 +337,7 @@ public bool Consume(int index, ref ReadOnlyMemory<char> text)
Contracts.Assert(_indexPrev < index & index < _size);

TItem tmp = default(TItem);
bool f = _conv(ref text, out tmp);
bool f = _conv(in text, out tmp);
if (_count < _size)
{
if (_count < _size / 2)
Expand Down
20 changes: 10 additions & 10 deletions src/Microsoft.ML.Data/DataView/LambdaFilter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ namespace Microsoft.ML.Runtime.Data
public static class LambdaFilter
{
public static IDataView Create<TSrc>(IHostEnvironment env, string name, IDataView input,
string src, ColumnType typeSrc, RefPredicate<TSrc> predicate)
string src, ColumnType typeSrc, InPredicate<TSrc> predicate)
{
Contracts.CheckValue(env, nameof(env));
env.CheckNonEmpty(name, nameof(name));
Expand Down Expand Up @@ -58,7 +58,7 @@ public static IDataView Create<TSrc>(IHostEnvironment env, string name, IDataVie
else
{
Func<IHostEnvironment, string, IDataView, int,
RefPredicate<int>, ValueMapper<int, int>, Impl<int, int>> del = CreateImpl<int, int>;
InPredicate<int>, ValueMapper<int, int>, Impl<int, int>> del = CreateImpl<int, int>;
var meth = del.GetMethodInfo().GetGenericMethodDefinition()
.MakeGenericMethod(typeOrig.RawType, typeof(TSrc));
impl = (IDataView)meth.Invoke(null, new object[] { env, name, input, colSrc, predicate, conv });
Expand All @@ -69,19 +69,19 @@ public static IDataView Create<TSrc>(IHostEnvironment env, string name, IDataVie

private static Impl<T1, T2> CreateImpl<T1, T2>(
IHostEnvironment env, string name, IDataView input, int colSrc,
RefPredicate<T2> pred, ValueMapper<T1, T2> conv)
InPredicate<T2> pred, ValueMapper<T1, T2> conv)
{
return new Impl<T1, T2>(env, name, input, colSrc, pred, conv);
}

private sealed class Impl<T1, T2> : FilterBase
{
private readonly int _colSrc;
private readonly RefPredicate<T2> _pred;
private readonly InPredicate<T2> _pred;
private readonly ValueMapper<T1, T2> _conv;

public Impl(IHostEnvironment env, string name, IDataView input,
int colSrc, RefPredicate<T2> pred, ValueMapper<T1, T2> conv = null)
int colSrc, InPredicate<T2> pred, ValueMapper<T1, T2> conv = null)
: base(env, name, input)
{
Host.AssertValue(pred);
Expand Down Expand Up @@ -150,7 +150,7 @@ private Func<int, bool> GetActive(Func<int, bool> predicate, out bool[] active)
private sealed class RowCursor : LinkedRowFilterCursorBase
{
private readonly ValueGetter<T1> _getSrc;
private readonly RefPredicate<T1> _pred;
private readonly InPredicate<T1> _pred;
private T1 _src;

public RowCursor(Impl<T1, T2> parent, IRowCursor input, bool[] active)
Expand All @@ -160,26 +160,26 @@ public RowCursor(Impl<T1, T2> parent, IRowCursor input, bool[] active)
if (parent._conv == null)
{
Ch.Assert(typeof(T1) == typeof(T2));
_pred = (RefPredicate<T1>)(Delegate)parent._pred;
_pred = (InPredicate<T1>)(Delegate)parent._pred;
}
else
{
T2 val = default(T2);
var pred = parent._pred;
var conv = parent._conv;
_pred =
(ref T1 src) =>
(in T1 src) =>
{
conv(ref _src, ref val);
return pred(ref val);
return pred(in val);
};
}
}

protected override bool Accept()
{
_getSrc(ref _src);
return _pred(ref _src);
return _pred(in _src);
}
}

Expand Down
8 changes: 4 additions & 4 deletions src/Microsoft.ML.Data/DataView/Transposer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -461,7 +461,7 @@ protected override ValueGetter<VBuffer<T>> GetGetterCore()
len++;
Ch.Assert(len <= _len);
getter(ref value);
if (isDefault(ref value))
if (isDefault(in value))
continue;
Utils.EnsureSize(ref indices, ++count);
indices[count - 1] = len;
Expand Down Expand Up @@ -567,7 +567,7 @@ private void EnsureValid()
var type = _view.Schema.GetColumnType(_colCurr);
Ch.Assert(type.ItemType.RawType == typeof(T));
Ch.Assert(type.ValueCount > 0);
RefPredicate<T> isDefault = Conversion.Conversions.Instance.GetIsDefaultPredicate<T>(type.ItemType);
InPredicate<T> isDefault = Conversion.Conversions.Instance.GetIsDefaultPredicate<T>(type.ItemType);
int vecLen = type.ValueCount;
int maxPossibleSize = _rbuff.Length * vecLen;
const int sparseThresholdRatio = 5;
Expand Down Expand Up @@ -619,7 +619,7 @@ private void EnsureValid()
if (rbuff.IsDense)
{
// Store it as sparse. We will densify later, if we must.
if (!isDefault(ref rbuff.Values[s]))
if (!isDefault(in rbuff.Values[s]))
{
indices[_counts[s]] = rowNum;
values[_counts[s]++] = rbuff.Values[s];
Expand All @@ -630,7 +630,7 @@ private void EnsureValid()
int ii = _rbuffIndices[r];
if (ii < rbuff.Count && rbuff.Indices[ii] == s)
{
if (!isDefault(ref rbuff.Values[ii]))
if (!isDefault(in rbuff.Values[ii]))
{
indices[_counts[s]] = rowNum;
values[_counts[s]++] = rbuff.Values[ii];
Expand Down
4 changes: 2 additions & 2 deletions src/Microsoft.ML.Data/Transforms/ConvertTransform.cs
Original file line number Diff line number Diff line change
Expand Up @@ -646,13 +646,13 @@ private TypeNaInfo KindReport<T>(IChannel ch, PrimitiveType type)
ch.Assert(type.IsStandardScalar);

var conv = Conversions.Instance;
RefPredicate<T> isNaDel;
InPredicate<T> isNaDel;
bool hasNaPred = conv.TryGetIsNAPredicate(type, out isNaDel);
bool defaultIsNa = false;
if (hasNaPred)
{
T def = default(T);
defaultIsNa = isNaDel(ref def);
defaultIsNa = isNaDel(in def);
}
return new TypeNaInfo(hasNaPred, defaultIsNa);
}
Expand Down
4 changes: 2 additions & 2 deletions src/Microsoft.ML.Data/Transforms/KeyToValueTransform.cs
Original file line number Diff line number Diff line change
Expand Up @@ -305,7 +305,7 @@ private class KeyToValueMap<TKey, TValue> : KeyToValueMap
private readonly TValue _na;

private readonly bool _naMapsToDefault;
private readonly RefPredicate<TValue> _isDefault;
private readonly InPredicate<TValue> _isDefault;

private readonly ValueMapper<TKey, UInt32> _convertToUInt;

Expand Down Expand Up @@ -441,7 +441,7 @@ public override Delegate GetMappingGetter(IRow input)
// Current slot has an explicitly defined value.
Parent.Host.Assert(islotSrc < srcCount);
MapKey(ref srcValues[islotSrc], ref dstItem);
if (!_isDefault(ref dstItem))
if (!_isDefault(in dstItem))
{
dstValues[islotDst] = dstItem;
dstIndices[islotDst++] = srcIndices[islotSrc];
Expand Down
10 changes: 5 additions & 5 deletions src/Microsoft.ML.Data/Transforms/NAFilter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -328,10 +328,10 @@ private static ValueVec<T> CreateVec<T>(RowCursor cursor, ColInfo info)
private abstract class TypedValue<T> : Value
{
private readonly ValueGetter<T> _getSrc;
private readonly RefPredicate<T> _hasBad;
private readonly InPredicate<T> _hasBad;
public T Src;

protected TypedValue(RowCursor cursor, ValueGetter<T> getSrc, RefPredicate<T> hasBad)
protected TypedValue(RowCursor cursor, ValueGetter<T> getSrc, InPredicate<T> hasBad)
: base(cursor)
{
Contracts.AssertValue(getSrc);
Expand All @@ -343,15 +343,15 @@ protected TypedValue(RowCursor cursor, ValueGetter<T> getSrc, RefPredicate<T> ha
public override bool Refresh()
{
_getSrc(ref Src);
return !_hasBad(ref Src);
return !_hasBad(in Src);
}
}

private sealed class ValueOne<T> : TypedValue<T>
{
private readonly ValueGetter<T> _getter;

public ValueOne(RowCursor cursor, ValueGetter<T> getSrc, RefPredicate<T> hasBad)
public ValueOne(RowCursor cursor, ValueGetter<T> getSrc, InPredicate<T> hasBad)
: base(cursor, getSrc, hasBad)
{
_getter = GetValue;
Expand All @@ -373,7 +373,7 @@ private sealed class ValueVec<T> : TypedValue<VBuffer<T>>
{
private readonly ValueGetter<VBuffer<T>> _getter;

public ValueVec(RowCursor cursor, ValueGetter<VBuffer<T>> getSrc, RefPredicate<VBuffer<T>> hasBad)
public ValueVec(RowCursor cursor, ValueGetter<VBuffer<T>> getSrc, InPredicate<VBuffer<T>> hasBad)
: base(cursor, getSrc, hasBad)
{
_getter = GetValue;
Expand Down
14 changes: 7 additions & 7 deletions src/Microsoft.ML.Data/Transforms/TermTransformImpl.cs
Original file line number Diff line number Diff line change
Expand Up @@ -63,9 +63,9 @@ private static Builder CreateCore<T>(PrimitiveType type, bool sorted)
// If this is a type with NA values, we should ignore those NA values for the purpose
// of building our term dictionary. For the other types (practically, only the UX types),
// we should ignore nothing.
RefPredicate<T> mapsToMissing;
InPredicate<T> mapsToMissing;
if (!Runtime.Data.Conversion.Conversions.Instance.TryGetIsNAPredicate(type, out mapsToMissing))
mapsToMissing = (ref T val) => false;
mapsToMissing = (in T val) => false;
return new Impl<T>(type, mapsToMissing, sorted);
}

Expand Down Expand Up @@ -142,7 +142,7 @@ private sealed class Impl<T> : Builder<T>
{
// Because we can't know the actual mapping till we finish.
private readonly HashArray<T> _values;
private readonly RefPredicate<T> _mapsToMissing;
private readonly InPredicate<T> _mapsToMissing;
private readonly bool _sort;

public override int Count
Expand All @@ -158,7 +158,7 @@ public override int Count
/// to the missing value. If this returns true for a value then we do not attempt
/// to store it in the map.</param>
/// <param name="sort">Indicates whether to sort mapping IDs by input values.</param>
public Impl(PrimitiveType type, RefPredicate<T> mapsToMissing, bool sort)
public Impl(PrimitiveType type, InPredicate<T> mapsToMissing, bool sort)
: base(type)
{
Contracts.Assert(type.RawType == typeof(T));
Expand All @@ -171,7 +171,7 @@ public Impl(PrimitiveType type, RefPredicate<T> mapsToMissing, bool sort)

public override bool TryAdd(ref T val)
{
return !_mapsToMissing(ref val) && _values.TryAdd(val);
return !_mapsToMissing(in val) && _values.TryAdd(val);
}

public override TermMap Finish()
Expand Down Expand Up @@ -212,7 +212,7 @@ public override void ParseAddTermArg(ref ReadOnlyMemory<char> terms, IChannel ch
term = ReadOnlyMemoryUtils.TrimSpaces(term);
if (term.IsEmpty)
ch.Warning("Empty strings ignored in 'terms' specification");
else if (!tryParse(ref term, out val))
else if (!tryParse(in term, out val))
ch.Warning("Item '{0}' ignored in 'terms' specification since it could not be parsed as '{1}'", term, ItemType);
else if (!TryAdd(ref val))
ch.Warning("Duplicate item '{0}' ignored in 'terms' specification", term);
Expand All @@ -237,7 +237,7 @@ public override void ParseAddTermArg(string[] terms, IChannel ch)
term = ReadOnlyMemoryUtils.TrimSpaces(term);
if (term.IsEmpty)
ch.Warning("Empty strings ignored in 'term' specification");
else if (!tryParse(ref term, out val))
else if (!tryParse(in term, out val))
ch.Warning("Item '{0}' ignored in 'term' specification since it could not be parsed as '{1}'", term, ItemType);
else if (!TryAdd(ref val))
ch.Warning("Duplicate item '{0}' ignored in 'term' specification", term);
Expand Down
2 changes: 1 addition & 1 deletion src/Microsoft.ML.FastTree/FastTree.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1488,7 +1488,7 @@ private Dataset Construct(RoleMappedData examples, ref int numExamples, int maxB
out BinUpperBounds[iFeature]);
}
else
hasMissing = hasMissingPred(ref temp);
hasMissing = hasMissingPred(in temp);

if (hasMissing)
{
Expand Down
4 changes: 2 additions & 2 deletions src/Microsoft.ML.FastTree/TreeEnsembleFeaturizer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -747,7 +747,7 @@ private static IDataView AppendFloatMapper<TInput>(IHostEnvironment env, IChanne
mapper =
(ref TInput src, ref Single dst) =>
{
if (isNa(ref src))
if (isNa(in src))
{
dst = Single.NaN;
return;
Expand All @@ -763,7 +763,7 @@ private static IDataView AppendFloatMapper<TInput>(IHostEnvironment env, IChanne
mapper =
(ref TInput src, ref Single dst) =>
{
if (isNa(ref src))
if (isNa(in src))
{
dst = Single.NaN;
return;
Expand Down
6 changes: 3 additions & 3 deletions src/Microsoft.ML.PipelineInference/ColumnTypeInference.cs
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,7 @@ public void Apply(IntermediateColumn[] columns)
.All(x =>
{
bool value;
return Conversions.Instance.TryParse(ref x, out value);
return Conversions.Instance.TryParse(in x, out value);
})
)
{
Expand All @@ -144,7 +144,7 @@ public void Apply(IntermediateColumn[] columns)
col.SuggestedType = BoolType.Instance;
bool first;

col.HasHeader = !Conversions.Instance.TryParse(ref col.RawData[0], out first);
col.HasHeader = !Conversions.Instance.TryParse(in col.RawData[0], out first);
}
}
}
Expand All @@ -159,7 +159,7 @@ public void Apply(IntermediateColumn[] columns)
.All(x =>
{
Single value;
return Conversions.Instance.TryParse(ref x, out value);
return Conversions.Instance.TryParse(in x, out value);
})
)
{
Expand Down
Loading