import type { VariantMap } from '@rimbu/collection-types'; import type { OptLazy, RelatedTo } from '@rimbu/common'; import type { Stream, Streamable } from '@rimbu/stream'; import type { ValuedGraphElement, ValuedLink, VariantGraphBase, WithGraphValues, } from '../../../common/index.mjs'; export interface VariantValuedGraphBase< N, V, Tp extends VariantValuedGraphBase.Types = VariantValuedGraphBase.Types, > extends VariantGraphBase { /** * Returns the nested Map representation of the graph connections. * @example * ```ts * ArrowValuedGraphHashed.of([1, 2, 'a'], [2, 3, 'b']).linkMap.toArray() * // => [[1, HashMap(2 -> 'a')], [2, HashMap(3 -> 'b')]] * ``` */ readonly linkMap: WithGraphValues['linkMap']; /** * Returns the value of the connection between given `node1` and `node2` * @param node1 - the first connection node * @param node2 - the second connection node * @param otherwise - (default: undefined) the fallback value to return if the connection does not exist * @example * ```ts * const g = ArrowValuedGraphHashed.of([1, 2, 'a'], [2, 3, 'b']) * g.getValue(1, 2) // => 'a' * g.getValue(3, 4) // => undefined * g.getValue(1, 2, 'z') // => 'a' * g.getValue(3, 4, 'z') // => 'z' * ``` */ getValue( node1: RelatedTo, node2: RelatedTo ): V | undefined; getValue( node1: RelatedTo, node2: RelatedTo, otherwise: OptLazy ): V | O; /** * Returns a graph with the same connections, but where the given `mapFun` function is applied to each connection value. * @param mapFun - a function taking a `value` and connection's `node1` and `node2`, and returning a new value * @example * ```ts * ArrowValuedGraphHashed.of([1, 2, 'a'], [2, 3, 'bc']).mapValues(v => v.length).toArray() * // => [[1, 2, 1], [2, 3, 2]] * ``` */ mapValues( mapFun: (value: V, node1: N, node2: N) => V2 ): WithGraphValues['normal']; } export namespace VariantValuedGraphBase { export interface NonEmpty< N, V, Tp extends VariantValuedGraphBase.Types = VariantValuedGraphBase.Types, > extends VariantGraphBase.NonEmpty, Omit< VariantValuedGraphBase, keyof VariantGraphBase.NonEmpty >, Streamable.NonEmpty> { /** * Returns the nested non-empty Map representation of the graph connections. * @example * ```ts * ArrowValuedGraphHashed.of([1, 2, 'a'], [2, 3, 'b']).linkMap.toArray() * // => [[1, HashMap(2 -> 'a')], [2, HashMap(3 -> 'b')]] * ``` */ readonly linkMap: WithGraphValues['linkMapNonEmpty']; /** * Returns a non-empty `Stream` containing all graph elements of this collection as single tuples for isolated nodes * and 3-valued tuples containing the source node, target node, and connection value for connections. * @example * ```ts * ArrowValuedGraphHashed.of([1, 2, 'a'], [2, 3, 'b']).stream().toArray() * // => [[1, 2, 'a'], [2, 3, 'b']] * ``` */ stream(): Stream.NonEmpty>; /** * Returns a non-empty graph with the same connections, but where the given `mapFun` function is applied to each connection value. * @param mapFun - a function taking a `value` and connection's `node1` and `node2`, and returning a new value * @example * ```ts * ArrowValuedGraphHashed.of([1, 2, 'a'], [2, 3, 'bc']).mapValues(v => v.length).toArray() * // => [[1, 2, 1], [2, 3, 2]] * ``` */ mapValues( mapFun: (value: V, node1: N, node2: N) => V2 ): WithGraphValues['nonEmpty']; } /** * Utility interface that provides higher-kinded types for this collection. */ export interface Types extends VariantGraphBase.Types { readonly normal: VariantValuedGraphBase; readonly nonEmpty: VariantValuedGraphBase.NonEmpty; readonly link: ValuedLink; readonly linkTarget: ValuedLink.Target; readonly linkMap: VariantMap< this['_N'], VariantMap >; readonly linkMapNonEmpty: VariantMap.NonEmpty< this['_N'], VariantMap >; } }