torc-master
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
Primitive.hpp
Go to the documentation of this file.
1 // Torc - Copyright 2011-2013 University of Southern California. All Rights Reserved.
2 // $HeadURL$
3 // $Id$
4 
5 // This program is free software: you can redistribute it and/or modify it under the terms of the
6 // GNU General Public License as published by the Free Software Foundation, either version 3 of the
7 // License, or (at your option) any later version.
8 //
9 // This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
10 // without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See
11 // the GNU General Public License for more details.
12 //
13 // You should have received a copy of the GNU General Public License along with this program. If
14 // not, see <http://www.gnu.org/licenses/>.
15 
16 /// \file
17 /// \brief Header for the Primitive class.
18 
19 #ifndef TORC_PACKER_PRIMITIVE_HPP
20 #define TORC_PACKER_PRIMITIVE_HPP
21 
23 #include "torc/packer/Element.hpp"
24 
25 
26 #include <string>
27 
28 namespace torc {
29 namespace physical {
30 
31 /// \brief primitive.
32 
33 class Primitive : public Component {
34 
35 // friends
36 /// \brief The Factory class has direct access to our internals.
37 friend class RcFactory;
38 
39 protected:
40 // types
41 /// \brief Imported type name.
43 // members
44 /// \brief Vector of element shared pointers.
46 
47 // constructors
48 Primitive(const string& inName) : Component(inName){}
49 public:
50 
51 // types
52 /// \brief Constant iterator for Element shared pointers.
53 typedef ElementSharedPtrVector::const_iterator ElementSharedPtrConstIterator;
54 /// \brief Non-constant iterator for Element shared pointers.
55 typedef ElementSharedPtrVector::iterator ElementSharedPtrIterator;
56 // functions
57 /// \brief Find a primitive element by name.
58 /// \param inName The element name to look for.
59 /// \returns an iterator for the specified element, or elementsEnd() if the name was not
60 /// found.
61 ElementSharedPtrIterator findElement(const string& inName) {
62 NameComparator predicate(inName);
63 return std::find_if(elementsBegin(), elementsEnd(), predicate);
64 }
65 /// \brief Add a element to the primitive.
66 /// \param inElementPtr The element to add.
67 /// \returns true if the element was added, or false if a element with the same name already
68 /// exists in the primitive.
69 bool addElement(ElementSharedPtr& inElementPtr) {
70 /// \todo Acquire mutex.
71 ElementSharedPtrVector::iterator e = mElements.end();
72 ElementSharedPtrVector::iterator result = findElement(inElementPtr->getName());
73 if(result == e) mElements.push_back(inElementPtr);
74 return result == e; // return true if we added the element
75 /// \todo Release mutex.
76 }
77 /// \brief Remove a element from the primitive.
78 /// \param inElementPtr The element to remove.
79 /// \returns true if the element was removed, or false if the element did not exist.
80 bool removeElement(ElementSharedPtr& inElementPtr) {
81 /// \todo Acquire mutex.
82 ElementSharedPtrVector::iterator e = mElements.end();
83 ElementSharedPtrVector::iterator result = std::find(mElements.begin(), e, inElementPtr);
84 if(result == e) return false;
85 
86 //adjust connections
87 
88  ConnectionPinVector sinkPins;
89  ConnectionPinVector sourcePins;
90  ConnectionSharedPtrVector sourceConnections;
91  ConnectionSharedPtrVector sinkConnections;
92  ElementSharedPtrVector sourceElements;
93  ElementSharedPtrVector sinkElements;
94 
97 
98  ConnectionPin pin;
99 
100  ConnectionSharedPtr cnPtr;
101  ElementSharedPtr elemPtr;
102  for( cnIt = inElementPtr->connectionsBegin(); cnIt != inElementPtr->connectionsEnd(); ++cnIt){
103  if( (*cnIt)->getSource()->getElementName() == inElementPtr->getName() ){
104  pin = *(*cnIt)->getSink();
105  sinkPins.push_back(pin);
106  elemPtr = *findElement(pin.getElementName());
107  sinkElements.push_back(elemPtr);
108  for(cnIt2 = elemPtr->connectionsBegin(); cnIt2 != elemPtr->connectionsEnd(); ++cnIt2){
109  if( (*cnIt2)->getSink()->getPinName() == pin.getPinName()){
110  sinkConnections.push_back(*cnIt2);
111  elemPtr->removeConnection(cnIt2);
112  break;
113  }
114  }
115  }
116  else{
117  pin = *(*cnIt)->getSource();
118  sourcePins.push_back(pin);
119  elemPtr = *findElement(pin.getElementName());
120  sourceElements.push_back(elemPtr);
121  for(cnIt2 = elemPtr->connectionsBegin(); cnIt2 != elemPtr->connectionsEnd(); ++cnIt2){
122  if( (*cnIt2)->getSource()->getPinName() == pin.getPinName()){
123  sourceConnections.push_back(*cnIt2);
124  elemPtr->removeConnection(cnIt2);
125  break;
126  }
127  }
128  }
129  }
130  for(size_t i=0; i<sourcePins.size(); ++i){
131  for(size_t j=0; j<sinkPins.size(); ++j){
132  ConnectionSharedPtr connectionPtr(new Connection(sourcePins[i].getPinName()));
133  ConnectionSharedPtr connectionPtr1(new Connection(sourcePins[i].getPinName()));
134  connectionPtr->addConnectionPin(sourcePins[i]);
135  connectionPtr->addConnectionPin(sinkPins[j]);
136  sinkElements[j]->addConnection(connectionPtr);
137 
138  connectionPtr1->addConnectionPin(sourcePins[i]);
139  connectionPtr1->addConnectionPin(sinkPins[j]);
140  sourceElements[i]->addConnection(connectionPtr1);
141 
142  }
143  }
144  mElements.erase(result);
145 return true;
146 }
147 
148 // operators
149  /// \brief Equality operator.
150  /// \details This function deems elements equal if their names are identical.
151  /// \param rhs The element to compare against.
152  /// \returns true if both element names are identical, or false otherwise.
153  bool operator ==(const Primitive& rhs) const { return mName == rhs.mName; }
154 
155 
156 // accessors
157 
158 size_t getElementCount(void) const { return mElements.size(); }
159 // iterators
160 /// \brief Returns the begin constant iterator for elements.
162 /// \brief Returns the end constant iterator for elements.
164 /// \brief Returns the begin non-constant iterator for elements.
166 /// \brief Returns the end non-constant iterator for elements.
168 };
169 
170 /// \brief Shared pointer encapsulation of a Primitive.
171 typedef boost::shared_ptr<Primitive> PrimitiveSharedPtr;
172 
173 /// \brief Vector of Primitive shared pointers.
174 typedef std::vector<PrimitiveSharedPtr> PrimitiveSharedPtrVector;
175 
176 
177 
178 } // namespace physical
179 } // namespace torc
180 
181 #endif // TORC_PACKER_PRIMITIVE_HPP
Hierarchical componenet.
Definition: Connection.hpp:29
bool removeElement(ElementSharedPtr &inElementPtr)
Remove a element from the primitive.
Definition: Primitive.hpp:80
bool addElement(ElementSharedPtr &inElementPtr)
Add a element to the primitive.
Definition: Primitive.hpp:69
boost::shared_ptr< Primitive > PrimitiveSharedPtr
Shared pointer encapsulation of a Primitive.
Definition: Primitive.hpp:171
bool operator==(const Primitive &rhs) const
Equality operator.
Definition: Primitive.hpp:153
string mName
The name of the object.
Definition: Named.hpp:43
boost::shared_ptr< Element > ElementSharedPtr
Shared pointer encapsulation of a element.
Definition: Element.hpp:119
RcFactory class for physical netlist elements.
Definition: RcFactory.hpp:34
ElementSharedPtrIterator elementsEnd(void)
Returns the end non-constant iterator for elements.
Definition: Primitive.hpp:167
Physical design connection-pin pair, suitable for specifying a net endpoint.
std::string string
ElementSharedPtrVector::iterator ElementSharedPtrIterator
Non-constant iterator for Element shared pointers.
Definition: Primitive.hpp:55
Comparator class to serve as a predicate when searching for names.
Definition: Named.hpp:61
size_t getElementCount(void) const
Definition: Primitive.hpp:158
std::vector< ConnectionSharedPtr > ConnectionSharedPtrVector
Vector of componenet shared pointers.
Definition: Connection.hpp:88
boost::shared_ptr< Connection > ConnectionSharedPtr
Shared pointer encapsulation of a componenet.
Definition: Connection.hpp:82
ElementSharedPtrConstIterator elementsBegin(void) const
Returns the begin constant iterator for elements.
Definition: Primitive.hpp:161
ElementSharedPtrIterator findElement(const string &inName)
Find a primitive element by name.
Definition: Primitive.hpp:61
Hierarchical componenet.
Definition: Component.hpp:30
const PinName & getPinName(void) const
Returns the pin name.
ElementSharedPtrVector mElements
Vector of element shared pointers.
Definition: Primitive.hpp:45
ElementSharedPtrVector::const_iterator ElementSharedPtrConstIterator
Constant iterator for Element shared pointers.
Definition: Primitive.hpp:53
std::vector< ElementSharedPtr > ElementSharedPtrVector
Vector of element shared pointers.
Definition: Element.hpp:125
ConnectionSharedPtrVector::iterator ConnectionSharedPtrIterator
Definition: Element.hpp:55
ConnectionPinVector::iterator ConnectionPinSharedPtrIterator
Definition: Connection.hpp:41
const string & getElementName(void) const
Returns the element pointer or NULL.
std::string string
Imported type name.
Definition: Primitive.hpp:42
std::vector< PrimitiveSharedPtr > PrimitiveSharedPtrVector
Vector of Primitive shared pointers.
Definition: Primitive.hpp:174
ElementSharedPtrIterator elementsBegin(void)
Returns the begin non-constant iterator for elements.
Definition: Primitive.hpp:165
ElementSharedPtrConstIterator elementsEnd(void) const
Returns the end constant iterator for elements.
Definition: Primitive.hpp:163
std::vector< ConnectionPin > ConnectionPinVector
Vector of connection pins.
Primitive(const string &inName)
Definition: Primitive.hpp:48