C++Talk.NET Forum Index C++Talk.NET
C++ language newsgroups
 
Archives   FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

How to return pointers of specified positions of a vector

 
Post new topic   Reply to topic    C++Talk.NET Forum Index -> C++ Language (Moderated)
View previous topic :: View next topic  
Author Message
KK
Guest





PostPosted: Thu Nov 18, 2004 12:48 am    Post subject: How to return pointers of specified positions of a vector Reply with quote



Greetings,
I am building my own vector class (not the one provided in STL) and I
am faced with the challenge of returning pointers of some specified
positions of the vector.

For example,
If A,B,C are objects of class vector such that
A =[10 3 1 0];
B =[0 2];
C= [-10 -10];

I want to overload operator () so that upon the following operation
A(B)=C;

I should have, A=[-10 3 -10 0];

How can I achieve such a task?
Thank you for your time and consideration.
-KK

[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
Back to top
Branimir Maksimovic
Guest





PostPosted: Fri Nov 19, 2004 12:48 pm    Post subject: Re: How to return pointers of specified positions of a vecto Reply with quote



[email]kewlkarun (AT) yahoo (DOT) com[/email] (KK) wrote in message news:<c8fd5039.0411171422.7e3aeb8c (AT) posting (DOT) google.com>...
Quote:
Greetings,
I am building my own vector class (not the one provided in STL) and I
am faced with the challenge of returning pointers of some specified
positions of the vector.

For example,
If A,B,C are objects of class vector such that
A =[10 3 1 0];
B =[0 2];
C= [-10 -10];

I want to overload operator () so that upon the following operation
A(B)=C;

I should have, A=[-10 3 -10 0];

How can I achieve such a task?
Besides necessary assertions, I don't see any problem in implementig such

vector as plain array. Interpret elements of B as indexes into A and B,
and be aware that elements of B must be convertable to size_t.

Greetings, Bane.

[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]

Back to top
anton muhin
Guest





PostPosted: Fri Nov 19, 2004 12:49 pm    Post subject: Re: How to return pointers of specified positions of a vecto Reply with quote



KK wrote:
Quote:
Greetings,
I am building my own vector class (not the one provided in STL) and I
am faced with the challenge of returning pointers of some specified
positions of the vector.

For example,
If A,B,C are objects of class vector such that
A =[10 3 1 0];
B =[0 2];
C= [-10 -10];

I want to overload operator () so that upon the following operation
A(B)=C;

I should have, A=[-10 3 -10 0];

How can I achieve such a task?
Thank you for your time and consideration.

I didn't test it, but I suppose it to work: A(C) could return an object
with assignment operator that places the objects at needed positions.
Somehting along these lines:

class MyVector {

MySubVector operator () (MyVector const &indices) {
return MySubVector(*this, indices);
}

class MySubVector {
MySubVector(MyVector &v, MyVector const &indices) : v_(v),
indices_(indices) {
}

MySubVector& operator = (MyVector const &v) {
for (int i = 0; i < v.length(); ++i) {
v_[indices_[i]] = v[i];
}
return (*this);
}
};
};

Hope, this helps.

anton.

[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]

Back to top
Stephan Br?nnimann
Guest





PostPosted: Fri Nov 19, 2004 12:55 pm    Post subject: Re: How to return pointers of specified positions of a vecto Reply with quote

[email]kewlkarun (AT) yahoo (DOT) com[/email] (KK) wrote in message news:<c8fd5039.0411171422.7e3aeb8c (AT) posting (DOT) google.com>...
Quote:
Greetings,
I am building my own vector class (not the one provided in STL) and I
am faced with the challenge of returning pointers of some specified
positions of the vector.

For example,
If A,B,C are objects of class vector such that
A =[10 3 1 0];
B =[0 2];
C= [-10 -10];

I want to overload operator () so that upon the following operation
A(B)=C;

I should have, A=[-10 3 -10 0];

How can I achieve such a task?
Thank you for your time and consideration.
-KK

Sorry, I can't see your problem.
If you have an operator[] (possibly with range checking and/or
automatic extension)for your class then using an int you simply
iterate over B and write C[i] to A[B[i]].
What's going to if the size of B and C is not the same is up to you.

Stephan Brönnimann
[email]broeni (AT) osb-systems (DOT) com[/email]
Open source rating and billing engine for communication networks.

[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]

Back to top
Michiel Salters
Guest





PostPosted: Fri Nov 19, 2004 12:57 pm    Post subject: Re: How to return pointers of specified positions of a vecto Reply with quote

[email]kewlkarun (AT) yahoo (DOT) com[/email] (KK) wrote in message news:<c8fd5039.0411171422.7e3aeb8c (AT) posting (DOT) google.com>...
Quote:
Greetings,
I am building my own vector class (not the one provided in STL) and I
am faced with the challenge of returning pointers of some specified
positions of the vector.

For example,
If A,B,C are objects of class vector such that
A =[10 3 1 0];
B =[0 2];
C= [-10 -10];

I want to overload operator () so that upon the following operation
A(B)=C;

I should have, A=[-10 3 -10 0];

How can I achieve such a task?

You probably need to implement a "shim" class. That class represents the
result of your vector<T>::operator(vector<int>). The shim contains
a vector of T*s. When you assign a vector<T> to the shim, each T
is assigned to one of the vectors.

In your example, the shim would contain two int*s, one to the first
element of A and one to the third.

A problem of such shim classes is that they are vulnerable to the
lifetime of the object the refer to. You might want to disable
the copy ctor.

Regards,
Michiel Salters

[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]

Back to top
Ron Natalie
Guest





PostPosted: Fri Nov 19, 2004 2:53 pm    Post subject: Re: How to return pointers of specified positions of a vecto Reply with quote

KK wrote:

Quote:

I should have, A=[-10 3 -10 0];

How can I achieve such a task?
Thank you for your time and consideration.

Something like:

class WierdVectorPointer {
public:
WierdVectorPointer(WierdVector* a, const WierdVector& b) : dest(a), index(b) { }
WierdVector operator=(const WierdVector& that) {
int imax = max(that.size(), index.size();
for(int i = 0; i < imax; ++i)
(*dest)[index[i]] = that[i];
return *dest;
}
private:
WierdVector index;
WierdVector* dest;
};

class WierdVector {
public:
WierdVectorPointer operator()(const WierdVector& ix) {
return WierdVectorPointer(this, ix);
}
//...
};

[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]


Back to top
Falk Tannhäuser
Guest





PostPosted: Fri Nov 19, 2004 2:54 pm    Post subject: Re: How to return pointers of specified positions of a vecto Reply with quote

KK wrote:
Quote:
I am building my own vector class (not the one provided in STL) and I
am faced with the challenge of returning pointers of some specified
positions of the vector.

For example,
If A,B,C are objects of class vector such that
A =[10 3 1 0];
B =[0 2];
C= [-10 -10];

I want to overload operator () so that upon the following operation
A(B)=C;

I should have, A=[-10 3 -10 0];

Have operator() return a proxy class holding references to the operands
and having an operator= that does what you want:
_______________________________________________________________________
#include <iostream>
#include <ostream>
#include <vector>
#include <iterator>
#include <algorithm>
#include <cassert>

template<typename T> class MyVector_assign_proxy;

template<typename T>
class MyVector
{
std::vector<T> implem;
public:
typedef T value_type;

MyVector() {}

template<typename IntputIterator>
MyVector(IntputIterator i1, IntputIterator i2) : implem(i1, i2) {}

template<typename U, unsigned N>
MyVector(U const (&array)[N]) : implem(array, array+N) {}

value_type operator[](unsigned i) const
{ return i<implem.size() ? implem[i] : 0; }

value_type& operator[](unsigned i)
{
if(i >= implem.size())
implem.resize(i+1);
return implem[i];
}

MyVector_assign_proxy<T> operator()(MyVector<unsigned> const&); // defined below

unsigned size() const { return implem.size(); }

void print_to(std::ostream& o) const
{
o << "[ ";
std::copy(implem.begin(), implem.end(), std::ostream_iterator o << "]";
}
}; // class MyVector

template std::ostream& operator<<(std::ostream& o, MyVector { mv.print_to(o); return o; }


template<typename T>
class MyVector_assign_proxy
{
MyVector<T>& a;
MyVector<unsigned> const& b;
public:
MyVector_assign_proxy(MyVector<T>& a, MyVector<unsigned> const& b) : a(a), b(b) {}

void operator=(MyVector<T> const& c)
{
for(unsigned i = 0; i < b.size(); ++i)
a[b[i]] = c[i];
}
}; // class MyVector_assign_proxy


template MyVector_assign_proxy<T> MyVector<T>::operator()(MyVector<unsigned> const& b)
{
assert(static_cast<void*>(this) != &b); // Beware of aliasing problems!
return MyVector_assign_proxy<T>(*this, b);
}


static int const Ainit[] = { 10, 3, 1, 0 };
static unsigned const Binit[] = { 0, 2 };
static int const Cinit[] = { -10, -10 };

int main()
{
MyVector<int> A(Ainit), C(Cinit);
MyVector<unsigned> B(Binit);

std::cout << A << ' ' << B << ' ' << C << 'n';
A(B) = C;
std::cout << A << ' ' << B << ' ' << C << 'n';
return 0;
}
_______________________________________________________________________

Falk

[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]

Back to top
Bob Bell
Guest





PostPosted: Fri Nov 19, 2004 3:25 pm    Post subject: Re: How to return pointers of specified positions of a vecto Reply with quote

[email]kewlkarun (AT) yahoo (DOT) com[/email] (KK) wrote in message news:<c8fd5039.0411171422.7e3aeb8c (AT) posting (DOT) google.com>...
Quote:
Greetings,
I am building my own vector class (not the one provided in STL) and I
am faced with the challenge of returning pointers of some specified
positions of the vector.

For example,
If A,B,C are objects of class vector such that
A =[10 3 1 0];
B =[0 2];
C= [-10 -10];

I want to overload operator () so that upon the following operation
A(B)=C;

I should have, A=[-10 3 -10 0];

How can I achieve such a task?
Thank you for your time and consideration.
-KK

I have no idea why you would need such a thing, but here's a short
example that shows one way you can do it.

#include <iostream>
#include <vector>

class VectorAssign;

class Vector : public std::vector<int> {
public:
VectorAssign operator()(const Vector& iArg);
};

class VectorAssign {
public:
VectorAssign(Vector& iTarget, const Vector& iIndices);
Vector& operator=(const Vector& iArg);
private:
Vector& mTarget;
const Vector& mIndices;
};

VectorAssign Vector::operator()(const Vector& iArg)
{
return VectorAssign(*this, iArg);
}

VectorAssign::VectorAssign(Vector& iTarget, const Vector& iIndices) :
mTarget(iTarget),
mIndices(iIndices)
{
}

Vector& VectorAssign::operator=(const Vector& iArg)
{
for (Vector::size_type i = 0; i != mIndices.size(); ++i)
mTarget[mIndices[i]] = iArg[i];
return mTarget;
}

void F()
{
Vector a, b, c;

a.push_back(10);
a.push_back(3);
a.push_back(1);
a.push_back(0);

b.push_back(0);
b.push_back(2);

c.push_back(-10);
c.push_back(-10);

a(b) = c;

for (Vector::size_type i = 0; i != a.size(); ++i)
std::cout << a[i] << "n";
}

Output:

-10
3
-10
0

I think it may not actually be correct because it calls the assignment
operator on the temporary returned by Vector::operator()(Vector), but
it does compile and run in gcc 3.2. Also, I didn't bother to decide
what should happen if the indices in the various vectors are out of
range.

Bob

[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]

Back to top
Falk Tannhäuser
Guest





PostPosted: Fri Nov 19, 2004 4:50 pm    Post subject: Re: How to return pointers of specified positions of a vecto Reply with quote

KK wrote:
Quote:
If A,B,C are objects of class vector such that
A =[10 3 1 0];
B =[0 2];
C= [-10 -10];

I want to overload operator () so that upon the following operation
A(B)=C;

I should have, A=[-10 3 -10 0];

Hello again,
There is perhaps a much simpler solution than the ones suggested so far:
Check the std::valarray class template in the C++ Standard Library, and
in particular the "valarray subset operations" from § 26.3.2.4:

valarray<T> operator[](slice) const;
slice_array<T> operator[](slice);
valarray<T> operator[](const gslice&) const;
gslice_array<T> operator[](const gslice&);
valarray<T> operator[](const valarray<bool>&) const;
mask_array<T> operator[](const valarray<bool>&);
valarray<T> operator[](const valarray<size_t>&) const;
indirect_array<T> operator[](const valarray<size_t>&);

These operations are implemented in a similar way then the ones suggested
by several posters (comprising myself) but the hard work was already done
by the library implementers.
The last operator seems to do exactly what you wanted; but the remaining
ones could be interesting too in this context.

Falk


[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]

Back to top
Tom Widmer
Guest





PostPosted: Fri Nov 19, 2004 4:52 pm    Post subject: Re: How to return pointers of specified positions of a vecto Reply with quote

On 17 Nov 2004 19:48:56 -0500, [email]kewlkarun (AT) yahoo (DOT) com[/email] (KK) wrote:

Quote:
Greetings,
I am building my own vector class (not the one provided in STL) and I
am faced with the challenge of returning pointers of some specified
positions of the vector.

For example,
If A,B,C are objects of class vector such that
A =[10 3 1 0];
B =[0 2];
C= [-10 -10];

I want to overload operator () so that upon the following operation
A(B)=C;

I should have, A=[-10 3 -10 0];

How can I achieve such a task?

Have a look at the valarray class. That basically provides the
operations you are talking about, so you can examine the
implementation or use it directly. You might consider overloading
operator[] instead to provide a similar interface to valarray. For
valarray the code would be as follows:

#include <valarray>
#include <iostream>
#include <algorithm>
#include <iterator>
using std::valarray;

int main()
{
size_t Bvals[2] = {0, 2};
valarray<size_t> B(Bvals, 2);
double Avals[4] = {10, 3, 1, 0};
valarray<double> A(Avals, 4);
double Cvals[2] = {-10, -10};
valarray<double> C(Cvals, 2);
A[B] = C;
std::copy(&A[0], &A[0] + A.size(),
std::ostream_iterator<double>(std::cout, "n"));
}

Tom

[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]

Back to top
syro555
Guest





PostPosted: Fri Nov 19, 2004 4:54 pm    Post subject: Re: How to return pointers of specified positions of a vecto Reply with quote

KK wrote:

Quote:
Greetings,
I am building my own vector class (not the one provided in STL) and I
am faced with the challenge of returning pointers of some specified
positions of the vector.

For example,
If A,B,C are objects of class vector such that
A =[10 3 1 0];
B =[0 2];
C= [-10 -10];

I want to overload operator () so that upon the following operation
A(B)=C;

I should have, A=[-10 3 -10 0];

How can I achieve such a task?
Thank you for your time and consideration.

If haven't made a mistake, you could do it this way:

-----------

// your vector class
class Vec
{
//...

public:

//...

class Accessor
{
Vec &_vec;
const Vec &_positions;

public:

Accessor(Vec &vec, const Vec &pos)
: _vec(vec), _positions(pos)
{}

void operator = (const Vec &vec)
// the return value can be Accessor or Vector, but in your example
code it does not matter
{
// here, you have to replace positions _positions of vector _vec
// with values from vec
}
};

Accessor operator () (const Vec &positions)
{
return Accessor(*this, positions);
}
};

Greetings,
Luke

[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]

Back to top
Ahti Legonkov
Guest





PostPosted: Fri Nov 19, 2004 4:55 pm    Post subject: Re: How to return pointers of specified positions of a vecto Reply with quote

KK wrote:
Quote:
I am building my own vector class (not the one provided in STL) and I
am faced with the challenge of returning pointers of some specified
positions of the vector.

For example,
If A,B,C are objects of class vector such that
A =[10 3 1 0];
B =[0 2];
C= [-10 -10];

I want to overload operator () so that upon the following operation
A(B)=C;

I should have, A=[-10 3 -10 0];

How can I achieve such a task?
Thank you for your time and consideration.

Make the operator() return some object that will do the operation
you described when its operator=() is called.


class vector
{
public:
//...

class replacer
{
vector& m_modify;
vector const& m_indices;
replacer(vector& modify, vector const& indices)
: m_modify(modify)
, m_indices(indices) {}
replacer& operator=(vector const& newValue);
};

replacer operator()(vector const& indices)
{
return replacer(*this, indices);
}
};

vector::replacer& vector::replacer::operator=(vector const& newValues)
{
// n = 0
// for each index in m_indices
// m_modify[index] = vewValues[n++]
return *this;
}

--
Ahti Legonkov
leg zero at hot dot ee


[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]

Back to top
Thomas Mang
Guest





PostPosted: Sat Nov 20, 2004 4:13 pm    Post subject: Re: How to return pointers of specified positions of a vecto Reply with quote


"Bob Bell" <belvis (AT) pacbell (DOT) net> schrieb im Newsbeitrag
news:c87c1cfb.0411181729.1945d318 (AT) posting (DOT) google.com...
Quote:
kewlkarun (AT) yahoo (DOT) com (KK) wrote in message
news:<c8fd5039.0411171422.7e3aeb8c (AT) posting (DOT) google.com>...
Greetings,
I am building my own vector class (not the one provided in STL) and I
am faced with the challenge of returning pointers of some specified
positions of the vector.

For example,
If A,B,C are objects of class vector such that
A =[10 3 1 0];
B =[0 2];
C= [-10 -10];

I want to overload operator () so that upon the following operation
A(B)=C;

I should have, A=[-10 3 -10 0];

How can I achieve such a task?
Thank you for your time and consideration.
-KK

I have no idea why you would need such a thing, but here's a short
example that shows one way you can do it.

#include <iostream
#include
class VectorAssign;

class Vector : public std::vector public:
VectorAssign operator()(const Vector& iArg);
};


deriving publicly from std::vector is a very questionable construct.
While it might be the right thing to do, it can cause lots of confusion.

Storing the vector as data member, or using private inheritance is usually
much clearer and less error-prone.


Thomas



[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]


Back to top
Display posts from previous:   
Post new topic   Reply to topic    C++Talk.NET Forum Index -> C++ Language (Moderated) All times are GMT
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2006 phpBB Group
SEO toolkit © 2004-2006 webmedic.