Skip to content

Commit

Permalink
Add ErrVertexNotFound error (#60)
Browse files Browse the repository at this point in the history
  • Loading branch information
dominikbraun authored Nov 1, 2022
1 parent 19132ea commit f1f0bea
Show file tree
Hide file tree
Showing 4 changed files with 13 additions and 5 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,13 @@ adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).

## [0.14.0] - 2022-11-01

### Added
* Added the `ErrVertexNotFound` error instance.

### Changed
* Changed `TopologicalSort` to fail at runtime when a cycle is detected.
* Changed `TransitiveReduction` to return the transitive reduction as a new graph and fail at runtime when a cycle is detected.
* Changed `Vertex` to return `ErrVertexNotFound` if the desired vertex couldn't be found.

## [0.13.0] - 2022-10-15

Expand Down
2 changes: 1 addition & 1 deletion directed.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ func (d *directed[K, T]) AddVertex(value T, options ...func(*VertexProperties))
func (d *directed[K, T]) Vertex(hash K) (T, error) {
vertex, ok := d.vertices[hash]
if !ok {
return vertex, fmt.Errorf("vertex with hash %v doesn't exist", hash)
return vertex, ErrVertexNotFound
}

return vertex, nil
Expand Down
10 changes: 7 additions & 3 deletions graph.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,12 @@ package graph

import "errors"

// ErrEdgeNotFound will be returned when a desired edge cannot be found.
var ErrEdgeNotFound = errors.New("edge not found")
var (
// ErrVertexNotFound will be returned when a desired vertex cannot be found.
ErrVertexNotFound = errors.New("vertex not found")
// ErrEdgeNotFound will be returned when a desired edge cannot be found.
ErrEdgeNotFound = errors.New("edge not found")
)

// Graph represents a generic graph data structure consisting of vertices and edges. Its vertices
// are of type T, and each vertex is identified by a hash of type K.
Expand All @@ -26,7 +30,7 @@ type Graph[K comparable, T any] interface {
//
AddVertex(value T, options ...func(*VertexProperties)) error

// Vertex returns the vertex with the given hash or an error if the vertex doesn't exist.
// Vertex returns the vertex with the given hash or ErrVertexNotFound if it doesn't exist.
Vertex(hash K) (T, error)

// VertexWithProperties returns the vertex with the given hash along with its properties, or an
Expand Down
2 changes: 1 addition & 1 deletion undirected.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ func (u *undirected[K, T]) AddVertex(value T, options ...func(*VertexProperties)
func (u *undirected[K, T]) Vertex(hash K) (T, error) {
vertex, ok := u.vertices[hash]
if !ok {
return vertex, fmt.Errorf("vertex with hash %v doesn't exist", hash)
return vertex, ErrVertexNotFound
}

return vertex, nil
Expand Down

0 comments on commit f1f0bea

Please sign in to comment.