2882
|
1 /* |
|
2 |
|
3 Copyright (C) 1996, 1997 John W. Eaton |
|
4 |
|
5 This file is part of Octave. |
|
6 |
|
7 Octave is free software; you can redistribute it and/or modify it |
|
8 under the terms of the GNU General Public License as published by the |
|
9 Free Software Foundation; either version 2, or (at your option) any |
|
10 later version. |
|
11 |
|
12 Octave is distributed in the hope that it will be useful, but WITHOUT |
|
13 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
|
14 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
|
15 for more details. |
|
16 |
|
17 You should have received a copy of the GNU General Public License |
|
18 along with Octave; see the file COPYING. If not, write to the Free |
|
19 Software Foundation, 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. |
|
20 |
|
21 */ |
|
22 |
|
23 #if defined (__GNUG__) |
|
24 #pragma implementation |
|
25 #endif |
|
26 |
|
27 #ifdef HAVE_CONFIG_H |
|
28 #include <config.h> |
|
29 #endif |
|
30 |
|
31 #include <iostream.h> |
|
32 |
|
33 #include "lo-utils.h" |
|
34 |
|
35 #include "defun.h" |
|
36 #include "error.h" |
|
37 #include "help.h" |
|
38 #include "ov-list.h" |
|
39 #include "unwind-prot.h" |
|
40 |
|
41 octave_allocator |
|
42 octave_list::allocator (sizeof (octave_list)); |
|
43 |
|
44 int |
|
45 octave_list::t_id (-1); |
|
46 |
|
47 const string |
|
48 octave_list::t_name ("list"); |
|
49 |
|
50 octave_value |
|
51 octave_list::index (const octave_value_list& idx) const |
|
52 { |
|
53 octave_value retval; |
|
54 |
|
55 if (idx.length () == 1) |
|
56 { |
|
57 double d = idx(0).double_value (); |
|
58 |
|
59 if (! error_state) |
|
60 { |
|
61 if (D_NINT (d) == d) |
|
62 { |
|
63 int n = lst.length (); |
|
64 |
|
65 int i = static_cast<int> (d); |
|
66 |
|
67 if (i > 0 && i <= n) |
|
68 retval = lst(i-1); |
|
69 else |
|
70 error ("list index = %d out of range", i); |
|
71 } |
|
72 else |
|
73 error ("list index must be an integer"); |
|
74 } |
|
75 } |
|
76 else |
|
77 error ("lists may only be indexed by a single scalar"); |
|
78 |
|
79 return retval; |
|
80 } |
|
81 |
|
82 void |
2901
|
83 octave_list::print (ostream& os, bool) const |
|
84 { |
|
85 print_raw (os); |
|
86 } |
|
87 |
|
88 void |
|
89 octave_list::print_raw (ostream& os, bool) const |
2882
|
90 { |
|
91 begin_unwind_frame ("octave_list_print"); |
|
92 |
2905
|
93 newline (os); |
2901
|
94 indent (os); |
|
95 os << "("; |
|
96 newline (os); |
2882
|
97 |
2901
|
98 increment_indent_level (); |
2882
|
99 |
|
100 int n = lst.length (); |
|
101 |
|
102 for (int i = 0; i < n; i++) |
|
103 { |
|
104 octave_value val = lst(i); |
|
105 |
|
106 val.print (os); |
|
107 } |
|
108 |
2901
|
109 decrement_indent_level (); |
2882
|
110 |
2901
|
111 indent (os); |
|
112 os << ")"; |
|
113 newline (os); |
2882
|
114 |
|
115 run_unwind_frame ("octave_list_print"); |
|
116 } |
|
117 |
2901
|
118 bool |
|
119 octave_list::print_name_tag (ostream& os, const string& name) const |
|
120 { |
|
121 indent (os); |
|
122 os << name << " ="; |
|
123 return false; |
|
124 } |
|
125 |
2882
|
126 DEFUN (make_list, args, , |
|
127 "make_list (ARGS)\n\ |
|
128 \n\ |
|
129 Create a new list from ARGS.") |
|
130 { |
|
131 return octave_value (args); |
|
132 } |
|
133 |
|
134 DEFUN (append, args, , |
|
135 "append (LIST, ARGS)\n\ |
|
136 \n\ |
|
137 Return a new list created by appending ARGS to LIST") |
|
138 { |
|
139 octave_value retval; |
|
140 |
|
141 int nargin = args.length (); |
|
142 |
|
143 if (nargin > 1) |
|
144 { |
|
145 octave_value_list tmp = args(0).list_value (); |
|
146 |
|
147 if (! error_state) |
|
148 { |
|
149 for (int i = 1; i < nargin; i++) |
|
150 tmp.append (args(i)); |
|
151 |
|
152 retval = tmp; |
|
153 } |
|
154 } |
|
155 else |
|
156 print_usage ("append"); |
|
157 |
|
158 return retval; |
|
159 } |
|
160 |
|
161 DEFUN (reverse, args, , |
|
162 "reverse (LIST)\n\ |
|
163 \n\ |
|
164 Return a new list created by reversing the elements of LIST") |
|
165 { |
|
166 octave_value retval; |
|
167 |
|
168 int nargin = args.length (); |
|
169 |
|
170 if (nargin == 1) |
|
171 { |
|
172 octave_value_list tmp = args(0).list_value (); |
|
173 |
|
174 if (! error_state) |
|
175 retval = tmp.reverse (); |
|
176 } |
|
177 else |
|
178 print_usage ("reverse"); |
|
179 |
|
180 return retval; |
|
181 } |
|
182 |
|
183 /* |
|
184 ;;; Local Variables: *** |
|
185 ;;; mode: C++ *** |
|
186 ;;; End: *** |
|
187 */ |