mirror of
https://github.com/microsoft/HybridRow.git
synced 2026-01-24 03:43:18 +00:00
Simplified Utf8String some more
This commit is contained in:
@@ -16,21 +16,20 @@ import com.fasterxml.jackson.databind.ser.std.StdSerializer;
|
|||||||
import com.google.common.base.Objects;
|
import com.google.common.base.Objects;
|
||||||
import com.google.common.base.Suppliers;
|
import com.google.common.base.Suppliers;
|
||||||
import com.google.common.base.Utf8;
|
import com.google.common.base.Utf8;
|
||||||
import com.google.common.collect.Streams;
|
|
||||||
import io.netty.buffer.ByteBuf;
|
import io.netty.buffer.ByteBuf;
|
||||||
import io.netty.buffer.ByteBufHolder;
|
import io.netty.buffer.ByteBufHolder;
|
||||||
import io.netty.buffer.Unpooled;
|
import io.netty.buffer.Unpooled;
|
||||||
import io.netty.util.ByteProcessor;
|
import io.netty.util.ByteProcessor;
|
||||||
|
import it.unimi.dsi.fastutil.ints.IntIterator;
|
||||||
|
|
||||||
import javax.annotation.Nonnull;
|
import javax.annotation.Nonnull;
|
||||||
import javax.annotation.Nullable;
|
import javax.annotation.Nullable;
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
import java.util.Iterator;
|
|
||||||
import java.util.NoSuchElementException;
|
import java.util.NoSuchElementException;
|
||||||
import java.util.Optional;
|
import java.util.Optional;
|
||||||
|
import java.util.PrimitiveIterator;
|
||||||
import java.util.Spliterator;
|
import java.util.Spliterator;
|
||||||
import java.util.function.Consumer;
|
import java.util.Spliterators;
|
||||||
import java.util.function.IntConsumer;
|
|
||||||
import java.util.function.Supplier;
|
import java.util.function.Supplier;
|
||||||
import java.util.stream.IntStream;
|
import java.util.stream.IntStream;
|
||||||
import java.util.stream.StreamSupport;
|
import java.util.stream.StreamSupport;
|
||||||
@@ -114,12 +113,15 @@ public final class Utf8String implements ByteBufHolder, CharSequence, Comparable
|
|||||||
* Non-allocating enumeration of each code point in the UTF-8 stream
|
* Non-allocating enumeration of each code point in the UTF-8 stream
|
||||||
*/
|
*/
|
||||||
public final IntStream codePoints() {
|
public final IntStream codePoints() {
|
||||||
|
|
||||||
if (this.buffer == null || this.buffer.writerIndex() == 0) {
|
if (this.buffer == null || this.buffer.writerIndex() == 0) {
|
||||||
return IntStream.empty();
|
return IntStream.empty();
|
||||||
}
|
}
|
||||||
return StreamSupport.intStream(new CodePointIterable(this.buffer, this.codePointCount.get()), false);
|
|
||||||
}
|
|
||||||
|
|
||||||
|
return StreamSupport.intStream(
|
||||||
|
() -> Spliterators.spliteratorUnknownSize(new CodePointIterator(this.buffer), Spliterator.ORDERED),
|
||||||
|
Spliterator.ORDERED,false);
|
||||||
|
}
|
||||||
|
|
||||||
public final int compareTo(@Nonnull final Utf8String other) {
|
public final int compareTo(@Nonnull final Utf8String other) {
|
||||||
|
|
||||||
@@ -146,14 +148,17 @@ public final class Utf8String implements ByteBufHolder, CharSequence, Comparable
|
|||||||
return other == null ? 0 : -1;
|
return other == null ? 0 : -1;
|
||||||
}
|
}
|
||||||
|
|
||||||
// TODO: DANOBLE: Consider optimizing this method based on the cost of zipping boxed int streams
|
PrimitiveIterator.OfInt t = this.codePoints().iterator();
|
||||||
|
PrimitiveIterator.OfInt o = other.codePoints().iterator();
|
||||||
|
|
||||||
Optional<Integer> compare = Streams
|
while (t.hasNext() && o.hasNext()) {
|
||||||
.zip(this.codePoints().boxed(), other.codePoints().boxed(), (x, y) -> x - y)
|
final int compare = t.nextInt() - o.nextInt();
|
||||||
.filter(delta -> delta != 0)
|
if (compare != 0) {
|
||||||
.findFirst();
|
return compare;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
return compare.orElse(this.length() - other.length());
|
return this.length() - other.length();
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -398,51 +403,30 @@ public final class Utf8String implements ByteBufHolder, CharSequence, Comparable
|
|||||||
return new Utf8String(buffer);
|
return new Utf8String(buffer);
|
||||||
}
|
}
|
||||||
|
|
||||||
private static final class CodePointIterable extends UTF8CodePointGetter implements
|
private static final class CodePointIterator extends UTF8CodePointGetter implements IntIterator.OfInt {
|
||||||
Iterable<Integer>, Iterator<Integer>, Spliterator.OfInt {
|
|
||||||
|
|
||||||
private static final int CHARACTERISTICS = Spliterator.IMMUTABLE | Spliterator.NONNULL | Spliterator.ORDERED;
|
|
||||||
|
|
||||||
private final ByteBuf buffer;
|
private final ByteBuf buffer;
|
||||||
private final int codePointCount;
|
|
||||||
|
|
||||||
private int start, length;
|
private int start, length;
|
||||||
|
|
||||||
CodePointIterable(final ByteBuf buffer, final int codePointCount) {
|
CodePointIterator(final ByteBuf buffer) {
|
||||||
this.codePointCount = codePointCount;
|
|
||||||
this.buffer = buffer;
|
this.buffer = buffer;
|
||||||
this.start = 0;
|
this.start = 0;
|
||||||
this.length = buffer.writerIndex();
|
this.length = buffer.writerIndex();
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
|
||||||
public int characteristics() {
|
|
||||||
return this.codePointCount == -1 ? CHARACTERISTICS : CHARACTERISTICS | Spliterator.SIZED | Spliterator.SUBSIZED;
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public long estimateSize() {
|
|
||||||
return this.codePointCount < 0 ? Long.MAX_VALUE : this.codePointCount;
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public void forEachRemaining(final Consumer<? super Integer> action) {
|
|
||||||
OfInt.super.forEachRemaining(action);
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public boolean hasNext() {
|
public boolean hasNext() {
|
||||||
return this.length > 0;
|
return this.length > 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Returns the next {@code int} element in the iteration.
|
||||||
|
*
|
||||||
|
* @return the next {@code int} element in the iteration
|
||||||
|
* @throws NoSuchElementException if the iteration has no more elements
|
||||||
|
*/
|
||||||
@Override
|
@Override
|
||||||
@Nonnull
|
public int nextInt() {
|
||||||
public Iterator<Integer> iterator() {
|
|
||||||
return this;
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public Integer next() {
|
|
||||||
|
|
||||||
if (!this.hasNext()) {
|
if (!this.hasNext()) {
|
||||||
throw new NoSuchElementException();
|
throw new NoSuchElementException();
|
||||||
@@ -453,30 +437,6 @@ public final class Utf8String implements ByteBufHolder, CharSequence, Comparable
|
|||||||
|
|
||||||
return this.codePoint();
|
return this.codePoint();
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
|
||||||
public Spliterator<Integer> spliterator() {
|
|
||||||
return this;
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public boolean tryAdvance(@Nonnull final IntConsumer action) {
|
|
||||||
|
|
||||||
checkNotNull(action, "expected non-null action");
|
|
||||||
|
|
||||||
if (this.hasNext()) {
|
|
||||||
action.accept(this.next());
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public OfInt trySplit() {
|
|
||||||
return null;
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
static final class Deserializer extends StdDeserializer<Utf8String> {
|
static final class Deserializer extends StdDeserializer<Utf8String> {
|
||||||
|
|||||||
Reference in New Issue
Block a user