Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- {.passL:"-lstdc++".}
- import std/tables # Equivalent to C++ std::map
- #{.push dynlib:"libstd++.so".}
- {.push header:"<utility>".}
- type
- CppPair*[T,P] {.importcpp:"std::pair"} = object
- proc makePair*[T,P](a:T; b:P):CppPair[T,P] {.importcpp:"std::make_pair(@)" .}
- proc first*[T,P](p:CppPair[T,P]):T {.importcpp:"#.first" .}
- proc second*[T,P](p:CppPair[T,P]):P {.importcpp:"#.second" .}
- {.pop.}
- {.push header:"<map>"}
- type
- CppMap*[T,P] {.importcpp:"std::map".} = object
- CppMapIterator* {.importcpp: "std::map<'0,'1>::iterator".} [K,V] = object
- proc insert*[T,P](self:CppMap[T,P],val:CppPair[T,P]) {.importcpp:"#.insert(@)" .}
- proc `[]=`*[K,V](self:var CppMap[K,V], key:K, val:V) {.importcpp:"#[#] = #" .}
- # Iterators
- proc begin*[K,V](x: CppMap[K,V]): CppMapIterator[K,V] {.importcpp: "begin".}
- proc `end`*[K,V](x: CppMap[K,V]): CppMapIterator[K,V] {.importcpp: "end".}
- proc `!=`*[K,V](a,b:CppMapIterator[K,V]):bool {.importcpp: "# != #".} # Used for the iterator
- {.pop.}
- {.push header:"<iterator>".}
- proc first*[K,V](self:CppMapIterator[K,V]):K {.importcpp:"@->first" .}
- proc second*[K,V](self:CppMapIterator[K,V]):V {.importcpp:"@->second" .}
- proc inc*[K,V](self:CppMapIterator[K,V]) {.importcpp: "++#".} #"#.operator++()" .}
- {.pop.}
- iterator pairs*[K,V](self:CppMap[K,V]):tuple[key:K,value:V] =
- var ini:CppMapIterator[K,V] = self.begin()
- var `end`:CppMapIterator[K,V] = self.`end`()
- while (ini != `end` ):
- yield (ini.first, ini.second)
- inc(ini)
- #================
- type
- PersonMap = CppMap[string,int]
- Person = CppPair[string,int]
- var mymap:PersonMap
- var tmp1:Person = makePair("jose", 10)
- mymap.insert(tmp1)
- mymap.insert( makePair("ana", 20) )
- mymap.insert( makePair("maria", 30) )
- mymap["jose"] = 15
- for (k,v) in mymap.pairs():
- echo k
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement