Advertisement
mantielero

Untitled

Oct 11th, 2022
2,780
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Nim 1.87 KB | None | 0 0
  1. {.passL:"-lstdc++".}
  2.  
  3. #{.push dynlib:"libstd++.so".}
  4.  
  5. {.push header:"<utility>".}
  6. type
  7.   CppPair*[T,P] {.importcpp:"std::pair"} = object
  8.  
  9. proc makePair*[T,P](a:T; b:P):CppPair[T,P] {.importcpp:"std::make_pair(@)" .}
  10.  
  11. proc first*[T,P](p:CppPair[T,P]):T {.importcpp:"#.first" .}
  12.  
  13. proc second*[T,P](p:CppPair[T,P]):P {.importcpp:"#.second" .}
  14.  
  15. {.pop.}
  16.  
  17. {.push header:"<map>"}
  18. type
  19.   CppMap*[T,P] {.importcpp:"std::map".} = object
  20.   CppMapIterator* {.importcpp: "std::map<'0,'1>::iterator".} [K,V] = object
  21.  
  22. proc insert*[T,P](self:CppMap[T,P],val:CppPair[T,P]) {.importcpp:"#.insert(@)" .}
  23.  
  24. proc `[]=`*[K,V](self:var CppMap[K,V],key:K, val:V) {.importcpp:"#[#] = #" .}
  25.  
  26.  
  27.  
  28. # Iterators
  29. proc begin*[K,V](x: CppMap[K,V]): CppMapIterator[K,V] {.importcpp: "begin".}
  30. proc `end`*[K,V](x: CppMap[K,V]): CppMapIterator[K,V] {.importcpp: "end".}
  31.  
  32. {.pop.}
  33.  
  34.  
  35. {.push header:"<iterator>".}
  36. proc first*[K,V](self:CppMapIterator[K,V]):K {.importcpp:"@->first" .}
  37. proc second*[K,V](self:CppMapIterator[K,V]):V {.importcpp:"@->second" .}
  38.  
  39. proc inc*[K,V](self:var CppMapIterator[K,V]) {.importcpp:"#.operator++()" .}   #<<<<< ISSUE
  40. {.pop.}
  41.  
  42. iterator pairs*[K,V](self:CppMap[K,V]):tuple[key:K,value:V] =
  43.   var result:tuple[key:K,value:V]
  44.   var ini:CppMapIterator[K,V] = self.begin()
  45.   var tmp:ptr CppMapIterator[K,V]
  46.   echo (ini.first,ini.second)
  47.   yield (ini.first,ini.second)
  48.   inc(ini)   # <<<< ISSUE
  49.   echo (ini.first,ini.second)
  50.   yield (ini.first,ini.second)
  51.  
  52.  
  53. #================
  54.  
  55. type
  56.   PersonMap = CppMap[string,int]
  57.   Person = CppPair[string,int]
  58.  
  59. var mymap:PersonMap
  60. var tmp1:Person = makePair("jose", 10)
  61. mymap.insert(tmp1)
  62. mymap.insert( makePair("ana", 20) )
  63. mymap.insert( makePair("maria", 30) )
  64. mymap["jose"] = 15
  65.  
  66. var ini:CppMapIterator[string,int] = mymap.begin()
  67. var fin = mymap.`end`()
  68. echo ini.first
  69. echo ini.second
  70. for i in mymap.pairs():
  71.   echo i.key
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement