csv

package
v0.1.0 Latest Latest
Warning

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

Go to latest
Published: Jun 2, 2026 License: MIT Imports: 9 Imported by: 0

Documentation

Overview

Package csv reads and writes graphs as edge lists in CSV format.

The format is a simple table of columns: source, destination, and (optionally) a weight. Lines beginning with the comment character (default '#') are skipped. A header row may declare the column types; without it the reader assumes a fixed (src, dst[, weight]) layout.

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

func ReadInto

func ReadInto(r io.Reader, opts Options) (*adjlist.AdjList[string, int64], int, error)

ReadInto streams a CSV from r into an adjacency list, returning the loaded list and the number of rows ingested. Each row must have at least two fields (src, dst); a third field is parsed as a int64 weight.

Example

ExampleReadInto parses a CSV edge list (src,dst[,weight] per row, '#' comment lines skipped) into a mutable adjacency list and reports the resulting order, size and one edge.

package main

import (
	"fmt"
	"strings"

	"github.com/FlavioCFOliveira/GoGraph/graph/io/csv"
)

func main() {
	const data = "# a tiny directed triangle\n" +
		"a,b,1\n" +
		"b,c,2\n" +
		"c,a,3\n"

	opts := csv.DefaultOptions()
	opts.Directed = true

	g, rows, err := csv.ReadInto(strings.NewReader(data), opts)
	if err != nil {
		panic(err)
	}

	fmt.Println("rows:", rows)
	fmt.Println("order:", g.Order())
	fmt.Println("size:", g.Size())
	fmt.Println("a->b:", g.HasEdge("a", "b"))
}
Output:
rows: 3
order: 3
size: 3
a->b: true

func ReadIntoCtx

func ReadIntoCtx(ctx context.Context, r io.Reader, opts Options) (*adjlist.AdjList[string, int64], int, error)

ReadIntoCtx is the context-aware variant of ReadInto. ctx.Err() is checked every 4096 rows; on cancellation returns (partialAdj, rowsConsumed, wrapped ctx.Err()).

func Write

func Write(w io.Writer, a *adjlist.AdjList[string, int64], opts Options) (int, error)

Write streams every edge of a in src,dst,weight order to w. Returns the number of rows written.

Example

ExampleWrite shows a CSV round-trip: build a graph, Write it to a buffer, then ReadInto a fresh graph and confirm the edges survived. The serialised row order follows internal NodeID assignment, so the example asserts on edge presence rather than on exact bytes.

package main

import (
	"bytes"
	"fmt"

	"github.com/FlavioCFOliveira/GoGraph/graph/adjlist"
	"github.com/FlavioCFOliveira/GoGraph/graph/io/csv"
)

func main() {
	src := adjlist.New[string, int64](adjlist.Config{Directed: true})
	_ = src.AddEdge("a", "b", 1)
	_ = src.AddEdge("a", "c", 2)
	_ = src.AddEdge("b", "c", 3)

	var buf bytes.Buffer
	rows, err := csv.Write(&buf, src, csv.DefaultOptions())
	if err != nil {
		panic(err)
	}

	readOpts := csv.DefaultOptions()
	readOpts.Directed = true
	dst, _, err := csv.ReadInto(&buf, readOpts)
	if err != nil {
		panic(err)
	}

	fmt.Println("rows written:", rows)
	fmt.Println("edges survive:", dst.HasEdge("a", "b") && dst.HasEdge("a", "c") && dst.HasEdge("b", "c"))
}
Output:
rows written: 3
edges survive: true

func WriteCtx

func WriteCtx(ctx context.Context, w io.Writer, a *adjlist.AdjList[string, int64], opts Options) (int, error)

WriteCtx is the context-aware variant of Write. ctx.Err() is checked every 4096 rows; on cancellation returns (rowsWritten, wrapped ctx.Err()).

Types

type Options

type Options struct {
	// Delimiter is the column separator; defaults to ','.
	Delimiter rune
	// Comment is the comment character; defaults to '#'.
	Comment rune
	// HasHeader skips the first line when true.
	HasHeader bool
	// Directed selects the underlying adjacency-list config.
	Directed bool
	// Multigraph allows parallel edges.
	Multigraph bool
}

Options controls Reader / Writer behaviour.

func DefaultOptions

func DefaultOptions() Options

DefaultOptions returns the minimal config: comma delimiter, '#' comments, directed simple graph, no header.

Jump to

Keyboard shortcuts

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