Compiling the following code gives the error message: type illegal.
int main()
{
// Compilation error - switch expression of type illegal
switch(std::string("raj"))
{
case"sda":
}
}
You cannot use string in either switch or case. Why? Is there any solution that works nicely to support logic similar to switch on strings?
The reason why has to do with the type system. C/C++ doesn't really support strings as a type. It does support the idea of a constant char array but it doesn't really fully understand the notion of a string.
In order to generate the code for a switch statement the compiler must understand what it means for two values to be equal. For items like ints and enums, this is a trivial bit comparison. But how should the compiler compare 2 string values? Case sensitive, insensitive, culture aware, etc ... Without a full awareness of a string this cannot be accurately answered.
Additionally, C/C++ switch statements are typically generated as branch tables. It's not nearly as easy to generate a branch table for a string style switch.
As mentioned previously, compilers like to build lookup tables that optimize switch statements to near O(1) timing whenever possible. Combine this with the fact that the C++ Language doesn't have a string type - std::string is part of the Standard Library which is not part of the Language per se.
I will offer an alternative that you might want to consider, I've used it in the past to good effect. Instead of switching over the string itself, switch over the result of a hash function that uses the string as input. Your code will be almost as clear as switching over the string if you are using a predetermined set of strings:
enum string_code {
eFred,
eBarney,
eWilma,
eBetty,
...
};
string_code hashit (std::string const& inString) {
if (inString == "Fred") return eFred;
if (inString == "Barney") return eBarney;
...
}
void foo() {
switch (hashit(stringValue)) {
case eFred:
...
case eBarney:
...
}
}
There are a bunch of obvious optimizations that pretty much follow what the C compiler would do with a switch statement... funny how that happens.
C++
constexpr hash function:
constexpr unsigned int hash(const char *s, int off = 0) {
return !s[off] ? 5381 : (hash(s, off+1)*33) ^ s[off];
}
switch( hash(str) ){
case hash("one") : // do something
case hash("two") : // do something
}
Update:
The example above is C++11. There constexpr function must be with single statement. This was relaxed in next C++ versions.
In C++14 and C++17 you can use following hash function:
constexpr uint32_t hash(const char* data, size_t const size) noexcept{
uint32_t hash = 5381;
for(const char *c = data; c < data + size; ++c)
hash = ((hash << 5) + hash) + (unsigned char) *c;
return hash;
}
Also C++17 have std::string_view, so you can use it instead of const char *.
In C++20, you can try using consteval.
C++ 11 update of apparently not #MarmouCorp above but http://www.codeguru.com/cpp/cpp/cpp_mfc/article.php/c4067/Switch-on-Strings-in-C.htm
Uses two maps to convert between the strings and the class enum (better than plain enum because its values are scoped inside it, and reverse lookup for nice error messages).
The use of static in the codeguru code is possible with compiler support for initializer lists which means VS 2013 plus. gcc 4.8.1 was ok with it, not sure how much farther back it would be compatible.
/// <summary>
/// Enum for String values we want to switch on
/// </summary>
enum class TestType
{
SetType,
GetType
};
/// <summary>
/// Map from strings to enum values
/// </summary>
std::map<std::string, TestType> MnCTest::s_mapStringToTestType =
{
{ "setType", TestType::SetType },
{ "getType", TestType::GetType }
};
/// <summary>
/// Map from enum values to strings
/// </summary>
std::map<TestType, std::string> MnCTest::s_mapTestTypeToString
{
{TestType::SetType, "setType"},
{TestType::GetType, "getType"},
};
...
std::string someString = "setType";
TestType testType = s_mapStringToTestType[someString];
switch (testType)
{
case TestType::SetType:
break;
case TestType::GetType:
break;
default:
LogError("Unknown TestType ", s_mapTestTypeToString[testType]);
}
The problem is that for reasons of optimization the switch statement in C++ does not work on anything but primitive types, and you can only compare them with compile time constants.
Presumably the reason for the restriction is that the compiler is able to apply some form of optimization compiling the code down to one cmp instruction and a goto where the address is computed based on the value of the argument at runtime. Since branching and and loops don't play nicely with modern CPUs, this can be an important optimization.
To go around this, I am afraid you will have to resort to if statements.
std::map + C++11 lambdas pattern without enums
unordered_map for the potential amortized O(1): What is the best way to use a HashMap in C++?
#include <functional>
#include <iostream>
#include <string>
#include <unordered_map>
#include <vector>
int main() {
int result;
const std::unordered_map<std::string,std::function<void()>> m{
{"one", [&](){ result = 1; }},
{"two", [&](){ result = 2; }},
{"three", [&](){ result = 3; }},
};
const auto end = m.end();
std::vector<std::string> strings{"one", "two", "three", "foobar"};
for (const auto& s : strings) {
auto it = m.find(s);
if (it != end) {
it->second();
} else {
result = -1;
}
std::cout << s << " " << result << std::endl;
}
}
Output:
one 1
two 2
three 3
foobar -1
Usage inside methods with static
To use this pattern efficiently inside classes, initialize the lambda map statically, or else you pay O(n) every time to build it from scratch.
Here we can get away with the {} initialization of a static method variable: Static variables in class methods , but we could also use the methods described at: static constructors in C++? I need to initialize private static objects
It was necessary to transform the lambda context capture [&] into an argument, or that would have been undefined: const static auto lambda used with capture by reference
Example that produces the same output as above:
#include <functional>
#include <iostream>
#include <string>
#include <unordered_map>
#include <vector>
class RangeSwitch {
public:
void method(std::string key, int &result) {
static const std::unordered_map<std::string,std::function<void(int&)>> m{
{"one", [](int& result){ result = 1; }},
{"two", [](int& result){ result = 2; }},
{"three", [](int& result){ result = 3; }},
};
static const auto end = m.end();
auto it = m.find(key);
if (it != end) {
it->second(result);
} else {
result = -1;
}
}
};
int main() {
RangeSwitch rangeSwitch;
int result;
std::vector<std::string> strings{"one", "two", "three", "foobar"};
for (const auto& s : strings) {
rangeSwitch.method(s, result);
std::cout << s << " " << result << std::endl;
}
}
To add a variation using the simplest container possible (no need for an ordered map)... I wouldn't bother with an enum--just put the container definition immediately before the switch so it'll be easy to see which number represents which case.
This does a hashed lookup in the unordered_map and uses the associated int to drive the switch statement. Should be quite fast. Note that at is used instead of [], as I've made that container const. Using [] can be dangerous--if the string isn't in the map, you'll create a new mapping and may end up with undefined results or a continuously growing map.
Note that the at() function will throw an exception if the string isn't in the map. So you may want to test first using count().
const static std::unordered_map<std::string,int> string_to_case{
{"raj",1},
{"ben",2}
};
switch(string_to_case.at("raj")) {
case 1: // this is the "raj" case
break;
case 2: // this is the "ben" case
break;
}
The version with a test for an undefined string follows:
const static std::unordered_map<std::string,int> string_to_case{
{"raj",1},
{"ben",2}
};
// in C++20, you can replace .count with .contains
switch(string_to_case.count("raj") ? string_to_case.at("raj") : 0) {
case 1: // this is the "raj" case
break;
case 2: // this is the "ben" case
break;
case 0: //this is for the undefined case
}
In C++ and C switches only work on integer types. Use an if else ladder instead. C++ could obviously have implemented some sort of swich statement for strings - I guess nobody thought it worthwhile, and I agree with them.
Why not? You can use switch implementation with equivalent syntax and same semantics.
The C language does not have objects and strings objects at all, but
strings in C is null terminated strings referenced by pointer.
The C++ language have possibility to make overload functions for
objects comparision or checking objects equalities.
As C as C++ is enough flexible to have such switch for strings for C
language and for objects of any type that support comparaison or check
equality for C++ language. And modern C++11 allow to have this switch
implementation enough effective.
Your code will be like this:
std::string name = "Alice";
std::string gender = "boy";
std::string role;
SWITCH(name)
CASE("Alice") FALL
CASE("Carol") gender = "girl"; FALL
CASE("Bob") FALL
CASE("Dave") role = "participant"; BREAK
CASE("Mallory") FALL
CASE("Trudy") role = "attacker"; BREAK
CASE("Peggy") gender = "girl"; FALL
CASE("Victor") role = "verifier"; BREAK
DEFAULT role = "other";
END
// the role will be: "participant"
// the gender will be: "girl"
It is possible to use more complicated types for example std::pairs or any structs or classes that support equality operations (or comarisions for quick mode).
Features
any type of data which support comparisions or checking equality
possibility to build cascading nested switch statemens.
possibility to break or fall through case statements
possibility to use non constatnt case expressions
possible to enable quick static/dynamic mode with tree searching (for C++11)
Sintax differences with language switch is
uppercase keywords
need parentheses for CASE statement
semicolon ';' at end of statements is not allowed
colon ':' at CASE statement is not allowed
need one of BREAK or FALL keyword at end of CASE statement
For C++97 language used linear search.
For C++11 and more modern possible to use quick mode wuth tree search where return statement in CASE becoming not allowed.
The C language implementation exists where char* type and zero-terminated string comparisions is used.
Read more about this switch implementation.
I think the reason is that in C strings are not primitive types, as tomjen said, think in a string as a char array, so you can not do things like:
switch (char[]) { // ...
switch (int[]) { // ...
In c++ strings are not first class citizens. The string operations are done through standard library. I think, that is the reason. Also, C++ uses branch table optimization to optimize the switch case statements. Have a look at the link.
http://en.wikipedia.org/wiki/Switch_statement
Late to the party, here's a solution I came up with some time ago, which completely abides to the requested syntax.
#include <uberswitch/uberswitch.hpp>
int main()
{
uswitch (std::string("raj"))
{
ucase ("sda"): /* ... */ break; //notice the parenthesis around the value.
}
}
Here's the code: https://github.com/falemagn/uberswitch
You could put the strings in an array and use a constexpr to convert them to indices at compile time.
constexpr const char* arr[] = { "bar", "foo" };
constexpr int index(const char* str) { /*...*/ }
do_something(std::string str)
{
switch(quick_index(str))
{
case index("bar"):
// ...
break;
case index("foo"):
// ...
break;
case -1:
default:
// ...
break;
}
For quick_index, which doesn't have to be constexpr, you could e.g. use an unordered_map to do it O(1) at runtime. (Or sort the array and use binary search, see here for an example.)
Here's a full example for C++11, with a simple custom constexpr string comparer. Duplicate cases and cases not in the array (index gives -1) will be detected at compile time. Missing cases are obviously not detected. Later C++ versions have more flexible constexpr expressions, allowing for simpler code.
#include <iostream>
#include <algorithm>
#include <unordered_map>
constexpr const char* arr[] = { "bar", "foo", "foobar" };
constexpr int cmp(const char* str1, const char* str2)
{
return *str1 == *str2 && (!*str1 || cmp(str1+1, str2+1));
}
constexpr int index(const char* str, int pos=0)
{
return pos == sizeof(arr)/sizeof(arr[0]) ? -1 : cmp(str, arr[pos]) ? pos : index(str,pos+1);
}
int main()
{
// initialize hash table once
std::unordered_map<std::string,int> lookup;
int i = 0;
for(auto s : arr) lookup[s] = i++;
auto quick_index = [&](std::string& s)
{ auto it = lookup.find(s); return it == lookup.end() ? -1 : it->second; };
// usage in code
std::string str = "bar";
switch(quick_index(str))
{
case index("bar"):
std::cout << "bartender" << std::endl;
break;
case index("foo"):
std::cout << "fighter" << std::endl;
break;
case index("foobar"):
std::cout << "fighter bartender" << std::endl;
break;
case -1:
default:
std::cout << "moo" << std::endl;
break;
}
}
hare's comment to Nick's solution is really cool. here the complete code example (in C++11):
constexpr uint32_t hash(const std::string& s) noexcept
{
uint32_t hash = 5381;
for (const auto& c : s)
hash = ((hash << 5) + hash) + (unsigned char)c;
return hash;
}
constexpr inline uint32_t operator"" _(char const* p, size_t) { return hash(p); }
std::string s = "raj";
switch (hash(s)) {
case "sda"_:
// do_something();
break;
default:
break;
}
In C++ you can only use a switch statement on int and char
cout << "\nEnter word to select your choice\n";
cout << "ex to exit program (0)\n";
cout << "m to set month(1)\n";
cout << "y to set year(2)\n";
cout << "rm to return the month(4)\n";
cout << "ry to return year(5)\n";
cout << "pc to print the calendar for a month(6)\n";
cout << "fdc to print the first day of the month(1)\n";
cin >> c;
cout << endl;
a = c.compare("ex") ?c.compare("m") ?c.compare("y") ? c.compare("rm")?c.compare("ry") ? c.compare("pc") ? c.compare("fdc") ? 7 : 6 : 5 : 4 : 3 : 2 : 1 : 0;
switch (a)
{
case 0:
return 1;
case 1: ///m
{
cout << "enter month\n";
cin >> c;
cout << endl;
myCalendar.setMonth(c);
break;
}
case 2:
cout << "Enter year(yyyy)\n";
cin >> y;
cout << endl;
myCalendar.setYear(y);
break;
case 3:
myCalendar.getMonth();
break;
case 4:
myCalendar.getYear();
case 5:
cout << "Enter month and year\n";
cin >> c >> y;
cout << endl;
myCalendar.almanaq(c,y);
break;
case 6:
break;
}
More functional workaround to the switch problem:
class APIHandlerImpl
{
// define map of "cases"
std::map<string, std::function<void(server*, websocketpp::connection_hdl, string)>> in_events;
public:
APIHandlerImpl()
{
// bind handler method in constructor
in_events["/hello"] = std::bind(&APIHandlerImpl::handleHello, this, _1, _2, _3);
in_events["/bye"] = std::bind(&APIHandlerImpl::handleBye, this, _1, _2, _3);
}
void onEvent(string event = "/hello", string data = "{}")
{
// execute event based on incomming event
in_events[event](s, hdl, data);
}
void APIHandlerImpl::handleHello(server* s, websocketpp::connection_hdl hdl, string data)
{
// ...
}
void APIHandlerImpl::handleBye(server* s, websocketpp::connection_hdl hdl, string data)
{
// ...
}
}
You can use switch on strings.
What you need is table of strings, check every string
char** strings[4] = {"Banana", "Watermelon", "Apple", "Orange"};
unsigned get_case_string(char* str, char** _strings, unsigned n)
{
while(n)
{
n--
if(strcmp(str, _strings[n]) == 0) return n;
}
return 0;
}
unsigned index = get_case_string("Banana", strings, 4);
switch(index)
{
case 1: break;/*Found string `Banana`*/
default: /*No string*/
}
You can't use string in switch case.Only int & char are allowed. Instead you can try enum for representing the string and use it in the switch case block like
enum MyString(raj,taj,aaj);
Use it int the swich case statement.
That's because C++ turns switches into jump tables. It performs a trivial operation on the input data and jumps to the proper address without comparing. Since a string is not a number, but an array of numbers, C++ cannot create a jump table from it.
movf INDEX,W ; move the index value into the W (working) register from memory
addwf PCL,F ; add it to the program counter. each PIC instruction is one byte
; so there is no need to perform any multiplication.
; Most architectures will transform the index in some way before
; adding it to the program counter
table ; the branch table begins here with this label
goto index_zero ; each of these goto instructions is an unconditional branch
goto index_one ; of code
goto index_two
goto index_three
index_zero
; code is added here to perform whatever action is required when INDEX = zero
return
index_one
...
(code from wikipedia https://en.wikipedia.org/wiki/Branch_table)
in many cases you can avid extra work by pulling the first char from the string and switching on that. may end up having to do a nested switch on charat(1) if your cases start with the same value. anyone reading your code would appreciate a hint though because most would prob just if-else-if
Switches only work with integral types (int, char, bool, etc.). Why not use a map to pair a string with a number and then use that number with the switch?
Related
Is there a way in C# to extend a function like following ?
void foo()
{
// function body, instructions
return;
}
and then somewhere else in the code
foo += (x) => { // some functionality }
Which in practice would add the lambda functionality to be fired at the end of a foo() execution.
What would be the name and the internal details of such mechanism ?
Is something of this nature possible in C++?
Your example won't compile with the message :
error CS1656: Cannot assign to 'foo' because it is a 'method group'
It's not possible to do stuff like that. What you saw was probably operator += for events. Here's an example with a lambda.
Regarding C++
It's not possible to add a call to the end of a different function (the same as for C#). But you can implement events and overload operator += for your events to accept pointers to functions.
It is just a possibility just to show you that a one minute trial can give you an insight. But as Kirill Daybov mentioned in one of his comment I would encourage you to google delegate c++, you will find articles with much stronger technical hints
#include <iostream>
#include <list>
#include <functional> //c++11
using namespace std;
//For exercice only, C++11 required
template<typename U> class naive_delegate
{
list<function<void(U)>>_list;
public:
naive_delegate<U> & operator+= (function<void(U)> && fref)
{ _list.push_back(fref); return *this;}
void operator()(U && input_param)
{
if (_list.empty() )
cout << "Nothing to do for call of delegate with param " << input_param << endl;
else
for ( const auto & elem : _list)
elem(input_param);
}
};
void anwser(int i) { cout << "The answer is " << i << endl; }
int main()
{
naive_delegate<int> ndel;
ndel(1);
ndel += [](int i) { cout << "What is the answer ? " << endl; };
ndel += anwser;
ndel(42);
return 0;
}
The results are
Nothing to do for call of delegate with param 1
What is the answer ?
The answer is 42
Note that among others, I am not able to treat removal (-=) ...
This is C++ code for reading traces of address of main memory for cache memory simulation:
char hex[20];
ifstream infile;
infile.open(filename,ios::in);
if(!infile) {
cout<<"Error! File not found...";
exit(0);
}
int set, tag, found;
while(!infile.eof()) { //Reading each address from trace file
if(base!=10) {
infile>>hex;
address = changebase(hex, base);
} else {
infile>>address;
}
set = (address / block_size) % no_set;
tag = address / (block_size * no_set);
}
I have converted this to C# code:
char[] hex = new char[20];
FileStream infile=new FileStream(filename, FileMode.Open);
if (infile == null) {
Console.Write("Error! File not found...");
Environment.Exit(0);
}
int set;
int tag;
int found;
while (!infile.CanRead) { //Reading each address from trace file
if (#base != 10) {
infile >> hex;
address = changebase(hex, #base);
} else {
infile >> address;
}
set = (address / block_size) % no_set;
tag = address / (block_size * no_set);
}
The problem is on line infile >> hex;
C# is giving syntax errors, as shift right operator cannot be applied to string operators.
Why this is not working? I'm making a small cache hit and miss calculation project.
To quantify what Eric means:
C++ is quite flexible in the operators that can be overloaded. It has become an "idiom" that the bitshift operators << and >> also be used for input and output. This actually makes kind of sense as it is a logical construct and the eye registers some kind of "flow" between objects.
In C#, you don't overload those operators. What Eric means is, you need to say explicitly, on a stream object, to write (or indeed, read) something. This means calling the methods directly.
In essence you're doing the same thing - the operator overloading is just a nice shortcut, but at the end of the day some method is going to be called - be it a nice decorative "operator overload" or a plain old function call with a name.
So, in C++ we might write:
std::cout << "Hello" << std::endl;
Whereas in C# we'd write:
Console.WriteLine("Hello");
If we ignore the fact that std::cout could potentially be different from the console window (this is illustrative), the concept is exactly the same.
To expand on the idea of the operators, you'll also have probably come across things such as stringstream.. a class that acts like a stream for strings. It's really quite useful:
std::stringstream ss;
int age = 25;
ss << "So you must be " << age << " years old.";
In C#, we achieve this with the StringBuilder class:
StringBuilder sb = new StringBuilder();
int age = 25;
sb.Append("So you must be ").Append(age).Append(" years old");
They both do exactly the same thing. We could also do:
sb.AppendFormat("So you must be {0} years old", age);
This is more akin (in my opinion) to the more C-like sprintf methods, or more recently, boost's format library.
C# does not use the bizarre C++ convention that bitshifting also means stream manipulation. You'll have to actually call methods for I/O.
I'm trying to write a CPU emulator in C#. The machine's object looks like this:
class Machine
{
short a,b,c,d; //these are registers.
short[] ram=new short[0x10000]; //RAM organised as 65536 16-bit words
public void tick() { ... } //one instruction is processed
}
When I execute an instruction, I have a switch statement which decides what the result of the instruction will be stored in (either a register or a word of RAM)
I want to be able to do this:
short* resultContainer;
if (destination == register)
{
switch (resultSymbol) //this is really an opcode, made a char for clarity
{
case 'a': resultContainer=&a;
case 'b': resultContainer=&b;
//etc
}
}
else
{
//must be a place in RAM
resultContainer = &RAM[location];
}
then, when I've performed the instruction, I can simply store the result like:
*resultContainer = result;
I've been trying to figure out how to do this without upsetting C#.
How do I accomplish this using unsafe{} and fixed(){ } and perhaps other things I'm not aware of?
This is how I would implement it:
class Machine
{
short a, b, c, d;
short[] ram = new short[0x10000];
enum AddressType
{
Register,
DirectMemory,
}
// Gives the address for an operand or for the result.
// `addressType`and `addrCode` are extracted from instruction opcode
// `regPointers` and `ramPointer` are fixed pointers to registers and RAM.
private unsafe short* GetAddress(AddressType addressType, short addrCode, short*[] regPointers, short* ramPointer)
{
switch (addressType)
{
case AddressType.Register:
return regPointers[addrCode]; //just an example implementation
case AddressType.DirectMemory:
return ramPointer + addrCode; //just an example implementation
default:
return null;
}
}
public unsafe void tick()
{
fixed (short* ap = &a, bp = &b, cp = &c, dp = &d, ramPointer = ram)
{
short*[] regPointers = new short*[] { ap, bp, cp, dp };
short* pOperand1, pOperand2, pResult;
AddressType operand1AddrType, operand2AddrType, resultAddrType;
short operand1AddrCode, operand2AddrCode, resultAddrCode;
// ... decipher the instruction and extract the address types and codes
pOperand1 = GetAddress(operand1AddrType, operand1AddrCode, regPointers, ramPointer);
pOperand2 = GetAddress(operand2AddrType, operand2AddrCode, regPointers, ramPointer);
pResult = GetAddress(resultAddrType, resultAddrCode, regPointers, ramPointer);
// execute the instruction, using `*pOperand1` and `*pOperand2`, storing the result in `*pResult`.
}
}
}
To get the address of the registers and RAM array, you have to use the fixed statement. Also you can only use the acquired pointers in the fixed block. So you have to pass around the pointers.
What if we look at the problem from another view? Execute the instruction and store the result in a variable (result), and then decide on where you should put the result. Doesn't work?
A good option, one I had planned for DevKit but not yet implemented, is to generate your emulation code. It's a time-honoured, tried and tested solution. For every opcode/register combination, or some efficient subset, generate the code for ADD A,B and SUB X,Y separately. You can use a bitmask to grab the specific case you're looking for and simply execute the correct code. Compiler should be able to use a jump table under the hood, and there's no lookups, no conditionals, should be pretty efficient.
I need to write something like
switch (nameOfType)
{
case "burk":
return "zzzz";
in my c++ DLL (I need this to compare type names)
Where nameOfType is a string that came from c# (via DLLImport)
but I am quite new in c++ - what type I must use to operate in c++ with strings the same way as it is in c#?
The simplest strings in C/C++ are NULL terminated character arrays. You can normally marshal a managed string from C# into a const char* type.
The code you posted will not work in C++. The switch statement in C++ only permits integral types as the operand. The simplest way to get what you want is repeated if:
if (strcmp(nameOfType, "burk") == 0)
return "zzzz";
else if (strcmp(nameOfType, "xyz") == 0)
return "yyyy";
else ...
If you need more string functionality, you should consider using the std::string class. It supports the normal searching, comparison, inserting and substring operations.
You cannot use char* in switch statements in C++ like C#. One thing you can do is replace it with an enum
enum StringEnum { burk , foo , bar };
map<string,StringNum> m;
m["burk"] = burk;
m["foo"] = foo;
m["bar"] = bar;
Now you can use a switch statement like below
StringEnum e = m[nameOfType];
switch(e)
{
case bruk;
etc etc
In python I have the following:
graph = {}
graph[1] = {}
graph[2] = {}
graph[3] = {}
graph[1][3] = graph[3]
graph[2][1] = graph[1]
graph[2][3] = graph[3]
graph[3][2] = graph[2]
this is a structure to represent a graph and that I find nice because its structure is the same as the one of one of it's nodes so I can use it directly to initiate a search (as in depth-first). The printed version of it is:
{1: {3: {2: {1: {...}, 3: {...}}}}, 2: {1: {3: {2: {...}}}, 3: {2: {...}}}, 3: {
2: {1: {3: {...}}, 3: {...}}}}
And it can be used like:
graph[1][3][2][3][2][1][3][2][1][3][2].keys()
Now, I'm curious to know how would one implement it in C++, C# and Java without resorting to "Object" tricks that would fill the code with ugly casts. For C++ I was thinking in templatemeta programming but that would generate "finite data types" when what is needed is something like
map<int,map<int,...>> or map<int,...>
In Java, I would go with a Node class which represents any node of a graph.
public class Node<T> {
private List<Node<T>> children = new ArrayList<Node<T>>();
private T value;
public Node(T value) {
this.value = value;
}
public void addChild(Node<T> child) {
this.children.add(child);
}
public Node<T> getChild(int index) {
return this.children.get(index);
}
public List<Node<T>> getChildren() {
return this.children;
}
public T getValue() {
return this.value;
}
}
If you want a graph that will contain int values you can instantiate it and use it with:
Node<Integer> graph = new Node<Integer>(10); //value of the first node is 10
graph.addChild(new Node<Integer>(-3));
graph.getChild(0).addChild(new Node<Integer>(5));
System.out.println(graph.getChild(0).getChild(0).getValue()); //prints 5
Here's a simple hack:
#include <map>
#include <memory>
struct GraphNode
{
std::map<std::size_t, std::unique_ptr<GraphNode>> m;
GraphNode & operator[](std::size_t i)
{
if (!m[i]) { m[i].reset(new GraphNode); }
return *m[i];
}
};
We add some ostream overloads for printing:
#include <prettyprint.hpp>
std::ostream & operator<<(std::ostream & o, GraphNode const & g)
{
if (g.m.empty()) return o << "*";
return o << g.m;
}
std::ostream & operator<<(std::ostream & o, std::unique_ptr<GraphNode> const & p)
{
if (!p) return o << "*";
return o << *p;
}
Example usage:
#include <iostream>
int main()
{
GraphNode n;
n[2] = GraphNode();
n[4] = GraphNode();
n[2][3] = GraphNode();
n[2][8] = GraphNode();
n[2][1] = GraphNode();
std::cout << n << std::endl;
}
Prints: [(2, [(1, *), (3, *), (8, *)]), (4, *)]
Further features are easily added; at the moment it's not clear to me whether you want all nodes to also support satellite data ("values"), or whether only leaf nodes can have values, or if there isn't any additional data.
For storing either further graphs or "terminal" values (actually, both of these approaches generalize to arbitarily many types with any interpretation, as long as they can be enumerated at compiletime), you use either:
Unions (possibly discriminated), or
Polymorphism (specifically, subtype polymorphism)
In either case, you have a type Graph behind which you can hide both nested graphs and stored values.
In C++ specifically, you'd probably use the former a union or Boost::Variant (more typesafe and convenient to handle). You may need to forward-declare the class so it's visible at the time you define it. A union offers enough place to store one value of either type, and (either implicitly in Boost::Variant or explicitly with plain old union) a "tag" indicating which one is the case. You can then look at any stored value and tell if it's another graph or a terminal value, and extract the associated value.
In Java and C#, you don't have that much support for straight-forward union types, so you'd use the second option. There's an interface (or abstract) class IGraph, with one implementation for graphs (refering to IGraphs) and one for wrapping non-graph values in another subtype of IGraph. Basically you use subtype polymorphism. This is possible in C++ too, but I get the impression that a union is a better idea if the possible types are known beforehand, unlikely to ever change, and small in number. It also allows you to avoid some pointers/references - both unions and Boost::Variants can be stored on the stack, while polymorphism requires indirection.