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> |
2916
|
32 #include <strstream.h> |
2882
|
33 |
|
34 #include "lo-utils.h" |
|
35 |
|
36 #include "defun.h" |
|
37 #include "error.h" |
|
38 #include "help.h" |
|
39 #include "ov-list.h" |
|
40 #include "unwind-prot.h" |
|
41 |
|
42 octave_allocator |
|
43 octave_list::allocator (sizeof (octave_list)); |
|
44 |
|
45 int |
|
46 octave_list::t_id (-1); |
|
47 |
|
48 const string |
|
49 octave_list::t_name ("list"); |
|
50 |
|
51 octave_value |
2974
|
52 octave_list::do_index_op (const octave_value_list& idx) |
2882
|
53 { |
|
54 octave_value retval; |
|
55 |
|
56 if (idx.length () == 1) |
|
57 { |
|
58 double d = idx(0).double_value (); |
|
59 |
|
60 if (! error_state) |
|
61 { |
|
62 if (D_NINT (d) == d) |
|
63 { |
|
64 int n = lst.length (); |
|
65 |
|
66 int i = static_cast<int> (d); |
|
67 |
|
68 if (i > 0 && i <= n) |
|
69 retval = lst(i-1); |
|
70 else |
|
71 error ("list index = %d out of range", i); |
|
72 } |
|
73 else |
|
74 error ("list index must be an integer"); |
|
75 } |
|
76 } |
|
77 else |
|
78 error ("lists may only be indexed by a single scalar"); |
|
79 |
|
80 return retval; |
|
81 } |
|
82 |
|
83 void |
2901
|
84 octave_list::print (ostream& os, bool) const |
|
85 { |
|
86 print_raw (os); |
|
87 } |
|
88 |
|
89 void |
|
90 octave_list::print_raw (ostream& os, bool) const |
2882
|
91 { |
|
92 begin_unwind_frame ("octave_list_print"); |
|
93 |
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 { |
2916
|
104 ostrstream buf; |
|
105 buf << "[" << i+1 << "]" << ends; |
|
106 const char *nm = buf.str (); |
|
107 |
2882
|
108 octave_value val = lst(i); |
|
109 |
2916
|
110 val.print_with_name (os, nm); |
|
111 |
|
112 delete [] nm; |
2882
|
113 } |
|
114 |
2901
|
115 decrement_indent_level (); |
2882
|
116 |
2901
|
117 indent (os); |
|
118 os << ")"; |
|
119 newline (os); |
2882
|
120 |
|
121 run_unwind_frame ("octave_list_print"); |
|
122 } |
|
123 |
2901
|
124 bool |
|
125 octave_list::print_name_tag (ostream& os, const string& name) const |
|
126 { |
|
127 indent (os); |
|
128 os << name << " ="; |
2916
|
129 newline (os); |
2901
|
130 return false; |
|
131 } |
|
132 |
2882
|
133 DEFUN (make_list, args, , |
|
134 "make_list (ARGS)\n\ |
|
135 \n\ |
|
136 Create a new list from ARGS.") |
|
137 { |
|
138 return octave_value (args); |
|
139 } |
|
140 |
|
141 DEFUN (append, args, , |
|
142 "append (LIST, ARGS)\n\ |
|
143 \n\ |
|
144 Return a new list created by appending ARGS to LIST") |
|
145 { |
|
146 octave_value retval; |
|
147 |
|
148 int nargin = args.length (); |
|
149 |
|
150 if (nargin > 1) |
|
151 { |
|
152 octave_value_list tmp = args(0).list_value (); |
|
153 |
|
154 if (! error_state) |
|
155 { |
|
156 for (int i = 1; i < nargin; i++) |
|
157 tmp.append (args(i)); |
|
158 |
|
159 retval = tmp; |
|
160 } |
|
161 } |
|
162 else |
|
163 print_usage ("append"); |
|
164 |
|
165 return retval; |
|
166 } |
|
167 |
|
168 DEFUN (reverse, args, , |
|
169 "reverse (LIST)\n\ |
|
170 \n\ |
|
171 Return a new list created by reversing the elements of LIST") |
|
172 { |
|
173 octave_value retval; |
|
174 |
|
175 int nargin = args.length (); |
|
176 |
|
177 if (nargin == 1) |
|
178 { |
|
179 octave_value_list tmp = args(0).list_value (); |
|
180 |
|
181 if (! error_state) |
|
182 retval = tmp.reverse (); |
|
183 } |
|
184 else |
|
185 print_usage ("reverse"); |
|
186 |
|
187 return retval; |
|
188 } |
|
189 |
|
190 /* |
|
191 ;;; Local Variables: *** |
|
192 ;;; mode: C++ *** |
|
193 ;;; End: *** |
|
194 */ |