util

package
v1.2.23 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jul 22, 2026 License: Apache-2.0 Imports: 10 Imported by: 0

Documentation

Overview

Copyright Consensys Software Inc.

Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.

SPDX-License-Identifier: Apache-2.0

Copyright Consensys Software Inc.

Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.

SPDX-License-Identifier: Apache-2.0

Copyright Consensys Software Inc.

Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.

SPDX-License-Identifier: Apache-2.0

Copyright Consensys Software Inc.

Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.

SPDX-License-Identifier: Apache-2.0

Copyright Consensys Software Inc.

Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.

SPDX-License-Identifier: Apache-2.0

Copyright Consensys Software Inc.

Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.

SPDX-License-Identifier: Apache-2.0

Copyright Consensys Software Inc.

Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.

SPDX-License-Identifier: Apache-2.0

Copyright Consensys Software Inc.

Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.

SPDX-License-Identifier: Apache-2.0

Copyright Consensys Software Inc.

Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.

SPDX-License-Identifier: Apache-2.0

Copyright Consensys Software Inc.

Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.

SPDX-License-Identifier: Apache-2.0

Copyright Consensys Software Inc.

Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.

SPDX-License-Identifier: Apache-2.0

Copyright Consensys Software Inc.

Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.

SPDX-License-Identifier: Apache-2.0

Copyright Consensys Software Inc.

Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.

SPDX-License-Identifier: Apache-2.0

Copyright Consensys Software Inc.

Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.

SPDX-License-Identifier: Apache-2.0

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Assert added in v1.2.21

func Assert(value bool, message string, args ...any)

Assert that a given value holds, otherwise panic with a given message.

func Cast added in v1.2.21

func Cast[S uint8 | uint16 | uint32, T uint8 | uint16 | uint32 | uint](val T) S

Cast provides a safe cast from a value of one type to a value of another (typically smaller) type. In particular, if the value does not fit in the given type then this will panic.

func GenerateRandomElements

func GenerateRandomElements[E any](n uint, elems []E) []E

GenerateRandomElements generates n elements selected at random from the given array.

func GenerateRandomInts

func GenerateRandomInts(n uint, m int) []int

GenerateRandomInts generates n random unsigned integers in the range -m..m.

func GenerateRandomUint64s

func GenerateRandomUint64s(n, m uint64) []uint64

GenerateRandomUint64s generates n random 64bit unsigned integers in the range 0..m.

func GenerateRandomUints

func GenerateRandomUints(n, m uint) []uint

GenerateRandomUints generates n random unsigned integers in the range 0..m.

func ParExec

func ParExec[J ParBatchJob](worklist []J) error

ParExec executes a set of jobs in parallel using go-routines.

func SampleElements

func SampleElements[E comparable](n uint, elems []E) []E

SampleElements selects exactly n elements from the given array when its length is greater (otherwise returns the array untouched).

func ShallowCloneMap

func ShallowCloneMap[K comparable, V any](orig map[K]V) map[K]V

ShallowCloneMap makes a shallow clone of a given map.

Types

type BigInter added in v1.2.21

type BigInter interface {
	BigInt() *big.Int
}

BigInter captures any type which can be convered into a big.Int value. NOTE: the return value should be fresh (i.e. not an alias for some internal state).

type Boundable

type Boundable interface {
	// Determine the well-definedness bounds for this expression for both the
	// negative (left) or positive (right) directions.  For example, consider an
	// expression such as "(shift X -1)".  This is technically undefined for the
	// first row of any trace and, by association, any constraint evaluating
	// this expression on that first row is also undefined (and hence must pass).
	Bounds() Bounds
}

Boundable captures computations which are well-defined only for a specific subrange of rows (the bounds).

type Bounds

type Bounds struct {
	// Number of rows from first row where computation starts being defined.
	Start uint
	// Number of rows before last row where computation is no longer defined.
	End uint
}

Bounds captures the subrange of rows for which a computation is well-defined.

var EMPTY_BOUND Bounds = Bounds{0, 0}

EMPTY_BOUND is the bound which overlaps exactly with the original range. It represents the maximum possible bound.

func BoundsForArray

func BoundsForArray[E Boundable](args []E) Bounds

BoundsForArray determines the bounds for an array of expressions.

func NewBounds

func NewBounds(start uint, end uint) Bounds

NewBounds constructs a new set of bounds.

func (*Bounds) Union

func (p *Bounds) Union(q *Bounds)

Union merges one set of bounds into another.

type Comparable

type Comparable[T any] interface {
	// Cmp returns < 0 if this is less than other, or 0 if they are equal, or >
	// 0 if this is greater than other.
	Cmp(other T) int
}

Comparable interface which can be implemented by non-primitive types.

type ComparableUint64

type ComparableUint64 interface {
	// Cmp returns < 0 if this is less than other, or 0 if they are equal, or >
	// 0 if this is greater than other.
	Cmp64(other uint64) int
}

ComparableUint64 interface which can be implemented by non-primitive types.

type Comparator

type Comparator[W Comparable[W]] interface {
	// Cmp returns true when the underlying comparison (e.g. l < r) holds.
	Cmp(l, r W) bool
}

Comparator provides a generic concept of comparing two values (e.g. l < r).

type Counter added in v1.2.21

type Counter struct {
	// contains filtered or unexported fields
}

Counter is a simple countdown which fires (returns true from Tick) once every `initial` ticks. It begins at its initial value and, on each Tick, decrements the current count; upon reaching zero it resets to the initial value and reports true. This is useful, for example, for triggering an action periodically (e.g. taking a checkpoint every N instructions).

func NewCounter added in v1.2.21

func NewCounter(n uint64) Counter

NewCounter constructs a counter which fires every n ticks.

func NewCounterOnce added in v1.2.21

func NewCounterOnce(initial uint64) Counter

NewCounterOnce constructs a counter which fires once after n ticks.

func (*Counter) Tick added in v1.2.21

func (p *Counter) Tick() bool

Tick decrements the counter, returning true if it has reached zero. When it fires, the counter is automatically reset to its initial value.

type Equal

type Equal[W Comparable[W]] struct {
}

Equal comparator holds when l == r

func (Equal[W]) Cmp

func (p Equal[W]) Cmp(l, r W) bool

Cmp implementation for Comparable interface

type FlowGraph added in v1.2.21

type FlowGraph struct {
	// contains filtered or unexported fields
}

FlowGraph renders a set of vertical columns down a grid of rows. Each range occupies its own column (in the order added) and is "active" on the rows within its span. The intention is to visualise, at a glance, which rows each range covers.

func NewFlowGraph added in v1.2.21

func NewFlowGraph(ranges ...Range) FlowGraph

NewFlowGraph constructs a FlowGraph from the given ranges, one column per range in the order provided.

func (*FlowGraph) Add added in v1.2.21

func (p *FlowGraph) Add(start, finish uint)

Add appends a new range (column) spanning rows start..finish (inclusive).

func (*FlowGraph) Render added in v1.2.21

func (p *FlowGraph) Render(n uint) []string

Render produces n strings, one per row. Ranges are packed into columns such that non-overlapping ranges share a column; each column contributes two adjacent characters (in left-to-right order), and adjacent columns are separated by a space. A range draws a vertical arrow spanning it: a horizontal lead-in at the source row, and an arrow head turning left toward the instruction at the target row. Rows outside every range in a column render as blank there.

type GreaterThan

type GreaterThan[W Comparable[W]] struct {
}

GreaterThan comparator holds when l > r

func (GreaterThan[W]) Cmp

func (p GreaterThan[W]) Cmp(l, r W) bool

Cmp implementation for Comparable interface

type GreaterThanOrEqual

type GreaterThanOrEqual[W Comparable[W]] struct {
}

GreaterThanOrEqual comparator holds when l >= r

func (GreaterThanOrEqual[W]) Cmp

func (p GreaterThanOrEqual[W]) Cmp(l, r W) bool

Cmp implementation for Comparable interface

type LessThan

type LessThan[W Comparable[W]] struct {
}

LessThan comparator holds when l < r

func (LessThan[W]) Cmp

func (p LessThan[W]) Cmp(l, r W) bool

Cmp implementation for Comparable interface

type LessThanOrEqual

type LessThanOrEqual[W Comparable[W]] struct {
}

LessThanOrEqual comparator holds when l <= r

func (LessThanOrEqual[W]) Cmp

func (p LessThanOrEqual[W]) Cmp(l, r W) bool

Cmp implementation for Comparable interface

type NotEqual

type NotEqual[W Comparable[W]] struct {
}

NotEqual comparator holds when l != r

func (NotEqual[W]) Cmp

func (p NotEqual[W]) Cmp(l, r W) bool

Cmp implementation for Comparable interface

type Option

type Option[T any] struct {
	// contains filtered or unexported fields
}

Option provides a simple encoding for an optional value. A key advantage over a pointer is that this serialises nicely.

func MapOption added in v1.2.21

func MapOption[S any, T any](opt Option[S], fn func(S) T) Option[T]

MapOption maps an option from one type to another using a given mapping function.

func None

func None[T any]() Option[T]

None constructs an option which doesn't hold a value.

func Some

func Some[T any](val T) Option[T]

Some constructs an option which holds a value.

func (*Option[T]) GobDecode

func (o *Option[T]) GobDecode(data []byte) error

GobDecode a previously encoded option

func (*Option[T]) GobEncode

func (o *Option[T]) GobEncode() (data []byte, err error)

GobEncode an option. This allows it to be marshalled into a binary form.

func (Option[T]) HasValue

func (o Option[T]) HasValue() bool

HasValue indicates whether or not this option contains an actual value, or whether it is empty.

func (Option[T]) IsEmpty

func (o Option[T]) IsEmpty() bool

IsEmpty indicates whether or not this option is empty (i.e. contains no value).

func (Option[T]) Unwrap

func (o Option[T]) Unwrap() T

Unwrap returns the value contained, or panics if this option is empty.

func (Option[T]) UnwrapOr added in v1.2.21

func (o Option[T]) UnwrapOr(value T) T

UnwrapOr returns the value contained, or a given default value is the option is empty.

type Pair

type Pair[S any, T any] struct {
	Left  S
	Right T
}

Pair provides a simple encapsulation of two items paired together.

func NewPair

func NewPair[S any, T any](left S, right T) Pair[S, T]

NewPair returns a new instance of Pair by value.

func NewPairRef

func NewPairRef[S any, T any](left S, right T) *Pair[S, T]

NewPairRef returns a reference to a new instance of Pair.

func (*Pair[S, T]) Split

func (p *Pair[S, T]) Split() (S, T)

Split returns both the left and right elements of this pair.

type ParBatchJob

type ParBatchJob interface {
	// Get the job identifies for all jobs in this batch.
	Jobs() []uint
	// Get the jobs on which this at least one job in this batch dependend.  In
	// otherwords, all of the return jobs must be complete before this batch can
	// run.
	Dependencies() []uint
	// Run this batch job
	Run() error
}

ParBatchJob represents an atomic division of work which is composed of one or more jobs. The idea is that all of these jobs must be computed together in one large batch, and cannot be further broken down.

type PerfStats

type PerfStats struct {
	// contains filtered or unexported fields
}

PerfStats provides a snapshot of memory allocation at a given point in time.

func NewPerfStats

func NewPerfStats() *PerfStats

NewPerfStats creates a new snapshot of the current amount of memory allocated.

func (*PerfStats) Log

func (p *PerfStats) Log(prefix string)

Log logs the difference between the state now and as it was when the PerfStats object was created.

func (*PerfStats) Reset

func (p *PerfStats) Reset()

Reset the performance counter

func (*PerfStats) String

func (p *PerfStats) String() string

String provides a string representation of the usage thus far.

type Range added in v1.2.21

type Range struct {
	Start  uint
	Finish uint
}

Range identifies a contiguous span of rows, from Start up to and including Finish, during which its column is considered active. The endpoints may be given in either order.

func (Range) Contains added in v1.2.21

func (p Range) Contains(row uint) bool

Contains determines whether a given row falls within this range (inclusive of both endpoints).

func (Range) Overlaps added in v1.2.21

func (p Range) Overlaps(other Range) bool

Overlaps determines whether this range shares any row with the other. Both endpoints are treated as inclusive, so ranges which merely touch (share a single endpoint row) are also considered to overlap.

type Uinter64

type Uinter64 interface {
	// Convert the given type into a uint64 value.
	Uint64() uint64
}

Uinter64 captures any type which can be converted into a uint64 value.

type Union

type Union[S, T any] struct {
	// contains filtered or unexported fields
}

Union represents a value which is either of the first type or of the second type.

func Union1

func Union1[S, T any](value S) Union[S, T]

Union1 constructs a union holding a value of the first type.

func Union2

func Union2[S, T any](value T) Union[S, T]

Union2 constructs a union holding a value of the second type.

func (Union[S, T]) First

func (u Union[S, T]) First() S

First returns the contained value of the first type. If the union does not hold a value of the first type, then this will panic.

func (Union[S, T]) HasFirst

func (u Union[S, T]) HasFirst() bool

HasFirst indicates whether this union holds a value of the first type (or not).

func (Union[S, T]) HasSecond

func (u Union[S, T]) HasSecond() bool

HasSecond indicates whether this union holds a value of the second type (or not).

func (Union[S, T]) Second

func (u Union[S, T]) Second() T

Second returns the contained value of the second type. If the union does not hold a value of the second type, then this will panic.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL