Mercurial > hg > octave-nkf
annotate src/oct-obj.h @ 8523:ad3afaaa19c1
implement non-copying contiguous range indexing
author | Jaroslav Hajek <highegg@gmail.com> |
---|---|
date | Thu, 15 Jan 2009 07:22:24 +0100 |
parents | fd11a08a9b31 |
children | 3d8a914c580e |
rev | line source |
---|---|
498 | 1 /* |
2 | |
7017 | 3 Copyright (C) 1994, 1995, 1996, 1997, 1998, 2000, 2002, 2003, 2004, |
4 2005, 2006, 2007 John W. Eaton | |
498 | 5 |
6 This file is part of Octave. | |
7 | |
8 Octave is free software; you can redistribute it and/or modify it | |
9 under the terms of the GNU General Public License as published by the | |
7016 | 10 Free Software Foundation; either version 3 of the License, or (at your |
11 option) any later version. | |
498 | 12 |
13 Octave is distributed in the hope that it will be useful, but WITHOUT | |
14 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or | |
15 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License | |
16 for more details. | |
17 | |
18 You should have received a copy of the GNU General Public License | |
7016 | 19 along with Octave; see the file COPYING. If not, see |
20 <http://www.gnu.org/licenses/>. | |
498 | 21 |
22 */ | |
23 | |
504 | 24 #if !defined (octave_oct_obj_h) |
25 #define octave_oct_obj_h 1 | |
26 | |
1968 | 27 #include <string> |
4591 | 28 #include <vector> |
1968 | 29 |
2970 | 30 #include "oct-alloc.h" |
2943 | 31 #include "str-vec.h" |
498 | 32 |
2369 | 33 #include "ov.h" |
498 | 34 |
737 | 35 class |
6109 | 36 OCTINTERP_API |
2872 | 37 octave_value_list |
508 | 38 { |
39 public: | |
40 | |
2086 | 41 octave_value_list (void) |
2872 | 42 : data () { } |
1968 | 43 |
5275 | 44 octave_value_list (octave_idx_type n, const octave_value& val) |
2872 | 45 : data (n, val) { } |
511 | 46 |
2086 | 47 octave_value_list (const octave_value& tc) |
2872 | 48 : data (1, tc) { } |
1968 | 49 |
2086 | 50 octave_value_list (const octave_value_list& obj) |
4150 | 51 : data (obj.data), names (obj.names) { } |
508 | 52 |
4189 | 53 ~octave_value_list (void) { } |
54 | |
2970 | 55 void *operator new (size_t size) |
56 { return allocator.alloc (size); } | |
57 | |
4280 | 58 void operator delete (void *p, size_t size) |
59 { allocator.free (p, size); } | |
60 | |
5775 | 61 // FIXME -- without this, I have errors with the stack of |
4214 | 62 // octave_value_list objects in ov-usr-fcn.h. Why? |
63 void *operator new (size_t size, void *p) | |
64 { return ::operator new (size, p); } | |
65 | |
4280 | 66 void operator delete (void *p, void *) |
4352 | 67 { |
68 #if defined (HAVE_PLACEMENT_DELETE) | |
69 ::operator delete (p, static_cast<void *> (0)); | |
70 #else | |
71 ::operator delete (p); | |
72 #endif | |
73 } | |
2970 | 74 |
2086 | 75 octave_value_list& operator = (const octave_value_list& obj) |
508 | 76 { |
1968 | 77 if (this != &obj) |
4150 | 78 { |
79 data = obj.data; | |
80 names = obj.names; | |
81 } | |
1968 | 82 |
508 | 83 return *this; |
84 } | |
85 | |
2872 | 86 // Assignment will resize on range errors. |
508 | 87 |
5275 | 88 octave_value& operator () (octave_idx_type n) { return elem (n); } |
1968 | 89 |
5275 | 90 octave_value operator () (octave_idx_type n) const { return elem (n); } |
526 | 91 |
5275 | 92 octave_idx_type length (void) const { return data.size (); } |
2872 | 93 |
2951 | 94 bool empty (void) const { return length () == 0; } |
95 | |
5275 | 96 void resize (octave_idx_type n) { data.resize (n); } |
2872 | 97 |
5275 | 98 void resize (octave_idx_type n, const octave_value& val); |
2872 | 99 |
100 octave_value_list& prepend (const octave_value& val); | |
101 | |
102 octave_value_list& append (const octave_value& val); | |
103 | |
104 octave_value_list& append (const octave_value_list& lst); | |
105 | |
106 octave_value_list& reverse (void); | |
107 | |
6335 | 108 octave_value_list |
109 splice (octave_idx_type offset, octave_idx_type len, | |
110 const octave_value_list& lst = octave_value_list ()) const; | |
3195 | 111 |
2872 | 112 bool all_strings_p (void) const; |
1968 | 113 |
8455
fd11a08a9b31
disallow invalid {}-indexed assigments
Jaroslav Hajek <highegg@gmail.com>
parents:
8437
diff
changeset
|
114 bool all_scalars (void) const; |
fd11a08a9b31
disallow invalid {}-indexed assigments
Jaroslav Hajek <highegg@gmail.com>
parents:
8437
diff
changeset
|
115 |
5846 | 116 bool has_magic_colon (void) const; |
117 | |
8034
f61bd8e0d682
fix default_save_options parsing and allow mixing options with other arguments.
Jaroslav Hajek <highegg@gmail.com>
parents:
7017
diff
changeset
|
118 string_vector make_argv (const std::string& = std::string()) const; |
526 | 119 |
2943 | 120 void stash_name_tags (const string_vector& nm) { names = nm; } |
121 | |
122 string_vector name_tags (void) const { return names; } | |
123 | |
8523
ad3afaaa19c1
implement non-copying contiguous range indexing
Jaroslav Hajek <highegg@gmail.com>
parents:
8455
diff
changeset
|
124 void make_storable_values (void); |
8150
283989f2da9b
make null assignment matlab compatible
Jaroslav Hajek <highegg@gmail.com>
parents:
8034
diff
changeset
|
125 |
526 | 126 private: |
508 | 127 |
2970 | 128 static octave_allocator allocator; |
129 | |
4591 | 130 std::vector<octave_value> data; |
2872 | 131 |
2943 | 132 // This list of strings can be used to tag each element of data with |
133 // a name. By default, it is empty. | |
134 string_vector names; | |
135 | |
2872 | 136 // This constructor is private with no definition to keep statements |
137 // like | |
138 // | |
139 // octave_value_list foo = 5; | |
140 // octave_value_list foo = 5.0; | |
141 // | |
142 // from doing different things. Instead, you have to use the | |
143 // constructor | |
144 // | |
145 // octave_value_list (n, val); | |
146 // | |
147 // and supply a default value to create a vector-valued | |
148 // octave_value_list. | |
565 | 149 |
5275 | 150 octave_value_list (octave_idx_type n); |
565 | 151 |
4591 | 152 octave_value_list (const Array<octave_value>& d); |
526 | 153 |
5275 | 154 octave_value& elem (octave_idx_type n) |
1968 | 155 { |
4591 | 156 static Matrix empty_matrix; |
157 | |
158 if (n >= length ()) | |
159 resize (n+1, empty_matrix); | |
160 | |
161 return data[n]; | |
1968 | 162 } |
163 | |
5275 | 164 octave_value elem (octave_idx_type n) const |
1968 | 165 { |
4591 | 166 #if defined (BOUNDS_CHECKING) |
167 return data.at (n); | |
168 #else | |
169 return data[n]; | |
170 #endif | |
1968 | 171 } |
508 | 172 }; |
498 | 173 |
504 | 174 #endif |
175 | |
498 | 176 /* |
177 ;;; Local Variables: *** | |
178 ;;; mode: C++ *** | |
179 ;;; End: *** | |
180 */ |