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 |
2901
|
93 indent (os); |
|
94 os << "("; |
|
95 newline (os); |
2882
|
96 |
2901
|
97 increment_indent_level (); |
2882
|
98 |
|
99 int n = lst.length (); |
|
100 |
|
101 for (int i = 0; i < n; i++) |
|
102 { |
|
103 octave_value val = lst(i); |
|
104 |
2901
|
105 indent (os); |
2882
|
106 |
|
107 val.print (os); |
|
108 } |
|
109 |
2901
|
110 decrement_indent_level (); |
2882
|
111 |
2901
|
112 indent (os); |
|
113 os << ")"; |
|
114 newline (os); |
2882
|
115 |
|
116 run_unwind_frame ("octave_list_print"); |
|
117 } |
|
118 |
2901
|
119 bool |
|
120 octave_list::print_name_tag (ostream& os, const string& name) const |
|
121 { |
|
122 indent (os); |
|
123 os << name << " ="; |
|
124 newline (os); |
|
125 return false; |
|
126 } |
|
127 |
2882
|
128 DEFUN (make_list, args, , |
|
129 "make_list (ARGS)\n\ |
|
130 \n\ |
|
131 Create a new list from ARGS.") |
|
132 { |
|
133 return octave_value (args); |
|
134 } |
|
135 |
|
136 DEFUN (append, args, , |
|
137 "append (LIST, ARGS)\n\ |
|
138 \n\ |
|
139 Return a new list created by appending ARGS to LIST") |
|
140 { |
|
141 octave_value retval; |
|
142 |
|
143 int nargin = args.length (); |
|
144 |
|
145 if (nargin > 1) |
|
146 { |
|
147 octave_value_list tmp = args(0).list_value (); |
|
148 |
|
149 if (! error_state) |
|
150 { |
|
151 for (int i = 1; i < nargin; i++) |
|
152 tmp.append (args(i)); |
|
153 |
|
154 retval = tmp; |
|
155 } |
|
156 } |
|
157 else |
|
158 print_usage ("append"); |
|
159 |
|
160 return retval; |
|
161 } |
|
162 |
|
163 DEFUN (reverse, args, , |
|
164 "reverse (LIST)\n\ |
|
165 \n\ |
|
166 Return a new list created by reversing the elements of LIST") |
|
167 { |
|
168 octave_value retval; |
|
169 |
|
170 int nargin = args.length (); |
|
171 |
|
172 if (nargin == 1) |
|
173 { |
|
174 octave_value_list tmp = args(0).list_value (); |
|
175 |
|
176 if (! error_state) |
|
177 retval = tmp.reverse (); |
|
178 } |
|
179 else |
|
180 print_usage ("reverse"); |
|
181 |
|
182 return retval; |
|
183 } |
|
184 |
|
185 /* |
|
186 ;;; Local Variables: *** |
|
187 ;;; mode: C++ *** |
|
188 ;;; End: *** |
|
189 */ |