Commit 491075d2

491075d233d7Merged

Yukihiro "Matz" Matsumoto <[email protected]> · 7 months ago · 1 file · ~1 modified

Description

feat: add Array#rotate(n)

Returns a new array with elements rotated by n positions. Uses modular arithmetic to handle wrap-around.

Co-Authored-By: Claude Opus 4.6 (1M context) [email protected]

Details

Commit
491075d233d7
Parent
ad8d0452437e

spinel_codegen.rbmodifiedView file

@@ -1739,7 +1739,7 @@
17391739 end
17401740 return "int"
17411741 end
1742- if mname == "take" || mname == "drop"
1742+ if mname == "take" || mname == "drop" || mname == "rotate"
17431743 if recv >= 0
17441744 return infer_type(recv)
17451745 end
@@ -11974,6 +11974,21 @@
1197411974 emit(" sp_" + pfx + "_push(" + tmp + ", sp_" + pfx + "_get(" + rc + ", " + itmp + "));")
1197511975 return tmp
1197611976 end
11977+ if mname == "rotate"
11978+ pfx = array_c_prefix(recv_type)
11979+ n = compile_arg0(nid)
11980+ if n == "0"
11981+ n = "1"
11982+ end
11983+ tmp = new_temp
11984+ itmp = new_temp
11985+ len_tmp = new_temp
11986+ emit(" mrb_int " + len_tmp + " = sp_" + pfx + "_length(" + rc + ");")
11987+ emit(" " + c_type(recv_type) + tmp + " = sp_" + pfx + "_new();")
11988+ emit(" for (mrb_int " + itmp + " = 0; " + itmp + " < " + len_tmp + "; " + itmp + "++)")
11989+ emit(" sp_" + pfx + "_push(" + tmp + ", sp_" + pfx + "_get(" + rc + ", ((" + itmp + " + " + n + ") % " + len_tmp + " + " + len_tmp + ") % " + len_tmp + "));")
11990+ return tmp
11991+ end
1197711992 if mname == "sample"
1197811993 pfx = array_c_prefix(recv_type)
1197911994 return "sp_" + pfx + "_get(" + rc + ", rand() % sp_" + pfx + "_length(" + rc + "))"