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

builtin: add more docs to array.v (#7194)

This commit is contained in:
Swastik Baranwal 2020-12-15 14:01:35 +05:30 committed by GitHub
parent d155d8d3f0
commit b2685c6060
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -5,16 +5,18 @@ module builtin
import strings import strings
// array is a struct used for denoting array types in V
pub struct array { pub struct array {
pub: pub:
element_size int element_size int // size in bytes of one element in the array.
pub mut: pub mut:
data voidptr // Using a void pointer allows to implement arrays without generics and without generating data voidptr
// extra code for every type. len int // length of the array.
len int cap int // capacity of the array.
cap int
} }
// array.data uses a void pointer, which allows implementing arrays without generics and without generating
// extra code for every type.
// Internal function, used by V (`nums := []int`) // Internal function, used by V (`nums := []int`)
fn __new_array(mylen int, cap int, elm_size int) array { fn __new_array(mylen int, cap int, elm_size int) array {
cap_ := if cap < mylen { mylen } else { cap } cap_ := if cap < mylen { mylen } else { cap }
@ -415,6 +417,7 @@ pub fn (mut a3 array) push_many(val voidptr, size int) {
a3.len += size a3.len += size
} }
// reverse_in_place reverses existing array data, modifying original array.
pub fn (mut a array) reverse_in_place() { pub fn (mut a array) reverse_in_place() {
if a.len < 2 { if a.len < 2 {
return return
@ -580,8 +583,8 @@ pub fn (mut a []int) sort() {
a.sort_with_compare(compare_ints) a.sort_with_compare(compare_ints)
} }
// index returns the index of the first element equal to the given value, // index returns the first index at which a given element can be found in the array
// or -1 if the value is not found in the array. // or -1 if the value is not found.
pub fn (a []string) index(v string) int { pub fn (a []string) index(v string) int {
for i in 0 .. a.len { for i in 0 .. a.len {
if a[i] == v { if a[i] == v {
@ -591,8 +594,8 @@ pub fn (a []string) index(v string) int {
return -1 return -1
} }
// index returns the index of the first element equal to the given value, // index returns the first index at which a given element can be found in the array
// or -1 if the value is not found in the array. // or -1 if the value is not found.
pub fn (a []int) index(v int) int { pub fn (a []int) index(v int) int {
for i in 0 .. a.len { for i in 0 .. a.len {
if a[i] == v { if a[i] == v {
@ -602,8 +605,8 @@ pub fn (a []int) index(v int) int {
return -1 return -1
} }
// index returns the index of the first element equal to the given value, // index returns the first index at which a given element can be found in the array
// or -1 if the value is not found in the array. // or -1 if the value is not found.
pub fn (a []byte) index(v byte) int { pub fn (a []byte) index(v byte) int {
for i in 0 .. a.len { for i in 0 .. a.len {
if a[i] == v { if a[i] == v {
@ -613,6 +616,8 @@ pub fn (a []byte) index(v byte) int {
return -1 return -1
} }
// index returns the first index at which a given element can be found in the array
// or -1 if the value is not found.
pub fn (a []rune) index(v rune) int { pub fn (a []rune) index(v rune) int {
for i in 0 .. a.len { for i in 0 .. a.len {
if a[i] == v { if a[i] == v {
@ -622,8 +627,8 @@ pub fn (a []rune) index(v rune) int {
return -1 return -1
} }
// index returns the index of the first element equal to the given value, // index returns the first index at which a given element can be found in the array
// or -1 if the value is not found in the array. // or -1 if the value is not found.
// TODO is `char` type yet in the language? // TODO is `char` type yet in the language?
pub fn (a []char) index(v char) int { pub fn (a []char) index(v char) int {
for i in 0 .. a.len { for i in 0 .. a.len {
@ -681,6 +686,8 @@ pub fn (a []f32) eq(a2 []f32) bool {
return array_eq(a, a2) return array_eq(a, a2)
} }
*/ */
// eq checks if the arrays have the same elements or not.
// TODO: make it work with all types.
pub fn (a1 []string) eq(a2 []string) bool { pub fn (a1 []string) eq(a2 []string) bool {
// return array_eq(a, a2) // return array_eq(a, a2)
if a1.len != a2.len { if a1.len != a2.len {