iterable

package
v0.3.14 Latest Latest
Warning

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

Go to latest
Published: Nov 10, 2023 License: Apache-2.0 Imports: 4 Imported by: 1

Documentation

Overview

Copyright 2023 The acquirecloud Authors

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.

Package iterable contains the definition of the Iterator interface and the containers that may expose the iterator objects to iterate over the container elements.

One of the examples is iterable.Map the hash-table with the functionality of iterating over its elements in the order of the elements were added to the map (FIFO).

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.

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.

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.

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.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type EmptyIterator

type EmptyIterator[V any] struct{}

func (*EmptyIterator[V]) Close

func (ei *EmptyIterator[V]) Close() error

func (*EmptyIterator[V]) HasNext

func (ei *EmptyIterator[V]) HasNext() bool

func (*EmptyIterator[V]) Next

func (ei *EmptyIterator[V]) Next() (V, bool)

type Iterator

type Iterator[V any] interface {
	// HasNext returns true if the collection contains next element for the iterator. Please see Next() function
	HasNext() bool

	// Next returns the next element and shifts the iterator to next one if it exists. Second
	// parameter indicates that the iterator returned a value. This function may return default
	// value for the type V, if the Next element does not exist (second parameter is false).
	//
	// The imparity may be observed between HasNext() and Next() functions results if the
	// element the iterator was pointing to was the last element and it was removed in between this 2 calls.
	// This case the HasNext() will return true, but the Next() will returns default values because the element
	// is deleted.
	Next() (V, bool)

	// Close closes the iterator and releases resources. The iterator object must not be used after the call.
	// The function must be always called for any iterator to release the resources properly. Violating the
	// contract may cause a memory leak.
	Close() error
}

Iterator interface provides some functions that allow to move over some sorted collection. It has three functions: HasNext(), Next() and Close() that can be used for iterating over the collection elements and releasing resources after usage.

func WrapIntSlice

func WrapIntSlice(i []int) Iterator[int]

type Map

type Map[K comparable, V any] struct {
	// contains filtered or unexported fields
}

Map implements a map with an mapIterator capability. The mapIterator allows considering elements from start to end in the order of the elements added into the map. It is safe to remove and add new elements into the map while an iterator exists, it will support the order the elements were added into the map

func NewMap

func NewMap[K comparable, V any]() *Map[K, V]

NewMap creates the new instance of Map[K, V]

func (*Map[K, V]) Add

func (im *Map[K, V]) Add(k K, v V) error

Add allows adding new key-value pair into the map. The function returns error if the key already exists in the map

func (*Map[K, V]) First

func (im *Map[K, V]) First() (K, bool)

First returns the first key and the whether the key exist or not

func (*Map[K, V]) Get

func (im *Map[K, V]) Get(k K) (V, bool)

Get allows returning value by its key

func (*Map[K, V]) Iterator

func (im *Map[K, V]) Iterator() Iterator[MapEntry[K, V]]

Iterator returns &mapIterator[K, V] object, which implements the Iterator[MapEntry[K, V]] interface. The iterator must be released (Closed) after usage for the proper resource management

func (*Map[K, V]) Len

func (im *Map[K, V]) Len() int

Len returns current map size

func (*Map[K, V]) Remove

func (im *Map[K, V]) Remove(k K)

Remove removes the value by its key

type MapEntry

type MapEntry[K comparable, V any] struct {
	Key   K
	Value V
}

type Mixer

type Mixer[E any] struct {
	// contains filtered or unexported fields
}

Mixer allows to mix 2 Iterators to one. Mixer provides the Iterator interface

func (*Mixer[E]) Close

func (mr *Mixer[E]) Close() error

func (*Mixer[E]) HasNext

func (mr *Mixer[E]) HasNext() bool

HasNext is the part of the Iterator interface

func (*Mixer[E]) Init

func (mr *Mixer[E]) Init(sf SelectF[E], it1, it2 Iterator[E])

Init initializes the mixer

func (*Mixer[E]) Next

func (mr *Mixer[E]) Next() (E, bool)

Next is the part of Iterator interface

func (*Mixer[E]) Reset

func (mr *Mixer[E]) Reset() error

Reset allows to reset the mixer internals and retry underlying iterators. This function will also try to reset underlying iterators if they support Resetable interface. If the underlying iterators do not support Reset(), the Reset() will report ErrUnimplemented

type SelectF

type SelectF[E any] func(ev1, ev2 E) bool

SelectF decides which one should be selected, it returns true if ev1 must be selected instead of ev2. If ev2 should be used then it returns false

Jump to

Keyboard shortcuts

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