473,769 Members | 2,100 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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_dimensi ons;

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 2256

"Brian McGuinness" <br************ ****@lmco.com> wrote in message
news:3f******** *************** ***@posting.goo gle.com...
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_fun ction 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_fun ction 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
2072
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 looked like about 20:20 split on the following syntaxes: 1 def func(arg1, arg2, arg3) : function... 2 def func(arg1, arg2, arg3): function...
20
3853
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 and a single number (e.g. multiply all the values in a vector by 3 or so). So, this is my intent: #ifndef ALGORITHM_EXT_HH #define ALGORITHM_EXT_HH
12
6083
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 parameter to the xslt file. When I select a value from the drop-down list nothing appears. I want just a basic listing of Title, Image, and Description outputed. I have done a basic JavaScript that uses the .write() function and that worked so the...
0
1944
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" xmlns:xalan="http://xml.apache.org/xalan" xmlns:my-ext="ext1" extension-element-prefixes="my-ext"> <!--The component and its script are in the xalan namespace and define the
2
1024
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 well for simple transforms. But as soon as I add Xsl variables or For each loops to the XSL string in the db, it fails to transform the XML. I can see that it will transform everything until that point. ALso If I copy the XSL & XML I am trying to...
11
3362
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
808
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; // save begin vector<char>::const_iterator i = ik->begin(); vector<char>::const_iterator e = ik->end(); vector<char>::iterator s = is->begin();
3
5084
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() xslt.Load("Filename") xslt.Transform("InFile", "ResultFile")
19
3253
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 implementations of the standard C <ctype.h> header define functions like toupper/tolower/etc as macros. To make it work in STL algorithms, you have to include <cctypeheader instead of <ctype.h>. At least on my PC (Debian/gcc 3.3),...
0
9423
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10215
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
9996
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9865
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
8872
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7410
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5307
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
2
3564
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2815
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.