History

moved to local implementation of array.shuffle()

Ge Wang committed 3 years ago

Commit 3dd124e1 parent 772c0aa5 files · +49 −21Browse files

Unified · Split

5 files
  1. examples/array/array_reverse.ck +1 −1
  2. examples/array/array_shuffle.ck +2 −2
  3. src/core/chuck_lang.cpp +6 −6
  4. src/core/chuck_oo.cpp +28 −6
  5. src/core/chuck_oo.h +12 −6
examples/array/array_reverse.ck+1−1
@@ -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}
examples/array/array_shuffle.ck+2−2
@@ -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 replacement23// one way to do random without replacement
24repeat( 3 )24repeat( 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 shuffle29 // just go through array in order after shuffle
src/core/chuck_lang.cpp+6−6
@@ -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 programmer974 // note 1.4.1.0: in the future, should deprecate and encourage programmer
980 // to explicitly use .size() OR .capacity(), depending on the context975 // 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.";
src/core/chuck_oo.cpp+28−6
@@ -700,7 +700,27 @@ void Chuck_Array4::get_keys( std::vector<std::string> & keys ) · 699 unmodified lines
700 700
701 701
702 702
703int my_ck_random(int i) { return ck_random() % i;}703
704t_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//-----------------------------------------------------------------------------
710template<class RandomIt>
711static 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//-----------------------------------------------------------------------------
709void Chuck_Array4::shuffle()729void 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//-----------------------------------------------------------------------------
1132void Chuck_Array8::shuffle()1152void 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 array1523// desc: shuffle the contents of the array
1503//-----------------------------------------------------------------------------1524//-----------------------------------------------------------------------------
1504void Chuck_Array16::shuffle()1525void 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 array1986// desc: shuffle the contents of the array
1965//-----------------------------------------------------------------------------1987//-----------------------------------------------------------------------------
1966void Chuck_Array24::shuffle()1988void 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//-----------------------------------------------------------------------------
2326void Chuck_Array32::shuffle()2348void 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
src/core/chuck_oo.h+12−6
@@ -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) nshaheed235 // 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) azaday237 // 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) nshaheed281 // 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) azaday283 // 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
286public:288public:
@@ -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) nshaheed335 // 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) azaday337 // 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
339public:342public:
@@ -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) nshaheed385 // 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) azaday387 // 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
388public:392public:
@@ -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) nshaheed435 // 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) azaday437 // 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
437public:442public:
@@ -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) nshaheed483 // 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) azaday485 // 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
484public:490public:

Keyboard shortcuts

General

Show keyboard shortcuts
?
Focus the current search
/

Issues and pull requests

Open filters
f

Issues, pull requests and Inbox

Select next item
j↓
Select previous item
k↑
Open selected item
Enter
Dismiss filters or clear selection
Esc

Repository code

Go to file
t