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-sort.cpp
More file actions
322 lines (252 loc) · 11 KB
/
module-sort.cpp
File metadata and controls
322 lines (252 loc) · 11 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
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
/* 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 compare_t MCSortCompareText(void *context, MCValueRef p_left, MCValueRef p_right)
{
// Since there are no default type conversions, sort all strings before anything else.
MCStringOptions t_options = *(MCStringOptions *)context;
return MCStringCompareTo((MCStringRef)p_left, (MCStringRef)p_right, t_options);
}
static compare_t MCSortCompareBinary(void *context, MCValueRef p_left, MCValueRef p_right)
{
return MCDataCompareTo((MCDataRef)p_left, (MCDataRef)p_right);
}
static compare_t MCSortCompareNumeric(void *context, MCValueRef p_left, MCValueRef p_right)
{
return (MCNumberFetchAsReal((MCNumberRef)p_left) < MCNumberFetchAsReal((MCNumberRef)p_right)) ? -1 : 1;
}
static compare_t MCSortCompareDateTime(void *context, MCValueRef p_left, MCValueRef p_right)
{
// Date time objects not yet implemented
return 0;
}
extern "C" MC_DLLEXPORT_DEF void MCSortExecSortList(MCProperListRef& x_target, bool p_descending)
{
MCValueTypeCode t_type;
if (!MCProperListIsHomogeneous(x_target, t_type))
{
MCErrorCreateAndThrow(kMCGenericErrorTypeInfo, "reason", MCSTR("list elements are not all of the same type"), nil);
return;
}
MCAutoProperListRef t_mutable_list;
if (!MCProperListMutableCopy(x_target, &t_mutable_list))
return;
switch (t_type)
{
case kMCValueTypeCodeString:
// AL-2015-02-13: [[ Bug 14599 ]] Use exact comparison here for consistency.
MCStringOptions t_option;
t_option = kMCStringOptionCompareExact;
MCProperListStableSort(*t_mutable_list, p_descending, MCSortCompareText, &t_option);
break;
case kMCValueTypeCodeData:
MCProperListStableSort(*t_mutable_list, p_descending, MCSortCompareBinary, nil);
break;
case kMCValueTypeCodeNumber:
MCProperListStableSort(*t_mutable_list, p_descending, MCSortCompareNumeric, nil);
break;
default:
MCErrorCreateAndThrow(kMCGenericErrorTypeInfo, "reason", MCSTR("list type does not have default comparison operator"), nil);
return;
}
MCAutoProperListRef t_sorted_list;
if (!MCProperListCopy(*t_mutable_list, &t_sorted_list))
return;
MCValueAssign(x_target, *t_sorted_list);
}
extern "C" MC_DLLEXPORT_DEF void MCSortExecSortListAscending(MCProperListRef& x_target)
{
MCSortExecSortList(x_target, false);
}
extern "C" MC_DLLEXPORT_DEF void MCSortExecSortListDescending(MCProperListRef& x_target)
{
MCSortExecSortList(x_target, true);
}
extern "C" MC_DLLEXPORT_DEF void MCSortExecSortListText(MCProperListRef& x_target, bool p_descending)
{
if (!MCProperListIsListOfType(x_target, kMCValueTypeCodeString))
{
MCErrorCreateAndThrow(kMCGenericErrorTypeInfo, "reason", MCSTR("list contains non-string element"), nil);
return;
}
MCAutoProperListRef t_mutable_list;
if (!MCProperListMutableCopy(x_target, &t_mutable_list))
return;
// AL-2015-02-13: [[ Bug 14599 ]] Use exact comparison here for consistency.
MCStringOptions t_option;
t_option = kMCStringOptionCompareExact;
MCProperListStableSort(*t_mutable_list, p_descending, MCSortCompareText, &t_option);
MCAutoProperListRef t_sorted_list;
if (!MCProperListCopy(*t_mutable_list, &t_sorted_list))
return;
MCValueAssign(x_target, *t_sorted_list);
}
extern "C" MC_DLLEXPORT_DEF void MCSortExecSortListAscendingText(MCProperListRef& x_target)
{
MCSortExecSortListText(x_target, false);
}
extern "C" MC_DLLEXPORT_DEF void MCSortExecSortListDescendingText(MCProperListRef& x_target)
{
MCSortExecSortListText(x_target, true);
}
extern "C" MC_DLLEXPORT_DEF void MCSortExecSortListBinary(MCProperListRef& x_target, bool p_descending)
{
if (!MCProperListIsListOfType(x_target, kMCValueTypeCodeData))
{
MCErrorCreateAndThrow(kMCGenericErrorTypeInfo, "reason", MCSTR("list contains non-data element"), nil);
return;
}
MCAutoProperListRef t_mutable_list;
if (!MCProperListMutableCopy(x_target, &t_mutable_list))
return;
MCProperListStableSort(*t_mutable_list, p_descending, MCSortCompareBinary, nil);
MCAutoProperListRef t_sorted_list;
if (!MCProperListCopy(*t_mutable_list, &t_sorted_list))
return;
MCValueAssign(x_target, *t_sorted_list);
}
extern "C" MC_DLLEXPORT_DEF void MCSortExecSortListAscendingBinary(MCProperListRef& x_target)
{
MCSortExecSortListBinary(x_target, false);
}
extern "C" MC_DLLEXPORT_DEF void MCSortExecSortListDescendingBinary(MCProperListRef& x_target)
{
MCSortExecSortListBinary(x_target, true);
}
extern "C" MC_DLLEXPORT_DEF void MCSortExecSortListNumeric(MCProperListRef& x_target, bool p_descending)
{
if (!MCProperListIsListOfType(x_target, kMCValueTypeCodeNumber))
{
MCErrorCreateAndThrow(kMCGenericErrorTypeInfo, "reason", MCSTR("list contains non-numeric element"), nil);
return;
}
MCAutoProperListRef t_mutable_list;
if (!MCProperListMutableCopy(x_target, &t_mutable_list))
return;
MCProperListStableSort(*t_mutable_list, p_descending, MCSortCompareNumeric, nil);
MCAutoProperListRef t_sorted_list;
if (!MCProperListCopy(*t_mutable_list, &t_sorted_list))
return;
MCValueAssign(x_target, *t_sorted_list);
}
extern "C" MC_DLLEXPORT_DEF void MCSortExecSortListAscendingNumeric(MCProperListRef& x_target)
{
MCSortExecSortListNumeric(x_target, false);
}
extern "C" MC_DLLEXPORT_DEF void MCSortExecSortListDescendingNumeric(MCProperListRef& x_target)
{
MCSortExecSortListNumeric(x_target, true);
}
static void MCSortExecSortListDateTime(MCProperListRef& x_target, bool p_descending)
{
MCAutoProperListRef t_mutable_list;
if (!MCProperListMutableCopy(x_target, &t_mutable_list))
return;
MCProperListStableSort(*t_mutable_list, false, MCSortCompareDateTime, nil);
MCAutoProperListRef t_sorted_list;
if (!MCProperListCopy(*t_mutable_list, &t_sorted_list))
return;
MCValueAssign(x_target, *t_sorted_list);
}
extern "C" MC_DLLEXPORT_DEF void MCSortExecSortListAscendingDateTime(MCProperListRef& x_target)
{
MCSortExecSortListDateTime(x_target, false);
}
extern "C" MC_DLLEXPORT_DEF void MCSortExecSortListDescendingDateTime(MCProperListRef& x_target)
{
MCSortExecSortListDateTime(x_target, true);
}
////////////////////////////////////////////////////////////////
static compare_t MCSortCompareUsingHandler(void *context, MCValueRef p_left, MCValueRef p_right)
{
MCHandlerRef t_handler;
t_handler = *(MCHandlerRef *)context;
MCAutoValueRefArray t_values;
t_values.Push(p_left);
t_values.Push(p_right);
MCAutoValueRef t_result;
MCHandlerInvoke(t_handler, t_values . Ptr(), t_values . Size(), &t_result);
if (*t_result != nil && MCValueGetTypeCode(*t_result) == kMCValueTypeCodeNumber)
return MCNumberFetchAsInteger((MCNumberRef)*t_result);
return 0;
}
extern "C" MC_DLLEXPORT_DEF void MCSortExecSortListUsingHandler(MCProperListRef& x_target, MCHandlerRef p_handler)
{
MCAutoProperListRef t_mutable_list;
if (!MCProperListMutableCopy(x_target, &t_mutable_list))
return;
MCProperListStableSort(*t_mutable_list, false, MCSortCompareUsingHandler, &p_handler);
MCAutoProperListRef t_sorted_list;
if (!MCProperListCopy(*t_mutable_list, &t_sorted_list))
return;
MCValueAssign(x_target, *t_sorted_list);
}
////////////////////////////////////////////////////////////////
extern "C" bool com_livecode_sort_Initialize (void)
{
return true;
}
extern "C" void com_livecode_sort_Finalize (void)
{
}
////////////////////////////////////////////////////////////////////////////////////////////////////
#ifdef _TEST
extern void log(const char *module, const char *test, bool result);
#define log_result(test, result) log("SORT MODULE", test, result)
void MCSortRunTests()
{
/*
void MCSortExecSortListAscendingText(MCProperListRef& x_target)
void MCSortExecSortListDescendingText(MCProperListRef& x_target)
void MCSortExecSortListAscendingBinary(MCProperListRef& x_target)
void MCSortExecSortListDescendingBinary(MCProperListRef& x_target)
void MCSortExecSortListAscendingNumeric(MCProperListRef& x_target)
void MCSortExecSortListDescendingNumeric(MCProperListRef& x_target)
void MCSortExecSortListAscendingDateTime(MCProperListRef& x_target)
void MCSortExecSortListDescendingDateTime(MCProperListRef& x_target)
*/
MCAutoNumberRef t_1n, t_2n;
MCNumberCreateWithInteger(1, &t_1n);
MCNumberCreateWithInteger(2, &t_2n);
MCAutoStringRef t_string1, t_string2;
MCStringCreateWithNativeChars((const char_t *)"a", 1, &t_string1);
MCStringCreateWithNativeChars((const char_t *)"b", 1, &t_string2);
byte_t t_bytes[2] = { 0x01, 0x02 };
MCAutoDataRef t_data1, t_data2;
MCDataCreateWithBytes((const byte_t *)t_bytes, 1, &t_data1);
MCDataCreateWithBytes((const byte_t *)(t_bytes + 1), 1, &t_data2);
MCAutoArray<MCValueRef> t_list_elts;
t_list_elts . Push(*t_2n);
t_list_elts . Push(*t_string2);
t_list_elts . Push(*t_data2);
t_list_elts . Push(*t_1n);
t_list_elts . Push(*t_string1);
t_list_elts . Push(*t_data1);
MCProperListRef t_list;
MCProperListCreateMutable(t_list);
MCProperListPushElementsOntoBack(t_list, t_list_elts . Ptr(), t_list_elts . Size());
MCSortExecSortListAscendingNumeric(t_list);
// should be 1,2,b,0x02,a,0x01
log_result("sort numeric", MCNumberFetchAsInteger((MCNumberRef)MCProperListFetchHead(t_list)) == 1);
log_result("sort numeric stable", MCDataIsEqualTo((MCDataRef)MCProperListFetchTail(t_list), *t_data1));
MCSortExecSortListAscendingText(t_list);
// should be a,b,1,2,0x02,0x01
log_result("sort text", MCStringIsEqualTo((MCStringRef)MCProperListFetchHead(t_list), *t_string1, kMCStringOptionCompareCaseless));
log_result("sort text stable", MCDataIsEqualTo((MCDataRef)MCProperListFetchTail(t_list), *t_data1));
MCSortExecSortListAscendingBinary(t_list);
// should be 0x01,0x02,a,b,1,2
log_result("sort binary", MCDataIsEqualTo((MCDataRef)MCProperListFetchHead(t_list), *t_data1));
log_result("sort binary stable", MCNumberFetchAsInteger((MCNumberRef)MCProperListFetchTail(t_list)) == 2);
MCValueRelease(t_list);
}
#endif