Skip to content

Commit

Permalink
Merge pull request #55 from hmcalister/ErrorRefactor
Browse files Browse the repository at this point in the history
Error refactor
  • Loading branch information
hmcalister authored Apr 28, 2024
2 parents 886fb6b + 3a330da commit f8009e2
Show file tree
Hide file tree
Showing 33 changed files with 124 additions and 196 deletions.
8 changes: 0 additions & 8 deletions heap/MaxBinaryHeap/errors.go

This file was deleted.

23 changes: 12 additions & 11 deletions heap/MaxBinaryHeap/maxBinaryHeap.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
package maxbinaryheap

import (
comparator "github.com/hmcalister/Go-DSA/Comparator"
comparator "github.com/hmcalister/Go-DSA/utils/Comparator"
dsa_error "github.com/hmcalister/Go-DSA/utils/DSA_Error"
)

type MaxBinaryHeap[T any] struct {
Expand Down Expand Up @@ -53,25 +54,25 @@ func (heap *MaxBinaryHeap[T]) maxHeapify(targetIndex int) {

// Peek at the max-element of this heap. The item is not removed from the heap.
//
// If the heap is empty, a EmptyHeapError is returned.
// If the heap is empty, a dsa_error.ErrorDataStructureEmpty is returned.
func (heap *MaxBinaryHeap[T]) PeekMax() (T, error) {
if len(heap.heapData) == 0 {
return *new(T), ErrorEmptyHeap
return *new(T), dsa_error.ErrorDataStructureEmpty
}

return heap.heapData[0], nil
}

// Find the first item in a heap matching a predicate.
//
// Returns (item, nil) if the item is present, or (*new(T), ErrorItemNotPresent) if the item is not present.
// Returns (item, nil) if the item is present, or (*new(T), dsa_error.ErrorItemNotFound) if the item is not present.
func (heap *MaxBinaryHeap[T]) Find(predicate func(item T) bool) (T, error) {
for _, item := range heap.heapData {
if predicate(item) {
return item, nil
}
}
return *new(T), ErrorItemNotFound
return *new(T), dsa_error.ErrorItemNotFound
}

// Find the first item in a heap matching a predicate.
Expand Down Expand Up @@ -121,10 +122,10 @@ func (heap *MaxBinaryHeap[T]) Add(item T) {

// Remove (and return) the top (maximal) item from this Heap.
//
// If the heap is empty, a EmptyHeapError is returned.
// If the heap is empty, a dsa_error.ErrorDataStructureEmpty is returned.
func (heap *MaxBinaryHeap[T]) RemoveMax() (T, error) {
if len(heap.heapData) == 0 {
return *new(T), ErrorEmptyHeap
return *new(T), dsa_error.ErrorDataStructureEmpty
}

// Get the root element, so we can return it later
Expand All @@ -146,11 +147,11 @@ func (heap *MaxBinaryHeap[T]) RemoveMax() (T, error) {
}

// Remove (and return) an item from the heap.
// If the heap is empty, a ErrorItemNotPresent is returned.
// If the item is not present in the tree, a ErrorItemNotPresent is returned.
// If the heap is empty, a dsa_error.ErrorDataStructureEmpty is returned.
// If the item is not present in the tree, a dsa_error.ErrorItemNotFound is returned.
func (heap *MaxBinaryHeap[T]) RemoveItem(item T) (T, error) {
if len(heap.heapData) == 0 {
return *new(T), ErrorEmptyHeap
return *new(T), dsa_error.ErrorDataStructureEmpty
}

// First, see if the element exists
Expand All @@ -164,7 +165,7 @@ func (heap *MaxBinaryHeap[T]) RemoveItem(item T) (T, error) {
}
// If we did not set the index, we did not find the item
if targetItemIndex == -1 {
return *new(T), ErrorItemNotFound
return *new(T), dsa_error.ErrorItemNotFound
}

// Here's the sneaky trick:
Expand Down
2 changes: 1 addition & 1 deletion heap/MaxBinaryHeap/maxBinaryHeap_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@ import (
"slices"
"testing"

comparator "github.com/hmcalister/Go-DSA/Comparator"
maxbinaryheap "github.com/hmcalister/Go-DSA/heap/MaxBinaryHeap"
comparator "github.com/hmcalister/Go-DSA/utils/Comparator"
)

// ----------------------------------------------------------------------------
Expand Down
23 changes: 12 additions & 11 deletions heap/MinBinaryHeap/minBinaryHeap.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
package minbinaryheap

import (
comparator "github.com/hmcalister/Go-DSA/Comparator"
comparator "github.com/hmcalister/Go-DSA/utils/Comparator"
dsa_error "github.com/hmcalister/Go-DSA/utils/DSA_Error"
)

type MinBinaryHeap[T any] struct {
Expand Down Expand Up @@ -53,25 +54,25 @@ func (heap *MinBinaryHeap[T]) minHeapify(targetIndex int) {

// Get the Min-element of this heap. The item is not removed from the heap.
//
// If the heap is empty, a EmptyHeapError is returned.
// If the heap is empty, a dsa_error.ErrorDataStructureEmpty is returned.
func (heap *MinBinaryHeap[T]) PeekMin() (T, error) {
if len(heap.heapData) == 0 {
return *new(T), ErrorEmptyHeap
return *new(T), dsa_error.ErrorDataStructureEmpty
}

return heap.heapData[0], nil
}

// Find the first item in a heap matching a predicate.
//
// Returns (item, nil) if the item is present, or (*new(T), ErrorItemNotPresent) if the item is not present.
// Returns (item, nil) if the item is present, or (*new(T), dsa_error.ErrorItemNotFound) if the item is not present.
func (heap *MinBinaryHeap[T]) Find(predicate func(item T) bool) (T, error) {
for _, item := range heap.heapData {
if predicate(item) {
return item, nil
}
}
return *new(T), ErrorItemNotFound
return *new(T), dsa_error.ErrorItemNotFound
}

// Find the first item in a heap matching a predicate.
Expand Down Expand Up @@ -121,10 +122,10 @@ func (heap *MinBinaryHeap[T]) Add(item T) {

// Remove (and return) the top (Minimal) item from this Heap.
//
// If the heap is empty, a EmptyHeapError is returned.
// If the heap is empty, a dsa_error.ErrorDataStructureEmpty is returned.
func (heap *MinBinaryHeap[T]) RemoveMin() (T, error) {
if len(heap.heapData) == 0 {
return *new(T), ErrorEmptyHeap
return *new(T), dsa_error.ErrorDataStructureEmpty
}

// Get the root element, so we can return it later
Expand All @@ -146,11 +147,11 @@ func (heap *MinBinaryHeap[T]) RemoveMin() (T, error) {
}

// Remove (and return) an item from the heap.
// If the heap is empty, a ErrorItemNotPresent is returned.
// If the item is not present in the tree, a ErrorItemNotPresent is returned.
// If the heap is empty, a dsa_error.ErrorDataStructureEmpty is returned.
// If the item is not present in the tree, a dsa_error.ErrorItemNotFound is returned.
func (heap *MinBinaryHeap[T]) RemoveItem(item T) (T, error) {
if len(heap.heapData) == 0 {
return *new(T), ErrorEmptyHeap
return *new(T), dsa_error.ErrorDataStructureEmpty
}

// First, see if the element exists
Expand All @@ -164,7 +165,7 @@ func (heap *MinBinaryHeap[T]) RemoveItem(item T) (T, error) {
}
// If we did not set the index, we did not find the item
if targetItemIndex == -1 {
return *new(T), ErrorItemNotFound
return *new(T), dsa_error.ErrorItemNotFound
}

// Here's the sneaky trick:
Expand Down
2 changes: 1 addition & 1 deletion heap/MinBinaryHeap/minBinaryHeap_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@ import (
"slices"
"testing"

comparator "github.com/hmcalister/Go-DSA/Comparator"
minbinaryheap "github.com/hmcalister/Go-DSA/heap/MinBinaryHeap"
comparator "github.com/hmcalister/Go-DSA/utils/Comparator"
)

// ----------------------------------------------------------------------------
Expand Down
11 changes: 0 additions & 11 deletions list/LinkedList/errors.go

This file was deleted.

30 changes: 17 additions & 13 deletions list/LinkedList/linkedlist.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
package linkedlist

import dsa_error "github.com/hmcalister/Go-DSA/utils/DSA_Error"

// An implementation of a doubly linked list.
type LinkedList[T any] struct {
// Head of the list, the first Node.
//
Expand Down Expand Up @@ -33,7 +36,7 @@ func (list *LinkedList[T]) Length() int {
// Get and Find methods

// Find and return the first item in the list satisfying a predicate function.
// If no item satisfies the predicate, an error is returned instead.
// If no item satisfies the predicate, a dsa_error.ErrorItemNotFound is returned instead.
//
// The list is walked forward during this search.
func (list *LinkedList[T]) Find(predicate func(item T) bool) (T, error) {
Expand All @@ -45,11 +48,11 @@ func (list *LinkedList[T]) Find(predicate func(item T) bool) (T, error) {
currentNode = currentNode.next
}

return *new(T), ErrorItemNotFound
return *new(T), dsa_error.ErrorItemNotFound
}

// Find and return the last item in the list satisfying a predicate function.
// If no item satisfies the predicate, an error is returned instead.
// If no item satisfies the predicate, a dsa_error.ErrorItemNotFound is returned instead.
//
// The list is walked backward during this search.
func (list *LinkedList[T]) ReverseFind(predicate func(item T) bool) (T, error) {
Expand All @@ -61,7 +64,7 @@ func (list *LinkedList[T]) ReverseFind(predicate func(item T) bool) (T, error) {
currentNode = currentNode.prev
}

return *new(T), ErrorItemNotFound
return *new(T), dsa_error.ErrorItemNotFound
}

// Find ALL of the items in the list satisfying a predicate.
Expand Down Expand Up @@ -98,10 +101,10 @@ func (list *LinkedList[T]) ReverseFindAll(predicate func(item T) bool) []T {

// Get the item at the specified index.
//
// Returns an error if the index is out of bounds.
// Returns a dsa_error.ErrorIndexOutOfBounds if the index is out of bounds.
func (list *LinkedList[T]) ItemAtIndex(index int) (T, error) {
if list.length <= index {
return *new(T), ErrorIndexOutOfBounds
return *new(T), dsa_error.ErrorIndexOutOfBounds
}

// If the target index is after the halfway point
Expand Down Expand Up @@ -249,7 +252,7 @@ func (list *LinkedList[T]) Add(item T) {

// Add a new item to the list in the specified position.
//
// Returns a IndexOutOfBoundsError if the specified index is out of bounds.
// Returns a dsa_error.ErrorIndexOutOfBounds if the specified index is out of bounds.
//
// Example:
//
Expand All @@ -267,7 +270,7 @@ func (list *LinkedList[T]) Add(item T) {
func (list *LinkedList[T]) AddAtIndex(item T, index int) error {
// Note here we allow list.length==index, as we *can* insert at the end of the list
if list.length < index {
return ErrorIndexOutOfBounds
return dsa_error.ErrorIndexOutOfBounds
}

newNode := &LinkedListNode[T]{
Expand Down Expand Up @@ -331,12 +334,12 @@ func (list *LinkedList[T]) AddAtIndex(item T, index int) error {

// Remove and return the item from the end of the list.
//
// Returns the item removed, or an error if the list is empty.
// Returns the item removed, or a dsa_error.ErrorDataStructureEmpty if the list is empty.
func (list *LinkedList[T]) Remove() (T, error) {
if list.length == 0 {
// Apparently idiomatic "zero-value" of a generic T is *new(T)... feels odd.
// https://stackoverflow.com/questions/70585852/return-default-value-for-generic-type
return *new(T), ErrorEmptyList
return *new(T), dsa_error.ErrorDataStructureEmpty
}

// If we have only one element, we must remove both head *and* tail
Expand All @@ -362,7 +365,8 @@ func (list *LinkedList[T]) Remove() (T, error) {

// Remove and return the item from a particular index.
//
// Returns an error if the list is empty, or is the target index is out of range.
// Returns a dsa_error.ErrorDataStructureEmpty if the list is empty,
// or a dsa_error.ErrorIndexOutOfBounds if the target index is out of range.
//
// Example:
//
Expand All @@ -385,13 +389,13 @@ func (list *LinkedList[T]) RemoveAtIndex(index int) (T, error) {
if list.length == 0 {
// Apparently idiomatic "zero-value" of a generic T is *new(T)... feels odd.
// https://stackoverflow.com/questions/70585852/return-default-value-for-generic-type
return *new(T), ErrorEmptyList
return *new(T), dsa_error.ErrorDataStructureEmpty
}

// Note here we do not allow RemoveAtIndex(list.Length()) as this is "out of bounds"
// and unlike inserting it does not make sense to define it here.
if list.length <= index {
return *new(T), ErrorIndexOutOfBounds
return *new(T), dsa_error.ErrorIndexOutOfBounds
}

// If we are removing at the tail of the list,
Expand Down
14 changes: 8 additions & 6 deletions queue/ArrayQueue/arrayqueue.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package arrayqueue

import dsa_error "github.com/hmcalister/Go-DSA/utils/DSA_Error"

// Implement a queue using a array / slice.
//
// Queues are a first in, first out data structure. Items added to the queue are removed in the order they were added.
Expand All @@ -22,10 +24,10 @@ func New[T any]() *ArrayQueue[T] {

// Peek at the front item in the queue.
//
// Returns an error if the queue is empty.
// Returns a dsa_error.ErrorDataStructureEmpty if the queue is empty.
func (queue *ArrayQueue[T]) Peek() (T, error) {
if len(queue.queueData) == 0 {
return *new(T), ErrorQueueEmpty
return *new(T), dsa_error.ErrorDataStructureEmpty
}

item := queue.queueData[0]
Expand All @@ -35,14 +37,14 @@ func (queue *ArrayQueue[T]) Peek() (T, error) {
// Find the first item in a queue matching a predicate.
// The queue is traversed from front to back.
//
// Returns (item, nil) if the item is present, or (*new(T), ErrorItemNotFound) if the item is not present.
// Returns (item, nil) if the item is present, or (*new(T), dsa_error.ErrorItemNotFound) if the item is not present.
func (queue *ArrayQueue[T]) Find(predicate func(item T) bool) (T, error) {
for _, item := range queue.queueData {
if predicate(item) {
return item, nil
}
}
return *new(T), ErrorItemNotFound
return *new(T), dsa_error.ErrorItemNotFound
}

// Find all items in a queue matching a predicate.
Expand Down Expand Up @@ -77,10 +79,10 @@ func (queue *ArrayQueue[T]) Add(item T) {

// Dequeue an item, removing from the front of the queue.
//
// Returns an error if the queue is empty.
// Returns a dsa_error.ErrorDataStructureEmpty error if the queue is empty.
func (queue *ArrayQueue[T]) Remove() (T, error) {
if len(queue.queueData) == 0 {
return *new(T), ErrorQueueEmpty
return *new(T), dsa_error.ErrorDataStructureEmpty
}

item := queue.queueData[0]
Expand Down
8 changes: 0 additions & 8 deletions queue/ArrayQueue/errors.go

This file was deleted.

8 changes: 0 additions & 8 deletions queue/LinkedListQueue/errors.go

This file was deleted.

Loading

0 comments on commit f8009e2

Please sign in to comment.