Finish porting Facet.Encoding
Project: http://git-wip-us.apache.org/repos/asf/lucenenet/repo Commit: http://git-wip-us.apache.org/repos/asf/lucenenet/commit/b3627229 Tree: http://git-wip-us.apache.org/repos/asf/lucenenet/tree/b3627229 Diff: http://git-wip-us.apache.org/repos/asf/lucenenet/diff/b3627229 Branch: refs/heads/branch_4x Commit: b3627229e957b089c034b598dceed31cab96d912 Parents: 760e9a1 Author: Paul Irwin <[email protected]> Authored: Wed Nov 6 09:34:35 2013 -0500 Committer: Paul Irwin <[email protected]> Committed: Wed Nov 6 09:34:35 2013 -0500 ---------------------------------------------------------------------- .../Facet/Complements/TotalFacetCounts.cs | 8 ++ src/contrib/Facet/Contrib.Facet.csproj | 13 ++++ src/contrib/Facet/Encoding/ChunksIntEncoder.cs | 74 ++++++++++++++++++ src/contrib/Facet/Encoding/DGapIntDecoder.cs | 34 +++++++++ src/contrib/Facet/Encoding/DGapIntEncoder.cs | 40 ++++++++++ .../Facet/Encoding/EightFlagsIntDecoder.cs | 79 +++++++++++++++++++ .../Facet/Encoding/EightFlagsIntEncoder.cs | 59 +++++++++++++++ .../Facet/Encoding/FourFlagsIntDecoder.cs | 80 ++++++++++++++++++++ .../Facet/Encoding/FourFlagsIntEncoder.cs | 63 +++++++++++++++ src/contrib/Facet/Encoding/NOnesIntDecoder.cs | 71 +++++++++++++++++ src/contrib/Facet/Encoding/NOnesIntEncoder.cs | 72 ++++++++++++++++++ src/contrib/Facet/Encoding/SimpleIntDecoder.cs | 33 ++++++++ src/contrib/Facet/Encoding/SimpleIntEncoder.cs | 42 ++++++++++ src/contrib/Facet/Encoding/VInt8IntDecoder.cs | 42 ++++++++++ src/contrib/Facet/Encoding/VInt8IntEncoder.cs | 72 ++++++++++++++++++ 15 files changed, 782 insertions(+) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/lucenenet/blob/b3627229/src/contrib/Facet/Complements/TotalFacetCounts.cs ---------------------------------------------------------------------- diff --git a/src/contrib/Facet/Complements/TotalFacetCounts.cs b/src/contrib/Facet/Complements/TotalFacetCounts.cs index 945acec..637bdb2 100644 --- a/src/contrib/Facet/Complements/TotalFacetCounts.cs +++ b/src/contrib/Facet/Complements/TotalFacetCounts.cs @@ -128,7 +128,15 @@ namespace Lucene.Net.Facet.Complements internal static TotalFacetCounts Compute(IndexReader indexReader, TaxonomyReader taxonomy, FacetIndexingParams facetIndexingParams) { int partitionSize = PartitionsUtils.PartitionSize(facetIndexingParams, taxonomy); + int[][] counts = new int[(int)Math.Ceiling(taxonomy.Size / (float)partitionSize)][]; + + // .NET Port: this is needed since we can't initialize jagged array sizes + for (int i = 0; i < counts.Length; i++) + { + counts[i] = new int[partitionSize]; + } + FacetSearchParams newSearchParams = new FacetSearchParams(facetIndexingParams, DUMMY_REQ); StandardFacetsAccumulator sfa = new AnonymousStandardFacetsAccumulator(newSearchParams, indexReader, taxonomy, counts, facetIndexingParams); sfa.ComplementThreshold = StandardFacetsAccumulator.DISABLE_COMPLEMENT; http://git-wip-us.apache.org/repos/asf/lucenenet/blob/b3627229/src/contrib/Facet/Contrib.Facet.csproj ---------------------------------------------------------------------- diff --git a/src/contrib/Facet/Contrib.Facet.csproj b/src/contrib/Facet/Contrib.Facet.csproj index 8e52f70..1c26fcb 100644 --- a/src/contrib/Facet/Contrib.Facet.csproj +++ b/src/contrib/Facet/Contrib.Facet.csproj @@ -54,13 +54,26 @@ <Compile Include="Complements\ComplementCountingAggregator.cs" /> <Compile Include="Complements\TotalFacetCounts.cs" /> <Compile Include="Complements\TotalFacetCountsCache.cs" /> + <Compile Include="Encoding\ChunksIntEncoder.cs" /> + <Compile Include="Encoding\DGapIntDecoder.cs" /> + <Compile Include="Encoding\DGapIntEncoder.cs" /> <Compile Include="Encoding\DGapVInt8IntDecoder.cs" /> <Compile Include="Encoding\DGapVInt8IntEncoder.cs" /> + <Compile Include="Encoding\EightFlagsIntDecoder.cs" /> + <Compile Include="Encoding\EightFlagsIntEncoder.cs" /> + <Compile Include="Encoding\FourFlagsIntDecoder.cs" /> + <Compile Include="Encoding\FourFlagsIntEncoder.cs" /> <Compile Include="Encoding\IntDecoder.cs" /> <Compile Include="Encoding\IntEncoder.cs" /> <Compile Include="Encoding\IntEncoderFilter.cs" /> + <Compile Include="Encoding\NOnesIntDecoder.cs" /> + <Compile Include="Encoding\NOnesIntEncoder.cs" /> + <Compile Include="Encoding\SimpleIntDecoder.cs" /> + <Compile Include="Encoding\SimpleIntEncoder.cs" /> <Compile Include="Encoding\SortingIntEncoder.cs" /> <Compile Include="Encoding\UniqueValuesIntEncoder.cs" /> + <Compile Include="Encoding\VInt8IntDecoder.cs" /> + <Compile Include="Encoding\VInt8IntEncoder.cs" /> <Compile Include="Params\CategoryListParams.cs" /> <Compile Include="Params\FacetIndexingParams.cs" /> <Compile Include="Params\FacetSearchParams.cs" /> http://git-wip-us.apache.org/repos/asf/lucenenet/blob/b3627229/src/contrib/Facet/Encoding/ChunksIntEncoder.cs ---------------------------------------------------------------------- diff --git a/src/contrib/Facet/Encoding/ChunksIntEncoder.cs b/src/contrib/Facet/Encoding/ChunksIntEncoder.cs new file mode 100644 index 0000000..d2fc2ca --- /dev/null +++ b/src/contrib/Facet/Encoding/ChunksIntEncoder.cs @@ -0,0 +1,74 @@ +using Lucene.Net.Util; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; + +namespace Lucene.Net.Facet.Encoding +{ + public abstract class ChunksIntEncoder : IntEncoder + { + protected readonly IntsRef encodeQueue; + protected int indicator = 0; + protected byte ordinal = 0; + + protected ChunksIntEncoder(int chunkSize) + { + encodeQueue = new IntsRef(chunkSize); + } + + protected virtual void EncodeChunk(BytesRef buf) + { + int maxBytesRequired = buf.length + 1 + encodeQueue.length * 4; + if (buf.bytes.Length < maxBytesRequired) + { + buf.Grow(maxBytesRequired); + } + + buf.bytes[buf.length++] = ((sbyte)indicator); + for (int i = 0; i < encodeQueue.length; i++) + { + int value = encodeQueue.ints[i]; + if ((value & ~0x7F) == 0) + { + buf.bytes[buf.length] = (sbyte)value; + buf.length++; + } + else if ((value & ~0x3FFF) == 0) + { + buf.bytes[buf.length] = (sbyte)(0x80 | ((value & 0x3F80) >> 7)); + buf.bytes[buf.length + 1] = (sbyte)(value & 0x7F); + buf.length += 2; + } + else if ((value & ~0x1FFFFF) == 0) + { + buf.bytes[buf.length] = (sbyte)(0x80 | ((value & 0x1FC000) >> 14)); + buf.bytes[buf.length + 1] = (sbyte)(0x80 | ((value & 0x3F80) >> 7)); + buf.bytes[buf.length + 2] = (sbyte)(value & 0x7F); + buf.length += 3; + } + else if ((value & ~0xFFFFFFF) == 0) + { + buf.bytes[buf.length] = (sbyte)(0x80 | ((value & 0xFE00000) >> 21)); + buf.bytes[buf.length + 1] = (sbyte)(0x80 | ((value & 0x1FC000) >> 14)); + buf.bytes[buf.length + 2] = (sbyte)(0x80 | ((value & 0x3F80) >> 7)); + buf.bytes[buf.length + 3] = (sbyte)(value & 0x7F); + buf.length += 4; + } + else + { + buf.bytes[buf.length] = (sbyte)(0x80 | ((value & 0xF0000000) >> 28)); + buf.bytes[buf.length + 1] = (sbyte)(0x80 | ((value & 0xFE00000) >> 21)); + buf.bytes[buf.length + 2] = (sbyte)(0x80 | ((value & 0x1FC000) >> 14)); + buf.bytes[buf.length + 3] = (sbyte)(0x80 | ((value & 0x3F80) >> 7)); + buf.bytes[buf.length + 4] = (sbyte)(value & 0x7F); + buf.length += 5; + } + } + + ordinal = 0; + indicator = 0; + encodeQueue.length = 0; + } + } +} http://git-wip-us.apache.org/repos/asf/lucenenet/blob/b3627229/src/contrib/Facet/Encoding/DGapIntDecoder.cs ---------------------------------------------------------------------- diff --git a/src/contrib/Facet/Encoding/DGapIntDecoder.cs b/src/contrib/Facet/Encoding/DGapIntDecoder.cs new file mode 100644 index 0000000..6b6d02e --- /dev/null +++ b/src/contrib/Facet/Encoding/DGapIntDecoder.cs @@ -0,0 +1,34 @@ +using Lucene.Net.Util; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; + +namespace Lucene.Net.Facet.Encoding +{ + public sealed class DGapIntDecoder : IntDecoder + { + private readonly IntDecoder decoder; + + public DGapIntDecoder(IntDecoder decoder) + { + this.decoder = decoder; + } + + public override void Decode(BytesRef buf, IntsRef values) + { + decoder.Decode(buf, values); + int prev = 0; + for (int i = 0; i < values.length; i++) + { + values.ints[i] += prev; + prev = values.ints[i]; + } + } + + public override string ToString() + { + return @"DGap(" + decoder.ToString() + @")"; + } + } +} http://git-wip-us.apache.org/repos/asf/lucenenet/blob/b3627229/src/contrib/Facet/Encoding/DGapIntEncoder.cs ---------------------------------------------------------------------- diff --git a/src/contrib/Facet/Encoding/DGapIntEncoder.cs b/src/contrib/Facet/Encoding/DGapIntEncoder.cs new file mode 100644 index 0000000..e5d346a --- /dev/null +++ b/src/contrib/Facet/Encoding/DGapIntEncoder.cs @@ -0,0 +1,40 @@ +using Lucene.Net.Util; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; + +namespace Lucene.Net.Facet.Encoding +{ + public sealed class DGapIntEncoder : IntEncoderFilter + { + public DGapIntEncoder(IntEncoder encoder) + : base(encoder) + { + } + + public override void Encode(IntsRef values, BytesRef buf) + { + int prev = 0; + int upto = values.offset + values.length; + for (int i = values.offset; i < upto; i++) + { + int tmp = values.ints[i]; + values.ints[i] -= prev; + prev = tmp; + } + + encoder.Encode(values, buf); + } + + public override IntDecoder CreateMatchingDecoder() + { + return new DGapIntDecoder(encoder.CreateMatchingDecoder()); + } + + public override string ToString() + { + return @"DGap(" + encoder.ToString() + @")"; + } + } +} http://git-wip-us.apache.org/repos/asf/lucenenet/blob/b3627229/src/contrib/Facet/Encoding/EightFlagsIntDecoder.cs ---------------------------------------------------------------------- diff --git a/src/contrib/Facet/Encoding/EightFlagsIntDecoder.cs b/src/contrib/Facet/Encoding/EightFlagsIntDecoder.cs new file mode 100644 index 0000000..3c7800b --- /dev/null +++ b/src/contrib/Facet/Encoding/EightFlagsIntDecoder.cs @@ -0,0 +1,79 @@ +using Lucene.Net.Support; +using Lucene.Net.Util; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; + +namespace Lucene.Net.Facet.Encoding +{ + public class EightFlagsIntDecoder : IntDecoder + { + private static readonly byte[,] DECODE_TABLE = new byte[256,8]; + + static EightFlagsIntDecoder() + { + for (int i = 256; i != 0; ) + { + --i; + for (int j = 8; j != 0; ) + { + --j; + DECODE_TABLE[i,j] = (byte)(Number.URShift(i, j) & 0x1); + } + } + } + + public override void Decode(BytesRef buf, IntsRef values) + { + values.offset = values.length = 0; + int upto = buf.offset + buf.length; + int offset = buf.offset; + while (offset < upto) + { + int indicator = buf.bytes[offset++] & 0xFF; + int ordinal = 0; + int capacityNeeded = values.length + 8; + if (values.ints.Length < capacityNeeded) + { + values.Grow(capacityNeeded); + } + + while (ordinal != 8) + { + if (DECODE_TABLE[indicator,ordinal++] == 0) + { + if (offset == upto) + { + return; + } + + int value = 0; + while (true) + { + sbyte b = buf.bytes[offset++]; + if (b >= 0) + { + values.ints[values.length++] = ((value << 7) | (byte)b) + 2; + break; + } + else + { + value = (value << 7) | (b & 0x7F); + } + } + } + else + { + values.ints[values.length++] = 1; + } + } + } + } + + public override string ToString() + { + return @"EightFlags(VInt8)"; + } + } +} http://git-wip-us.apache.org/repos/asf/lucenenet/blob/b3627229/src/contrib/Facet/Encoding/EightFlagsIntEncoder.cs ---------------------------------------------------------------------- diff --git a/src/contrib/Facet/Encoding/EightFlagsIntEncoder.cs b/src/contrib/Facet/Encoding/EightFlagsIntEncoder.cs new file mode 100644 index 0000000..714337f --- /dev/null +++ b/src/contrib/Facet/Encoding/EightFlagsIntEncoder.cs @@ -0,0 +1,59 @@ +using Lucene.Net.Util; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; + +namespace Lucene.Net.Facet.Encoding +{ + public class EightFlagsIntEncoder : ChunksIntEncoder + { + private static readonly byte[] ENCODE_TABLE = new byte[] { + 0x1, 0x2, 0x4, 0x8, 0x10, 0x20, 0x40, (byte)0x80 + }; + + public EightFlagsIntEncoder() + : base(8) + { + } + + public override void Encode(IntsRef values, BytesRef buf) + { + buf.offset = buf.length = 0; + int upto = values.offset + values.length; + for (int i = values.offset; i < upto; i++) + { + int value = values.ints[i]; + if (value == 1) + { + indicator |= ENCODE_TABLE[ordinal]; + } + else + { + encodeQueue.ints[encodeQueue.length++] = value - 2; + } + + ++ordinal; + if (ordinal == 8) + { + EncodeChunk(buf); + } + } + + if (ordinal != 0) + { + EncodeChunk(buf); + } + } + + public override IntDecoder CreateMatchingDecoder() + { + return new EightFlagsIntDecoder(); + } + + public override string ToString() + { + return @"EightFlags(VInt)"; + } + } +} http://git-wip-us.apache.org/repos/asf/lucenenet/blob/b3627229/src/contrib/Facet/Encoding/FourFlagsIntDecoder.cs ---------------------------------------------------------------------- diff --git a/src/contrib/Facet/Encoding/FourFlagsIntDecoder.cs b/src/contrib/Facet/Encoding/FourFlagsIntDecoder.cs new file mode 100644 index 0000000..d654b04 --- /dev/null +++ b/src/contrib/Facet/Encoding/FourFlagsIntDecoder.cs @@ -0,0 +1,80 @@ +using Lucene.Net.Support; +using Lucene.Net.Util; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; + +namespace Lucene.Net.Facet.Encoding +{ + public class FourFlagsIntDecoder : IntDecoder + { + private static readonly byte[,] DECODE_TABLE = new byte[256, 4]; + + static FourFlagsIntDecoder() + { + for (int i = 256; i != 0; ) + { + --i; + for (int j = 4; j != 0; ) + { + --j; + DECODE_TABLE[i,j] = (byte)(Number.URShift(i, (j << 1)) & 0x3); + } + } + } + + public override void Decode(BytesRef buf, IntsRef values) + { + values.offset = values.length = 0; + int upto = buf.offset + buf.length; + int offset = buf.offset; + while (offset < upto) + { + int indicator = buf.bytes[offset++] & 0xFF; + int ordinal = 0; + int capacityNeeded = values.length + 4; + if (values.ints.Length < capacityNeeded) + { + values.Grow(capacityNeeded); + } + + while (ordinal != 4) + { + byte decodeVal = DECODE_TABLE[indicator,ordinal++]; + if (decodeVal == 0) + { + if (offset == upto) + { + return; + } + + int value = 0; + while (true) + { + sbyte b = buf.bytes[offset++]; + if (b >= 0) + { + values.ints[values.length++] = ((value << 7) | (byte)b) + 4; + break; + } + else + { + value = (value << 7) | (b & 0x7F); + } + } + } + else + { + values.ints[values.length++] = decodeVal; + } + } + } + } + + public override string ToString() + { + return @"FourFlags(VInt)"; + } + } +} http://git-wip-us.apache.org/repos/asf/lucenenet/blob/b3627229/src/contrib/Facet/Encoding/FourFlagsIntEncoder.cs ---------------------------------------------------------------------- diff --git a/src/contrib/Facet/Encoding/FourFlagsIntEncoder.cs b/src/contrib/Facet/Encoding/FourFlagsIntEncoder.cs new file mode 100644 index 0000000..d417b43 --- /dev/null +++ b/src/contrib/Facet/Encoding/FourFlagsIntEncoder.cs @@ -0,0 +1,63 @@ +using Lucene.Net.Util; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; + +namespace Lucene.Net.Facet.Encoding +{ + public class FourFlagsIntEncoder : ChunksIntEncoder + { + private static readonly byte[][] ENCODE_TABLE = new byte[][] + { + new byte[] { 0x00, 0x00, 0x00, 0x00 }, + new byte[] { 0x01, 0x04, 0x10, 0x40 }, + new byte[] { 0x02, 0x08, 0x20, (byte)0x80 }, + new byte[] { 0x03, 0x0C, 0x30, (byte)0xC0 } + }; + + public FourFlagsIntEncoder() + : base(4) + { + } + + public override void Encode(IntsRef values, BytesRef buf) + { + buf.offset = buf.length = 0; + int upto = values.offset + values.length; + for (int i = values.offset; i < upto; i++) + { + int value = values.ints[i]; + if (value <= 3) + { + indicator |= ENCODE_TABLE[value][ordinal]; + } + else + { + encodeQueue.ints[encodeQueue.length++] = value - 4; + } + + ++ordinal; + if (ordinal == 4) + { + EncodeChunk(buf); + } + } + + if (ordinal != 0) + { + EncodeChunk(buf); + } + } + + public override IntDecoder CreateMatchingDecoder() + { + return new FourFlagsIntDecoder(); + } + + public override string ToString() + { + return @"FourFlags(VInt)"; + } + } +} http://git-wip-us.apache.org/repos/asf/lucenenet/blob/b3627229/src/contrib/Facet/Encoding/NOnesIntDecoder.cs ---------------------------------------------------------------------- diff --git a/src/contrib/Facet/Encoding/NOnesIntDecoder.cs b/src/contrib/Facet/Encoding/NOnesIntDecoder.cs new file mode 100644 index 0000000..f9345fd --- /dev/null +++ b/src/contrib/Facet/Encoding/NOnesIntDecoder.cs @@ -0,0 +1,71 @@ +using Lucene.Net.Util; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; + +namespace Lucene.Net.Facet.Encoding +{ + public class NOnesIntDecoder : FourFlagsIntDecoder + { + private readonly int n; + private readonly IntsRef internalBuffer; + + public NOnesIntDecoder(int n) + { + this.n = n; + internalBuffer = new IntsRef(100); + } + + public override void Decode(BytesRef buf, IntsRef values) + { + values.offset = values.length = 0; + internalBuffer.length = 0; + base.Decode(buf, internalBuffer); + if (values.ints.Length < internalBuffer.length) + { + values.Grow(internalBuffer.length * n / 2); + } + + for (int i = 0; i < internalBuffer.length; i++) + { + int decode = internalBuffer.ints[i]; + if (decode == 1) + { + if (values.length == values.ints.Length) + { + values.Grow(values.length + 10); + } + + values.ints[values.length++] = 1; + } + else if (decode == 2) + { + if (values.length + n >= values.ints.Length) + { + values.Grow(values.length + n); + } + + for (int j = 0; j < n; j++) + { + values.ints[values.length++] = 1; + } + } + else + { + if (values.length == values.ints.Length) + { + values.Grow(values.length + 10); + } + + values.ints[values.length++] = decode - 1; + } + } + } + + public override string ToString() + { + return @"NOnes(" + n + @") (" + base.ToString() + @")"; + } + } +} http://git-wip-us.apache.org/repos/asf/lucenenet/blob/b3627229/src/contrib/Facet/Encoding/NOnesIntEncoder.cs ---------------------------------------------------------------------- diff --git a/src/contrib/Facet/Encoding/NOnesIntEncoder.cs b/src/contrib/Facet/Encoding/NOnesIntEncoder.cs new file mode 100644 index 0000000..a8e4d50 --- /dev/null +++ b/src/contrib/Facet/Encoding/NOnesIntEncoder.cs @@ -0,0 +1,72 @@ +using Lucene.Net.Util; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; + +namespace Lucene.Net.Facet.Encoding +{ + public class NOnesIntEncoder : FourFlagsIntEncoder + { + private readonly IntsRef internalBuffer; + private readonly int n; + + public NOnesIntEncoder(int n) + { + this.n = n; + internalBuffer = new IntsRef(n); + } + + public override void Encode(IntsRef values, BytesRef buf) + { + internalBuffer.length = 0; + if (values.length > internalBuffer.ints.Length) + { + internalBuffer.Grow(values.length); + } + + int onesCounter = 0; + int upto = values.offset + values.length; + for (int i = values.offset; i < upto; i++) + { + int value = values.ints[i]; + if (value == 1) + { + if (++onesCounter == n) + { + internalBuffer.ints[internalBuffer.length++] = 2; + onesCounter = 0; + } + } + else + { + while (onesCounter > 0) + { + --onesCounter; + internalBuffer.ints[internalBuffer.length++] = 1; + } + + internalBuffer.ints[internalBuffer.length++] = value + 1; + } + } + + while (onesCounter > 0) + { + --onesCounter; + internalBuffer.ints[internalBuffer.length++] = 1; + } + + base.Encode(internalBuffer, buf); + } + + public override IntDecoder CreateMatchingDecoder() + { + return new NOnesIntDecoder(n); + } + + public override string ToString() + { + return @"NOnes(" + n + @") (" + base.ToString() + @")"; + } + } +} http://git-wip-us.apache.org/repos/asf/lucenenet/blob/b3627229/src/contrib/Facet/Encoding/SimpleIntDecoder.cs ---------------------------------------------------------------------- diff --git a/src/contrib/Facet/Encoding/SimpleIntDecoder.cs b/src/contrib/Facet/Encoding/SimpleIntDecoder.cs new file mode 100644 index 0000000..84fc1a0 --- /dev/null +++ b/src/contrib/Facet/Encoding/SimpleIntDecoder.cs @@ -0,0 +1,33 @@ +using Lucene.Net.Util; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; + +namespace Lucene.Net.Facet.Encoding +{ + public sealed class SimpleIntDecoder : IntDecoder + { + public override void Decode(BytesRef buf, IntsRef values) + { + values.offset = values.length = 0; + int numValues = buf.length / 4; + if (values.ints.Length < numValues) + { + values.ints = new int[ArrayUtil.Oversize(numValues, RamUsageEstimator.NUM_BYTES_INT)]; + } + + int offset = buf.offset; + int upto = buf.offset + buf.length; + while (offset < upto) + { + values.ints[values.length++] = ((buf.bytes[offset++] & 0xFF) << 24) | ((buf.bytes[offset++] & 0xFF) << 16) | ((buf.bytes[offset++] & 0xFF) << 8) | (buf.bytes[offset++] & 0xFF); + } + } + + public override string ToString() + { + return @"Simple"; + } + } +} http://git-wip-us.apache.org/repos/asf/lucenenet/blob/b3627229/src/contrib/Facet/Encoding/SimpleIntEncoder.cs ---------------------------------------------------------------------- diff --git a/src/contrib/Facet/Encoding/SimpleIntEncoder.cs b/src/contrib/Facet/Encoding/SimpleIntEncoder.cs new file mode 100644 index 0000000..25f78e2 --- /dev/null +++ b/src/contrib/Facet/Encoding/SimpleIntEncoder.cs @@ -0,0 +1,42 @@ +using Lucene.Net.Support; +using Lucene.Net.Util; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; + +namespace Lucene.Net.Facet.Encoding +{ + public sealed class SimpleIntEncoder : IntEncoder + { + public override void Encode(IntsRef values, BytesRef buf) + { + buf.offset = buf.length = 0; + int bytesNeeded = values.length * 4; + if (buf.bytes.Length < bytesNeeded) + { + buf.Grow(bytesNeeded); + } + + int upto = values.offset + values.length; + for (int i = values.offset; i < upto; i++) + { + int value = values.ints[i]; + buf.bytes[buf.length++] = (sbyte)Number.URShift(value, 24); + buf.bytes[buf.length++] = (sbyte)((value >> 16) & 0xFF); + buf.bytes[buf.length++] = (sbyte)((value >> 8) & 0xFF); + buf.bytes[buf.length++] = (sbyte)(value & 0xFF); + } + } + + public override IntDecoder CreateMatchingDecoder() + { + return new SimpleIntDecoder(); + } + + public override string ToString() + { + return @"Simple"; + } + } +} http://git-wip-us.apache.org/repos/asf/lucenenet/blob/b3627229/src/contrib/Facet/Encoding/VInt8IntDecoder.cs ---------------------------------------------------------------------- diff --git a/src/contrib/Facet/Encoding/VInt8IntDecoder.cs b/src/contrib/Facet/Encoding/VInt8IntDecoder.cs new file mode 100644 index 0000000..fac8738 --- /dev/null +++ b/src/contrib/Facet/Encoding/VInt8IntDecoder.cs @@ -0,0 +1,42 @@ +using Lucene.Net.Util; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; + +namespace Lucene.Net.Facet.Encoding +{ + public sealed class VInt8IntDecoder : IntDecoder + { + public override void Decode(BytesRef buf, IntsRef values) + { + values.offset = values.length = 0; + if (values.ints.Length < buf.length) + { + values.ints = new int[ArrayUtil.Oversize(buf.length, RamUsageEstimator.NUM_BYTES_INT)]; + } + + int upto = buf.offset + buf.length; + int value = 0; + int offset = buf.offset; + while (offset < upto) + { + sbyte b = buf.bytes[offset++]; + if (b >= 0) + { + values.ints[values.length++] = (value << 7) | (byte)b; + value = 0; + } + else + { + value = (value << 7) | (b & 0x7F); + } + } + } + + public override string ToString() + { + return @"VInt8"; + } + } +} http://git-wip-us.apache.org/repos/asf/lucenenet/blob/b3627229/src/contrib/Facet/Encoding/VInt8IntEncoder.cs ---------------------------------------------------------------------- diff --git a/src/contrib/Facet/Encoding/VInt8IntEncoder.cs b/src/contrib/Facet/Encoding/VInt8IntEncoder.cs new file mode 100644 index 0000000..0478569 --- /dev/null +++ b/src/contrib/Facet/Encoding/VInt8IntEncoder.cs @@ -0,0 +1,72 @@ +using Lucene.Net.Util; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; + +namespace Lucene.Net.Facet.Encoding +{ + public sealed class VInt8IntEncoder : IntEncoder + { + public override void Encode(IntsRef values, BytesRef buf) + { + buf.offset = buf.length = 0; + int maxBytesNeeded = 5 * values.length; + if (buf.bytes.Length < maxBytesNeeded) + { + buf.Grow(maxBytesNeeded); + } + + int upto = values.offset + values.length; + for (int i = values.offset; i < upto; i++) + { + int value = values.ints[i]; + if ((value & ~0x7F) == 0) + { + buf.bytes[buf.length] = (sbyte)value; + buf.length++; + } + else if ((value & ~0x3FFF) == 0) + { + buf.bytes[buf.length] = (sbyte)(0x80 | ((value & 0x3F80) >> 7)); + buf.bytes[buf.length + 1] = (sbyte)(value & 0x7F); + buf.length += 2; + } + else if ((value & ~0x1FFFFF) == 0) + { + buf.bytes[buf.length] = (sbyte)(0x80 | ((value & 0x1FC000) >> 14)); + buf.bytes[buf.length + 1] = (sbyte)(0x80 | ((value & 0x3F80) >> 7)); + buf.bytes[buf.length + 2] = (sbyte)(value & 0x7F); + buf.length += 3; + } + else if ((value & ~0xFFFFFFF) == 0) + { + buf.bytes[buf.length] = (sbyte)(0x80 | ((value & 0xFE00000) >> 21)); + buf.bytes[buf.length + 1] = (sbyte)(0x80 | ((value & 0x1FC000) >> 14)); + buf.bytes[buf.length + 2] = (sbyte)(0x80 | ((value & 0x3F80) >> 7)); + buf.bytes[buf.length + 3] = (sbyte)(value & 0x7F); + buf.length += 4; + } + else + { + buf.bytes[buf.length] = (sbyte)(0x80 | ((value & 0xF0000000) >> 28)); + buf.bytes[buf.length + 1] = (sbyte)(0x80 | ((value & 0xFE00000) >> 21)); + buf.bytes[buf.length + 2] = (sbyte)(0x80 | ((value & 0x1FC000) >> 14)); + buf.bytes[buf.length + 3] = (sbyte)(0x80 | ((value & 0x3F80) >> 7)); + buf.bytes[buf.length + 4] = (sbyte)(value & 0x7F); + buf.length += 5; + } + } + } + + public override IntDecoder CreateMatchingDecoder() + { + return new VInt8IntDecoder(); + } + + public override string ToString() + { + return @"VInt8"; + } + } +}
