473,320 Members | 1,946 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

Using transform() in a function

I have a question about using the STL transform algorithm in a function.

What I want to do is define a group of array classes to represent
APL-style arrays (arrays in which the number of dimensions and the
length of any dimension can be changed at any time). What I currently
plan is to have an abstract base class at the top, to allow
polymorphism, e.g.:

#include "basedefs.h" // Basic data types, e.g. typedef long Integer

typedef vector<Integer> Dimensions;

class Array {
private:
Dimensions shape;

public:
bool IsScalar() { return shape.empty(); }

...
};

and then have a child template array class so that I can have arrays of
characters, booleans, integers, doubles, and pointers (to support arrays
of arrays). The data would be stored as vector<type>, since Bjarne
Stroustrup does not recommend using valarrays for arrays intended to
vary in size after they are created, with sets of array indices resolved
into single indices into the vector as needed, possibly by implementing
a vector version of gslice and so on.

Now, I want to be able to perform a number of operations on these
arrays, following some simple rules regarding compatible dimensions.
For example, for numeric arrays, I want to be able to add two arrays to
produce a new array. For example, add(a, b) would add corresponding
elements of arrays a and b. I define a function to check if the arrays
have identical dimensions, so we know what elements to match up:

bool Conform (const Array &l, const Array &r) {
return l.shape == r.shape;
}

Then, to support a wide variety of binary operations using the same
shape rules, I would like to do something like this:

Array ScalarDyadic (const Array &l, const Array &r, BinaryFunction op) {
Array a;

if (l.IsScalar()) {
transform (r.start(), r.end(), a.start(), bind1st(op, *l.start()));
}
else if (r.IsScalar()) {
transform (l.start(), l.end(), a.start(), bind2nd(op, *r.start()));
}
else if (Conform (l, r)) {
transform (l.start(), l.end(), r.start(), a.start(), op);
}
else throw invalid_dimensions;

return a;
}

But I am not sure how to declare the third (operation) argument,
presumably a functor, so I can pass it to transform(). The STL
documentation seems to imply that there is a general functor type
called BinaryFunction, but I do not see this in the STL header files,
and I have seen no indications that the functors used have to be
derived from a standard class. On the other hand, I could probably
define the arguent as a template, but then I would end up creating a
large number of copies of the ScalarDyadic() function, one for each
operation. In that case it would probably be more efficient to pass
pointers to binary functions in order to avoid using a template, but I
don't know if transform() would support that.

I would appreciate suggestions.

Thanks.

--- Brian
Jul 22 '05 #1
1 2223

"Brian McGuinness" <br****************@lmco.com> wrote in message
news:3f**************************@posting.google.c om...
I have a question about using the STL transform algorithm in a function.
[snip]

But I am not sure how to declare the third (operation) argument,
presumably a functor, so I can pass it to transform(). The STL
documentation seems to imply that there is a general functor type
called BinaryFunction, but I do not see this in the STL header files,
and I have seen no indications that the functors used have to be
derived from a standard class.
It's called std::binary_function and its in the <functional> header file.
But it isn't for deriving from in the way that you are thinking. The purpose
of deriving from std::binary_function is to define certain typedefs such as
result_type.
On the other hand, I could probably
define the arguent as a template, but then I would end up creating a
large number of copies of the ScalarDyadic() function, one for each
operation.
I think that is what you are going to have to do.
In that case it would probably be more efficient to pass
pointers to binary functions in order to avoid using a template, but I
don't know if transform() would support that.


A pointer to a binary function is fine for std::transform, but it is likely
to be less efficient not more. Pointers to functions are potentially less
efficient than functors because they cannot be inlined.

john
Jul 22 '05 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: Michael Sparks | last post by:
Anyway... At Europython Guido discussed with everyone the outstanding issue with decorators and there was a clear majority in favour of having them, which was good. From where I was sitting it...
20
by: Steffen Brinkmann | last post by:
Hi! I tried to modify the transform algorithm in a way that it doesn't take iterators, but a reference to a container class and a value, because Mostly I need to do an operation of a container...
12
by: Mark Constant | last post by:
I have a drop-down list now and I got it so when something is selected from the drop down list it calls a JavaScript function. I want it so the value selected from the drop-down list is sent as a...
0
by: Frank | last post by:
Hey all, I can't seem to get javascript running in my XSL document. <?xml version="1.0"?> <xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0"...
2
by: John Lehmann | last post by:
I have an interesting problem. I am performing an XSL transform using the System.Xml.Xsl.Transform class. I have a database that contains the XSL style sheet string. And it seems to work pretty...
11
by: TheDD | last post by:
Hello, i don't manage to use the tolower() function: #include <algorithm> #include <iostream> #include <locale> #include <string> using std::cout;
9
by: Patrick Guio | last post by:
Dear all, I am trying to use the std::transform algorithm to to the following vector< vector<char> >::iterator ik = keys.begin(); // key list iterator vector< vector<char> >::iterator is = ik;...
3
by: Rob | last post by:
All I want to do is execute a simple transformation in VB.net.... I know this has to be simple. I tried the following as suggested by a web page I found.... Dim xslt as New XslTransform() ...
19
by: Taras_96 | last post by:
Hi all, A poster at http://bytes.com/forum/thread60652.html implies that using strtoupper in transform doesn't work because ctype.h may define strtoupper as a macro: "The problem is that most...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
0
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.