Mercurial > hg > octave-nkf
comparison src/ov-list.cc @ 2882:05926e1b367d
[project @ 1997-04-24 09:48:59 by jwe]
author | jwe |
---|---|
date | Thu, 24 Apr 1997 09:51:05 +0000 |
parents | |
children | e6d25bc478dd |
comparison
equal
deleted
inserted
replaced
2881:b99a6a2619aa | 2882:05926e1b367d |
---|---|
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 | |
83 octave_list::print (ostream& os, bool) | |
84 { | |
85 begin_unwind_frame ("octave_list_print"); | |
86 | |
87 unwind_protect_int (list_indent); | |
88 | |
89 os.form ("\n%*s{\n", list_indent, ""); | |
90 | |
91 increment_list_indent (); | |
92 | |
93 int n = lst.length (); | |
94 | |
95 for (int i = 0; i < n; i++) | |
96 { | |
97 bool pad_after = false; | |
98 | |
99 octave_value val = lst(i); | |
100 | |
101 os.form ("%*s", list_indent, ""); | |
102 | |
103 if (val.print_as_scalar ()) | |
104 os << " "; | |
105 else if (val.is_list ()) | |
106 pad_after = true; | |
107 else | |
108 { | |
109 pad_after = true; | |
110 | |
111 os << "\n\n"; | |
112 } | |
113 | |
114 val.print (os); | |
115 | |
116 if (pad_after) | |
117 os << "\n"; | |
118 } | |
119 | |
120 decrement_list_indent (); | |
121 | |
122 os.form ("%*s%s", list_indent, "", "}\n"); | |
123 | |
124 run_unwind_frame ("octave_list_print"); | |
125 } | |
126 | |
127 DEFUN (make_list, args, , | |
128 "make_list (ARGS)\n\ | |
129 \n\ | |
130 Create a new list from ARGS.") | |
131 { | |
132 return octave_value (args); | |
133 } | |
134 | |
135 DEFUN (append, args, , | |
136 "append (LIST, ARGS)\n\ | |
137 \n\ | |
138 Return a new list created by appending ARGS to LIST") | |
139 { | |
140 octave_value retval; | |
141 | |
142 int nargin = args.length (); | |
143 | |
144 if (nargin > 1) | |
145 { | |
146 octave_value_list tmp = args(0).list_value (); | |
147 | |
148 if (! error_state) | |
149 { | |
150 for (int i = 1; i < nargin; i++) | |
151 tmp.append (args(i)); | |
152 | |
153 retval = tmp; | |
154 } | |
155 } | |
156 else | |
157 print_usage ("append"); | |
158 | |
159 return retval; | |
160 } | |
161 | |
162 DEFUN (reverse, args, , | |
163 "reverse (LIST)\n\ | |
164 \n\ | |
165 Return a new list created by reversing the elements of LIST") | |
166 { | |
167 octave_value retval; | |
168 | |
169 int nargin = args.length (); | |
170 | |
171 if (nargin == 1) | |
172 { | |
173 octave_value_list tmp = args(0).list_value (); | |
174 | |
175 if (! error_state) | |
176 retval = tmp.reverse (); | |
177 } | |
178 else | |
179 print_usage ("reverse"); | |
180 | |
181 return retval; | |
182 } | |
183 | |
184 /* | |
185 ;;; Local Variables: *** | |
186 ;;; mode: C++ *** | |
187 ;;; End: *** | |
188 */ |