This repository was archived by the owner on Aug 31, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 228
Expand file tree
/
Copy pathmodule-array.cpp
More file actions
203 lines (157 loc) · 5.98 KB
/
module-array.cpp
File metadata and controls
203 lines (157 loc) · 5.98 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
/* Copyright (C) 2003-2015 LiveCode Ltd.
This file is part of LiveCode.
LiveCode is free software; you can redistribute it and/or modify it under
the terms of the GNU General Public License v3 as published by the Free
Software Foundation.
LiveCode is distributed in the hope that it will be useful, but WITHOUT ANY
WARRANTY; without even the implied warranty of MERCHANTABILITY or
FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
for more details.
You should have received a copy of the GNU General Public License
along with LiveCode. If not see <http://www.gnu.org/licenses/>. */
#include <foundation.h>
#include <foundation-auto.h>
static bool is_not_among_the_elements_of(void *context, MCArrayRef p_target, MCNameRef p_key, MCValueRef p_value)
{
MCValueRef t_needle = (MCValueRef)context;
return !MCValueIsEqualTo(t_needle, p_value);
}
static bool list_array_keys(void *context, MCArrayRef p_target, MCNameRef p_key, MCValueRef p_value)
{
MCProperListRef t_list = (MCProperListRef)context;
return MCProperListPushElementOntoBack(t_list, MCNameGetString(p_key));
}
static bool list_array_elements(void *context, MCArrayRef p_target, MCNameRef p_key, MCValueRef p_value)
{
MCProperListRef t_list = (MCProperListRef)context;
return MCProperListPushElementOntoBack(t_list, p_value);
}
extern "C" MC_DLLEXPORT_DEF void MCArrayEvalKeysOf(MCArrayRef p_target, MCProperListRef& r_output)
{
MCProperListRef t_list;
if (MCProperListCreateMutable(t_list) &&
MCArrayApply(p_target, list_array_keys, t_list) &&
MCProperListCopyAndRelease(t_list, r_output))
return;
}
extern "C" MC_DLLEXPORT_DEF void MCArrayEvalElementsOf(MCArrayRef p_target, MCProperListRef& r_output)
{
MCProperListRef t_list;
if (MCProperListCreateMutable(t_list) &&
MCArrayApply(p_target, list_array_elements, t_list) &&
MCProperListCopyAndRelease(t_list, r_output))
return;
}
extern "C" MC_DLLEXPORT_DEF void MCArrayEvalNumberOfElementsIn(MCArrayRef p_target, uindex_t& r_output)
{
r_output = MCArrayGetCount(p_target);
}
extern "C" MC_DLLEXPORT_DEF void MCArrayEvalIsAmongTheElementsOf(MCValueRef p_needle, bool p_is_not, MCArrayRef p_target, bool& r_output)
{
MCValueRef t_value;
t_value = p_needle != nil ? p_needle : kMCNull;
r_output = !MCArrayApply(p_target, is_not_among_the_elements_of, t_value);
}
extern "C" MC_DLLEXPORT_DEF void MCArrayEvalIsAmongTheKeysOfCaseless(MCStringRef p_needle, bool p_is_not, MCArrayRef p_target, bool& r_output)
{
MCNewAutoNameRef t_key;
if (!MCNameCreate(p_needle, &t_key))
return;
MCValueRef t_value;
t_value = nil;
r_output = MCArrayFetchValue(p_target, false, *t_key, t_value);
if (p_is_not)
r_output = !r_output;
}
extern "C" MC_DLLEXPORT_DEF void MCArrayFetchElementOfCaseless(MCArrayRef p_target, MCStringRef p_key, MCValueRef& r_output)
{
MCNewAutoNameRef t_key;
if (!MCNameCreate(p_key, &t_key))
return;
MCValueRef t_value;
t_value = nil;
if (!MCArrayFetchValue(p_target, false, *t_key, t_value))
{
MCErrorCreateAndThrow(kMCGenericErrorTypeInfo, "reason", MCSTR("array key does not exist"), nil);
return;
}
r_output = MCValueRetain(t_value);
}
extern "C" MC_DLLEXPORT_DEF void MCArrayStoreElementOfCaseless(MCValueRef p_value, MCArrayRef& x_target, MCStringRef p_key)
{
MCAutoArrayRef t_array;
MCArrayMutableCopy(x_target, &t_array);
MCValueRef t_value;
t_value = p_value != nil ? p_value : kMCNull;
MCNewAutoNameRef t_key;
if (!MCNameCreate(p_key, &t_key) ||
!MCArrayStoreValue(*t_array, false, *t_key, t_value))
return;
MCAutoArrayRef t_new_array;
if (!MCArrayCopy(*t_array, &t_new_array))
return;
MCValueAssign(x_target, *t_new_array);
}
extern "C" MC_DLLEXPORT_DEF void MCArrayDeleteElementOfCaseless(MCArrayRef& x_target, MCStringRef p_key)
{
MCAutoArrayRef t_array;
MCArrayMutableCopy(x_target, &t_array);
MCNewAutoNameRef t_key;
if (!MCNameCreate(p_key, &t_key) ||
!MCArrayRemoveValue(*t_array, false, *t_key))
return;
MCAutoArrayRef t_new_array;
if (!MCArrayCopy(*t_array, &t_new_array))
return;
MCValueAssign(x_target, *t_new_array);
}
extern "C" MC_DLLEXPORT_DEF void MCArrayEvalEmpty(MCArrayRef& r_output)
{
r_output = MCValueRetain(kMCEmptyArray);
}
extern "C" bool MC_DLLEXPORT_DEF MCArrayRepeatForEachElement(void*& x_iterator, MCValueRef& r_iterand, MCArrayRef p_array)
{
MCValueRef t_value;
// If this is a numerical array, do it in order
/* if (MCArrayIsSequence(p_array))
{
uindex_t t_offset;
t_offset = (uindex_t)x_iterator;
if (t_offset == MCArrayGetCount(p_array))
return false;
if (!MCArrayFetchValueAtIndex(p_array, t_offset, t_value))
return false;
}
else */
{
MCNameRef t_key;
uintptr_t t_ptr;
t_ptr = (uintptr_t)x_iterator;
if (!MCArrayIterate(p_array, t_ptr, t_key, t_value))
return false;
x_iterator = (void *)(t_ptr);
}
r_iterand = MCValueRetain(t_value);
return true;
}
extern "C" bool MC_DLLEXPORT_DEF MCArrayRepeatForEachKey(void*& x_iterator, MCStringRef& r_iterand, MCArrayRef p_array)
{
MCNameRef t_key;
MCValueRef t_value;
uintptr_t t_offset;
t_offset = (uintptr_t)x_iterator;
if (!MCArrayIterate(p_array, t_offset, t_key, t_value))
return false;
r_iterand = MCValueRetain(MCNameGetString(t_key));
x_iterator = (void *)(t_offset);
return true;
}
////////////////////////////////////////////////////////////////////////////////////////////////////
extern "C" bool com_livecode_array_Initialize(void)
{
return true;
}
extern "C" void com_livecode_array_Finalize(void)
{
}
////////////////////////////////////////////////////////////////////////////////////////////////////