Skip to content

Commit

Permalink
Add new flatTo.
Browse files Browse the repository at this point in the history
  • Loading branch information
cao-awa committed Aug 27, 2024
1 parent a61c7e7 commit f2cd6dd
Show file tree
Hide file tree
Showing 7 changed files with 133 additions and 1 deletion.
2 changes: 1 addition & 1 deletion gradle.properties
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
kotlin.code.style=official
version_name=1.0.20
version_name=1.0.21
artifact_id=catheter
group_id=com.github.cao.awa
22 changes: 22 additions & 0 deletions src/main/java/com/github/cao/awa/catheter/BooleanCatheter.java
Original file line number Diff line number Diff line change
Expand Up @@ -1011,6 +1011,28 @@ public <X> Catheter<X> flatTo(Function<Boolean, Catheter<X>> function) {
return result;
}

public <X> Catheter<X> flatToByCollection(Function<Boolean, Collection<X>> function) {
Catheter<Collection<X>> catheter = Catheter.makeCapacity(count());
Receptacle<Integer> totalSize = new Receptacle<>(0);
alternate(0, (index, element) -> {
Collection<X> flatting = function.apply(element);
catheter.fetch(index, flatting);
totalSize.set(totalSize.get() + flatting.size());
return index + 1;
});

Catheter<X> result = Catheter.makeCapacity(totalSize.get());

catheter.alternate(0, (currentIndex, inner) -> {
int i = currentIndex;
for (X element : inner) {
result.fetch(i++, element);
}
return currentIndex;
});
return result;
}

public BooleanCatheter reset() {
this.targets = array(0);
return this;
Expand Down
22 changes: 22 additions & 0 deletions src/main/java/com/github/cao/awa/catheter/ByteCatheter.java
Original file line number Diff line number Diff line change
Expand Up @@ -1014,6 +1014,28 @@ public <X> Catheter<X> flatTo(Function<Byte, Catheter<X>> function) {
return result;
}

public <X> Catheter<X> flatToByCollection(Function<Byte, Collection<X>> function) {
Catheter<Collection<X>> catheter = Catheter.makeCapacity(count());
Receptacle<Integer> totalSize = new Receptacle<>(0);
alternate(0, (index, element) -> {
Collection<X> flatting = function.apply(element);
catheter.fetch(index, flatting);
totalSize.set(totalSize.get() + flatting.size());
return index + 1;
});

Catheter<X> result = Catheter.makeCapacity(totalSize.get());

catheter.alternate(0, (currentIndex, inner) -> {
int i = currentIndex;
for (X element : inner) {
result.fetch(i++, element);
}
return currentIndex;
});
return result;
}

public ByteCatheter reset() {
this.targets = array(0);
return this;
Expand Down
22 changes: 22 additions & 0 deletions src/main/java/com/github/cao/awa/catheter/Catheter.java
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,28 @@ public <X> Catheter<X> flatTo(Function<T, Catheter<X>> function) {
return result;
}

public <X> Catheter<X> flatToByCollection(Function<T, Collection<X>> function) {
Catheter<Collection<X>> catheter = Catheter.makeCapacity(count());
Receptacle<Integer> totalSize = new Receptacle<>(0);
alternate(0, (index, element) -> {
Collection<X> flatting = function.apply(element);
catheter.fetch(index, flatting);
totalSize.set(totalSize.get() + flatting.size());
return index + 1;
});

Catheter<X> result = Catheter.makeCapacity(totalSize.get());

catheter.alternate(0, (currentIndex, inner) -> {
int i = currentIndex;
for (X element : inner) {
result.fetch(i++, element);
}
return currentIndex;
});
return result;
}

@SuppressWarnings("unchecked")
public static <X> Catheter<X> of(Collection<X> targets) {
if (targets == null) {
Expand Down
22 changes: 22 additions & 0 deletions src/main/java/com/github/cao/awa/catheter/DoubleCatheter.java
Original file line number Diff line number Diff line change
Expand Up @@ -1035,6 +1035,28 @@ public <X> Catheter<X> flatTo(Function<Double, Catheter<X>> function) {
return result;
}

public <X> Catheter<X> flatToByCollection(Function<Double, Collection<X>> function) {
Catheter<Collection<X>> catheter = Catheter.makeCapacity(count());
Receptacle<Integer> totalSize = new Receptacle<>(0);
alternate(0, (index, element) -> {
Collection<X> flatting = function.apply(element);
catheter.fetch(index, flatting);
totalSize.set(totalSize.get() + flatting.size());
return index + 1;
});

Catheter<X> result = Catheter.makeCapacity(totalSize.get());

catheter.alternate(0, (currentIndex, inner) -> {
int i = currentIndex;
for (X element : inner) {
result.fetch(i++, element);
}
return currentIndex;
});
return result;
}

public DoubleCatheter reset() {
this.targets = array(0);
return this;
Expand Down
22 changes: 22 additions & 0 deletions src/main/java/com/github/cao/awa/catheter/IntCatheter.java
Original file line number Diff line number Diff line change
Expand Up @@ -1035,6 +1035,28 @@ public <X> Catheter<X> flatTo(Function<Integer, Catheter<X>> function) {
return result;
}

public <X> Catheter<X> flatToByCollection(Function<Integer, Collection<X>> function) {
Catheter<Collection<X>> catheter = Catheter.makeCapacity(count());
Receptacle<Integer> totalSize = new Receptacle<>(0);
alternate(0, (index, element) -> {
Collection<X> flatting = function.apply(element);
catheter.fetch(index, flatting);
totalSize.set(totalSize.get() + flatting.size());
return index + 1;
});

Catheter<X> result = Catheter.makeCapacity(totalSize.get());

catheter.alternate(0, (currentIndex, inner) -> {
int i = currentIndex;
for (X element : inner) {
result.fetch(i++, element);
}
return currentIndex;
});
return result;
}

public IntCatheter reset() {
this.targets = array(0);
return this;
Expand Down
22 changes: 22 additions & 0 deletions src/main/java/com/github/cao/awa/catheter/LongCatheter.java
Original file line number Diff line number Diff line change
Expand Up @@ -1035,6 +1035,28 @@ public <X> Catheter<X> flatTo(Function<Long, Catheter<X>> function) {
return result;
}

public <X> Catheter<X> flatToByCollection(Function<Long, Collection<X>> function) {
Catheter<Collection<X>> catheter = Catheter.makeCapacity(count());
Receptacle<Integer> totalSize = new Receptacle<>(0);
alternate(0, (index, element) -> {
Collection<X> flatting = function.apply(element);
catheter.fetch(index, flatting);
totalSize.set(totalSize.get() + flatting.size());
return index + 1;
});

Catheter<X> result = Catheter.makeCapacity(totalSize.get());

catheter.alternate(0, (currentIndex, inner) -> {
int i = currentIndex;
for (X element : inner) {
result.fetch(i++, element);
}
return currentIndex;
});
return result;
}

public LongCatheter reset() {
this.targets = array(0);
return this;
Expand Down

0 comments on commit f2cd6dd

Please sign in to comment.