1 /*$Id: u_nodemap.cc,v 26.83 2008/06/05 04:46:59 al Exp $ -*- C++ -*-
2 * Copyright (C) 2002 Albert Davis
3 * Author: Albert Davis <aldavis@gnu.org>
4 *
5 * This file is part of "Gnucap", the Gnu Circuit Analysis Package
6 *
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 3, or (at your option)
10 * any later version.
11 *
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
20 * 02110-1301, USA.
21 *------------------------------------------------------------------
22 * node name to number mapping -- for named nodes
23 */
24 //testing=script,complete 2006.07.14
25 #include "e_node.h"
26 #include "u_nodemap.h"
27 /*--------------------------------------------------------------------------*/
28 NODE ground_node("0",0);
29 /*--------------------------------------------------------------------------*/
NODE_MAP()30 NODE_MAP::NODE_MAP()
31 : _node_map()
32 {
33 _node_map["0"] = &ground_node;
34 }
35 /*--------------------------------------------------------------------------*/
36 /* copy constructor: deep copy
37 * The std::map copy constructor does a shallow copy,
38 * then replace second with a deep copy.
39 */
NODE_MAP(const NODE_MAP & p)40 NODE_MAP::NODE_MAP(const NODE_MAP& p)
41 : _node_map(p._node_map)
42 {
43 unreachable();
44 for (iterator i = _node_map.begin(); i != _node_map.end(); ++i) {
45 untested();
46 if (i->first != "0") {
47 untested();
48 assert(i->second);
49 i->second = new NODE(i->second);
50 }else{
51 untested();
52 }
53 }
54 }
55 /*--------------------------------------------------------------------------*/
~NODE_MAP()56 NODE_MAP::~NODE_MAP()
57 {
58 for (iterator i = _node_map.begin(); i != _node_map.end(); ++i) {
59 if (i->first != "0") {
60 assert(i->second);
61 delete i->second;
62 }
63 }
64 }
65 /*--------------------------------------------------------------------------*/
66 /* return a pointer to a node given a string
67 * returns NULL pointer if no match
68 */
operator [](std::string s)69 NODE* NODE_MAP::operator[](std::string s)
70 {
71 const_iterator i = _node_map.find(s);
72 if (i != _node_map.end()) {
73 return i->second;
74 }else if (OPT::case_insensitive) {
75 notstd::to_lower(&s);
76 i = _node_map.find(s);
77 }else{
78 return NULL;
79 }
80 return (i != _node_map.end()) ? i->second : NULL;
81 }
82 /*--------------------------------------------------------------------------*/
83 /* return a pointer to a node given a string
84 * creates a new one if it isn't already there.
85 */
new_node(std::string s)86 NODE* NODE_MAP::new_node(std::string s)
87 {
88 if (OPT::case_insensitive) {
89 notstd::to_lower(&s);
90 }else{
91 }
92 NODE* node = _node_map[s];
93
94 // increments how_many() when lookup fails (new s)
95 if (!node) {
96 node = new NODE(s, how_many());
97 // ^^^^ is really the map number of the new node
98 _node_map[s] = node;
99 }
100 assert(node);
101 return node;
102 }
103 /*--------------------------------------------------------------------------*/
104 /*--------------------------------------------------------------------------*/
105