Implementing Sets in Fortran - fortran

Is there a way to implement sets in Fortran? Like there is a set data type in python, or like there is the Set class in C++ STL.
This is what I mean:
C++ sets:
set <int, greater <int> > myset;
Python sets:
myset = set()
I have tried looking at a few books and other resources, but I have not found any mention of sets.

Related

Efficient way to do std::map comparison based on their values

I have two maps which I want to compare based on values. The maps are of type:
std::map<std::string, CustomMap> compareMap;
where CustomMap is:
std::map<std::string, Struct> CustomMap;
and Struct is a struct of two strings. This is how the design is and I can't change it.
How do I efficiently compare two compareMaps based on their values? The only way I can think of now is to use 2 for loops. Is there some inbuilt way to compare the maps based on their values? Or some way to calculate the hash on the values and find out which entries differ and which entries are the same?
EDIT: Let me rephrase my question: Is there a better elegant way to compare the maps based on their values instead of using two for-loops? I know the maps are different and some keys have the same values in both the maps. I only want to know which keys have different values in the two maps.
P.S: I don't have C++11

Sol2 unable to iterate over vector

I'm attempting to give a list of integers to a Lua script, and iterate over them.
The error I'm getting is:
test.lua:12: bad argument #1 to 'pairs' (table expected, got userdata)
stack traceback:
[C]: in function 'pairs'
test.lua:12: in main chunk
The C++ code being used:
#include <sol.hpp>
struct Test {
std::vector<int> a;
};
int main(void) {
sol::state lua;
lua.open_libraries(sol::lib::base);
lua.new_usertype<Test>("test",
"a", &Test::a);
lua.script_file("test.lua", [](lua_State* L, sol::protected_function_result pfr) {
sol::error err = pfr;
std::cout << err.what() << std::endl;
return pfr;
});
}
The Lua script:
t = test.new()
for i in pairs(t.a) do
print(i)
end
SO I know this is an old question, but I came across this problem myself when first learning how to do this so I thought I'd add this answer for anyone coming across this now.
The problem isn't with your C++ code, it's with your Lua code. Lua uses colons ( : ) instead of full stops ( . ) for member calls, so it should look like this:
t = test:new()
for k,v in pairs(t.a) do
print(v)
end
Emphasis on member calls, mind you. Properties are still accessed with full stops. If you have a user type CObject with the function ExFnc, you could call it on object ExampleObj either of the two following ways:
ExampleObj:ExFnc()
or
CObject.ExFnc(ExampleObj)
sol (at least the version I used, 3.0.2) provides a whole assortment of container mappings already included that just work right out of the box. You can find details for them here: sol 3.0.2 containers documentation
It lists automatic compatibility with the following types:
std::vector
std::deque
std::list
std::forward_list
std::array
std::set
std::multi_set
std::map
std::multi_map
std::unordered_set
std::unordered_multiset
std::unordered_map
std::unordered_multimap
Keep in mind though that NAMESPACES in sol are tables, you have to use full stops for those.
pairs is a Lua function that only takes a single variable of type table as input. t is of type userdata, hence the error.
https://www.lua.org/manual/5.3/manual.html#pdf-pairs
You have two options here:
implement a version of pairs that can handle your userdata type
copy all elements into a table befor you feed that into pairs
Also ask yourself if it makes any sense to use pairs on a vector. A numeric for loop would feel much more natural. I'd even prefer ipairs over pairs

Array, Vector, Map and List data searching

I'm currently working on a C++ school assignment and I have 3 object types. Customer, Hire and Tour. Customer is liked to Tour and Hire. Requirement is to use Array, Vector, Map and List to hold this information based on users's selection of the data structure type. There are data files with 1000s of records and the application will read them and create the necessary objects. For a example if the user selects vector, it'll create 3 vectors containing above objects. Then following operations will be preformed on them.
load in the large datasets we have provided for you. If the
datastructure you are using supports sorting, it should be sorted by
description.
Prepare a list of customers whose have booked a tour that will occur
before the end of the year
Prepare a list of tours booked by customers who owe us more than
$2000, sorted by the date their account is due
Prepare a list of Hires by customers whose postcodes begin with a 5
I have following in my main application header file.
private:
string structureType;
Customer** customerListArray;
Tour** tourListArray;
EquipmentHire** equipmentsListArray;
vector<Customer *> customerListVector;
vector<Tour *> tourListVector;
vector<EquipmentHire *> equipmentsListVector;
std::map<string, Customer*> customerListMap;
std::map<string, Tour*> tourListMap;
std::map<string, EquipmentHire*> equipmentsListMap;
list<Customer *> customerListList;
list<Tour *> tourListList;
list<EquipmentHire *> equipmentsListList;
Then I load data on to those objects based on the user's selection. However my question is, Do I need to write different functions for each type of data structures to preform above operations, or is there a common interface I can use on all of them?
My C++ knowledge is very limited and requirement is to use C++98.
Thank you.
The common interface is "STL container." This is not a strong interface defined in C++, but is a concept that you can rely on all STL containers to implement. You can therefore use templates to write code once that will apply to all of them.
(Note that maps and vectors differ in that maps have std::pair<> as their element type. In practice this can be worked around by providing "identity" and "map" selectors to your search function.)

c++ set formation from two other sets

I Have two sets of strings.
set<string> A;
set<string> B;
I will insert some elements into both the sets and some of the elements that I insert into them are common elements.
I can basically get the elements
Present in A and not in B
Present in B and not in A
using
std::set_difference
I can also get the common elements in both the sets by
using
std::set_intersection
How can I get the elements which and present only in A and only in B and put them in a different set?
Total idea is creating a set which will not have the common elements but all the elements in both the sets.
In mathematical language:
(A(UNION)B)-(A(intersection)B)
How about std::set_symmetric_difference? It seems to fit what you want.
You can use set_symmetric_difference

C++ making Hash of Arrays

I have a problem with the creation of a hash of arrays. I need a Single Key - Multi Data system:
multimap <Type, vector<type> > var;
But how I can add elements to the vector?
Example: key = 3;
Now I need to append some elements into the vector whose key is 3.
Creating a temp-vector not an answer because I don't know when I need to input element into the vector with the current key.
sorry, understand my problem. i need fast-access struct, that will be operate with ~50,000 words with length ~20 each.
and i need something like tree.
also, have question:
how quick STL-structures, like vector,map,multimap and other?
What's wrong with std::map <KeyType, std::vector<SomeType> >, or some other collection as the value type? This gives you control over how to operate on the value collection. A multimap to me seems like a low-level form of std::map <KeyType, std::list<SomeType> >, but with none of the flexibility of a list.
To find the answer to your question you can look at the slides under point 6. at this site https://ece.uwaterloo.ca/~ece250/Lectures/Slides/
Hope that helps!