Hi, I have block of code in C++ like this:
++
std::vector<std::pair<int, int>> TL;
int texture, lightmap;
auto p = std::make_pair(texture, lightmap); // make pair
auto it = std::find(TL.begin(), TL.end(), p); // iterator
int pos = std::distance(TL.begin(), it); // the distance from the first pair
if (it != texturePairs.TL.end())
{
doSomething(pos);
}
Basicly, I take two ints, make pair out of them, then i search for that pair in a vector of pairs, if the pair is found, doSomething(pos) runs.
How do i make this in Nim? I looked for pairs, but in Nim they are similar to enumerations. https://forum.nim-lang.org/t/3570
You can iterate and check manually like
var
intpair = newseq[(int, int)]()
texture: int
lightmap: int
for thepair in intpair:
if thepair[0] == texture and thepair[1] == lightmap:
doSomething((texture, lightmap))
or you can use define
type
IntPair = tuple
a: int
b: int
proc `==`(a, b: IntPair): bool =
a.a == b.a and a.b == b.b
var
texture: int
lightmap: int
intpairs = newseq[IntPair]()
thepair = (texture, lightmap)
pos = intpairs.find thepair
if pos != -1: doSomething(thepair)
Thanks, it works!
In my case, i dont need std::distance, because system.find does the job. Can it be implemented with slice maybe?
Another question, is there something similar to std::map in Nim? https://thispointer.com/stdmap-tutorial-part-1-usage-detail-with-examples/
For example, if you have
var s = newSeq[seq[uint32]]()
you cant do s[42].add(5) for example, with map you can do that. is there something similar to std::map in Nim?
(from the link): std::map is an associative container that store elements in key-value pair.