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

pull/7342/head
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
1 changed files with 20 additions and 13 deletions

View File

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