Unified · Split
| @@ -23,4 +23,4 @@ fun void print_arr(int a[]) { · 22 unmodified lines | |||
| 23 | cherr <= a[i] <= " "; | 23 | cherr <= a[i] <= " "; |
| 24 | } | 24 | } |
| 25 | cherr <= IO.nl(); | 25 | cherr <= IO.nl(); |
| 26 | } | 26 | } |
| @@ -20,10 +20,10 @@ repeat( 10 ) · 19 unmodified lines | |||
| 20 | a.shuffle(); | 20 | a.shuffle(); |
| 21 | } | 21 | } |
| 22 | 22 | ||
| 23 | // a way to do random without replacement | 23 | // one way to do random without replacement |
| 24 | repeat( 3 ) | 24 | repeat( 3 ) |
| 25 | { | 25 | { |
| 26 | cherr <= "--- random with replacment --- " <= IO.nl(); | 26 | cherr <= "--- random without replacment --- " <= IO.nl(); |
| 27 | // shuffle! | 27 | // shuffle! |
| 28 | a.shuffle(); | 28 | a.shuffle(); |
| 29 | // just go through array in order after shuffle | 29 | // just go through array in order after shuffle |
| @@ -965,16 +965,11 @@ t_CKBOOL init_class_array( Chuck_Env * env, Chuck_Type * type ) · 964 unmodified lines | |||
| 965 | func->doc = "set the size of the array. If the new size is less than the current size, elements will be deleted from the end; if the new size is larger than the current size, 0 or null elements will be added to the end."; | 965 | func->doc = "set the size of the array. If the new size is less than the current size, elements will be deleted from the end; if the new size is larger than the current size, 0 or null elements will be added to the end."; |
| 966 | if( !type_engine_import_mfun( env, func ) ) goto error; | 966 | if( !type_engine_import_mfun( env, func ) ) goto error; |
| 967 | 967 | ||
| 968 | // (1.4.1.1 nshaheed) add getKeys() | 968 | // add getKeys() | (1.4.1.1) nshaheed |
| 969 | func = make_new_mfun( "void", "getKeys", array_get_keys ); | 969 | func = make_new_mfun( "void", "getKeys", array_get_keys ); |
| 970 | func->add_arg( "string[]", "keys" ); | 970 | func->add_arg( "string[]", "keys" ); |
| 971 | func->doc = "return all keys found in associative array in keys"; | 971 | func->doc = "return all keys found in associative array in keys"; |
| 972 | if( !type_engine_import_mfun( env, func ) ) goto error; | 972 | if( !type_engine_import_mfun( env, func ) ) goto error; |
| 973 | |||
| 974 | // (1.4.2.1 azaday) add reverse() | ||
| 975 | func = make_new_mfun( "void", "reverse", array_reverse ); | ||
| 976 | func->doc = "reverses the array in-place"; | ||
| 977 | if( !type_engine_import_mfun( env, func ) ) goto error; | ||
| 978 | 973 | ||
| 979 | // note 1.4.1.0: in the future, should deprecate and encourage programmer | 974 | // note 1.4.1.0: in the future, should deprecate and encourage programmer |
| 980 | // to explicitly use .size() OR .capacity(), depending on the context | 975 | // to explicitly use .size() OR .capacity(), depending on the context |
| @@ -1005,6 +1000,11 @@ t_CKBOOL init_class_array( Chuck_Env * env, Chuck_Type * type ) · 24 unmodified lines | |||
| 1005 | func->doc = "(map only) Erase all elements with the specified key."; | 1000 | func->doc = "(map only) Erase all elements with the specified key."; |
| 1006 | if( !type_engine_import_mfun( env, func ) ) goto error; | 1001 | if( !type_engine_import_mfun( env, func ) ) goto error; |
| 1007 | 1002 | ||
| 1003 | // add reverse() | (1.4.2.1) azaday | ||
| 1004 | func = make_new_mfun( "void", "reverse", array_reverse ); | ||
| 1005 | func->doc = "reverses the array in-place"; | ||
| 1006 | if( !type_engine_import_mfun( env, func ) ) goto error; | ||
| 1007 | |||
| 1008 | // add shuffle() | 1008 | // add shuffle() |
| 1009 | func = make_new_mfun( "void", "shuffle", array_shuffle ); | 1009 | func = make_new_mfun( "void", "shuffle", array_shuffle ); |
| 1010 | func->doc = "shuffle the contents of the array."; | 1010 | func->doc = "shuffle the contents of the array."; |
| @@ -700,7 +700,27 @@ void Chuck_Array4::get_keys( std::vector<std::string> & keys ) · 699 unmodified lines | |||
| 700 | 700 | ||
| 701 | 701 | ||
| 702 | 702 | ||
| 703 | int my_ck_random(int i) { return ck_random() % i;} | 703 | |
| 704 | t_CKINT my_ck_random( t_CKINT i ) { return ck_random() % i;} | ||
| 705 | //----------------------------------------------------------------------------- | ||
| 706 | // name: my_random_shuffle() | 1.4.2.1 | ||
| 707 | // desc: random shuffle an array | ||
| 708 | // adapted from https://en.cppreference.com/w/cpp/algorithm/random_shuffle | ||
| 709 | //----------------------------------------------------------------------------- | ||
| 710 | template<class RandomIt> | ||
| 711 | static void my_random_shuffle( RandomIt first, RandomIt last ) | ||
| 712 | { | ||
| 713 | typedef typename std::iterator_traits<RandomIt>::difference_type diff_t; | ||
| 714 | // iterate | ||
| 715 | for( diff_t i = last - first - 1; i > 0; --i ) | ||
| 716 | { | ||
| 717 | // swap | ||
| 718 | std::swap(first[i], first[my_ck_random(i + 1)]); | ||
| 719 | } | ||
| 720 | } | ||
| 721 | |||
| 722 | |||
| 723 | |||
| 704 | 724 | ||
| 705 | //----------------------------------------------------------------------------- | 725 | //----------------------------------------------------------------------------- |
| 706 | // name: shuffle() | 1.4.2.1 nshaheed, azaday, kunwoo, ge (added) | 726 | // name: shuffle() | 1.4.2.1 nshaheed, azaday, kunwoo, ge (added) |
| @@ -708,7 +728,7 @@ int my_ck_random(int i) { return ck_random() % i;} · 1 unmodified line | |||
| 708 | //----------------------------------------------------------------------------- | 728 | //----------------------------------------------------------------------------- |
| 709 | void Chuck_Array4::shuffle() | 729 | void Chuck_Array4::shuffle() |
| 710 | { | 730 | { |
| 711 | std::random_shuffle( m_vector.begin(), m_vector.end(), my_ck_random ); | 731 | my_random_shuffle( m_vector.begin(), m_vector.end() ); |
| 712 | } | 732 | } |
| 713 | 733 | ||
| 714 | 734 | ||
| @@ -1131,7 +1151,7 @@ void Chuck_Array8::reverse( ) · 416 unmodified lines | |||
| 1131 | //----------------------------------------------------------------------------- | 1151 | //----------------------------------------------------------------------------- |
| 1132 | void Chuck_Array8::shuffle() | 1152 | void Chuck_Array8::shuffle() |
| 1133 | { | 1153 | { |
| 1134 | std::random_shuffle( m_vector.begin(), m_vector.end(), my_ck_random ); | 1154 | my_random_shuffle( m_vector.begin(), m_vector.end() ); |
| 1135 | } | 1155 | } |
| 1136 | 1156 | ||
| 1137 | 1157 | ||
| @@ -1498,12 +1518,13 @@ void Chuck_Array16::reverse( ) · 360 unmodified lines | |||
| 1498 | 1518 | ||
| 1499 | 1519 | ||
| 1500 | 1520 | ||
| 1521 | //----------------------------------------------------------------------------- | ||
| 1501 | // name: shuffle() | 1.4.2.1 nshaheed, azaday, kunwoo, ge (added) | 1522 | // name: shuffle() | 1.4.2.1 nshaheed, azaday, kunwoo, ge (added) |
| 1502 | // desc: shuffle the contents of the array | 1523 | // desc: shuffle the contents of the array |
| 1503 | //----------------------------------------------------------------------------- | 1524 | //----------------------------------------------------------------------------- |
| 1504 | void Chuck_Array16::shuffle() | 1525 | void Chuck_Array16::shuffle() |
| 1505 | { | 1526 | { |
| 1506 | std::random_shuffle( m_vector.begin(), m_vector.end(), my_ck_random ); | 1527 | my_random_shuffle( m_vector.begin(), m_vector.end() ); |
| 1507 | } | 1528 | } |
| 1508 | 1529 | ||
| 1509 | 1530 | ||
| @@ -1960,12 +1981,13 @@ void Chuck_Array24::reverse( ) · 450 unmodified lines | |||
| 1960 | 1981 | ||
| 1961 | 1982 | ||
| 1962 | 1983 | ||
| 1984 | //----------------------------------------------------------------------------- | ||
| 1963 | // name: shuffle() | 1.4.2.1 nshaheed, azaday, kunwoo, ge (added) | 1985 | // name: shuffle() | 1.4.2.1 nshaheed, azaday, kunwoo, ge (added) |
| 1964 | // desc: shuffle the contents of the array | 1986 | // desc: shuffle the contents of the array |
| 1965 | //----------------------------------------------------------------------------- | 1987 | //----------------------------------------------------------------------------- |
| 1966 | void Chuck_Array24::shuffle() | 1988 | void Chuck_Array24::shuffle() |
| 1967 | { | 1989 | { |
| 1968 | std::random_shuffle( m_vector.begin(), m_vector.end(), my_ck_random ); | 1990 | my_random_shuffle( m_vector.begin(), m_vector.end() ); |
| 1969 | } | 1991 | } |
| 1970 | 1992 | ||
| 1971 | 1993 | ||
| @@ -2325,7 +2347,7 @@ void Chuck_Array32::reverse( ) · 353 unmodified lines | |||
| 2325 | //----------------------------------------------------------------------------- | 2347 | //----------------------------------------------------------------------------- |
| 2326 | void Chuck_Array32::shuffle() | 2348 | void Chuck_Array32::shuffle() |
| 2327 | { | 2349 | { |
| 2328 | std::random_shuffle( m_vector.begin(), m_vector.end(), my_ck_random ); | 2350 | my_random_shuffle( m_vector.begin(), m_vector.end() ); |
| 2329 | } | 2351 | } |
| 2330 | 2352 | ||
| 2331 | 2353 | ||
| @@ -234,8 +234,9 @@ public: · 233 unmodified lines | |||
| 234 | virtual void zero() = 0; // zero (all) | 234 | virtual void zero() = 0; // zero (all) |
| 235 | // get map keys | added (1.4.2.0) nshaheed | 235 | // get map keys | added (1.4.2.0) nshaheed |
| 236 | virtual void get_keys( std::vector<std::string> & keys ) = 0; | 236 | virtual void get_keys( std::vector<std::string> & keys ) = 0; |
| 237 | // reverse array order | added (1.4.2.0) azaday | 237 | // reverse array order | added (1.4.2.1) azaday |
| 238 | virtual void reverse() = 0; | 238 | virtual void reverse() = 0; |
| 239 | // reverse array order | added (1.4.2.1) kunwoo, nshaheed, azaday, ge | ||
| 239 | virtual void shuffle() = 0; | 240 | virtual void shuffle() = 0; |
| 240 | 241 | ||
| 241 | Chuck_Type * m_array_type; | 242 | Chuck_Type * m_array_type; |
| @@ -279,8 +280,9 @@ public: · 37 unmodified lines | |||
| 279 | virtual t_CKINT data_type_kind( ) { return CHUCK_ARRAY4_DATAKIND; } | 280 | virtual t_CKINT data_type_kind( ) { return CHUCK_ARRAY4_DATAKIND; } |
| 280 | // get map keys | added (1.4.2.0) nshaheed | 281 | // get map keys | added (1.4.2.0) nshaheed |
| 281 | virtual void get_keys( std::vector<std::string> & keys ); | 282 | virtual void get_keys( std::vector<std::string> & keys ); |
| 282 | // reverse array order | added (1.4.2.0) azaday | 283 | // reverse array order | added (1.4.2.1) azaday |
| 283 | virtual void reverse(); | 284 | virtual void reverse(); |
| 285 | // reverse array order | added (1.4.2.1) kunwoo, nshaheed, azaday, ge | ||
| 284 | virtual void shuffle(); | 286 | virtual void shuffle(); |
| 285 | 287 | ||
| 286 | public: | 288 | public: |
| @@ -332,8 +334,9 @@ public: · 45 unmodified lines | |||
| 332 | virtual t_CKINT data_type_kind( ) { return CHUCK_ARRAY8_DATAKIND; } | 334 | virtual t_CKINT data_type_kind( ) { return CHUCK_ARRAY8_DATAKIND; } |
| 333 | // get map keys | added (1.4.2.0) nshaheed | 335 | // get map keys | added (1.4.2.0) nshaheed |
| 334 | virtual void get_keys( std::vector<std::string> & keys ); | 336 | virtual void get_keys( std::vector<std::string> & keys ); |
| 335 | // reverse array order | added (1.4.2.0) azaday | 337 | // reverse array order | added (1.4.2.1) azaday |
| 336 | virtual void reverse(); | 338 | virtual void reverse(); |
| 339 | // reverse array order | added (1.4.2.1) kunwoo, nshaheed, azaday, ge | ||
| 337 | virtual void shuffle(); | 340 | virtual void shuffle(); |
| 338 | 341 | ||
| 339 | public: | 342 | public: |
| @@ -381,8 +384,9 @@ public: · 41 unmodified lines | |||
| 381 | virtual t_CKINT data_type_kind( ) { return CHUCK_ARRAY16_DATAKIND; } | 384 | virtual t_CKINT data_type_kind( ) { return CHUCK_ARRAY16_DATAKIND; } |
| 382 | // get map keys | added (1.4.2.0) nshaheed | 385 | // get map keys | added (1.4.2.0) nshaheed |
| 383 | virtual void get_keys( std::vector<std::string> & keys ); | 386 | virtual void get_keys( std::vector<std::string> & keys ); |
| 384 | // reverse array order | added (1.4.2.0) azaday | 387 | // reverse array order | added (1.4.2.1) azaday |
| 385 | virtual void reverse(); | 388 | virtual void reverse(); |
| 389 | // reverse array order | added (1.4.2.1) kunwoo, nshaheed, azaday, ge | ||
| 386 | virtual void shuffle(); | 390 | virtual void shuffle(); |
| 387 | 391 | ||
| 388 | public: | 392 | public: |
| @@ -430,8 +434,9 @@ public: · 41 unmodified lines | |||
| 430 | virtual t_CKINT data_type_kind( ) { return CHUCK_ARRAY24_DATAKIND; } | 434 | virtual t_CKINT data_type_kind( ) { return CHUCK_ARRAY24_DATAKIND; } |
| 431 | // get map keys | added (1.4.2.0) nshaheed | 435 | // get map keys | added (1.4.2.0) nshaheed |
| 432 | virtual void get_keys( std::vector<std::string> & keys ); | 436 | virtual void get_keys( std::vector<std::string> & keys ); |
| 433 | // reverse array order | added (1.4.2.0) azaday | 437 | // reverse array order | added (1.4.2.1) azaday |
| 434 | virtual void reverse(); | 438 | virtual void reverse(); |
| 439 | // reverse array order | added (1.4.2.1) kunwoo, nshaheed, azaday, ge | ||
| 435 | virtual void shuffle(); | 440 | virtual void shuffle(); |
| 436 | 441 | ||
| 437 | public: | 442 | public: |
| @@ -477,8 +482,9 @@ public: · 39 unmodified lines | |||
| 477 | virtual t_CKINT data_type_kind( ) { return CHUCK_ARRAY32_DATAKIND; } | 482 | virtual t_CKINT data_type_kind( ) { return CHUCK_ARRAY32_DATAKIND; } |
| 478 | // get map keys | added (1.4.1.2) nshaheed | 483 | // get map keys | added (1.4.1.2) nshaheed |
| 479 | virtual void get_keys( std::vector<std::string> & keys ); | 484 | virtual void get_keys( std::vector<std::string> & keys ); |
| 480 | // reverse array order | added (1.4.2.0) azaday | 485 | // reverse array order | added (1.4.2.1) azaday |
| 481 | virtual void reverse(); | 486 | virtual void reverse(); |
| 487 | // reverse array order | added (1.4.2.1) kunwoo, nshaheed, azaday, ge | ||
| 482 | virtual void shuffle(); | 488 | virtual void shuffle(); |
| 483 | 489 | ||
| 484 | public: | 490 | public: |