GeezerVisonThe World Wide Web, restored to 800 pixels of glory. |
| Home|About|Gaming cabinet|Archives|How it works|Starter sites |
GeezerVison is showing github.com — livecode/libscript/src/module-map.cpp at develop · livecode/livecode
This repository was archived by the owner on Aug 31, 2021. It is now read-only.
Expand file tree/ Copy pathmodule-map.cppMore file actions 531 lines (407 loc) · 18.4 KB / Copy pathmodule-map.cppFile metadata and controls531 lines (407 loc) · 18.4 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 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 /* 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 create_key_for_array(MCStringRef p_string, MCArrayRef p_target, MCNameRef& r_key) { MCAutoStringRef t_key_string; bool t_success; t_success = true; /* if (MCArrayIsFormSensitive(p_target) && !MCStringIsNative(p_string)) { t_success = MCStringCreateWithBytes((const byte_t *)MCStringGetCharPtr(p_string), MCStringGetLength(p_string) * 2, kMCStringEncodingNative, false, &t_key_string); } else */ t_key_string = p_string; if (t_success) t_success = MCNameCreate(*t_key_string, r_key); return t_success; } static bool create_key_for_matrix(MCProperListRef p_list, MCNameRef*& r_path, uindex_t& r_length) { bool t_success; t_success = true; MCAutoArray<MCNameRef> t_path; for (uindex_t i = 0; t_success && i < MCProperListGetLength(p_list); i++) { MCValueRef t_list_element; t_list_element = MCProperListFetchElementAtIndex(p_list, i); t_success = MCValueGetTypeCode(t_list_element) == kMCValueTypeCodeNumber && MCNumberIsInteger((MCNumberRef)t_list_element); MCAutoStringRef t_string; MCNameRef t_key; t_key = nil; if (t_success) t_success = MCStringFormat(&t_string, "%d", MCNumberFetchAsInteger((MCNumberRef)t_list_element)) && MCNameCreate(*t_string, t_key); if (t_success) t_success = t_path . Push(t_key); if (!t_success) MCValueRelease(t_key); } t_path . Take(r_path, r_length); return t_success; } 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" void MCMapEvalKeysOf(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; // ctxt . Throw() } extern "C" void MCMapEvalElementsOf(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; // ctxt . Throw() } extern "C" void MCMapEvalNumberOfElementsIn(MCArrayRef p_target, uindex_t& r_output) { r_output = MCArrayGetCount(p_target); } extern "C" void MCMapEvalIsAmongTheElementsOf(MCValueRef p_needle, bool p_is_not, MCArrayRef p_target, bool& r_output) { r_output = !MCArrayApply(p_target, is_not_among_the_elements_of, p_needle); } extern "C" void MCMapEvalIsAmongTheKeysOf(MCStringRef p_needle, bool p_is_not, MCArrayRef p_target, bool& r_output) { MCNewAutoNameRef t_key; create_key_for_array(p_needle, p_target, &t_key); MCValueRef t_value; t_value = nil; r_output = MCArrayFetchValue(p_target, MCArrayIsCaseSensitive(p_target), *t_key, t_value); if (p_is_not) r_output = !r_output; } void MCMapEvalIsAmongTheKeysOfNumeric(integer_t p_needle, bool p_is_not, MCArrayRef p_target, bool& r_output) { MCAutoStringRef t_string; if (MCStringFormat(&t_string, "%d", p_needle)) { MCMapEvalIsAmongTheKeysOf(*t_string, p_is_not, p_target, r_output); return; } // ctxt . Throw } void MCMapEvalIsAmongTheKeysOfMatrix(MCProperListRef p_needle, bool p_is_not, MCArrayRef p_target, bool& r_output) { bool t_output; t_output = false; MCNameRef *t_path; uindex_t t_path_length; if (create_key_for_matrix(p_needle, t_path, t_path_length)) { MCValueRef t_value; t_value = nil; t_output = MCArrayFetchValueOnPath(p_target, true, t_path, t_path_length, t_value); } for (uindex_t i = 0; i < t_path_length; i++) MCValueRelease(t_path[i]); MCMemoryDeleteArray(t_path); if (p_is_not) t_output = !t_output; r_output = t_output; } void MCMapFetchElementOfBinary(MCArrayRef p_target, MCDataRef p_key, MCValueRef& r_output) { MCAutoStringRef t_key_string; MCNewAutoNameRef t_key; if (MCStringCreateWithBytes(MCDataGetBytePtr(p_key), MCDataGetLength(p_key), kMCStringEncodingNative, false, &t_key_string) && MCNameCreate(*t_key_string, &t_key) && MCArrayFetchValue(p_target, false, *t_key, r_output)) { MCValueRetain(r_output); return; } } void MCMapStoreElementOfBinary(MCValueRef p_value, MCArrayRef& x_target, MCDataRef p_key) { MCAutoStringRef t_key_string; MCNewAutoNameRef t_key; MCAutoArrayRef t_array; MCArrayMutableCopy(x_target, &t_array); if (!MCStringCreateWithBytes(MCDataGetBytePtr(p_key), MCDataGetLength(p_key), kMCStringEncodingNative, false, &t_key_string) || !MCNameCreate(*t_key_string, &t_key) || !MCArrayStoreValue(*t_array, false, *t_key, p_value)) return; MCAutoArrayRef t_new_array; if (!MCArrayCopy(*t_array, &t_new_array)) return; MCValueAssign(x_target, *t_new_array); } extern "C" void MCMapFetchElementOf(MCArrayRef p_target, MCStringRef p_key, MCValueRef& r_output) { MCNewAutoNameRef t_key; if (create_key_for_array(p_key, p_target, &t_key) && MCArrayFetchValue(p_target, MCArrayIsCaseSensitive(p_target), *t_key, r_output)) { MCValueRetain(r_output); return; } } extern "C" void MCMapStoreElementOf(MCValueRef p_value, MCArrayRef& x_target, MCStringRef p_key) { MCNewAutoNameRef t_key; MCAutoArrayRef t_array; MCArrayMutableCopy(x_target, &t_array); if (!create_key_for_array(p_key, x_target, &t_key) || !MCArrayStoreValue(*t_array, MCArrayIsCaseSensitive(*t_array), *t_key, p_value)) return; MCAutoArrayRef t_new_array; if (!MCArrayCopy(*t_array, &t_new_array)) return; MCValueAssign(x_target, *t_new_array); } void MCMapFetchElementOfNumeric(MCArrayRef p_target, integer_t p_key, MCValueRef& r_output) { MCAutoStringRef t_string; if (MCStringFormat(&t_string, "%d", p_key)) { MCMapFetchElementOf(p_target, *t_string, r_output); return; } // ctxt . Throw } void MCMapStoreElementOfNumeric(MCValueRef p_value, MCArrayRef& x_target, integer_t p_key) { MCAutoStringRef t_string; if (MCStringFormat(&t_string, "%d", p_key)) { MCMapStoreElementOf(p_value, x_target, *t_string); return; } } void MCMapFetchElementOfMatrix(MCArrayRef p_target, MCProperListRef p_key, MCValueRef& r_output) { bool t_found; MCNameRef *t_path; uindex_t t_path_length; t_found = create_key_for_matrix(p_key, t_path, t_path_length) && MCArrayFetchValueOnPath(p_target, true, t_path, t_path_length, r_output); for (uindex_t i = 0; i < t_path_length; i++) MCValueRelease(t_path[i]); MCMemoryDeleteArray(t_path); if (t_found) { MCValueRetain(r_output); return; } // ctxt . Throw() } void MCMapStoreElementOfMatrix(MCValueRef p_value, MCArrayRef& x_target, MCProperListRef p_key) { bool t_success; MCNewAutoNameRef t_key; MCAutoArrayRef t_array; MCArrayMutableCopy(x_target, &t_array); MCNameRef *t_path; uindex_t t_path_length; t_success = create_key_for_matrix(p_key, t_path, t_path_length) && MCArrayStoreValueOnPath(*t_array, true, t_path, t_path_length, p_value); for (uindex_t i = 0; i < t_path_length; i++) MCValueRelease(t_path[i]); MCMemoryDeleteArray(t_path); if (!t_success) return; MCAutoArrayRef t_new_array; if (!MCArrayCopy(*t_array, &t_new_array)) return; MCValueAssign(x_target, *t_new_array); } #ifdef _TEST extern void log(const char *module, const char *test, bool result); #define log_result(test, result) log("MAP MODULE", test, result) void MCMapRunTests() { /* MCMapFetchElementOfBinary(MCArrayRef p_target, MCDataRef p_key, MCValueRef& r_output) MCMapStoreElementOfBinary(MCValueRef p_value, MCArrayRef& x_target, MCDataRef p_key) MCMapFetchElementOf(MCArrayRef p_target, MCStringRef p_key, MCValueRef& r_output) MCMapStoreElementOf(MCValueRef p_value, MCArrayRef& x_target, MCStringRef p_key) MCMapFetchElementOfNumeric(MCArrayRef p_target, integer_t p_key, MCValueRef& r_output) MCMapStoreElementOfNumeric(MCValueRef p_value, MCArrayRef& x_target, integer_t p_key) */ MCAutoArrayRef t_array, t_array_for_keys; MCAutoArrayRef t_caseless, t_cs_array, t_cs_fs_array, t_fs_array; MCArrayCreateMutable(&t_array); MCNewAutoNameRef t_1, t_2, t_3; MCNameCreateWithNativeChars((const char_t *)"1", 1, &t_1); MCNameCreateWithNativeChars((const char_t *)"2", 1, &t_2); MCNameCreateWithNativeChars((const char_t *)"3", 1, &t_3); MCAutoNumberRef t_1n, t_2n, t_3n; MCNumberCreateWithInteger(1, &t_1n); MCNumberCreateWithInteger(2, &t_2n); MCNumberCreateWithInteger(3, &t_3n); MCAutoArray<MCNameRef> t_path; t_path . Push(*t_1); t_path . Push(*t_2); t_path . Push(*t_3); MCAutoArray<MCValueRef> t_list_elts; t_list_elts . Push(*t_1n); t_list_elts . Push(*t_2n); t_list_elts . Push(*t_3n); unichar_t t_chars[6] = {0xE1, 'a', 0x301, 0xC1, 'A', 0x301}; MCAutoStringRef t_lc_norm_str, t_uc_norm_str, t_lc_decomp_str, t_uc_decomp_str; MCStringCreateWithChars((const unichar_t *)t_chars, 1, &t_lc_norm_str); MCStringCreateWithChars((const unichar_t *)&t_chars[1], 2, &t_lc_decomp_str); MCStringCreateWithChars((const unichar_t *)&t_chars[3], 1, &t_uc_norm_str); MCStringCreateWithChars((const unichar_t *)&t_chars[4], 2, &t_uc_decomp_str); MCNewAutoNameRef t_lc_norm, t_uc_norm, t_lc_decomp, t_uc_decomp; MCNameCreate(*t_lc_norm_str, &t_lc_norm); MCNameCreate(*t_uc_norm_str, &t_uc_norm); MCNameCreate(*t_lc_decomp_str, &t_lc_decomp); MCNameCreate(*t_uc_decomp_str, &t_uc_decomp); MCAutoArray<MCNameRef> t_keys; t_keys . Push(*t_lc_norm); t_keys . Push(*t_lc_decomp); t_keys . Push(*t_uc_norm); t_keys . Push(*t_uc_decomp); MCAutoArray<MCStringRef> t_strings; t_strings . Push(*t_lc_norm_str); t_strings . Push(*t_lc_decomp_str); t_strings . Push(*t_uc_norm_str); t_strings . Push(*t_uc_decomp_str); MCNameRef t_key = *t_lc_norm; MCArrayCreateWithOptions(false, false, &t_key, t_list_elts . Ptr(), 1, &t_caseless); t_key = *t_lc_decomp; MCArrayCreateWithOptions(false, true, &t_key, t_list_elts . Ptr(), 1, &t_fs_array); t_key = *t_uc_norm; MCArrayCreateWithOptions(true, false, &t_key, t_list_elts . Ptr(), 1, &t_cs_array); t_key = *t_uc_decomp; MCArrayCreateWithOptions(true, true, &t_key, t_list_elts . Ptr(), 1, &t_cs_fs_array); /* MCMapEvalIsAmongTheKeysOf(MCStringRef p_needle, bool p_is_not, MCArrayRef p_target, bool& r_output) */ bool t_none, t_cs, t_fs, t_cs_fs; MCMapEvalIsAmongTheKeysOf(*t_lc_norm_str, false, *t_caseless, t_none); MCMapEvalIsAmongTheKeysOf(*t_uc_norm_str, false, *t_caseless, t_cs); MCMapEvalIsAmongTheKeysOf(*t_lc_decomp_str, false, *t_caseless, t_fs); MCMapEvalIsAmongTheKeysOf(*t_uc_decomp_str, false, *t_caseless, t_cs_fs); log_result("caseless map, uncased string", t_none); log_result("caseless map, cased string", t_cs); log_result("caseless map, uncased decomp string", t_fs); log_result("caseless map, cased decomp string", t_cs_fs); MCMapEvalIsAmongTheKeysOf(*t_lc_norm_str, true, *t_cs_array, t_none); MCMapEvalIsAmongTheKeysOf(*t_uc_norm_str, false, *t_cs_array, t_cs); MCMapEvalIsAmongTheKeysOf(*t_lc_decomp_str, true, *t_cs_array, t_fs); MCMapEvalIsAmongTheKeysOf(*t_uc_decomp_str, false, *t_cs_array, t_cs_fs); log_result("case sensitive map, uncased string", t_none); log_result("case sensitive map, cased string", t_cs); log_result("case sensitive map, uncased decomp string", t_fs); log_result("case sensitive map, cased decomp string", t_cs_fs); MCMapEvalIsAmongTheKeysOf(*t_lc_norm_str, true, *t_fs_array, t_none); MCMapEvalIsAmongTheKeysOf(*t_uc_norm_str, true, *t_fs_array, t_cs); MCMapEvalIsAmongTheKeysOf(*t_lc_decomp_str, false, *t_fs_array, t_fs); MCMapEvalIsAmongTheKeysOf(*t_uc_decomp_str, false, *t_fs_array, t_cs_fs); log_result("form sensitive map, uncased string", t_none); log_result("form sensitive map, cased string", t_cs); log_result("form sensitive map, uncased decomp string", t_fs); log_result("form sensitive map, cased decomp string", t_cs_fs); MCMapEvalIsAmongTheKeysOf(*t_lc_norm_str, true, *t_cs_fs_array, t_none); MCMapEvalIsAmongTheKeysOf(*t_uc_norm_str, true, *t_cs_fs_array, t_cs); MCMapEvalIsAmongTheKeysOf(*t_lc_decomp_str, true, *t_cs_fs_array, t_fs); MCMapEvalIsAmongTheKeysOf(*t_uc_decomp_str, false, *t_cs_fs_array, t_cs_fs); log_result("case and form sensitive map, uncased string", t_none); log_result("case and form sensitive map map, cased string", t_cs); log_result("case and form sensitive map map, uncased decomp string", t_fs); log_result("case and form sensitive map map, cased decomp string", t_cs_fs); /*MCMapEvalKeysOf(MCArrayRef p_target, MCProperListRef& r_output)*/ MCAutoProperListRef t_keys_list; MCArrayCreateWithOptions(true, true, t_keys . Ptr(), (MCValueRef *)t_strings . Ptr(), t_keys . Size(), &t_array_for_keys); MCMapEvalKeysOf(*t_array_for_keys, &t_keys_list); bool t_result; t_result = MCProperListGetLength(*t_keys_list) == 4; uindex_t t_offset; if (t_result) t_result = MCProperListFirstIndexOfElement(*t_keys_list, *t_uc_decomp_str, 0, t_offset); if (t_result) t_result = MCProperListFirstIndexOfElement(*t_keys_list, *t_lc_decomp_str, 0, t_offset); if (t_result) t_result = MCProperListFirstIndexOfElement(*t_keys_list, *t_uc_norm_str, 0, t_offset); if (t_result) t_result = MCProperListFirstIndexOfElement(*t_keys_list, *t_lc_norm_str, 0, t_offset); log_result("the keys of", t_result); /*MCMapEvalElementsOf(MCArrayRef p_target, MCProperListRef& r_output)*/ MCAutoProperListRef t_elts_list; MCMapEvalElementsOf(*t_array_for_keys, &t_elts_list); t_result = MCProperListGetLength(*t_elts_list) == 4; if (t_result) t_result = MCProperListFirstIndexOfElement(*t_elts_list, *t_uc_decomp_str, 0, t_offset); if (t_result) t_result = MCProperListFirstIndexOfElement(*t_elts_list, *t_lc_decomp_str, 0, t_offset); if (t_result) t_result = MCProperListFirstIndexOfElement(*t_elts_list, *t_uc_norm_str, 0, t_offset); if (t_result) t_result = MCProperListFirstIndexOfElement(*t_elts_list, *t_lc_norm_str, 0, t_offset); log_result("the elements of", t_result); /*MCMapEvalNumberOfElementsIn(MCArrayRef p_target, uindex_t& r_output)*/ uindex_t t_number; MCMapEvalNumberOfElementsIn(*t_array_for_keys, t_number); log_result("the number of elements in", t_number == 4); /*MCMapEvalIsAmongTheElementsOf(MCValueRef p_needle, bool p_is_not, MCArrayRef p_target, bool& r_output)*/ t_result = true; if (t_result) MCMapEvalIsAmongTheElementsOf(*t_uc_decomp_str, false, *t_array_for_keys, t_result); if (t_result) MCMapEvalIsAmongTheElementsOf(*t_lc_decomp_str, false, *t_array_for_keys, t_result); if (t_result) MCMapEvalIsAmongTheElementsOf(*t_uc_norm_str, false, *t_array_for_keys, t_result); if (t_result) MCMapEvalIsAmongTheElementsOf(*t_lc_norm_str, false, *t_array_for_keys, t_result); log_result("is among the elements of", t_result); /*MCMapEvalIsAmongTheKeysOf(MCStringRef p_needle, bool p_is_not, MCArrayRef p_target, bool& r_output) MCMapEvalIsAmongTheKeysOfNumeric(integer_t p_needle, bool p_is_not, MCArrayRef p_target, bool& r_output) MCMapEvalIsAmongTheKeysOfMatrix(MCProperListRef p_needle, bool p_is_not, MCArrayRef p_target, bool& r_output)*/ MCArrayStoreValueOnPath(*t_array, true, t_path . Ptr(), t_path . Size(), kMCTrue); MCAutoProperListRef t_list; MCProperListCreateMutable(&t_list); MCProperListPushElementsOntoBack(*t_list, t_list_elts .Ptr(), t_list_elts . Size() - 1); bool t_is_among; MCMapEvalIsAmongTheKeysOfMatrix(*t_list, false, *t_array, t_is_among); log_result("is (partial) among the keys of matrix", t_is_among); MCProperListPushElementOntoBack(*t_list, *t_3n); MCMapEvalIsAmongTheKeysOfMatrix(*t_list, false, *t_array, t_is_among); log_result("is among the keys of matrix", t_is_among); /*MCMapStoreElementOfMatrix(MCValueRef p_value, MCArrayRef& x_target, MCProperListRef p_key)*/ MCAutoNumberRef t_4n; MCNumberCreateWithInteger(4, &t_4n); MCProperListPushElementOntoBack(*t_list, *t_4n); MCArrayRef t_matrix; MCArrayCreateMutable(t_matrix); MCMapStoreElementOfMatrix(kMCTrue, t_matrix, *t_list); /*MCMapFetchElementOfMatrix(MCArrayRef p_target, MCProperListRef p_key, MCValueRef& r_output)*/ MCValueRef t_value; MCMapFetchElementOfMatrix(t_matrix, *t_list, t_value); MCValueRelease(t_matrix); log_result("store/fetch element of matrix", t_value == kMCTrue); } #endif |
| You are visitor 01402503 — GeezerVison does not store the pages you look up. Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required. |