1
0
mirror of https://github.com/vlang/v.git synced 2023-08-10 21:13:21 +03:00

all: replace generic <> with [] - part 2 (#16536)

This commit is contained in:
yuyi
2022-11-27 00:23:26 +08:00
committed by GitHub
parent b19b97e7b1
commit ef5be22f81
297 changed files with 1959 additions and 1943 deletions

View File

@@ -2,38 +2,38 @@
struct Empty {}
struct Node<T> {
struct Node[T] {
value T
left Tree<T>
right Tree<T>
left Tree[T]
right Tree[T]
}
type Tree<T> = Empty | Node<T>
type Tree[T] = Empty | Node[T]
// return size(number of nodes) of BST
fn (tree Tree<T>) size<T>() int {
fn (tree Tree[T]) size[T]() int {
return match tree {
Empty { 0 }
Node<T> { 1 + tree.left.size() + tree.right.size() }
Node[T] { 1 + tree.left.size() + tree.right.size() }
}
}
// insert a value to BST
fn (tree Tree<T>) insert<T>(x T) Tree<T> {
fn (tree Tree[T]) insert[T](x T) Tree[T] {
return match tree {
Empty {
Node<T>{x, tree, tree}
Node[T]{x, tree, tree}
}
Node<T> {
Node[T] {
if x == tree.value {
tree
} else if x < tree.value {
Node<T>{
Node[T]{
...tree
left: tree.left.insert(x)
}
} else {
Node<T>{
Node[T]{
...tree
right: tree.right.insert(x)
}
@@ -43,12 +43,12 @@ fn (tree Tree<T>) insert<T>(x T) Tree<T> {
}
// whether able to find a value in BST
fn (tree Tree<T>) search<T>(x T) bool {
fn (tree Tree[T]) search[T](x T) bool {
return match tree {
Empty {
false
}
Node<T> {
Node[T] {
if x == tree.value {
true
} else if x < tree.value {
@@ -61,12 +61,12 @@ fn (tree Tree<T>) search<T>(x T) bool {
}
// find the minimal value of a BST
fn (tree Tree<T>) min<T>() T {
fn (tree Tree[T]) min[T]() T {
return match tree {
Empty {
T(1e9)
}
Node<T> {
Node[T] {
if tree.value < tree.left.min() {
tree.value
} else {
@@ -77,25 +77,25 @@ fn (tree Tree<T>) min<T>() T {
}
// delete a value in BST (if nonexistant do nothing)
fn (tree Tree<T>) delete<T>(x T) Tree<T> {
fn (tree Tree[T]) delete[T](x T) Tree[T] {
return match tree {
Empty {
tree
}
Node<T> {
Node[T] {
if tree.left !is Empty && tree.right !is Empty {
if x < tree.value {
Node<T>{
Node[T]{
...tree
left: tree.left.delete(x)
}
} else if x > tree.value {
Node<T>{
Node[T]{
...tree
right: tree.right.delete(x)
}
} else {
Node<T>{
Node[T]{
...tree
value: tree.right.min()
right: tree.right.delete(tree.right.min())
@@ -105,7 +105,7 @@ fn (tree Tree<T>) delete<T>(x T) Tree<T> {
if x == tree.value {
tree.left
} else {
Node<T>{
Node[T]{
...tree
left: tree.left.delete(x)
}
@@ -114,7 +114,7 @@ fn (tree Tree<T>) delete<T>(x T) Tree<T> {
if x == tree.value {
tree.right
} else {
Node<T>{
Node[T]{
...tree
right: tree.right.delete(x)
}
@@ -125,7 +125,7 @@ fn (tree Tree<T>) delete<T>(x T) Tree<T> {
}
fn main() {
mut tree := Tree<f64>(Empty{})
mut tree := Tree[f64](Empty{})
vals := [0.2, 0.0, 0.5, 0.3, 0.6, 0.8, 0.9, 1.0, 0.1, 0.4, 0.7]
for i in vals {
tree = tree.insert(i)

View File

@@ -15,7 +15,7 @@ fn (a KeyVal) < (b KeyVal) bool {
}
fn main() {
mut bst := datatypes.BSTree<KeyVal>{}
mut bst := datatypes.BSTree[KeyVal]{}
bst.insert(KeyVal{ key: 1, val: 12 })
println(bst.in_order_traversal())

View File

@@ -7,11 +7,11 @@ struct User {
fn main() {
data := 'name=Alice\nage=18'
user := decode<User>(data)
user := decode[User](data)
println(user)
}
fn decode<T>(data string) T {
fn decode[T](data string) T {
mut result := T{}
// compile-time `for` loop
// T.fields gives an array of a field metadata type

View File

@@ -23,7 +23,7 @@ mut:
// building a map of with all edges etc of a graph, represented from a matrix adjacency
// Input: matrix adjacency --> Output: edges list of src, dest and weight
fn build_map_edges_from_graph<T>(g [][]T) map[T]EDGE {
fn build_map_edges_from_graph[T](g [][]T) map[T]EDGE {
n := g.len // TOTAL OF NODES for this graph -- its dimmension
mut edges_map := map[int]EDGE{} // a graph represented by map of edges
@@ -52,7 +52,7 @@ fn print_sol(dist []int) {
// The main function that finds shortest distances from src
// to all other vertices using Bellman-Ford algorithm. The
// function also detects negative weight cycle
fn bellman_ford<T>(graph [][]T, src int) {
fn bellman_ford[T](graph [][]T, src int) {
mut edges := build_map_edges_from_graph(graph)
// this function was done to adapt a graph representation
// by a adjacency matrix, to list of adjacency (using a MAP)

View File

@@ -36,7 +36,7 @@ mut:
// Function to push according to priority ... the lower priority is goes ahead
// The "push" always sorted in pq
fn push_pq<T>(mut prior_queue []T, data int, priority int) {
fn push_pq[T](mut prior_queue []T, data int, priority int) {
mut temp := []T{}
lenght_pq := prior_queue.len
@@ -57,7 +57,7 @@ fn push_pq<T>(mut prior_queue []T, data int, priority int) {
}
// Change the priority of a value/node ... exist a value, change its priority
fn updating_priority<T>(mut prior_queue []T, search_data int, new_priority int) {
fn updating_priority[T](mut prior_queue []T, search_data int, new_priority int) {
mut i := 0
mut lenght_pq := prior_queue.len
@@ -76,7 +76,7 @@ fn updating_priority<T>(mut prior_queue []T, search_data int, new_priority int)
}
// a single departure or remove from queue
fn departure_priority<T>(mut prior_queue []T) int {
fn departure_priority[T](mut prior_queue []T) int {
mut x := prior_queue[0].data
prior_queue.delete(0) // or .delete_many(0, 1 )
return x
@@ -84,7 +84,7 @@ fn departure_priority<T>(mut prior_queue []T) int {
// give a NODE v, return a list with all adjacents
// Take care, only positive EDGES
fn all_adjacents<T>(g [][]T, v int) []int {
fn all_adjacents[T](g [][]T, v int) []int {
mut temp := []int{}
for i in 0 .. (g.len) {
if g[v][i] > 0 {
@@ -95,7 +95,7 @@ fn all_adjacents<T>(g [][]T, v int) []int {
}
// print the costs from origin up to all nodes
fn print_solution<T>(dist []T) {
fn print_solution[T](dist []T) {
print('Vertex \tDistance from Source')
for node in 0 .. (dist.len) {
print('\n ${node} ==> \t ${dist[node]}')
@@ -103,7 +103,7 @@ fn print_solution<T>(dist []T) {
}
// print all paths and their cost or weight
fn print_paths_dist<T>(path []T, dist []T) {
fn print_paths_dist[T](path []T, dist []T) {
print('\n Read the nodes from right to left (a path): \n')
for node in 1 .. (path.len) {

View File

@@ -30,7 +30,7 @@ mut:
// Function to push according to priority ... the lower priority is goes ahead
// The "push" always sorted in pq
fn push_pq<T>(mut prior_queue []T, data int, priority int) {
fn push_pq[T](mut prior_queue []T, data int, priority int) {
mut temp := []T{}
lenght_pq := prior_queue.len
@@ -50,7 +50,7 @@ fn push_pq<T>(mut prior_queue []T, data int, priority int) {
}
// Change the priority of a value/node ... exist a value, change its priority
fn updating_priority<T>(mut prior_queue []T, search_data int, new_priority int) {
fn updating_priority[T](mut prior_queue []T, search_data int, new_priority int) {
mut i := 0
mut lenght_pq := prior_queue.len
@@ -72,7 +72,7 @@ fn updating_priority<T>(mut prior_queue []T, search_data int, new_priority int)
}
// a single departure or remove from queue
fn departure_priority<T>(mut prior_queue []T) int {
fn departure_priority[T](mut prior_queue []T) int {
mut x := prior_queue[0].data
prior_queue.delete(0) // or .delete_many(0, 1 )
return x
@@ -80,7 +80,7 @@ fn departure_priority<T>(mut prior_queue []T) int {
// give a NODE v, return a list with all adjacents
// Take care, only positive EDGES
fn all_adjacents<T>(g [][]T, v int) []int {
fn all_adjacents[T](g [][]T, v int) []int {
mut temp := []int{}
for i in 0 .. (g.len) {
if g[v][i] > 0 {

View File

@@ -11,7 +11,7 @@ struct Story {
}
fn worker_fetch(mut p pool.PoolProcessor, cursor int, worker_id int) voidptr {
id := p.get_item<int>(cursor)
id := p.get_item[int](cursor)
resp := http.get('https://hacker-news.firebaseio.com/v0/item/${id}.json') or {
println('failed to fetch data from /v0/item/${id}.json')
return pool.no_result

View File

@@ -11,12 +11,12 @@ fn main() {
arr << rand.intn(gen_max) or { 0 }
}
println('length of random array is ${arr.len}')
println('before quick sort whether array is sorted: ${is_sorted<int>(arr)}')
quick_sort<int>(mut arr, 0, arr.len - 1)
println('after quick sort whether array is sorted: ${is_sorted<int>(arr)}')
println('before quick sort whether array is sorted: ${is_sorted[int](arr)}')
quick_sort[int](mut arr, 0, arr.len - 1)
println('after quick sort whether array is sorted: ${is_sorted[int](arr)}')
}
fn quick_sort<T>(mut arr []T, l int, r int) {
fn quick_sort[T](mut arr []T, l int, r int) {
if l >= r {
return
}
@@ -28,11 +28,11 @@ fn quick_sort<T>(mut arr []T, l int, r int) {
}
}
arr[l], arr[sep] = arr[sep], arr[l]
quick_sort<T>(mut arr, l, sep - 1)
quick_sort<T>(mut arr, sep + 1, r)
quick_sort[T](mut arr, l, sep - 1)
quick_sort[T](mut arr, sep + 1, r)
}
fn is_sorted<T>(arr []T) bool {
fn is_sorted[T](arr []T) bool {
for i in 0 .. arr.len - 1 {
if arr[i] > arr[i + 1] {
return false