Skip to content
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

W-15053193. Simple modification of methods AmfUpdaterIterator.hasNext and AmfUpdaterIterator.next to be tailrecursive. #1975

Merged
merged 1 commit into from
Apr 18, 2024
Merged
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
@@ -1,9 +1,10 @@
package amf.shapes.internal.domain.resolution
import amf.core.client.scala.model.document.BaseUnit
import amf.core.client.scala.model.domain.{AmfArray, AmfElement, AmfObject, DataNode, Shape}
import amf.core.client.scala.model.domain._
import amf.core.client.scala.traversal.iterator.{AmfIterator, InstanceCollector, VisitedCollector}
import amf.shapes.client.scala.model.domain.Example

import scala.annotation.tailrec
import scala.collection.immutable.Queue

case class AmfUpdaterIterator private (var buffer: Queue[AmfElement], updater: AmfElement => AmfElement)
Expand All @@ -22,31 +23,37 @@ case class AmfUpdaterIterator private (var buffer: Queue[AmfElement], updater: A
}
}

override def hasNext: Boolean = {
if (buffer.nonEmpty) {
val current = buffer.head
if (visited.visited(current)) {
buffer = buffer.tail
hasNext
} else true
override final def hasNext: Boolean = {
@tailrec
def innerHasNext(): Boolean = buffer match {
case current +: tail if visited.visited(current) =>
buffer = tail
innerHasNext()
case _ +: _ => true // nonEmpty
case _ => false // empty
}

} else false
innerHasNext()
}

override def next: AmfElement = {
val current = buffer.head
buffer = buffer.tail
if (!visited.visited(current)) {
val next = nextElements(current)
.map(updater)
.filter(e => !visited.visited(e) && !shouldSkip(e))
buffer = buffer ++ next
visited += current
current
} else {
next
override final def next: AmfElement = {
@tailrec
def innerNext(): AmfElement = {
val current = buffer.head
buffer = buffer.tail
if (!visited.visited(current)) {
val next = nextElements(current)
.map(updater)
.filter(e => !visited.visited(e) && !shouldSkip(e))
buffer = buffer ++ next
visited += current
current
} else {
innerNext()
}
}

innerNext()
}

private def nextElements(e: AmfElement): Iterable[AmfElement] = {
Expand Down