00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024 #if !defined (octave_oct_map_h)
00025 #define octave_oct_map_h 1
00026
00027 #include <algorithm>
00028 #include <map>
00029
00030 #include "Cell.h"
00031 #include "oct-obj.h"
00032
00033 class string_vector;
00034
00035 class
00036 OCTINTERP_API
00037 Octave_map
00038 {
00039 public:
00040
00041 typedef std::map<std::string, Cell>::iterator iterator;
00042 typedef std::map<std::string, Cell>::const_iterator const_iterator;
00043
00044 typedef std::list<std::string>::iterator key_list_iterator;
00045 typedef std::list<std::string>::const_iterator const_key_list_iterator;
00046
00047
00048
00049 Octave_map (const dim_vector& dv = dim_vector (0, 0),
00050 const Cell& key_vals = Cell ());
00051
00052 Octave_map (const std::string& k, const octave_value& value)
00053 : map (), key_list (), dimensions (1, 1)
00054 {
00055 map[k] = value;
00056 key_list.push_back (k);
00057 }
00058
00059 Octave_map (const string_vector& sv)
00060 : map (), key_list (), dimensions (0, 0)
00061 {
00062 for (octave_idx_type i = 0; i < sv.length (); i++)
00063 {
00064 std::string k = sv[i];
00065 map[k] = Cell ();
00066 key_list.push_back (k);
00067 }
00068 }
00069
00070 Octave_map (const std::string& k, const Cell& vals)
00071 : map (), key_list (), dimensions (vals.dims ())
00072 {
00073 map[k] = vals;
00074 key_list.push_back (k);
00075 }
00076
00077 Octave_map (const std::string& k, const octave_value_list& val_list)
00078 : map (), key_list (), dimensions (1, val_list.length ())
00079 {
00080 map[k] = val_list;
00081 key_list.push_back (k);
00082 }
00083
00084 Octave_map (const Octave_map& m)
00085 : map (m.map), key_list (m.key_list), dimensions (m.dimensions) { }
00086
00087 Octave_map& operator = (const Octave_map& m)
00088 {
00089 if (this != &m)
00090 {
00091 map = m.map;
00092 key_list = m.key_list;
00093 dimensions = m.dimensions;
00094 }
00095
00096 return *this;
00097 }
00098
00099 ~Octave_map (void) { }
00100
00101 Octave_map squeeze (void) const;
00102
00103 Octave_map permute (const Array<int>& vec, bool inv = false) const;
00104
00105
00106 octave_idx_type nfields (void) const { return map.size (); }
00107
00108 void del (const std::string& k)
00109 {
00110 iterator p = map.find (k);
00111
00112 if (p != map.end ())
00113 {
00114 map.erase (p);
00115
00116 key_list_iterator q
00117 = std::find (key_list.begin (), key_list.end (), k);
00118
00119 assert (q != key_list.end ());
00120
00121 key_list.erase (q);
00122 }
00123 }
00124
00125 iterator begin (void) { return iterator (map.begin ()); }
00126 const_iterator begin (void) const { return const_iterator (map.begin ()); }
00127
00128 iterator end (void) { return iterator (map.end ()); }
00129 const_iterator end (void) const { return const_iterator (map.end ()); }
00130
00131 std::string key (const_iterator p) const { return p->first; }
00132
00133 Cell& contents (const std::string& k);
00134 Cell contents (const std::string& k) const;
00135
00136 Cell& contents (iterator p)
00137 { return p->second; }
00138
00139 Cell contents (const_iterator p) const
00140 { return p->second; }
00141
00142 int intfield (const std::string& k, int def_val = 0) const;
00143
00144 std::string stringfield (const std::string& k,
00145 const std::string& def_val = std::string ()) const;
00146
00147 iterator seek (const std::string& k) { return map.find (k); }
00148 const_iterator seek (const std::string& k) const { return map.find (k); }
00149
00150 bool contains (const std::string& k) const
00151 { return (seek (k) != map.end ()); }
00152
00153 void clear (void)
00154 {
00155 map.clear ();
00156 key_list.clear ();
00157 }
00158
00159 string_vector keys (void) const;
00160
00161 octave_idx_type rows (void) const { return dimensions(0); }
00162
00163 octave_idx_type columns (void) const { return dimensions(1); }
00164
00165 dim_vector dims (void) const { return dimensions; }
00166
00167 int ndims (void) const { return dimensions.length (); }
00168
00169 Octave_map transpose (void) const;
00170
00171 Octave_map reshape (const dim_vector& new_dims) const;
00172
00173 void resize (const dim_vector& dv, bool fill = false);
00174
00175 octave_idx_type numel (void) const { return dimensions.numel (); }
00176
00177 Octave_map concat (const Octave_map& rb, const Array<octave_idx_type>& ra_idx);
00178
00179 Octave_map& maybe_delete_elements (const octave_value_list& idx);
00180
00181 Octave_map& assign (const octave_value_list& idx, const Octave_map& rhs);
00182
00183 Octave_map& assign (const octave_value_list& idx, const std::string& k,
00184 const Cell& rhs);
00185
00186 Octave_map& assign (const std::string& k, const octave_value& rhs);
00187
00188 Octave_map& assign (const std::string& k, const Cell& rhs);
00189
00190 Octave_map index (const octave_value_list& idx,
00191 bool resize_ok = false) const;
00192
00193 private:
00194
00195
00196 std::map<std::string, Cell> map;
00197
00198
00199
00200 std::list<std::string> key_list;
00201
00202
00203 mutable dim_vector dimensions;
00204
00205 void maybe_add_to_key_list (const std::string& k)
00206 {
00207 if (! contains (k))
00208 key_list.push_back (k);
00209 }
00210 };
00211
00212 #endif
00213
00214
00215
00216
00217
00218