From aaba4af64230614558322c9910e63191dff1ce77 Mon Sep 17 00:00:00 2001 From: f01c33 Date: Fri, 10 May 2024 16:23:04 -0300 Subject: [PATCH 1/3] made stuff generic --- BinarySearch/BinarySearch.go | 12 +++--- BinaryTree/BinaryTree.go | 39 +++++++++--------- BubbleSort/BubbleSort.go | 6 ++- CircularBuffer/CircularBuffer.go | 12 +++--- CocktailSort/CocktailSort.go | 8 ++-- CombSort/CombSort.go | 6 ++- DoublyLinkedList/DoublyLinkedList.go | 55 ++++++++++++++------------ ExponentialSearch/ExponentialSearch.go | 16 ++++---- GnomeSort/GnomeSort.go | 6 ++- HashTable/HashTable.go | 43 +++++++++++--------- InsertionSort/InsertionSort.go | 6 ++- JumpSearch/JumpSearch.go | 11 ++++-- LinearSearch/LinearSearch.go | 6 ++- LinkedList/LinkedList.go | 48 +++++++++++----------- MergeSort/MergeSort.go | 8 ++-- Queue(LinkedList)/Queue.go | 31 ++++++++------- ReverseArray/ReverseArray.go | 2 +- SelectionSort/SelectionSort.go | 6 ++- Stack(Array)/Stack.go | 21 +++++----- Stack(LinkedList)/Stack.go | 34 +++++++++------- TernarySearch/TernarySearch.go | 12 +++--- Trie/Trie.go | 22 +++++------ 22 files changed, 230 insertions(+), 180 deletions(-) diff --git a/BinarySearch/BinarySearch.go b/BinarySearch/BinarySearch.go index 9e34c28..1f96134 100644 --- a/BinarySearch/BinarySearch.go +++ b/BinarySearch/BinarySearch.go @@ -1,17 +1,19 @@ package BinarySearch -func BinarySearch(array []int, number int) int { +import "cmp" + +func BinarySearch[T cmp.Ordered](array []T, value T) int { minIndex := 0 maxIndex := len(array) - 1 for minIndex <= maxIndex { - midIndex := int((maxIndex + minIndex) / 2) + midIndex := (maxIndex + minIndex) / 2 midItem := array[midIndex] - if number == midItem { + if value == midItem { return midIndex } - if midItem < number { + if cmp.Less(midItem, value) { minIndex = midIndex + 1 - } else if midItem > number { + } else { maxIndex = midIndex - 1 } } diff --git a/BinaryTree/BinaryTree.go b/BinaryTree/BinaryTree.go index cb11727..84133c6 100644 --- a/BinaryTree/BinaryTree.go +++ b/BinaryTree/BinaryTree.go @@ -1,32 +1,34 @@ package BinaryTree -type Node struct { - data int - parent *Node - left *Node - right *Node +import "cmp" + +type Node[T cmp.Ordered] struct { + data T + parent *Node[T] + left *Node[T] + right *Node[T] } -type BinaryTree struct { - root *Node +type BinaryTree[T cmp.Ordered] struct { + root *Node[T] } -func (tree *BinaryTree) InsertItem(i int) { +func (tree *BinaryTree[T]) InsertItem(i T) { if tree.root == nil { - tree.root = &Node{data: i} + tree.root = &Node[T]{data: i} return } currentNode := tree.root for { if i > currentNode.data { if currentNode.right == nil { - currentNode.right = &Node{data: i, parent: currentNode} + currentNode.right = &Node[T]{data: i, parent: currentNode} return } currentNode = currentNode.right } else { if currentNode.left == nil { - currentNode.left = &Node{data: i, parent: currentNode} + currentNode.left = &Node[T]{data: i, parent: currentNode} return } currentNode = currentNode.left @@ -34,24 +36,25 @@ func (tree *BinaryTree) InsertItem(i int) { } } -func (tree *BinaryTree) SearchItem(i int) (*Node, bool) { +func (tree *BinaryTree[T]) SearchItem(i T) (*Node[T], bool) { if tree.root == nil { return nil, false } currentNode := tree.root for currentNode != nil { - if i == currentNode.data { + + if cmp.Compare(i, currentNode.data) == 0 { return currentNode, true - } else if i > currentNode.data { + } else if cmp.Compare(i, currentNode.data) == 1 { currentNode = currentNode.right - } else if i < currentNode.data { + } else if cmp.Compare(i, currentNode.data) == -1 { currentNode = currentNode.left } } return nil, false } -func (tree *BinaryTree) InorderTraversal(subtree *Node, callback func(int)) { +func (tree *BinaryTree[T]) InorderTraversal(subtree *Node[T], callback func(T)) { if subtree.left != nil { tree.InorderTraversal(subtree.left, callback) } @@ -61,7 +64,7 @@ func (tree *BinaryTree) InorderTraversal(subtree *Node, callback func(int)) { } } -func (tree *BinaryTree) PreorderTraversal(subtree *Node, callback func(int)) { +func (tree *BinaryTree[T]) PreorderTraversal(subtree *Node[T], callback func(T)) { callback(subtree.data) if subtree.left != nil { tree.PreorderTraversal(subtree.left, callback) @@ -71,7 +74,7 @@ func (tree *BinaryTree) PreorderTraversal(subtree *Node, callback func(int)) { } } -func (tree *BinaryTree) PostorderTraversal(subtree *Node, callback func(int)) { +func (tree *BinaryTree[T]) PostorderTraversal(subtree *Node[T], callback func(T)) { if subtree.left != nil { tree.PostorderTraversal(subtree.left, callback) } diff --git a/BubbleSort/BubbleSort.go b/BubbleSort/BubbleSort.go index 094158f..e028a2a 100644 --- a/BubbleSort/BubbleSort.go +++ b/BubbleSort/BubbleSort.go @@ -1,11 +1,13 @@ package BubbleSort -func BubbleSort(array []int) { +import "cmp" + +func BubbleSort[T cmp.Ordered](array []T) { swapCount := 1 for swapCount > 0 { swapCount = 0 for itemIndex := 0; itemIndex < len(array)-1; itemIndex++ { - if array[itemIndex] > array[itemIndex+1] { + if cmp.Compare(array[itemIndex], array[itemIndex+1]) == 1 { array[itemIndex], array[itemIndex+1] = array[itemIndex+1], array[itemIndex] swapCount += 1 } diff --git a/CircularBuffer/CircularBuffer.go b/CircularBuffer/CircularBuffer.go index 10e0fd3..f08312c 100644 --- a/CircularBuffer/CircularBuffer.go +++ b/CircularBuffer/CircularBuffer.go @@ -2,12 +2,12 @@ package CircularBuffer const arraySize = 10 -type CircularBuffer struct { - data [arraySize]int +type CircularBuffer[T any] struct { + data [arraySize]T pointer int } -func (b *CircularBuffer) InsertValue(i int) { +func (b *CircularBuffer[T]) InsertValue(i T) { if b.pointer == len(b.data) { b.pointer = 0 } @@ -15,12 +15,12 @@ func (b *CircularBuffer) InsertValue(i int) { b.pointer += 1 } -func (b *CircularBuffer) GetValues() [arraySize]int { +func (b *CircularBuffer[T]) GetValues() [arraySize]T { return b.data } -func (b *CircularBuffer) GetValuesFromPosition(i int) ([arraySize]int, bool) { - var out [arraySize]int +func (b *CircularBuffer[T]) GetValuesFromPosition(i int) ([arraySize]T, bool) { + var out [arraySize]T if i >= len(out) { return out, false diff --git a/CocktailSort/CocktailSort.go b/CocktailSort/CocktailSort.go index 7b8ea84..ec8b0d7 100644 --- a/CocktailSort/CocktailSort.go +++ b/CocktailSort/CocktailSort.go @@ -1,17 +1,19 @@ package CocktailSort -func CocktailSort(array []int) { +import "cmp" + +func CocktailSort[T cmp.Ordered](array []T) { swapCount := 1 for swapCount > 0 { swapCount = 0 for itemIndex := 0; itemIndex < len(array)-1; itemIndex++ { - if array[itemIndex] > array[itemIndex+1] { + if cmp.Compare(array[itemIndex], array[itemIndex+1]) == 1 { array[itemIndex], array[itemIndex+1] = array[itemIndex+1], array[itemIndex] swapCount += 1 } } for itemIndex := len(array) - 1; itemIndex > 0; itemIndex-- { - if array[itemIndex] < array[itemIndex-1] { + if cmp.Compare(array[itemIndex], array[itemIndex-1]) == -1 { array[itemIndex], array[itemIndex-1] = array[itemIndex-1], array[itemIndex] swapCount += 1 } diff --git a/CombSort/CombSort.go b/CombSort/CombSort.go index 962f403..568671e 100644 --- a/CombSort/CombSort.go +++ b/CombSort/CombSort.go @@ -1,6 +1,8 @@ package CombSort -func CombSort(array []int) { +import "cmp" + +func CombSort[T cmp.Ordered](array []T) { gapValue := len(array) swapCount := 1 for gapValue >= 1 && swapCount != 0 { @@ -11,7 +13,7 @@ func CombSort(array []int) { firstItem := 0 secondItem := gapValue for secondItem != len(array) { - if array[firstItem] > array[secondItem] { + if cmp.Compare(array[firstItem], array[secondItem]) == 1 { array[firstItem], array[secondItem] = array[secondItem], array[firstItem] swapCount += 1 } diff --git a/DoublyLinkedList/DoublyLinkedList.go b/DoublyLinkedList/DoublyLinkedList.go index e6e39f9..4733b1a 100644 --- a/DoublyLinkedList/DoublyLinkedList.go +++ b/DoublyLinkedList/DoublyLinkedList.go @@ -1,18 +1,20 @@ package DoublyLinkedList -type Node struct { - data int - next *Node - prev *Node +import "cmp" + +type Node[T cmp.Ordered] struct { + data T + next *Node[T] + prev *Node[T] } -type LinkedList struct { - head *Node - tail *Node +type LinkedList[T cmp.Ordered] struct { + head *Node[T] + tail *Node[T] } -func (list *LinkedList) InsertFirst(i int) { - data := &Node{data: i} +func (list *LinkedList[T]) InsertFirst(i T) { + data := &Node[T]{data: i} if list.head != nil { list.head.prev = data data.next = list.head @@ -20,8 +22,8 @@ func (list *LinkedList) InsertFirst(i int) { list.head = data } -func (list *LinkedList) InsertLast(i int) { - data := &Node{data: i} +func (list *LinkedList[T]) InsertLast(i T) { + data := &Node[T]{data: i} if list.head == nil { list.head = data list.tail = data @@ -34,23 +36,23 @@ func (list *LinkedList) InsertLast(i int) { list.tail = data } -func (list *LinkedList) RemoveByValue(i int) bool { +func (list *LinkedList[T]) RemoveByValue(i T) bool { if list.head == nil { return false } - if list.head.data == i { + if cmp.Compare(list.head.data, i) == 0 { list.head = list.head.next list.head.prev = nil return true } - if list.tail.data == i { + if cmp.Compare(list.tail.data, i) == 0 { list.tail = list.tail.prev list.tail.next = nil return true } current := list.head for current.next != nil { - if current.next.data == i { + if cmp.Compare(current.next.data, i) == 0 { if current.next.next != nil { current.next.next.prev = current } @@ -62,7 +64,7 @@ func (list *LinkedList) RemoveByValue(i int) bool { return false } -func (list *LinkedList) RemoveByIndex(i int) bool { +func (list *LinkedList[T]) RemoveByIndex(i int) bool { if list.head == nil { return false } @@ -88,13 +90,13 @@ func (list *LinkedList) RemoveByIndex(i int) bool { return true } -func (list *LinkedList) SearchValue(i int) bool { +func (list *LinkedList[T]) SearchValue(i T) bool { if list.head == nil { return false } current := list.head for current != nil { - if current.data == i { + if cmp.Compare(current.data, i) == 0 { return true } current = current.next @@ -102,14 +104,15 @@ func (list *LinkedList) SearchValue(i int) bool { return false } -func (list *LinkedList) GetFirst() (int, bool) { +func (list *LinkedList[T]) GetFirst() (T, bool) { + var t T if list.head == nil { - return 0, false + return t, false } return list.head.data, true } -func (list *LinkedList) GetLast() (int, bool) { +func (list *LinkedList[T]) GetLast() (T, bool) { if list.head == nil { return 0, false } @@ -120,7 +123,7 @@ func (list *LinkedList) GetLast() (int, bool) { return current.data, true } -func (list *LinkedList) GetSize() int { +func (list *LinkedList[T]) GetSize() int { count := 0 current := list.head for current != nil { @@ -131,8 +134,8 @@ func (list *LinkedList) GetSize() int { return count } -func (list *LinkedList) GetItemsFromStart() []int { - var items []int +func (list *LinkedList[T]) GetItemsFromStart() []T { + var items []T current := list.head for current != nil { items = append(items, current.data) @@ -141,8 +144,8 @@ func (list *LinkedList) GetItemsFromStart() []int { return items } -func (list *LinkedList) GetItemsFromEnd() []int { - var items []int +func (list *LinkedList[T]) GetItemsFromEnd() []T { + var items []T current := list.tail for current != nil { items = append(items, current.data) diff --git a/ExponentialSearch/ExponentialSearch.go b/ExponentialSearch/ExponentialSearch.go index 7435044..42b7227 100644 --- a/ExponentialSearch/ExponentialSearch.go +++ b/ExponentialSearch/ExponentialSearch.go @@ -1,28 +1,30 @@ package ExponentialSearch -func ExponentialSearch(array []int, number int) int { +import "cmp" + +func ExponentialSearch[T cmp.Ordered](array []T, val T) int { boundValue := 1 - for boundValue < len(array) && array[boundValue] < number { + for boundValue < len(array) && array[boundValue] < val { boundValue *= 2 } if boundValue > len(array) { boundValue = len(array) - 1 } - return BinarySearch(array, boundValue+1, number) + return BinarySearch(array, boundValue+1, val) } -func BinarySearch(array []int, bound, number int) int { +func BinarySearch[T cmp.Ordered](array []T, bound int, number T) int { minIndex := 0 maxIndex := bound - 1 for minIndex <= maxIndex { midIndex := int((maxIndex + minIndex) / 2) midItem := array[midIndex] - if number == midItem { + if cmp.Compare(number, midItem) == 0 { return midIndex } - if midItem < number { + if cmp.Compare(midItem, number) == -1 { minIndex = midIndex + 1 - } else if midItem > number { + } else { maxIndex = midIndex - 1 } } diff --git a/GnomeSort/GnomeSort.go b/GnomeSort/GnomeSort.go index 0a2d11d..3cbe6c5 100644 --- a/GnomeSort/GnomeSort.go +++ b/GnomeSort/GnomeSort.go @@ -1,9 +1,11 @@ package GnomeSort -func GnomeSort(array []int) { +import "cmp" + +func GnomeSort[T cmp.Ordered](array []T) { itemIndex := 0 for itemIndex < len(array)-1 { - if array[itemIndex] > array[itemIndex+1] { + if cmp.Compare(array[itemIndex], array[itemIndex+1]) == 1 { array[itemIndex], array[itemIndex+1] = array[itemIndex+1], array[itemIndex] if itemIndex != 0 { itemIndex -= 1 diff --git a/HashTable/HashTable.go b/HashTable/HashTable.go index 86e9ae8..358d3fe 100644 --- a/HashTable/HashTable.go +++ b/HashTable/HashTable.go @@ -1,35 +1,39 @@ package HashTable -import "hash/fnv" +import ( + "cmp" + "hash/fnv" + "unsafe" +) -type TableItem struct { - key string - data int - next *TableItem +type TableItem[K cmp.Ordered, V any] struct { + key K + data V + next *TableItem[K, V] } -type HashTable struct { - data [256]*TableItem +type HashTable[K cmp.Ordered, V any] struct { + data [256]*TableItem[K, V] } -func (table *HashTable) Add(key string, i int) { +func (table *HashTable[K, V]) Add(key K, i V) { position := generateHash(key) if table.data[position] == nil { - table.data[position] = &TableItem{key: key, data: i} + table.data[position] = &TableItem[K, V]{key: key, data: i} return } current := table.data[position] for current.next != nil { current = current.next } - current.next = &TableItem{key: key, data: i} + current.next = &TableItem[K, V]{key: key, data: i} } -func (table *HashTable) Get(key string) (int, bool) { +func (table *HashTable[K, V]) Get(key K) (V, bool) { position := generateHash(key) current := table.data[position] for current != nil { - if current.key == key { + if cmp.Compare(current.key, key) == 0 { return current.data, true } current = current.next @@ -37,11 +41,11 @@ func (table *HashTable) Get(key string) (int, bool) { return 0, false } -func (table *HashTable) Set(key string, value int) bool { +func (table *HashTable[K, V]) Set(key K, value V) bool { position := generateHash(key) current := table.data[position] for current != nil { - if current.key == key { + if cmp.Compare(current.key, key) == 0 { current.data = value return true } @@ -50,18 +54,18 @@ func (table *HashTable) Set(key string, value int) bool { return false } -func (table *HashTable) Remove(key string) bool { +func (table *HashTable[K, V]) Remove(key K) bool { position := generateHash(key) if table.data[position] == nil { return false } - if table.data[position].key == key { + if cmp.Compare(table.data[position].key, key) == 0 { table.data[position] = table.data[position].next return true } current := table.data[position] for current.next != nil { - if current.next.key == key { + if cmp.Compare(current.next.key, key) == 0 { current.next = current.next.next return true } @@ -70,8 +74,9 @@ func (table *HashTable) Remove(key string) bool { return false } -func generateHash(s string) uint8 { +func generateHash[K any](s K) uint8 { hash := fnv.New32a() - hash.Write([]byte(s)) + b := unsafe.Slice(&s, unsafe.Sizeof(s)) + hash.Write(b) return uint8(hash.Sum32() % 256) } diff --git a/InsertionSort/InsertionSort.go b/InsertionSort/InsertionSort.go index 250a2df..188b359 100644 --- a/InsertionSort/InsertionSort.go +++ b/InsertionSort/InsertionSort.go @@ -1,8 +1,10 @@ package InsertionSort -func InsertionSort(array []int) { +import "cmp" + +func InsertionSort[T cmp.Ordered](array []T) { for itemIndex, itemValue := range array { - for itemIndex != 0 && array[itemIndex-1] > itemValue { + for itemIndex != 0 && cmp.Compare(array[itemIndex-1], itemValue) == 1 { array[itemIndex] = array[itemIndex-1] itemIndex -= 1 } diff --git a/JumpSearch/JumpSearch.go b/JumpSearch/JumpSearch.go index f0e6495..420ea51 100644 --- a/JumpSearch/JumpSearch.go +++ b/JumpSearch/JumpSearch.go @@ -1,12 +1,15 @@ package JumpSearch -import "math" +import ( + "cmp" + "math" +) -func JumpSearch(array []int, number int) int { +func JumpSearch[T cmp.Ordered](array []T, number T) int { jumpValue := int(math.Floor(math.Sqrt(float64(len(array))))) minIndex := 0 maxIndex := jumpValue - for array[maxIndex] <= number { + for cmp.Compare(array[maxIndex], number) == -1 { minIndex += jumpValue maxIndex = minIndex + jumpValue if maxIndex >= len(array) { @@ -16,7 +19,7 @@ func JumpSearch(array []int, number int) int { } } for i := minIndex; i < maxIndex; i++ { - if array[i] == number { + if cmp.Compare(array[i], number) == 0 { return i } } diff --git a/LinearSearch/LinearSearch.go b/LinearSearch/LinearSearch.go index ef5c150..4107b05 100644 --- a/LinearSearch/LinearSearch.go +++ b/LinearSearch/LinearSearch.go @@ -1,8 +1,10 @@ package LinearSearch -func LinearSearch(array []int, number int) int { +import "cmp" + +func LinearSearch[T cmp.Ordered](array []T, number T) int { for index, value := range array { - if value == number { + if cmp.Compare(value, number) == 0 { return index } } diff --git a/LinkedList/LinkedList.go b/LinkedList/LinkedList.go index 4a741cd..c128a89 100644 --- a/LinkedList/LinkedList.go +++ b/LinkedList/LinkedList.go @@ -1,24 +1,26 @@ package LinkedList -type Node struct { - data int - next *Node +import "cmp" + +type Node[T cmp.Ordered] struct { + data T + next *Node[T] } -type LinkedList struct { - head *Node +type LinkedList[T cmp.Ordered] struct { + head *Node[T] } -func (list *LinkedList) InsertFirst(i int) { - data := &Node{data: i} +func (list *LinkedList[T]) InsertFirst(i T) { + data := &Node[T]{data: i} if list.head != nil { data.next = list.head } list.head = data } -func (list *LinkedList) InsertLast(i int) { - data := &Node{data: i} +func (list *LinkedList[T]) InsertLast(i T) { + data := &Node[T]{data: i} if list.head == nil { list.head = data return @@ -30,17 +32,17 @@ func (list *LinkedList) InsertLast(i int) { current.next = data } -func (list *LinkedList) RemoveByValue(i int) bool { +func (list *LinkedList[T]) RemoveByValue(i T) bool { if list.head == nil { return false } - if list.head.data == i { + if cmp.Compare(list.head.data, i) == 0 { list.head = list.head.next return true } current := list.head for current.next != nil { - if current.next.data == i { + if cmp.Compare(current.next.data, i) == 0 { current.next = current.next.next return true } @@ -49,7 +51,7 @@ func (list *LinkedList) RemoveByValue(i int) bool { return false } -func (list *LinkedList) RemoveByIndex(i int) bool { +func (list *LinkedList[T]) RemoveByIndex(i int) bool { if list.head == nil { return false } @@ -71,13 +73,13 @@ func (list *LinkedList) RemoveByIndex(i int) bool { return true } -func (list *LinkedList) SearchValue(i int) bool { +func (list *LinkedList[T]) SearchValue(i T) bool { if list.head == nil { return false } current := list.head for current != nil { - if current.data == i { + if cmp.Compare(current.data, i) == 0 { return true } current = current.next @@ -85,16 +87,18 @@ func (list *LinkedList) SearchValue(i int) bool { return false } -func (list *LinkedList) GetFirst() (int, bool) { +func (list *LinkedList[T]) GetFirst() (T, bool) { + var t T if list.head == nil { - return 0, false + return t, false } return list.head.data, true } -func (list *LinkedList) GetLast() (int, bool) { +func (list *LinkedList[T]) GetLast() (T, bool) { + var t T if list.head == nil { - return 0, false + return t, false } current := list.head for current.next != nil { @@ -103,7 +107,7 @@ func (list *LinkedList) GetLast() (int, bool) { return current.data, true } -func (list *LinkedList) GetSize() int { +func (list *LinkedList[T]) GetSize() int { count := 0 current := list.head for current != nil { @@ -113,8 +117,8 @@ func (list *LinkedList) GetSize() int { return count } -func (list *LinkedList) GetItems() []int { - var items []int +func (list *LinkedList[T]) GetItems() []T { + var items []T current := list.head for current != nil { items = append(items, current.data) diff --git a/MergeSort/MergeSort.go b/MergeSort/MergeSort.go index 895e183..78b85d3 100644 --- a/MergeSort/MergeSort.go +++ b/MergeSort/MergeSort.go @@ -1,7 +1,9 @@ package MergeSort -func mergeParts(array []int, leftIndex, divideIndex, rightIndex int) { - var tempArray1, tempArray2 []int +import "cmp" + +func mergeParts[T cmp.Ordered](array []T, leftIndex, divideIndex, rightIndex int) { + var tempArray1, tempArray2 []T for i := leftIndex; i <= divideIndex; i++ { tempArray1 = append(tempArray1, array[i]) } @@ -34,7 +36,7 @@ func mergeParts(array []int, leftIndex, divideIndex, rightIndex int) { } } -func MergeSort(array []int, leftIndex, rightIndex int) { +func MergeSort[T cmp.Ordered](array []T, leftIndex, rightIndex int) { if leftIndex >= rightIndex { return } diff --git a/Queue(LinkedList)/Queue.go b/Queue(LinkedList)/Queue.go index 74ddb20..ee4c270 100644 --- a/Queue(LinkedList)/Queue.go +++ b/Queue(LinkedList)/Queue.go @@ -1,25 +1,28 @@ package QueueLinkedList -type Node struct { - data int - next *Node +import "cmp" + +type Node[T cmp.Ordered] struct { + data T + next *Node[T] } -type Queue struct { - rear *Node +type Queue[T cmp.Ordered] struct { + rear *Node[T] } -func (list *Queue) Enqueue(i int) { - data := &Node{data: i} +func (list *Queue[T]) Enqueue(i T) { + data := &Node[T]{data: i} if list.rear != nil { data.next = list.rear } list.rear = data } -func (list *Queue) Dequeue() (int, bool) { +func (list *Queue[T]) Dequeue() (T, bool) { + var t T if list.rear == nil { - return 0, false + return t, false } if list.rear.next == nil { i := list.rear.data @@ -37,15 +40,15 @@ func (list *Queue) Dequeue() (int, bool) { } } -func (list *Queue) Peek() (int, bool) { +func (list *Queue[t]) Peek() (T, bool) { if list.rear == nil { return 0, false } return list.rear.data, true } -func (list *Queue) Get() []int { - var items []int +func (list *Queue[T]) Get() []T { + var items []T current := list.rear for current != nil { items = append(items, current.data) @@ -54,10 +57,10 @@ func (list *Queue) Get() []int { return items } -func (list *Queue) IsEmpty() bool { +func (list *Queue[T]) IsEmpty() bool { return list.rear == nil } -func (list *Queue) Empty() { +func (list *Queue[T]) Empty() { list.rear = nil } diff --git a/ReverseArray/ReverseArray.go b/ReverseArray/ReverseArray.go index bc7815c..806715e 100644 --- a/ReverseArray/ReverseArray.go +++ b/ReverseArray/ReverseArray.go @@ -1,6 +1,6 @@ package ReverseArray -func ReverseArray(a []int) { +func ReverseArray[T any](a []T) { i := 0 u := len(a) - 1 for i < u { diff --git a/SelectionSort/SelectionSort.go b/SelectionSort/SelectionSort.go index f0c91b3..2b503ea 100644 --- a/SelectionSort/SelectionSort.go +++ b/SelectionSort/SelectionSort.go @@ -1,11 +1,13 @@ package SelectionSort -func SelectionSort(array []int) { +import "cmp" + +func SelectionSort[T cmp.Ordered](array []T) { for arrayIndex := range array { minValue := array[arrayIndex] minIndex := arrayIndex for subArrayIndex := arrayIndex + 1; subArrayIndex < len(array); subArrayIndex++ { - if array[subArrayIndex] < minValue { + if cmp.Compare(array[subArrayIndex], minValue) == -1 { minValue = array[subArrayIndex] minIndex = subArrayIndex } diff --git a/Stack(Array)/Stack.go b/Stack(Array)/Stack.go index c6ae2d5..0405591 100644 --- a/Stack(Array)/Stack.go +++ b/Stack(Array)/Stack.go @@ -1,13 +1,15 @@ package StackArray +import "cmp" + const arraySize = 10 -type Stack struct { +type Stack[T cmp.Ordered] struct { top int - data [arraySize]int + data [arraySize]T } -func (s *Stack) Push(i int) bool { +func (s *Stack[T]) Push(i T) bool { if s.top == len(s.data) { return false } @@ -16,27 +18,28 @@ func (s *Stack) Push(i int) bool { return true } -func (s *Stack) Pop() (int, bool) { +func (s *Stack[T]) Pop() (T, bool) { + var t T if s.top == 0 { - return 0, false + return t, false } i := s.data[s.top-1] s.top -= 1 return i, true } -func (s *Stack) Peek() int { +func (s *Stack[T]) Peek() T { return s.data[s.top-1] } -func (s *Stack) Get() []int { +func (s *Stack[T]) Get() []T { return s.data[:s.top] } -func (s *Stack) IsEmpty() bool { +func (s *Stack[T]) IsEmpty() bool { return s.top == 0 } -func (s *Stack) Empty() { +func (s *Stack[T]) Empty() { s.top = 0 } diff --git a/Stack(LinkedList)/Stack.go b/Stack(LinkedList)/Stack.go index 73c6b2d..da4fbf3 100644 --- a/Stack(LinkedList)/Stack.go +++ b/Stack(LinkedList)/Stack.go @@ -1,41 +1,45 @@ package StackLinkedList -type Node struct { - data int - next *Node +import "cmp" + +type Node[T cmp.Ordered] struct { + data T + next *Node[T] } -type Stack struct { - top *Node +type Stack[T cmp.Ordered] struct { + top *Node[T] } -func (list *Stack) Push(i int) { - data := &Node{data: i} +func (list *Stack[T]) Push(i T) { + data := &Node[T]{data: i} if list.top != nil { data.next = list.top } list.top = data } -func (list *Stack) Pop() (int, bool) { +func (list *Stack[T]) Pop() (T, bool) { + var t T if list.top == nil { - return 0, false + return t, false } i := list.top.data list.top = list.top.next return i, true } -func (list *Stack) Peek() (int, bool) { +func (list *Stack[T]) Peek() (T, bool) { + var t T if list.top == nil { - return 0, false + return t, false } return list.top.data, true } -func (list *Stack) Get() []int { +func (list *Stack[T]) Get() []T { - var items []int + var items []T current := list.top for current != nil { @@ -45,10 +49,10 @@ func (list *Stack) Get() []int { return items } -func (list *Stack) IsEmpty() bool { +func (list *Stack[T]) IsEmpty() bool { return list.top == nil } -func (list *Stack) Empty() { +func (list *Stack[T]) Empty() { list.top = nil } diff --git a/TernarySearch/TernarySearch.go b/TernarySearch/TernarySearch.go index 165fee6..d3c0ff4 100644 --- a/TernarySearch/TernarySearch.go +++ b/TernarySearch/TernarySearch.go @@ -1,6 +1,8 @@ package TernarySearch -func TernarySearch(array []int, number int) int { +import "cmp" + +func TernarySearch[T cmp.Ordered](array []T, number T) int { minIndex := 0 maxIndex := len(array) - 1 for minIndex <= maxIndex { @@ -8,14 +10,14 @@ func TernarySearch(array []int, number int) int { midIndex2 := maxIndex - int((maxIndex-minIndex)/3) midItem1 := array[midIndex1] midItem2 := array[midIndex2] - if midItem1 == number { + if cmp.Compare(midItem1, number) == 0 { return midIndex1 - } else if midItem2 == number { + } else if cmp.Compare(midItem2, number) == 0 { return midIndex2 } - if midItem1 < number { + if cmp.Compare(midItem1, number) == -1 { minIndex = midIndex1 + 1 - } else if midItem2 > number { + } else if cmp.Compare(midItem2, number) == 1 { maxIndex = midIndex2 - 1 } else { minIndex = midIndex1 + 1 diff --git a/Trie/Trie.go b/Trie/Trie.go index d583187..42e5cbd 100644 --- a/Trie/Trie.go +++ b/Trie/Trie.go @@ -1,26 +1,26 @@ package Trie -type Node struct { +type Node[T any] struct { last bool - parent *Node - children map[int32]*Node + parent *Node[T] + children map[int32]*Node[T] } -type Trie struct { - root *Node +type Trie[T any] struct { + root *Node[T] } -func (trie *Trie) Init() { - trie.root = &Node{children: map[int32]*Node{}} +func (trie *Trie[T]) Init() { + trie.root = &Node[T]{children: map[int32]*Node[T]{}} } -func (trie Trie) Add(item string) { +func (trie Trie[T]) Add(item string) { currentNode := trie.root for _, r := range item { if _, ok := currentNode.children[r]; ok { currentNode = currentNode.children[r] } else { - node := &Node{children: map[int32]*Node{}, parent: currentNode} + node := &Node[T]{children: map[int32]*Node[T]{}, parent: currentNode} currentNode.children[r] = node currentNode = node } @@ -28,7 +28,7 @@ func (trie Trie) Add(item string) { currentNode.last = true } -func (trie Trie) Search(item string) bool { +func (trie Trie[T]) Search(item string) bool { currentNode := trie.root for _, r := range item { if _, ok := currentNode.children[r]; ok { @@ -43,7 +43,7 @@ func (trie Trie) Search(item string) bool { return true } -func (trie Trie) Remove(item string) bool { +func (trie Trie[T]) Remove(item string) bool { currentNode := trie.root for _, r := range item { if _, ok := currentNode.children[r]; ok { From b107e9ff94c7418974d45428ad1e14831c46d344 Mon Sep 17 00:00:00 2001 From: f01c33 Date: Fri, 10 May 2024 16:36:26 -0300 Subject: [PATCH 2/3] bugfixes and other stuff --- DoublyLinkedList/DoublyLinkedList.go | 3 ++- HashTable/HashTable.go | 8 ++++---- JumpSearch/JumpSearch.go | 3 ++- {Queue(LinkedList) => Queue_LinkedList}/Queue.go | 5 +++-- {Stack(Array) => Stack_Array}/Stack.go | 0 {Stack(LinkedList) => Stack_LinkedList}/Stack.go | 0 go.mod | 3 +++ 7 files changed, 14 insertions(+), 8 deletions(-) rename {Queue(LinkedList) => Queue_LinkedList}/Queue.go (93%) rename {Stack(Array) => Stack_Array}/Stack.go (100%) rename {Stack(LinkedList) => Stack_LinkedList}/Stack.go (100%) create mode 100644 go.mod diff --git a/DoublyLinkedList/DoublyLinkedList.go b/DoublyLinkedList/DoublyLinkedList.go index 4733b1a..1dc2c19 100644 --- a/DoublyLinkedList/DoublyLinkedList.go +++ b/DoublyLinkedList/DoublyLinkedList.go @@ -113,8 +113,9 @@ func (list *LinkedList[T]) GetFirst() (T, bool) { } func (list *LinkedList[T]) GetLast() (T, bool) { + var t T if list.head == nil { - return 0, false + return t, false } current := list.head for current.next != nil { diff --git a/HashTable/HashTable.go b/HashTable/HashTable.go index 358d3fe..068fec2 100644 --- a/HashTable/HashTable.go +++ b/HashTable/HashTable.go @@ -2,8 +2,8 @@ package HashTable import ( "cmp" + "fmt" "hash/fnv" - "unsafe" ) type TableItem[K cmp.Ordered, V any] struct { @@ -38,7 +38,8 @@ func (table *HashTable[K, V]) Get(key K) (V, bool) { } current = current.next } - return 0, false + var t V + return t, false } func (table *HashTable[K, V]) Set(key K, value V) bool { @@ -76,7 +77,6 @@ func (table *HashTable[K, V]) Remove(key K) bool { func generateHash[K any](s K) uint8 { hash := fnv.New32a() - b := unsafe.Slice(&s, unsafe.Sizeof(s)) - hash.Write(b) + hash.Write([]byte(fmt.Sprint(s))) return uint8(hash.Sum32() % 256) } diff --git a/JumpSearch/JumpSearch.go b/JumpSearch/JumpSearch.go index 420ea51..18a7969 100644 --- a/JumpSearch/JumpSearch.go +++ b/JumpSearch/JumpSearch.go @@ -9,7 +9,8 @@ func JumpSearch[T cmp.Ordered](array []T, number T) int { jumpValue := int(math.Floor(math.Sqrt(float64(len(array))))) minIndex := 0 maxIndex := jumpValue - for cmp.Compare(array[maxIndex], number) == -1 { + comparisson := cmp.Compare(array[maxIndex], number) + for comparisson == -1 || comparisson == 0 { minIndex += jumpValue maxIndex = minIndex + jumpValue if maxIndex >= len(array) { diff --git a/Queue(LinkedList)/Queue.go b/Queue_LinkedList/Queue.go similarity index 93% rename from Queue(LinkedList)/Queue.go rename to Queue_LinkedList/Queue.go index ee4c270..71a5ee1 100644 --- a/Queue(LinkedList)/Queue.go +++ b/Queue_LinkedList/Queue.go @@ -40,9 +40,10 @@ func (list *Queue[T]) Dequeue() (T, bool) { } } -func (list *Queue[t]) Peek() (T, bool) { +func (list *Queue[T]) Peek() (T, bool) { + var t T if list.rear == nil { - return 0, false + return t, false } return list.rear.data, true } diff --git a/Stack(Array)/Stack.go b/Stack_Array/Stack.go similarity index 100% rename from Stack(Array)/Stack.go rename to Stack_Array/Stack.go diff --git a/Stack(LinkedList)/Stack.go b/Stack_LinkedList/Stack.go similarity index 100% rename from Stack(LinkedList)/Stack.go rename to Stack_LinkedList/Stack.go diff --git a/go.mod b/go.mod new file mode 100644 index 0000000..22f428a --- /dev/null +++ b/go.mod @@ -0,0 +1,3 @@ +module Data-Structures-and-Algorithms + +go 1.22.3 From 5aeadfd7ebcfa460e4d6843d33a21e4c36b45608 Mon Sep 17 00:00:00 2001 From: f01c33 Date: Fri, 10 May 2024 16:40:12 -0300 Subject: [PATCH 3/3] go mod fix --- go.mod | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/go.mod b/go.mod index 22f428a..441763f 100644 --- a/go.mod +++ b/go.mod @@ -1,3 +1,3 @@ -module Data-Structures-and-Algorithms +module github.com/ua-nick/Data-Structures-and-Algorithms go 1.22.3