Reduce memory usage?

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Joseph Turian

    #1

    Reduce memory usage?

    Let's say I have a vector of Base.
    I want to convert it to a vector of Derived, where
    Derived::Derive d(const Base&); is defined.

    Is it possible to convert the vector of Base to a vector of Derived
    without incurring the memory overhead of two copies of everything? If
    these were vectors of the same kind, I could just use vector::swap, but
    unfortunately not in this example.

    Joseph

  • Stefan Näwe

    #2
    Re: Reduce memory usage?

    Joseph Turian wrote:[color=blue]
    > Let's say I have a vector of Base.[/color]

    A std::vector<Bas e*>, I presume ?
    [color=blue]
    > I want to convert it to a vector of Derived, where
    > Derived::Derive d(const Base&); is defined.
    >
    > Is it possible to convert the vector of Base to a vector of Derived
    > without incurring the memory overhead of two copies of everything? If
    > these were vectors of the same kind, I could just use vector::swap, but
    > unfortunately not in this example.[/color]

    Maybe something like this:

    1. Define a (template) functor that returns dynamic_cast<De rived*>(p) in operator()(cons t Base* p)
    2. use std::transform( v.begin(), v.end(), v.begin(), CastingFunctor< ...>())

    /S
    --
    Stefan Naewe
    naewe.s_AT_atla s_DOT_de

    Comment

    • Stefan Näwe

      #3
      Re: Reduce memory usage?

      Stefan Näwe wrote:[color=blue]
      > Joseph Turian wrote:
      >[color=green]
      >>Let's say I have a vector of Base.[/color]
      >
      >
      > A std::vector<Bas e*>, I presume ?
      >
      >[color=green]
      >>I want to convert it to a vector of Derived, where
      >>Derived::Deri ved(const Base&); is defined.
      >>
      >>Is it possible to convert the vector of Base to a vector of Derived
      >>without incurring the memory overhead of two copies of everything? If
      >>these were vectors of the same kind, I could just use vector::swap, but
      >>unfortunate ly not in this example.[/color]
      >
      >
      > Maybe something like this:
      >
      > 1. Define a (template) functor that returns dynamic_cast<De rived*>(p) in operator()(cons t Base* p)
      > 2. use std::transform( v.begin(), v.end(), v.begin(), CastingFunctor< ...>())[/color]

      This must of course be:

      std::transform( v.begin(), v.end(), w.begin(), CastingFunctor< ...>())

      Where w is a (large enough!) vector<Derived* >

      /S
      --
      Stefan Naewe
      naewe.s_AT_atla s_DOT_de

      Comment

      • Stefan Näwe

        #4
        Re: Reduce memory usage?

        Stefan Näwe wrote:[color=blue]
        > Stefan Näwe wrote:
        >[color=green]
        >>Joseph Turian wrote:
        >>
        >>[color=darkred]
        >>>Let's say I have a vector of Base.[/color]
        >>
        >>
        >>A std::vector<Bas e*>, I presume ?
        >>
        >>
        >>[color=darkred]
        >>>I want to convert it to a vector of Derived, where
        >>>Derived::Der ived(const Base&); is defined.
        >>>
        >>>Is it possible to convert the vector of Base to a vector of Derived
        >>>without incurring the memory overhead of two copies of everything? If
        >>>these were vectors of the same kind, I could just use vector::swap, but
        >>>unfortunatel y not in this example.[/color]
        >>
        >>
        >>Maybe something like this:
        >>
        >>1. Define a (template) functor that returns dynamic_cast<De rived*>(p) in operator()(cons t Base* p)[/color][/color]

        Oops...
        This must be:
        1. Define a (template) functor that returns dynamic_cast<De rived*>(p) in operator()(Base * p)
        [color=blue][color=green]
        >>2. use std::transform( v.begin(), v.end(), v.begin(), CastingFunctor< ...>())[/color]
        >
        >
        > This must of course be:
        >
        > std::transform( v.begin(), v.end(), w.begin(), CastingFunctor< ...>())
        >
        > Where w is a (large enough!) vector<Derived* >[/color]

        This might work for you:

        <----CODE---->

        #include <iostream>
        #include <iterator>
        #include <string>
        #include <algorithm>
        #include <vector>

        template<typena me To>
        struct CastingFunctor
        {
        template<typena me From>
        To* operator()(From * p) const { return dynamic_cast<To *>(p); }
        };

        struct Base
        {
        virtual ~Base() { }
        };

        struct Derived : public Base
        {
        };

        int main(int argc, char* argv[])
        {
        const size_t N = 10;
        std::vector<Bas e*> v;

        for(size_t i=0; i<N; ++i)
        {
        v.push_back(new Derived);
        v.push_back(new Base);
        }

        std::vector<Der ived*> w(v.size());
        std::transform( v.begin(), v.end(), w.begin(), CastingFunctor< Derived>());

        std::copy(v.beg in(), v.end(), std::ostream_it erator<void*>(s td::cout, "\n"));
        std::copy(w.beg in(), w.end(), std::ostream_it erator<void*>(s td::cout, "\n"));
        return 0;
        }

        <----/CODE---->


        /S
        --
        Stefan Naewe
        naewe.s_AT_atla s_DOT_de

        Comment

        Working...