http://issues.apache.org/activemq/browse/AMQCPP-128

Perf increase commits


git-svn-id: https://svn.apache.org/repos/asf/activemq/activemq-cpp/tags/activemq-cpp-2.1@555027 13f79535-47bb-0310-9956-ffa450edef68
diff --git a/src/main/activemq/util/Map.h b/src/main/activemq/util/Map.h
index 2f36c53..e330e72 100644
--- a/src/main/activemq/util/Map.h
+++ b/src/main/activemq/util/Map.h
@@ -15,8 +15,8 @@
  * limitations under the License.
  */
 
-#ifndef ACTIVEMQ_UTIL_MAP_H_
-#define ACTIVEMQ_UTIL_MAP_H_
+#ifndef _ACTIVEMQ_UTIL_MAP_H_
+#define _ACTIVEMQ_UTIL_MAP_H_
 
 #include <map>
 #include <vector>
@@ -32,59 +32,74 @@
      * a more user-friendly interface and to provide common
      * functions that do not exist in std::map.
      */
-    template <typename K, typename V> class Map : public concurrent::Synchronizable 
+    template <typename K, typename V> class Map : public concurrent::Synchronizable
     {
     private:
-    
+
         std::map<K,V> valueMap;
         concurrent::Mutex mutex;
-        
+
     public:
-    
+
         /**
          * Default constructor - does nothing.
          */
-    	Map(){};
-        
+        Map() {}
+
         /**
          * Copy constructor - copies the content of the given map into this
          * one.
          * @param source The source map.
          */
-        Map( const Map& source ){
+        Map( const Map& source ) {
             copy( source );
         }
-        
-    	virtual ~Map(){};
-        
+
+        virtual ~Map() {}
+
         /**
          * Comparison, equality is dependant on the method of determining
          * if the element are equal.
          * @param source - Map to compare to this one.
          * @returns true if the Map passed is equal in value to this one.
          */
-        virtual bool equals( const Map& source ) const;
-        
+        virtual bool equals( const Map& source ) const {
+            return this->valueMap == source.valueMap;
+        }
+
         /**
          * Copies the content of the source map into this map.  Erases
          * all existing data in this map.
          * @param source The source object to copy from.
          */
-        virtual void copy( const Map& source ); 
-        
+        virtual void copy( const Map& source ) {
+
+            // Erase the content of this object, and copy from the source
+            // all the elements.  We access source's private map since we
+            // are also a Map, this saves us a lot of time.
+            valueMap.clear();
+            valueMap.insert( source.valueMap.begin(), source.valueMap.end() );
+        }
+
         /**
          * Removes all keys and values from this map.
          */
-        virtual void clear();
-        
+        virtual void clear() {
+            valueMap.clear();
+        }
+
         /**
          * Indicates whether or this map contains a value for the
          * given key.
          * @param key The key to look up.
          * @return true if this map contains the value, otherwise false.
          */
-        virtual bool containsKey( const K& key ) const;
-        
+        virtual bool containsKey( const K& key ) const {
+            typename std::map<K,V>::const_iterator iter;
+            iter = valueMap.find(key);
+            return iter != valueMap.end();
+        }
+
         /**
          * Indicates whether or this map contains a value for the
          * given value, i.e. they are equal, this is done by operator==
@@ -92,53 +107,106 @@
          * @param value The Value to look up.
          * @return true if this map contains the value, otherwise false.
          */
-        virtual bool containsValue( const V& value ) const;
+        virtual bool containsValue( const V& value ) const {
+
+            if( valueMap.empty() ){
+                return false;
+            }
+
+            typename std::map<K,V>::const_iterator iter = valueMap.begin();
+            for( ; iter != valueMap.end(); ++iter ){
+                if( (*iter).second == value ) {
+                    return true;
+                }
+            }
+
+            return false;
+        }
 
         /**
          * @return if the Map contains any element or not, TRUE or FALSE
          */
-        virtual bool isEmpty() const;
+        virtual bool isEmpty() const {
+            return valueMap.empty();
+        }
 
         /**
          * @return The number of elements (key/value pairs) in this map.
          */
-        virtual std::size_t size() const;
-        
+        virtual std::size_t size() const {
+            return valueMap.size();
+        }
+
         /**
          * Gets the value for the specified key.
          * @param key The search key.
          * @return The value for the given key.
          * @throws activemq::exceptions::NoSuchElementException
          */
-        virtual V getValue( const K& key ) const 
-            throw(activemq::exceptions::NoSuchElementException);
-            
+        virtual V getValue( const K& key ) const
+            throw(activemq::exceptions::NoSuchElementException) {
+
+            typename std::map<K,V>::const_iterator iter;
+            iter = valueMap.find(key);
+            if( iter == valueMap.end() ){
+                throw activemq::exceptions::NoSuchElementException( __FILE__,
+                    __LINE__,
+                    "Key does not exist in map" );
+            }
+
+            return iter->second;
+        }
+
         /**
          * Sets the value for the specified key.
          * @param key The target key.
          * @param value The value to be set.
          */
-        virtual void setValue( const K& key, V value );
+        virtual void setValue( const K& key, V value ) {
+            valueMap[key] = value;
+        }
 
         /**
-         * Removes the value (key/value pair) for the specified key from 
+         * Removes the value (key/value pair) for the specified key from
          * the map.
          * @param key The search key.
-         */        
-        virtual void remove( const K& key );
-        
+         */
+        virtual void remove( const K& key ) {
+            valueMap.erase( key );
+        }
+
         /**
          * @return the entire set of keys in this map as a std::vector.
          */
-        virtual std::vector<K> getKeys() const;
-        
+        virtual std::vector<K> getKeys() const{
+            std::vector<K> keys( valueMap.size() );
+
+            typename std::map<K,V>::const_iterator iter;
+            iter=valueMap.begin();
+            for( int ix=0; iter != valueMap.end(); ++iter, ++ix ){
+                keys[ix] = iter->first;
+            }
+
+            return keys;
+        }
+
         /**
          * @return the entire set of values in this map as a std::vector.
          */
-        virtual std::vector<V> getValues() const;
-        
+        virtual std::vector<V> getValues() const {
+            std::vector<V> values( valueMap.size() );
+
+            typename std::map<K,V>::const_iterator iter;
+            iter=valueMap.begin();
+            for( int ix=0; iter != valueMap.end(); ++iter, ++ix ){
+                values[ix] = iter->second;
+            }
+
+            return values;
+        }
+
     public:     // Methods from Synchronizable
-    
+
         /**
          * Locks the object.
          * @throws ActiveMQException
@@ -154,7 +222,7 @@
         virtual void unlock() throw(exceptions::ActiveMQException) {
             mutex.unlock();
         }
-    
+
         /**
          * Waits on a signal from this object, which is generated
          * by a call to Notify.  Must have this object locked before
@@ -164,17 +232,17 @@
         virtual void wait() throw(exceptions::ActiveMQException) {
             mutex.wait();
         }
-    
+
         /**
          * Waits on a signal from this object, which is generated
          * by a call to Notify.  Must have this object locked before
          * calling.  This wait will timeout after the specified time
          * interval.
-         * @param millisecs the time in millisecsonds to wait, or 
+         * @param millisecs the time in millisecsonds to wait, or
          * WAIT_INIFINITE
          * @throws ActiveMQException
          */
-        virtual void wait(unsigned long millisecs) 
+        virtual void wait(unsigned long millisecs)
             throw(exceptions::ActiveMQException) {
             mutex.wait(millisecs);
         }
@@ -188,7 +256,7 @@
         virtual void notify() throw( exceptions::ActiveMQException ) {
             mutex.notify();
         }
-    
+
         /**
          * Signals the waiters on this object that it can now wake
          * up and continue.  Must have this object locked before
@@ -200,130 +268,6 @@
         }
     };
 
-    ////////////////////////////////////////////////////////////////////////////
-    template <typename K, typename V>    
-    bool Map<K,V>::equals( const Map& source ) const {
-        return this->valueMap == source.valueMap;        
-    }    
-
-    ////////////////////////////////////////////////////////////////////////////
-    template <typename K, typename V>    
-    void Map<K,V>::copy( const Map<K,V>& source ) {
-        
-        // Get an iterator to the beginning of the source map.
-        typename std::map<K,V>::const_iterator iter;
-        iter = source.valueMap.begin();
-        
-        // Erase the content of this object.
-        clear();
-        
-        // Add all of the entries to this map.
-        for( ; iter != source.valueMap.end(); iter++ ){            
-            setValue( iter->first, iter->second );
-        } 
-        
-    }
-    
-    ////////////////////////////////////////////////////////////////////////////
-    template <typename K, typename V>
-    void Map<K,V>::clear(){
-        valueMap.clear();
-    }
-    
-    ////////////////////////////////////////////////////////////////////////////
-    template <typename K, typename V>
-    bool Map<K,V>::containsKey(const K& key) const{
-        typename std::map<K,V>::const_iterator iter;
-        iter = valueMap.find(key);
-        return iter != valueMap.end();
-    }
-    
-    ////////////////////////////////////////////////////////////////////////////
-    template <typename K, typename V>
-    bool Map<K,V>::containsValue( const V& value ) const {
-
-        if( valueMap.empty() ){
-            return false;
-        }
-
-        typename std::map<K,V>::const_iterator iter = valueMap.begin();        
-        for( ; iter != valueMap.end(); ++iter ){
-            if( (*iter).second == value ) {
-                return true;
-            }
-        }
-
-        return false;
-    }
-
-    ////////////////////////////////////////////////////////////////////////////
-    template <typename K, typename V>
-    bool Map<K,V>::isEmpty() const{
-        return valueMap.empty();
-    }
-
-    ////////////////////////////////////////////////////////////////////////////
-    template <typename K, typename V>
-    std::size_t Map<K,V>::size() const{
-        return valueMap.size();
-    }
-    
-    ////////////////////////////////////////////////////////////////////////////
-    template <typename K, typename V>
-    V Map<K,V>::getValue( const K& key ) const 
-        throw(activemq::exceptions::NoSuchElementException){
-            
-        typename std::map<K,V>::const_iterator iter;
-        iter = valueMap.find(key);
-        if( iter == valueMap.end() ){
-            throw activemq::exceptions::NoSuchElementException( __FILE__, 
-                __LINE__, 
-                "Key does not exist in map" );
-        }
-        
-        return iter->second;
-    }
-        
-    ////////////////////////////////////////////////////////////////////////////
-    template <typename K, typename V>
-    void Map<K,V>::setValue( const K& key, V value ){
-        valueMap[key] = value;
-    }
-    
-    ////////////////////////////////////////////////////////////////////////////
-    template <typename K, typename V>
-    void Map<K,V>::remove( const K& key ){
-        valueMap.erase(key);
-    }
-    
-    ////////////////////////////////////////////////////////////////////////////
-    template <typename K, typename V>
-    std::vector<K> Map<K,V>::getKeys() const{
-        std::vector<K> keys(valueMap.size());
-        
-        typename std::map<K,V>::const_iterator iter;
-        iter=valueMap.begin();
-        for( int ix=0; iter != valueMap.end(); ++iter, ++ix ){
-            keys[ix] = iter->first;
-        }
-        
-        return keys;
-    }
-    
-    ////////////////////////////////////////////////////////////////////////////
-    template <typename K, typename V>
-    std::vector<V> Map<K,V>::getValues() const{
-        std::vector<V> values(valueMap.size());
-        
-        typename std::map<K,V>::const_iterator iter;
-        iter=valueMap.begin();
-        for( int ix=0; iter != valueMap.end(); ++iter, ++ix ){
-            values[ix] = iter->second;
-        }
-        
-        return values;
-    }
-
 }}
 
-#endif /*ACTIVEMQ_UTIL_MAP_H_*/
+#endif /*_ACTIVEMQ_UTIL_MAP_H_*/
diff --git a/src/main/activemq/util/Set.h b/src/main/activemq/util/Set.h
index 9dbc7c6..859f2cb 100644
--- a/src/main/activemq/util/Set.h
+++ b/src/main/activemq/util/Set.h
@@ -32,20 +32,20 @@
      * a more user-friendly interface and to provide common

      * functions that do not exist in std::map.

      */

-    template <typename E> class Set : public concurrent::Synchronizable 

+    template <typename E> class Set : public concurrent::Synchronizable

     {

     private:

-    

+

         std::set<E> values;

         concurrent::Mutex mutex;

-        

+

     public:

-    

+

         /**

          * Default constructor - does nothing.

          */

         Set(){};

-        

+

         /**

          * Copy constructor - copies the content of the given set into this

          * one.

@@ -54,57 +54,84 @@
         Set( const Set& source ){

             copy( source );

         }

-        

+

         virtual ~Set(){};

-        

+

         /**

          * Copies the content of the source set into this set.  Erases

          * all existing data in this st.

          * @param source The source object to copy from.

          */

-        virtual void copy( const Set& source ); 

-        

+        virtual void copy( const Set& source ) {

+            // Add all of the entries to this map.

+            values = source.values;

+        }

+

         /**

          * Removes all values from this set.

          */

-        virtual void clear();

-        

+        virtual void clear() {

+            values.clear();

+        }

+

         /**

          * Indicates whether or this set contains the given value.

          * @param value The value to look up.

          * @return true if this set contains the value, otherwise false.

          */

-        virtual bool contains( const E& value ) const;

+        virtual bool contains( const E& value ) const {

+            typename std::set<E>::const_iterator iter;

+            iter = values.find( value );

+            return iter != values.end();

+        }

 

         /**

          * @return if the set contains any element or not, TRUE or FALSE

          */

-        virtual bool isEmpty() const;

+        virtual bool isEmpty() const {

+            return values.empty();

+        }

 

         /**

          * @return The number of elements in this set.

          */

-        virtual std::size_t size() const;

-            

+        virtual std::size_t size() const {

+            return values.size();

+        }

+

         /**

          * Adds the given value to the set.

          * @param value The value to add.

          */

-        virtual void add( const E& value );

+        virtual void add( const E& value ) {

+            values.insert( value );

+        }

 

         /**

          * Removes the value from the set.

          * @param value The value to be removed.

-         */        

-        virtual void remove( const E& value );

-        

+         */

+        virtual void remove( const E& value ) {

+            values.erase( value );

+        }

+

         /**

          * @return the all values in this set as a std::vector.

          */

-        virtual std::vector<E> toArray() const;

-        

+        virtual std::vector<E> toArray() const {

+            std::vector<E> valueArray( values.size() );

+

+            typename std::set<E>::const_iterator iter;

+            iter=values.begin();

+            for( int ix=0; iter != values.end(); ++iter, ++ix ){

+                valueArray[ix] = *iter;

+            }

+

+            return valueArray;

+        }

+

     public:     // Methods from Synchronizable

-    

+

         /**

          * Locks the object.

          * @throws ActiveMQException

@@ -120,7 +147,7 @@
         virtual void unlock() throw(exceptions::ActiveMQException) {

             mutex.unlock();

         }

-    

+

         /**

          * Waits on a signal from this object, which is generated

          * by a call to Notify.  Must have this object locked before

@@ -130,19 +157,19 @@
         virtual void wait() throw(exceptions::ActiveMQException) {

             mutex.wait();

         }

-    

+

         /**

          * Waits on a signal from this object, which is generated

          * by a call to Notify.  Must have this object locked before

          * calling.  This wait will timeout after the specified time

          * interval.

-         * @param millisecs the time in millisecsonds to wait, or 

+         * @param millisecs the time in millisecsonds to wait, or

          * WAIT_INIFINITE

          * @throws ActiveMQException

          */

-        virtual void wait(unsigned long millisecs) 

+        virtual void wait( unsigned long millisecs )

             throw(exceptions::ActiveMQException) {

-            mutex.wait(millisecs);

+            mutex.wait( millisecs );

         }

 

         /**

@@ -154,7 +181,7 @@
         virtual void notify() throw( exceptions::ActiveMQException ) {

             mutex.notify();

         }

-    

+

         /**

          * Signals the waiters on this object that it can now wake

          * up and continue.  Must have this object locked before

@@ -165,66 +192,6 @@
             mutex.notifyAll();

         }

     };

-    

-    ////////////////////////////////////////////////////////////////////////////

-    template <typename E>    

-    void Set<E>::copy( const Set<E>& source ) {

-        

-        // Add all of the entries to this map.

-        values = source.values;        

-    }

-    

-    ////////////////////////////////////////////////////////////////////////////

-    template <typename E>

-    void Set<E>::clear(){

-        values.clear();

-    }

-    

-    ////////////////////////////////////////////////////////////////////////////

-    template <typename E>

-    bool Set<E>::contains(const E& value) const{

-        typename std::set<E>::const_iterator iter;

-        iter = values.find(value);

-        return iter != values.end();

-    }

-

-    ////////////////////////////////////////////////////////////////////////////

-    template <typename E>

-    bool Set<E>::isEmpty() const{

-        return values.empty();

-    }

-

-    ////////////////////////////////////////////////////////////////////////////

-    template <typename E>

-    std::size_t Set<E>::size() const{

-        return values.size();

-    }

-        

-    ////////////////////////////////////////////////////////////////////////////

-    template <typename E>

-    void Set<E>::add( const E& value ){

-        values.insert(value);

-    }

-    

-    ////////////////////////////////////////////////////////////////////////////

-    template <typename E>

-    void Set<E>::remove( const E& value ){

-        values.erase(value);

-    }

-    

-    ////////////////////////////////////////////////////////////////////////////

-    template <typename E>

-    std::vector<E> Set<E>::toArray() const{

-        std::vector<E> valueArray(values.size());

-        

-        typename std::set<E>::const_iterator iter;

-        iter=values.begin();

-        for( int ix=0; iter != values.end(); ++iter, ++ix ){

-            valueArray[ix] = *iter;

-        }

-        

-        return valueArray;

-    }

 

 }}