[ create a new paste ] login | about

Link: http://codepad.org/qur0oCaR    [ raw code | fork ]

C++, pasted on Apr 2:
#pragma once

#ifndef PAIRQUEUE_H
#define	PAIRQUEUE_H

#include "common/Defines.h"
#include "List.h"
        
    namespace A {
    namespace B {

        template < typename S, typename T >
        class PairQueue {
        public:

            PairQueue();
            virtual ~PairQueue();

            virtual void clear();
            virtual void remove(const S & key);
            T find(const S & key) const;
            T get(const unsigned int index) const;
            S getKey(const unsigned int index) const;
            T front() const;
            void pop();

            size_t size() const;

            bool contains(const S & key) const;

            List<S> getKeyList() const;

            void add(S key, T t);

        private:

            typedef std::pair < S, T > STPair;
            typedef List < STPair > STPairList;
            STPairList queue;

            class STPairListFunctor {
            public:

                STPairListFunctor(S const & key) {
                    orig = key;
                }

                bool operator()(STPair & elem) {
                    return orig == elem.first;
                }
            private:
                S orig;
            };

        };

        template <typename S, typename T>
        PairQueue<S, T>::PairQueue() {
            queue.clear();
        }

        template <typename S, typename T>
        PairQueue<S, T>::~PairQueue() {
        }

        template <typename S, typename T>
        void
        PairQueue<S, T>::clear() {
            queue.clear();
        }

        template <typename S, typename T>
        void
        PairQueue<S, T>::remove(const S & key) {

            typename STPairList::iterator it;

            it = std::find_if(queue.begin(),
                    queue.end(),
                    STPairListFunctor(key));

            if (it != queue.end()) {
                queue.erase(it);
            }//End if
        }

        template <typename S, typename T>
        T
        PairQueue<S, T>::front() const {
            return queue.front().second;
        }

        template <typename S, typename T>
        void
        PairQueue<S, T>::pop() {
            queue.erase(queue.begin());
        }

        template <typename S, typename T>
        T
        PairQueue<S, T>::get(unsigned int i) const {

            return (queue.get(i).second);

        }

        template <typename S, typename T>
        S
        PairQueue<S, T>::getKey(unsigned int i) const {

            return (queue.get(i).first);

        }

        template <typename S, typename T>
        T
        PairQueue<S, T>::find(const S & key) const {

            typename STPairList::iterator it;

            it = std::find_if(
                    queue.begin(),
                    queue.end(),
                    STPairListFunctor(key));

            if (it == queue.end()) {
                return NULL;
            }//End if

            return (it->second);


        }

        template <typename S, typename T>
        void
        PairQueue<S, T>::add(S key, T t) {

            STPair elem = std::make_pair(key, t);
            queue.add(elem);

        }

        template <typename S, typename T>
        std::size_t
        PairQueue<S, T>::size() const {
            return queue.size();
        }

        template <typename S, typename T>
        bool
        PairQueue<S, T>::contains(const S & key) const {
            typename STPairList::iterator it;

            it = std::find_if(queue.begin(),
                    queue.end(),
                    STPairListFunctor(key));

            if (it == queue.end()) {
                return false;
            }//End if

            return true;
        }

        template <typename S, typename T>
        List<S>
        PairQueue<S,T>::getKeyList() const {

            List<S> list;
            for(size_t i=0; i<queue.size(); i++){

                list.add(queue.get(i).first);

            }

            return list;
        }

    };
};

#endif	/* PAIRQUEUE_H */


Create a new paste based on this one


Comments: