line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
#include "EXTERN.h" |
2
|
|
|
|
|
|
|
#include "perl.h" |
3
|
|
|
|
|
|
|
#include "XSUB.h" |
4
|
|
|
|
|
|
|
|
5
|
|
|
|
|
|
|
MODULE = List::Utils::MoveElement PACKAGE = List::Utils::MoveElement |
6
|
|
|
|
|
|
|
|
7
|
|
|
|
|
|
|
void |
8
|
|
|
|
|
|
|
to_beginning(idx, ...) |
9
|
|
|
|
|
|
|
int idx; |
10
|
|
|
|
|
|
|
CODE: |
11
|
|
|
|
|
|
|
{ |
12
|
|
|
|
|
|
|
int tmp_idx; |
13
|
|
|
|
|
|
|
SV *tmp; |
14
|
|
|
|
|
|
|
|
15
|
|
|
|
|
|
|
/* If the index is > items - 2, it's an error */ |
16
|
6
|
50
|
|
|
|
|
if (idx > items - 2) { |
17
|
0
|
|
|
|
|
|
croak("Index out of range for array"); |
18
|
|
|
|
|
|
|
} |
19
|
|
|
|
|
|
|
|
20
|
|
|
|
|
|
|
/* Shift everything left 1 place, because first argument was idx */ |
21
|
48
|
100
|
|
|
|
|
for (tmp_idx = 0; tmp_idx < items; tmp_idx++) { |
22
|
42
|
|
|
|
|
|
ST(tmp_idx) = ST(tmp_idx + 1); |
23
|
|
|
|
|
|
|
} |
24
|
|
|
|
|
|
|
|
25
|
|
|
|
|
|
|
/* Check for no-ops */ |
26
|
6
|
100
|
|
|
|
|
if (idx == 0 || items == 2) { |
27
|
1
|
|
|
|
|
|
XSRETURN(items - 1); |
28
|
|
|
|
|
|
|
} |
29
|
|
|
|
|
|
|
|
30
|
|
|
|
|
|
|
/* Swap element tmp_idx with the one to its left then continue swapping until tmp_idx = 1 */ |
31
|
20
|
100
|
|
|
|
|
for (tmp_idx = idx; tmp_idx >= 1; tmp_idx--) { |
32
|
15
|
|
|
|
|
|
tmp = ST(tmp_idx - 1); |
33
|
15
|
|
|
|
|
|
ST(tmp_idx - 1) = ST(tmp_idx); |
34
|
15
|
|
|
|
|
|
ST(tmp_idx) = tmp; |
35
|
|
|
|
|
|
|
} |
36
|
5
|
|
|
|
|
|
XSRETURN(items - 1); |
37
|
|
|
|
|
|
|
} |
38
|
|
|
|
|
|
|
|
39
|
|
|
|
|
|
|
|
40
|
|
|
|
|
|
|
void |
41
|
|
|
|
|
|
|
to_end(idx, ...) |
42
|
|
|
|
|
|
|
int idx; |
43
|
|
|
|
|
|
|
CODE: |
44
|
|
|
|
|
|
|
{ |
45
|
|
|
|
|
|
|
int end_idx; |
46
|
|
|
|
|
|
|
int tmp_idx; |
47
|
|
|
|
|
|
|
SV *tmp; |
48
|
|
|
|
|
|
|
|
49
|
6
|
|
|
|
|
|
end_idx = items - 2; |
50
|
|
|
|
|
|
|
|
51
|
|
|
|
|
|
|
/* Index out of range? */ |
52
|
6
|
50
|
|
|
|
|
if (idx > end_idx) { |
53
|
0
|
|
|
|
|
|
croak("Index out of range for array"); |
54
|
|
|
|
|
|
|
} |
55
|
|
|
|
|
|
|
|
56
|
|
|
|
|
|
|
/* Shift everything left 1 place, dropping the first element */ |
57
|
48
|
100
|
|
|
|
|
for (tmp_idx = 0; tmp_idx < items; tmp_idx++) { |
58
|
42
|
|
|
|
|
|
ST(tmp_idx) = ST(tmp_idx + 1); |
59
|
|
|
|
|
|
|
} |
60
|
|
|
|
|
|
|
|
61
|
|
|
|
|
|
|
/* Check for no-ops */ |
62
|
6
|
100
|
|
|
|
|
if (idx == end_idx || items == 2) { |
63
|
1
|
|
|
|
|
|
XSRETURN(items - 1); |
64
|
|
|
|
|
|
|
} |
65
|
|
|
|
|
|
|
|
66
|
|
|
|
|
|
|
/* Move element to beginning, reposition remaining */ |
67
|
20
|
100
|
|
|
|
|
for (tmp_idx = idx; tmp_idx <= end_idx-1; tmp_idx++) { |
68
|
15
|
|
|
|
|
|
tmp = ST(tmp_idx + 1); |
69
|
15
|
|
|
|
|
|
ST(tmp_idx + 1) = ST(tmp_idx); |
70
|
15
|
|
|
|
|
|
ST(tmp_idx) = tmp; |
71
|
|
|
|
|
|
|
} |
72
|
5
|
|
|
|
|
|
XSRETURN(items - 1); |
73
|
|
|
|
|
|
|
} |
74
|
|
|
|
|
|
|
|
75
|
|
|
|
|
|
|
|
76
|
|
|
|
|
|
|
void |
77
|
|
|
|
|
|
|
left(idx, ...) |
78
|
|
|
|
|
|
|
int idx; |
79
|
|
|
|
|
|
|
CODE: |
80
|
|
|
|
|
|
|
{ |
81
|
|
|
|
|
|
|
int end_idx; |
82
|
|
|
|
|
|
|
int tmp_idx; |
83
|
|
|
|
|
|
|
SV *tmp; |
84
|
|
|
|
|
|
|
|
85
|
6
|
|
|
|
|
|
end_idx = items - 2; |
86
|
|
|
|
|
|
|
|
87
|
|
|
|
|
|
|
/* If the index is > items - 2, it's an error */ |
88
|
6
|
50
|
|
|
|
|
if (idx > end_idx) { |
89
|
0
|
|
|
|
|
|
croak("Index out of range for array"); |
90
|
|
|
|
|
|
|
} |
91
|
|
|
|
|
|
|
|
92
|
|
|
|
|
|
|
/* Shift everything left 1 place, dropping the first element */ |
93
|
48
|
100
|
|
|
|
|
for (tmp_idx = 0; tmp_idx < items; tmp_idx++) { |
94
|
42
|
|
|
|
|
|
ST(tmp_idx) = ST(tmp_idx + 1); |
95
|
|
|
|
|
|
|
} |
96
|
|
|
|
|
|
|
|
97
|
|
|
|
|
|
|
/* Check for no-ops */ |
98
|
6
|
100
|
|
|
|
|
if (idx == 0 || items == 2) { |
99
|
1
|
|
|
|
|
|
XSRETURN(items - 1); |
100
|
|
|
|
|
|
|
} |
101
|
|
|
|
|
|
|
|
102
|
|
|
|
|
|
|
/* Swap the element with the one to its left */ |
103
|
5
|
|
|
|
|
|
tmp = ST(idx - 1); |
104
|
5
|
|
|
|
|
|
ST(idx - 1) = ST(idx); |
105
|
5
|
|
|
|
|
|
ST(idx) = tmp; |
106
|
|
|
|
|
|
|
|
107
|
5
|
|
|
|
|
|
XSRETURN(items - 1); |
108
|
|
|
|
|
|
|
} |
109
|
|
|
|
|
|
|
|
110
|
|
|
|
|
|
|
|
111
|
|
|
|
|
|
|
void |
112
|
|
|
|
|
|
|
right(idx, ...) |
113
|
|
|
|
|
|
|
int idx; |
114
|
|
|
|
|
|
|
CODE: |
115
|
|
|
|
|
|
|
{ |
116
|
|
|
|
|
|
|
int end_idx; |
117
|
|
|
|
|
|
|
int tmp_idx; |
118
|
|
|
|
|
|
|
SV *tmp; |
119
|
|
|
|
|
|
|
|
120
|
6
|
|
|
|
|
|
end_idx = items - 2; |
121
|
|
|
|
|
|
|
|
122
|
|
|
|
|
|
|
/* If the index is > items - 2, it's an error */ |
123
|
6
|
50
|
|
|
|
|
if (idx > end_idx) { |
124
|
0
|
|
|
|
|
|
croak("Index out of range for array"); |
125
|
|
|
|
|
|
|
} |
126
|
|
|
|
|
|
|
|
127
|
|
|
|
|
|
|
/* Shift everything left 1 place, dropping the first element */ |
128
|
48
|
100
|
|
|
|
|
for (tmp_idx = 0; tmp_idx < items; tmp_idx++) { |
129
|
42
|
|
|
|
|
|
ST(tmp_idx) = ST(tmp_idx + 1); |
130
|
|
|
|
|
|
|
} |
131
|
|
|
|
|
|
|
|
132
|
|
|
|
|
|
|
/* Check for no-ops */ |
133
|
6
|
100
|
|
|
|
|
if (idx == end_idx || items == 2) { |
134
|
1
|
|
|
|
|
|
XSRETURN(items - 1); |
135
|
|
|
|
|
|
|
} |
136
|
|
|
|
|
|
|
|
137
|
|
|
|
|
|
|
/* Swap the element with the one to its right */ |
138
|
5
|
|
|
|
|
|
tmp = ST(idx + 1); |
139
|
5
|
|
|
|
|
|
ST(idx + 1) = ST(idx); |
140
|
5
|
|
|
|
|
|
ST(idx) = tmp; |
141
|
|
|
|
|
|
|
|
142
|
5
|
|
|
|
|
|
XSRETURN(items - 1); |
143
|
|
|
|
|
|
|
} |
144
|
|
|
|
|
|
|
|