• VEX —— Functions|Arrays


    目录

    array —— 高效创建数组

    len —— 返回数组等长度

    isvalidindex —— 检测指定的索引在数组或字符串中是否有效

    append —— 追加元素到数组或字符串

    push —— 添加元素到数组

    upush —— 添加统一值到数组

    pop —— 移除数组最后一个元素并返回

    insert —— 插入元素、数组、字符串

    removeindex —— 移除指定位置的元素

    removevalue —— 移除指定元素

    sort —— 返回升序后的数组

    argsort —— 返回数组排序后的索引

    reorder —— 重新排序数组或字符串

    resize —— 重置数组长度

    reverse —— 返回反转数组和字符串

    slice —— 对数组或字符串进行切片

    foreach —— 循环数组内的元素


    array —— 高效创建数组

    • 应使用函数样式,来确保数组成员正确;
    [] array(...)
    1. //结果为{{1,1,1}, {1,2,3}, {3,3,3}}
    2. vector v[] = vector[](array(1, {1,2,3}, 3));
    3. //结果为{1, 1, 3}
    4. float v[] = float[](array(1, {1,2,3}, 3));

    len —— 返回数组等长度

    1. int len(v)
    2. int len(m)
    3. int len(array[])
    4. int len(string s)
    5. int len(dict d)
    • length函数是计算vector大小;

    isvalidindex —— 检测指定的索引在数组或字符串中是否有效

    1. //在数组或字符串范围内返回1,否则返回0;
    2. //index < len(array) && index >= -len(array)
    3. int isvalidindex(&array[], int index)
    4. int isvalidindex(string str, int index)
    1. //如key在字典内返回1,否则返回0
    2. int isvalidindex(dict d, string key)

    append —— 追加元素到数组或字符串

    • 可使用arr[n] = x形式,单独追加;
    1. //字符串追加
    2. void append(string &array, string value)
    3. //数组追加,等价于 push(array, value)
    4. void append(&array[], value)
    5. //追加(或连接)数组,等价于 push(array, values)
    6. void append(&array[], values[])

    1. string s = "abcd";
    2. append(s, 'efg');
    3. int arr[] = {0,1,2};
    4. append(arr, 3);

    push —— 添加元素到数组

    • 可使用arr[n] = x形式,单独追加;
    1. //等价于 append(array, value)
    2. void push(&array[], value)
    3. //等价于 append(array, values)
    4. void push(&array[], values[])

    upush —— 添加统一值到数组

    • 非常专业的函数,主要用于在PBR lighting中的灯光输出;
    1. //对所有SIMD程序
    2. void upush(&array[], value)

    pop —— 移除数组最后一个元素并返回

    1. //移除最后一个元素
    2. pop(&array[])
    3. //移除指定元素
    4. pop(&array[], int index)

    insert —— 插入元素、数组、字符串

    1. //插入字符串,如索引大于长度则追加末尾
    2. void insert(string &str, int index, string value)
    1. //插入数组
    2. //如索引超过长度则使用0或空
    3. //如索引为负值,从数组末尾开始,大于长度则截止到0
    4. void insert(&array[], int index, value)
    5. void insert(&array[], int index, values[])
    1. //复制srcdict[srckey]值到dstdict[dstkey]
    2. //如srcdict内不存在srckey,则将从dstdict内移除
    3. //key存在返回1,否则返回0
    4. int insert(dict &dstdict, string dstkey, dict srcdict, string srckey)
    1. //将srcdict合并到dstdict,相同的key将覆盖
    2. void insert(dict &dstdict, dict srcdict)

    removeindex —— 移除指定位置的元素

    1. //移除指定位置元素并返回其值
    2. //等价于pop(array, index)
    3. removeindex(&array[], int index)
    1. //移除字典entry,无entry返回0,否则返回1
    2. int removeindex(dict &dictionary, string index)

    removevalue —— 移除指定元素

    1. //移除首个匹配的元素,移除返回1,否则返回0
    2. int removevalue(&array[], value)
    1. float nums[] = {0, 1, 2, 3, 1, 2, 3};
    2. removevalue(nums; 2); // == 1
    3. // nums == {0, 1, 3, 1, 2, 3}

    sort —— 返回升序后的数组

    1. int [] sort(int values[])
    2. float [] sort(float values[])
    3. string [] sort(string values[])

    argsort —— 返回数组排序后的索引

    • 可根据元素的特性,来排序数组,而不是数组本身;
    • argsort、sort使用一个稳定排序;
    • 使用reverse反转排序;
    int[] argsort(value[])
    1. //通常元素长度,排序字符串
    2. cvex main()
    3. {
    4. // Given an array of strings...
    5. string colors[] = {"Red", "Green", "Blue", "Orange", "Violet", "Indigo"};
    6. // Create an array with the corresponding lengths
    7. int[] lengths = {};
    8. foreach (string name; colors) {
    9. push(lengths, len(name));
    10. }
    11. // Sort the lengths and return an array containing the new ordering
    12. int[] ordering = argsort(lengths);
    13. // Get the array of color names but sorted by name length
    14. string colors_by_len[] = reorder(colors, ordering);
    15. printf("%s\n", colors_by_len);
    16. }

    reorder —— 重新排序数组或字符串

    • 通常使用argsort函数生成的索引列表;
    • 负值索引,从数组末尾读取;
    • 数组或字符串长度与索引数组相同;
    • 超出边界的值将插入零,应该被认为是错误;
    1. //UTF-8字符串
    2. string reorder(string value, int indices[])
    3. //数组
    4. [] reorder(values[], int indices[])

    resize —— 重置数组长度

    1. //调整尺寸,如大于当前长度则使用0或空
    2. void resize(&array[], int size)
    1. //调整尺寸,如大于当前长度则使用指定的val
    2. void resize(&array[], int size, val)

    reverse —— 返回反转数组和字符串

    1. //返回反转UTF-8字符串,与str[::-1](表示从开头到结尾步幅为-1);
    2. string reverse(string str)
    1. //返回反转的数组
    2. [] reverse(values[])

    slice —— 对数组或字符串进行切片

    • 等价于value[start:end:step]
    • 如果start或end是负值,从数组或字符串末尾开始;
    • 计算的范围被钳制在原始数组或字符串的边界;
    • 如step为o,将返回空数组或字符串;
    • 如step为负值,将方向返回,end应小于start;
    1. //提取子字符串
    2. string slice(string s, int start, int end)
    3. string slice(string s, int start, int end, int step)
    1. //提取子数组
    2. [] slice(s[], int start, int end)
    3. [] slice(s[], int start, int end, int step)
    1. //通用规则
    2. string slice(string s, int hasstart, int start, int hasend, int end, int hasstep, int step)
    3. [] slice(array[], int hasstart, int start, int hasend, int end, int hasstep, int step)
    1. int nums[] = {10, 20, 30, 40, 50, 60};
    2. slice(nums, 1, 3) == {20, 30} // nums[1:3]
    3. slice(nums, 1, -1) == {20, 30, 40, 50} // nums[1:-1]
    4. slice(nums, 0, 6, 2) == {10, 30, 50} // nums[0:6:2]
    5. slice(nums, 0, 0, 0, 0, 1, 2) == {10, 30, 50} // nums[::2]

    foreach —— 循环数组内的元素

    1. //针对array中的每个成员value,计算语句;
    2. //可选index,为每次计算的当前位置;
    3. foreach(value; array) statement
    4. foreach(index; value; array) statement

  • 相关阅读:
    SpringBoot+Mybatis+定时任务实现大数据量数据分表记录和查询
    WebAssembly核心编程[3]: Module 与 Instance
    【国科大——矩阵分析与应用】使用高斯消元法,测试二元一次方程系数产生的误差
    Waldom Electronics宣布成立顾问委员会
    排序【七大排序】
    Mycat中间件,分布式数据库中间件的佼佼者,带你从实战出发轻松掌握
    Bootstrap5 表单
    K8s稳居容器榜首,Docker冲顶技术热词,微服务应用热度不减,2021云原生开发者现状
    hive高级函数
    RAID5的配置流程及模拟硬盘损坏
  • 原文地址:https://blog.csdn.net/NapoleonCoder/article/details/133135376