History

reverse feature and unit test + example

Andrew Aday committed 3 years ago

Commit e7ef3c81 parent 7686e906 files · +122 −0Browse files

Showing the changes to src (5 of 6 files). Show all

Unified · Split

5 files
  1. src/core/chuck_lang.cpp +12 −0
  2. src/core/chuck_lang.h +1 −0
  3. src/core/chuck_oo.cpp +60 −0
  4. src/core/chuck_oo.h +12 −0
  5. src/test/01-Basic/126.ck +11 −0
src/core/chuck_lang.cpp+12−0
@@ -970,6 +970,11 @@ t_CKBOOL init_class_array( Chuck_Env * env, Chuck_Type * type ) · 969 unmodified lines
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;
973978
974979 // note 1.4.1.0: in the future, should deprecate and encourage programmer
975980 // to explicitly use .size() OR .capacity(), depending on the context
@@ -2782,6 +2787,13 @@ CK_DLL_MFUN( array_get_keys ) · 1806 unmodified lines
27822787 }
27832788 }
27842789
2790+// 1.4.2.1 azaday (added) array.reverse()
2791+CK_DLL_MFUN( array_reverse )
2792+{
2793+ Chuck_Array * array = (Chuck_Array *)SELF;
2794+ array->reverse();
2795+}
2796+
27852797
27862798
27872799 //-----------------------------------------------------------------------------
src/core/chuck_lang.h+1−0
@@ -190,6 +190,7 @@ CK_DLL_MFUN( array_clear ); · 189 unmodified lines
190190 CK_DLL_MFUN( array_reset );
191191 CK_DLL_MFUN( array_zero );
192192 CK_DLL_MFUN( array_get_keys );
193+CK_DLL_MFUN( array_reverse );
193194
194195
195196 //-----------------------------------------------------------------------------
src/core/chuck_oo.cpp+60−0
@@ -38,6 +38,7 @@ · 37 unmodified lines
3838 #include "chuck_errmsg.h"
3939 #include "chuck_dl.h"
4040
41+#include <algorithm>
4142 #include <iostream>
4243 #include <sstream>
4344 #include <iomanip>
@@ -699,6 +700,18 @@ void Chuck_Array4::get_keys( std::vector<std::string> & keys ) · 655 unmodified lines
699700
700701
701702
703+//-----------------------------------------------------------------------------
704+// name: reverse()
705+// desc: reverses array in-place
706+//-----------------------------------------------------------------------------
707+void Chuck_Array4::reverse( )
708+{
709+ std::reverse(m_vector.begin(), m_vector.end());
710+}
711+
712+
713+
714+
702715 //-----------------------------------------------------------------------------
703716 // name: back()
704717 // desc: ...
@@ -1087,6 +1100,18 @@ void Chuck_Array8::get_keys( std::vector<std::string> & keys ) · 382 unmodified lines
10871100
10881101
10891102
1103+//-----------------------------------------------------------------------------
1104+// name: reverse()
1105+// desc: reverses array in-place
1106+//-----------------------------------------------------------------------------
1107+void Chuck_Array8::reverse( )
1108+{
1109+ std::reverse(m_vector.begin(), m_vector.end());
1110+}
1111+
1112+
1113+
1114+
10901115 //-----------------------------------------------------------------------------
10911116 // name: back()
10921117 // desc: ...
@@ -1436,6 +1461,18 @@ void Chuck_Array16::get_keys( std::vector<std::string> & keys ) · 343 unmodified lines
14361461
14371462
14381463
1464+//-----------------------------------------------------------------------------
1465+// name: reverse()
1466+// desc: reverses array in-place
1467+//-----------------------------------------------------------------------------
1468+void Chuck_Array16::reverse( )
1469+{
1470+ std::reverse(m_vector.begin(), m_vector.end());
1471+}
1472+
1473+
1474+
1475+
14391476 //-----------------------------------------------------------------------------
14401477 // name: back()
14411478 // desc: ...
@@ -1875,6 +1912,18 @@ void Chuck_Array24::get_keys( std::vector<std::string> & keys ) · 433 unmodified lines
18751912
18761913
18771914
1915+//-----------------------------------------------------------------------------
1916+// name: reverse()
1917+// desc: reverses array in-place
1918+//-----------------------------------------------------------------------------
1919+void Chuck_Array24::reverse( )
1920+{
1921+ std::reverse(m_vector.begin(), m_vector.end());
1922+}
1923+
1924+
1925+
1926+
18781927 //-----------------------------------------------------------------------------
18791928 // name: Chuck_Array32()
18801929 // desc: constructor
@@ -2211,6 +2260,17 @@ void Chuck_Array32::get_keys( std::vector<std::string> & keys ) · 330 unmodified lines
22112260
22122261
22132262
2263+//-----------------------------------------------------------------------------
2264+// name: reverse()
2265+// desc: reverses array in-place
2266+//-----------------------------------------------------------------------------
2267+void Chuck_Array32::reverse( )
2268+{
2269+ std::reverse(m_vector.begin(), m_vector.end());
2270+}
2271+
2272+
2273+
22142274
22152275 // static
22162276 t_CKUINT Chuck_Event::our_can_wait = 0;
src/core/chuck_oo.h+12−0
@@ -234,6 +234,8 @@ 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
238+ virtual void reverse() = 0;
237239
238240 Chuck_Type * m_array_type;
239241 };
@@ -276,6 +278,8 @@ public: · 36 unmodified lines
276278 virtual t_CKINT data_type_kind( ) { return CHUCK_ARRAY4_DATAKIND; }
277279 // get map keys | added (1.4.2.0) nshaheed
278280 virtual void get_keys( std::vector<std::string> & keys );
281+ // reverse array order | added (1.4.2.0) azaday
282+ virtual void reverse();
279283
280284 public:
281285 std::vector<t_CKUINT> m_vector;
@@ -326,6 +330,8 @@ public: · 44 unmodified lines
326330 virtual t_CKINT data_type_kind( ) { return CHUCK_ARRAY8_DATAKIND; }
327331 // get map keys | added (1.4.2.0) nshaheed
328332 virtual void get_keys( std::vector<std::string> & keys );
333+ // reverse array order | added (1.4.2.0) azaday
334+ virtual void reverse();
329335
330336 public:
331337 std::vector<t_CKFLOAT> m_vector;
@@ -372,6 +378,8 @@ public: · 40 unmodified lines
372378 virtual t_CKINT data_type_kind( ) { return CHUCK_ARRAY16_DATAKIND; }
373379 // get map keys | added (1.4.2.0) nshaheed
374380 virtual void get_keys( std::vector<std::string> & keys );
381+ // reverse array order | added (1.4.2.0) azaday
382+ virtual void reverse();
375383
376384 public:
377385 std::vector<t_CKCOMPLEX> m_vector;
@@ -418,6 +426,8 @@ public: · 40 unmodified lines
418426 virtual t_CKINT data_type_kind( ) { return CHUCK_ARRAY24_DATAKIND; }
419427 // get map keys | added (1.4.2.0) nshaheed
420428 virtual void get_keys( std::vector<std::string> & keys );
429+ // reverse array order | added (1.4.2.0) azaday
430+ virtual void reverse();
421431
422432 public:
423433 std::vector<t_CKVEC3> m_vector;
@@ -462,6 +472,8 @@ public: · 38 unmodified lines
462472 virtual t_CKINT data_type_kind( ) { return CHUCK_ARRAY32_DATAKIND; }
463473 // get map keys | added (1.4.1.2) nshaheed
464474 virtual void get_keys( std::vector<std::string> & keys );
475+ // reverse array order | added (1.4.2.0) azaday
476+ virtual void reverse();
465477
466478 public:
467479 std::vector<t_CKVEC4> m_vector;
src/test/01-Basic/126.ck+11−0
@@ -0,0 +1,11 @@
1+[1,2,3,4,5,6] @=> int a[];
2+[1,2,3,4,5,6] @=> int b[];
3+
4+b.reverse();
5+
6+// test b is the reverse of a
7+for (int i; i < a.size(); i++) {
8+ if (a[i] != b[-i-1]) me.exit();
9+}
10+
11+<<< "success" >>>;

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