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
2323 cherr <= a[i] <= " ";
2424 }
2525 cherr <= IO.nl();
26−}
26+}
examples/array/array_shuffle.ck+2−2
@@ -20,10 +20,10 @@ repeat( 10 ) · 19 unmodified lines
2020 a.shuffle();
2121 }
2222
23−// a way to do random without replacement
23+// one way to do random without replacement
2424 repeat( 3 )
2525 {
26− cherr <= "--- random with replacment --- " <= IO.nl();
26+ cherr <= "--- random without replacment --- " <= IO.nl();
2727 // shuffle!
2828 a.shuffle();
2929 // 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
965965 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.";
966966 if( !type_engine_import_mfun( env, func ) ) goto error;
967967
968− // (1.4.1.1 nshaheed) add getKeys()
968+ // add getKeys() | (1.4.1.1) nshaheed
969969 func = make_new_mfun( "void", "getKeys", array_get_keys );
970970 func->add_arg( "string[]", "keys" );
971971 func->doc = "return all keys found in associative array in keys";
972972 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;
978973
979974 // note 1.4.1.0: in the future, should deprecate and encourage programmer
980975 // 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
10051000 func->doc = "(map only) Erase all elements with the specified key.";
10061001 if( !type_engine_import_mfun( env, func ) ) goto error;
10071002
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+
10081008 // add shuffle()
10091009 func = make_new_mfun( "void", "shuffle", array_shuffle );
10101010 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
700700
701701
702702
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+
704724
705725 //-----------------------------------------------------------------------------
706726 // 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
708728 //-----------------------------------------------------------------------------
709729 void Chuck_Array4::shuffle()
710730 {
711− std::random_shuffle( m_vector.begin(), m_vector.end(), my_ck_random );
731+ my_random_shuffle( m_vector.begin(), m_vector.end() );
712732 }
713733
714734
@@ -1131,7 +1151,7 @@ void Chuck_Array8::reverse( ) · 416 unmodified lines
11311151 //-----------------------------------------------------------------------------
11321152 void Chuck_Array8::shuffle()
11331153 {
1134− std::random_shuffle( m_vector.begin(), m_vector.end(), my_ck_random );
1154+ my_random_shuffle( m_vector.begin(), m_vector.end() );
11351155 }
11361156
11371157
@@ -1498,12 +1518,13 @@ void Chuck_Array16::reverse( ) · 360 unmodified lines
14981518
14991519
15001520
1521+//-----------------------------------------------------------------------------
15011522 // name: shuffle() | 1.4.2.1 nshaheed, azaday, kunwoo, ge (added)
15021523 // desc: shuffle the contents of the array
15031524 //-----------------------------------------------------------------------------
15041525 void Chuck_Array16::shuffle()
15051526 {
1506− std::random_shuffle( m_vector.begin(), m_vector.end(), my_ck_random );
1527+ my_random_shuffle( m_vector.begin(), m_vector.end() );
15071528 }
15081529
15091530
@@ -1960,12 +1981,13 @@ void Chuck_Array24::reverse( ) · 450 unmodified lines
19601981
19611982
19621983
1984+//-----------------------------------------------------------------------------
19631985 // name: shuffle() | 1.4.2.1 nshaheed, azaday, kunwoo, ge (added)
19641986 // desc: shuffle the contents of the array
19651987 //-----------------------------------------------------------------------------
19661988 void Chuck_Array24::shuffle()
19671989 {
1968− std::random_shuffle( m_vector.begin(), m_vector.end(), my_ck_random );
1990+ my_random_shuffle( m_vector.begin(), m_vector.end() );
19691991 }
19701992
19711993
@@ -2325,7 +2347,7 @@ void Chuck_Array32::reverse( ) · 353 unmodified lines
23252347 //-----------------------------------------------------------------------------
23262348 void Chuck_Array32::shuffle()
23272349 {
2328− std::random_shuffle( m_vector.begin(), m_vector.end(), my_ck_random );
2350+ my_random_shuffle( m_vector.begin(), m_vector.end() );
23292351 }
23302352
23312353
src/core/chuck_oo.h+12−6
@@ -234,8 +234,9 @@ public: · 233 unmodified lines
234234 virtual void zero() = 0; // zero (all)
235235 // get map keys | added (1.4.2.0) nshaheed
236236 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
238238 virtual void reverse() = 0;
239+ // reverse array order | added (1.4.2.1) kunwoo, nshaheed, azaday, ge
239240 virtual void shuffle() = 0;
240241
241242 Chuck_Type * m_array_type;
@@ -279,8 +280,9 @@ public: · 37 unmodified lines
279280 virtual t_CKINT data_type_kind( ) { return CHUCK_ARRAY4_DATAKIND; }
280281 // get map keys | added (1.4.2.0) nshaheed
281282 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
283284 virtual void reverse();
285+ // reverse array order | added (1.4.2.1) kunwoo, nshaheed, azaday, ge
284286 virtual void shuffle();
285287
286288 public:
@@ -332,8 +334,9 @@ public: · 45 unmodified lines
332334 virtual t_CKINT data_type_kind( ) { return CHUCK_ARRAY8_DATAKIND; }
333335 // get map keys | added (1.4.2.0) nshaheed
334336 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
336338 virtual void reverse();
339+ // reverse array order | added (1.4.2.1) kunwoo, nshaheed, azaday, ge
337340 virtual void shuffle();
338341
339342 public:
@@ -381,8 +384,9 @@ public: · 41 unmodified lines
381384 virtual t_CKINT data_type_kind( ) { return CHUCK_ARRAY16_DATAKIND; }
382385 // get map keys | added (1.4.2.0) nshaheed
383386 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
385388 virtual void reverse();
389+ // reverse array order | added (1.4.2.1) kunwoo, nshaheed, azaday, ge
386390 virtual void shuffle();
387391
388392 public:
@@ -430,8 +434,9 @@ public: · 41 unmodified lines
430434 virtual t_CKINT data_type_kind( ) { return CHUCK_ARRAY24_DATAKIND; }
431435 // get map keys | added (1.4.2.0) nshaheed
432436 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
434438 virtual void reverse();
439+ // reverse array order | added (1.4.2.1) kunwoo, nshaheed, azaday, ge
435440 virtual void shuffle();
436441
437442 public:
@@ -477,8 +482,9 @@ public: · 39 unmodified lines
477482 virtual t_CKINT data_type_kind( ) { return CHUCK_ARRAY32_DATAKIND; }
478483 // get map keys | added (1.4.1.2) nshaheed
479484 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
481486 virtual void reverse();
487+ // reverse array order | added (1.4.2.1) kunwoo, nshaheed, azaday, ge
482488 virtual void shuffle();
483489
484490 public:

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