Fun 0.41.5
The programming language that makes You have fun
Loading...
Searching...
No Matches
array_utils.c
Go to the documentation of this file.
1/*
2 * This file is part of the Fun programming language.
3 * https://fun-lang.xyz/
4 *
5 * Copyright 2025 Johannes Findeisen <you@hanez.org>
6 * Licensed under the terms of the Apache-2.0 license.
7 * https://opensource.org/license/apache-2-0
8 */
9
10#include "value.h"
11
16
26int array_contains(const Value *v, const Value *needle) {
27 if (!v || v->type != VAL_ARRAY || !v->arr) return 0;
28 int n = array_length(v);
29 for (int i = 0; i < n; ++i) {
30 Value item;
31 if (array_get_copy(v, i, &item)) {
32 int eq = value_equals(&item, needle);
33 free_value(item);
34 if (eq) return 1;
35 }
36 }
37 return 0;
38}
39
47int array_index_of(const Value *v, const Value *needle) {
48 if (!v || v->type != VAL_ARRAY || !v->arr) return -1;
49 int n = array_length(v);
50 for (int i = 0; i < n; ++i) {
51 Value item;
52 if (array_get_copy(v, i, &item)) {
53 int eq = value_equals(&item, needle);
54 free_value(item);
55 if (eq) return i;
56 }
57 }
58 return -1;
59}
60
69 if (!v || v->type != VAL_ARRAY || !v->arr) return;
70 int n = array_length(v);
71 for (int i = 0; i < n; ++i) {
72 Value item;
73 if (array_get_copy(v, i, &item)) {
74 free_value(item);
75 }
76 }
77 /* internal clear uses public API to reset to zero length */
78 /* Since we don't expose capacity, emulate by popping elements */
80 while (array_pop(v, &out)) {
82 }
83}
Value out
Definition apop.c:38
int array_index_of(const Value *v, const Value *needle)
Find the index of the first occurrence of an element in an array.
Definition array_utils.c:47
void array_clear(Value *v)
Remove all elements from an array Value, freeing their contents.
Definition array_utils.c:68
int array_contains(const Value *v, const Value *needle)
Check if an array Value contains a given element.
Definition array_utils.c:26
Value v
Definition cast.c:22
int eq
Definition eq.c:34
int n
Definition insert.c:41
Tagged union representing a Fun value.
Definition value.h:68
int array_pop(Value *v, Value *out)
Remove the last element from an array.
Definition value.c:282
int array_length(const Value *v)
Get the element count of an array Value.
Definition value.c:176
void free_value(Value v)
Free dynamic storage owned by a Value.
Definition value.c:517
int array_get_copy(const Value *v, int index, Value *out)
Copy an array element into out.
Definition value.c:192
int value_equals(const Value *a, const Value *b)
Compare two Values for equality.
Definition value.c:695
Defines the Value type and associated functions for the Fun VM.
@ VAL_ARRAY
Definition value.h:55