Skip to content

Commit

Permalink
Merge pull request #11 from iamcarbon/net8.0
Browse files Browse the repository at this point in the history
Target net8.0 and other improvements.
  • Loading branch information
PetroProtsyk authored Dec 24, 2023
2 parents 2b97c48 + dfc8b77 commit 17bf39b
Show file tree
Hide file tree
Showing 17 changed files with 69 additions and 129 deletions.
3 changes: 1 addition & 2 deletions .github/workflows/dotnet-core.yml
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,7 @@ jobs:
uses: actions/setup-dotnet@v3
with:
dotnet-version: |
6.0.x
7.0.x
8.0.x
- name: Install dependencies
run: dotnet restore ./Src/
- name: Build
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net6.0</TargetFramework>
<TargetFramework>net8.0</TargetFramework>
<Authors>Petro Protsyk</Authors>
<Product>PMS FullText Search</Product>
<GenerateAssemblyInfo>false</GenerateAssemblyInfo>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ public void BtreeP_Append_Test()

[Theory]
[InlineData(1000)]
public string TestBtreeP(int size)
public void TestBtreeP(int size)
{
var rnd = Enumerable.Range(1, size).ToArray().Shuffle();
var testFile = Path.Combine(TestFolder, "btree-test.bin");
Expand Down Expand Up @@ -94,7 +94,5 @@ public string TestBtreeP(int size)
Assert.False(tree.ContainsKey(i));
}
}

return testFile;
}
}
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>net6.0</TargetFramework>
<TargetFramework>net8.0</TargetFramework>
<IsPackable>false</IsPackable>
<GenerateAssemblyInfo>false</GenerateAssemblyInfo>
<ImplicitUsings>enable</ImplicitUsings>
Expand All @@ -12,9 +12,9 @@
</ItemGroup>

<ItemGroup>
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.0.0" />
<PackageReference Include="xunit" Version="2.4.1" />
<PackageReference Include="xunit.runner.visualstudio" Version="2.4.3">
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.8.0" />
<PackageReference Include="xunit" Version="2.6.4" />
<PackageReference Include="xunit.runner.visualstudio" Version="2.5.6">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
Expand Down
51 changes: 17 additions & 34 deletions Src/Protsyk.PMS.FullText.Core/Automata/FST.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,6 @@
using Protsyk.PMS.FullText.Core.Common;
using Protsyk.PMS.FullText.Core.Common.Persistance;

using StringComparer = System.StringComparer;

namespace Protsyk.PMS.FullText.Core.Automata;

public static class FSTExtensions
Expand Down Expand Up @@ -94,10 +92,10 @@ private sealed class StateWithTransitions

public int Id { get; private set; }

// When state is fronzen. Offset in the output file.
// When state is frozen. Offset in the output file.
public long Offset { get; set; }

public bool IsFronzen { get; set; }
public bool IsFrozen { get; set; }

public bool IsFinal { get; set; }

Expand All @@ -106,7 +104,7 @@ private sealed class StateWithTransitions
public StateWithTransitions()
{
Id = Interlocked.Increment(ref NextId);
IsFronzen = false;
IsFrozen = false;
Offset = 0;

IsFinal = false;
Expand Down Expand Up @@ -169,8 +167,9 @@ private readonly struct Transition

private static StateWithTransitions CopyOf(StateWithTransitions s)
{
var t = new StateWithTransitions();
t.IsFinal = s.IsFinal;
var t = new StateWithTransitions {
IsFinal = s.IsFinal
};
t.Arcs.AddRange(s.Arcs);
return t;
}
Expand All @@ -180,15 +179,15 @@ private StateWithTransitions FreezeState(StateWithTransitions s)
//TODO: Frozen state should only have toStateOffset in Arcs
// therefore other type
var r = CopyOf(s);
r.IsFronzen = true;
r.IsFrozen = true;
r.Offset = WriteState(r);
frozenStates.Add(r.Id, r);
return r;
}

private long WriteState(StateWithTransitions s)
{
if (!s.IsFronzen) throw new Exception("What?");
if (!s.IsFrozen) throw new Exception("What?");

long startOffset = storage.Length;
var size = 0;
Expand Down Expand Up @@ -346,7 +345,7 @@ private StateWithTransitions FindMinimized(StateWithTransitions s)
{
if (!minimalTransducerStatesDictionary.TryGetValue(dedupHash, out var listToAdd))
{
listToAdd = new List<LinkedListNode<int>>();
listToAdd = [];
minimalTransducerStatesDictionary.Add(dedupHash, listToAdd);
}
listToAdd.Add(usageQueue.AddFirst(r.Id));
Expand All @@ -358,7 +357,7 @@ private StateWithTransitions FindMinimized(StateWithTransitions s)

private void SetTransition(StateWithTransitions from, char c, StateWithTransitions to)
{
if (from.IsFronzen) throw new Exception("What?");
if (from.IsFrozen) throw new Exception("What?");

for (int i = 0; i < from.Arcs.Count; ++i)
{
Expand Down Expand Up @@ -398,7 +397,7 @@ private static T GetOutput(StateWithTransitions from, char c)

private static void SetOutput(StateWithTransitions from, char c, T output)
{
if (from.IsFronzen) throw new Exception("What?");
if (from.IsFrozen) throw new Exception("What?");

for (int i = 0; i < from.Arcs.Count; ++i)
{
Expand All @@ -419,7 +418,7 @@ private static void SetOutput(StateWithTransitions from, char c, T output)

private static void PropagateOutput(StateWithTransitions from, T output, IFSTOutput<T> outputType)
{
if (from.IsFronzen) throw new Exception("What?");
if (from.IsFrozen) throw new Exception("What?");

for (int i = 0; i < from.Arcs.Count; ++i)
{
Expand All @@ -435,15 +434,15 @@ private static void PropagateOutput(StateWithTransitions from, T output, IFSTOut

private static void ClearState(StateWithTransitions s)
{
if (s.IsFronzen) throw new Exception("What?");
if (s.IsFrozen) throw new Exception("What?");

s.IsFinal = false;
s.Arcs.Clear();
}

private static void SetFinal(StateWithTransitions s)
{
if (s.IsFronzen) throw new Exception("What?");
if (s.IsFrozen) throw new Exception("What?");

s.IsFinal = true;
}
Expand Down Expand Up @@ -492,12 +491,12 @@ public void Add(string currentWord, T currentOutput)
tempState = newTemp;
}

if (StringComparer.Ordinal.Compare(currentWord, previousWord) <= 0)
if (currentWord.AsSpan().CompareTo(previousWord, StringComparison.Ordinal) <= 0)
{
throw new Exception($"Input should be ordered and each item should be unique: {previousWord} < {currentWord}");
}

var prefixLengthPlusOne = 1 + Utils.LCP(previousWord, currentWord);
var prefixLengthPlusOne = 1 + previousWord.AsSpan().CommonPrefixLength(currentWord);

if (prefixLengthPlusOne == 1 + currentWord.Length)
{
Expand Down Expand Up @@ -1124,7 +1123,6 @@ public class FST<T> : IFST<T>
public IFSTOutput<T> OutputType => outputType;
#endregion

#region Methods
public FST(IFSTOutput<T> outputType)
{
this.states = new List<State>();
Expand All @@ -1134,7 +1132,6 @@ public FST(IFSTOutput<T> outputType)

Initial = 0;
}
#endregion

#region Serialization
public byte[] GetBytes()
Expand Down Expand Up @@ -1747,7 +1744,7 @@ private FSTStringOutput() { }

public string Min(string a, string b)
{
return a.Substring(0, Utils.LCP(a, b));
return a[..a.AsSpan().CommonPrefixLength(b)];
}

public string Sub(string a, string b)
Expand Down Expand Up @@ -1789,18 +1786,4 @@ public int WriteTo(string value, Span<byte> buffer)

return size + byteCount;
}
}

internal static class Utils
{
// Calculate length of the longest common prefix
public static int LCP(string a, string b)
{
int i = 0;
while (i < a.Length && i < b.Length && a[i] == b[i])
{
++i;
}
return i;
}
}
5 changes: 1 addition & 4 deletions Src/Protsyk.PMS.FullText.Core/Collections/Btree.cs
Original file line number Diff line number Diff line change
Expand Up @@ -32,10 +32,7 @@ public Btree()

public Btree(int order)
{
if (order < 1)
{
throw new ArgumentOutOfRangeException(nameof(order));
}
ArgumentOutOfRangeException.ThrowIfNegativeOrZero(order);

this.order = order;
this.maxChildren = 2 * order;
Expand Down
6 changes: 1 addition & 5 deletions Src/Protsyk.PMS.FullText.Core/Collections/BtreePersistent.cs
Original file line number Diff line number Diff line change
Expand Up @@ -38,11 +38,7 @@ public BtreePersistent(int order)
public BtreePersistent(IPersistentStorage persistentStorage, int order)
{
ArgumentNullException.ThrowIfNull(persistentStorage);

if (order < 1)
{
throw new ArgumentOutOfRangeException(nameof(order));
}
ArgumentOutOfRangeException.ThrowIfNegativeOrZero(order);

keySerializer = DataSerializer.GetDefault<TKey>();
valueSerializer = DataSerializer.GetDefault<TValue>();
Expand Down
6 changes: 1 addition & 5 deletions Src/Protsyk.PMS.FullText.Core/Collections/HeapExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -30,11 +30,7 @@ public static IEnumerable<T> TopN<T>(this IEnumerable<T> items, IComparer<T> com
{
ArgumentNullException.ThrowIfNull(items);
ArgumentNullException.ThrowIfNull(comparer);

if (n < 0)
{
throw new ArgumentOutOfRangeException(nameof(n));
}
ArgumentOutOfRangeException.ThrowIfNegative(n);

if (n == 0)
{
Expand Down
5 changes: 1 addition & 4 deletions Src/Protsyk.PMS.FullText.Core/Collections/LFUCache.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,7 @@ public class LFUCache<TKey, TValue>

public LFUCache(int capacity)
{
if (capacity <= 0)
{
throw new ArgumentOutOfRangeException();
}
ArgumentOutOfRangeException.ThrowIfNegativeOrZero(capacity);

this.capacity = capacity;
this.nextOrder = 0;
Expand Down
5 changes: 1 addition & 4 deletions Src/Protsyk.PMS.FullText.Core/Collections/LRUCache.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,7 @@ public LRUCache()

public LRUCache(int capacity)
{
if (capacity < 1)
{
throw new ArgumentOutOfRangeException(nameof(capacity));
}
ArgumentOutOfRangeException.ThrowIfNegativeOrZero(capacity);

items = new Dictionary<TKey, LinkedListNode<(TKey, TValue)>>();
usageQueue = new LinkedList<(TKey, TValue)>();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,10 +42,8 @@ public TValue this[long index]
get
{
var offset = linearIndex[index];
if (offset == 0)
{
throw new ArgumentOutOfRangeException(nameof(index));
}

ArgumentOutOfRangeException.ThrowIfZero(offset);

int dataSize = dataStorage.ReadInt32LittleEndian(offset);

Expand Down
16 changes: 8 additions & 8 deletions Src/Protsyk.PMS.FullText.Core/Common/PackedInts.cs
Original file line number Diff line number Diff line change
Expand Up @@ -227,7 +227,7 @@ public class PackedIntN8 : IPackedInts
private const int BitsInBaseType = 8;
private const int LastBitIndexInBaseType = 7;

private static readonly uint[] mask = new uint[] {
private static ReadOnlySpan<uint> mask => [
0b0000_0000,
0b0000_0001,
0b0000_0011,
Expand All @@ -237,7 +237,7 @@ public class PackedIntN8 : IPackedInts
0b0011_1111,
0b0111_1111,
0b1111_1111
};
];

private readonly byte[] data;
private readonly int bits;
Expand Down Expand Up @@ -393,7 +393,7 @@ public class PackedIntN16 : IPackedInts
private const int BitsInBaseType = 16;
private const int LastBitIndexInBaseType = 15;

private static readonly uint[] mask = new uint[] {
private static ReadOnlySpan<uint> mask => [
0b0000_0000_0000_0000,
0b0000_0000_0000_0001,
0b0000_0000_0000_0011,
Expand All @@ -411,7 +411,7 @@ public class PackedIntN16 : IPackedInts
0b0011_1111_1111_1111,
0b0111_1111_1111_1111,
0b1111_1111_1111_1111
};
];

private readonly ushort[] data;
private readonly int bits;
Expand Down Expand Up @@ -579,7 +579,7 @@ public class PackedIntN32 : IPackedInts
private const int BitsInBaseType = 32;
private const int LastBitIndexInBaseType = 31;

private static readonly uint[] mask = new uint[] {
private static ReadOnlySpan<uint> mask => [
0b0000_0000_0000_0000_0000_0000_0000_0000,
0b0000_0000_0000_0000_0000_0000_0000_0001,
0b0000_0000_0000_0000_0000_0000_0000_0011,
Expand Down Expand Up @@ -613,7 +613,7 @@ public class PackedIntN32 : IPackedInts
0b0011_1111_1111_1111_1111_1111_1111_1111,
0b0111_1111_1111_1111_1111_1111_1111_1111,
0b1111_1111_1111_1111_1111_1111_1111_1111
};
];

private readonly uint[] data;
private readonly int bits;
Expand Down Expand Up @@ -786,7 +786,7 @@ public class PackedIntN64 : IPackedInts, IPackedLongs
private const int BitsInBaseType = 64;
private const int LastBitIndexInBaseType = 63;

private static readonly ulong[] mask = new ulong[] {
private static ReadOnlySpan<ulong> mask => [
0b0000_0000_0000_0000_0000_0000_0000_0000_0000_0000_0000_0000_0000_0000_0000_0000,
0b0000_0000_0000_0000_0000_0000_0000_0000_0000_0000_0000_0000_0000_0000_0000_0001,
0b0000_0000_0000_0000_0000_0000_0000_0000_0000_0000_0000_0000_0000_0000_0000_0011,
Expand Down Expand Up @@ -852,7 +852,7 @@ public class PackedIntN64 : IPackedInts, IPackedLongs
0b0011_1111_1111_1111_1111_1111_1111_1111_1111_1111_1111_1111_1111_1111_1111_1111,
0b0111_1111_1111_1111_1111_1111_1111_1111_1111_1111_1111_1111_1111_1111_1111_1111,
0b1111_1111_1111_1111_1111_1111_1111_1111_1111_1111_1111_1111_1111_1111_1111_1111
};
];

private readonly ulong[] data;
private readonly int bits;
Expand Down
Loading

0 comments on commit 17bf39b

Please sign in to comment.