Livt packages

Livt.Base

Version 1.2.1

Foundational bit, array, string, conversion, formatting, and timing helpers for Livt hardware designs.

Published files (32)

src/ArrayIterator.lvt

namespace Livt.Iteration

using Livt.Lang

/**
 * A resettable cursor over an owned array snapshot initialized at construction.
 * The array shape is inferred statically; this is not a borrowed array view.
 */
component ArrayIterator<T> : IResettableIterator<T>
{
	// Owned copy of the iterable prefix.
	values: T[]

	// Valid prefix length and next unread position.
	count: int
	position: int

	/**
	 * Copies a prefix. count is explicit so padded hardware ports do not add elements.
	 */
	new(values: T[], count: int)
	{
		assert count >= 0 && count <= values.Length(), "Invalid array iterator count"
		this.values = values
		this.count = count
		this.position = 0
	}

	/**
	 * Rewinds the cursor to the start of the copied prefix without changing its values.
	 */
	override fn Reset()
	{
		this.position = 0
	}

	/**
	 * Reports whether the copied prefix has another element; does not advance the cursor.
	 */
	override fn HasNext() bool
	{
		return this.position < this.count
	}

	/**
	 * Returns the next copied value and advances the cursor. Requires HasNext(); exhaustion
	 * fails an assertion.
	 */
	override fn Next() T
	{
		assert this.position < this.count, "Array iterator is exhausted"
		var value = this.values[this.position]
		this.position++
		return value
	}
}