M
Modular•14mo ago
Geo7

How to store Struct(or string) into DynamicVector

I want to store struct into DynamicVector at first I tried
from utils.vector import DynamicVector


struct Person:
var first: String
var last: String
var id: Int

fn __init__(inout self, n: String, l: String, id: Int):
self.first = n
self.last = l
self.id = id


fn main() raises:
var x = DynamicVector[Person]()
let me = Person("A", "B", 1)
x.push_back(me)
from utils.vector import DynamicVector


struct Person:
var first: String
var last: String
var id: Int

fn __init__(inout self, n: String, l: String, id: Int):
self.first = n
self.last = l
self.id = id


fn main() raises:
var x = DynamicVector[Person]()
let me = Person("A", "B", 1)
x.push_back(me)
then I tried to change String Pointer[UInt8] now I do not know how to load string into Pointer[UInt8]
from utils.vector import DynamicVector

alias PointerString = Pointer[UInt8]
alias PointerStrings = Pointer[PointerString]
alias BufferPtrType = DTypePointer[DType.uint8]


@value
@register_passable("trivial")
struct Person:
var id: Int
var first: PointerString
var last: PointerString

fn __init__(inout self, id: Int, f: PointerString, l: PointerString) -> Self:
self.id = id
self.first = f
self.last = l
return self


fn main() raises:
var x = DynamicVector[Person]()
let first = PointerString.alloc(100)
first.store(0, 12)
let last = PointerString.alloc(100)
last.store(0, 17)
let id = 1
let me = Person(id, first, last)
x.push_back(me)
from utils.vector import DynamicVector

alias PointerString = Pointer[UInt8]
alias PointerStrings = Pointer[PointerString]
alias BufferPtrType = DTypePointer[DType.uint8]


@value
@register_passable("trivial")
struct Person:
var id: Int
var first: PointerString
var last: PointerString

fn __init__(inout self, id: Int, f: PointerString, l: PointerString) -> Self:
self.id = id
self.first = f
self.last = l
return self


fn main() raises:
var x = DynamicVector[Person]()
let first = PointerString.alloc(100)
first.store(0, 12)
let last = PointerString.alloc(100)
last.store(0, 17)
let id = 1
let me = Person(id, first, last)
x.push_back(me)
6 Replies
NKspartan
NKspartan•14mo ago
you can do it using pointers:
var myVector = DynamicVector[Pointer[MyStruct]] ()
var myStructInstance = MyStruct(1, 2)
var hello = __get_lvalue_as_address(myStructInstance) # Get address of value

myVector.push_back(hello)
print(__get_address_as_lvalue(myVector[0].address).field2) # get the address of the value and then convert it to a reference (lvalue)
var myVector = DynamicVector[Pointer[MyStruct]] ()
var myStructInstance = MyStruct(1, 2)
var hello = __get_lvalue_as_address(myStructInstance) # Get address of value

myVector.push_back(hello)
print(__get_address_as_lvalue(myVector[0].address).field2) # get the address of the value and then convert it to a reference (lvalue)
Michael K
Michael K•14mo ago
There is also the example in llama2.mojo:
fn str_to_ptr(s: String) -> PointerString:
let ret = PointerString.alloc(len(s) + 1)
for i in range(len(s)):
ret.store(i, ord(s[i]))
ret.store(len(s), 0)
return ret
fn str_to_ptr(s: String) -> PointerString:
let ret = PointerString.alloc(len(s) + 1)
for i in range(len(s)):
ret.store(i, ord(s[i]))
ret.store(len(s), 0)
return ret
PointerString is the same alias (Pointer[UInt8]) in that repo as in your example here.
GitHub
llama2.mojo/llama2.mojo at master · tairov/llama2.mojo
Inference Llama 2 in one file of pure 🔥. Contribute to tairov/llama2.mojo development by creating an account on GitHub.
ModularBot
ModularBot•14mo ago
Congrats @Michael K, you just advanced to level 1!
Geo7
Geo7OP•14mo ago
Thanks for the responses, I tried @NKspartan example and it is working, But when I tried to write 2 helper function it does not work anymore what I've done wrong?
struct Person:
var id: Int
var name: String

fn __init__(inout self, i: Int, n: String):
self.id = i
self.name = n

fn __copyinit__(inout self, borrowed other: Self):
self.id = other.id
self.name = other.id


fn append(inout v: DynamicVector[Pointer[Person]], owned p: Person):
let _tmp = __get_lvalue_as_address(p)
v.push_back(_tmp)


fn get(inout v: DynamicVector[Pointer[Person]], index: Int) -> Person:
let _tmp = __get_address_as_lvalue(v[index].address)
return _tmp


fn main():
var v = DynamicVector[Pointer[Person]]()
let p1 = Person(1, "George")
let p2 = Person(2, "Rose")
append(v, p1)
append(v, p2)
let p = get(v, 0)
print(p.name) # It is printing id instead of name !!!
struct Person:
var id: Int
var name: String

fn __init__(inout self, i: Int, n: String):
self.id = i
self.name = n

fn __copyinit__(inout self, borrowed other: Self):
self.id = other.id
self.name = other.id


fn append(inout v: DynamicVector[Pointer[Person]], owned p: Person):
let _tmp = __get_lvalue_as_address(p)
v.push_back(_tmp)


fn get(inout v: DynamicVector[Pointer[Person]], index: Int) -> Person:
let _tmp = __get_address_as_lvalue(v[index].address)
return _tmp


fn main():
var v = DynamicVector[Pointer[Person]]()
let p1 = Person(1, "George")
let p2 = Person(2, "Rose")
append(v, p1)
append(v, p2)
let p = get(v, 0)
print(p.name) # It is printing id instead of name !!!
Michael K
Michael K•14mo ago
In __copyinit__ you have self.name = other.id. This should be self.name = other.name.
Geo7
Geo7OP•14mo ago
Thanks a lot, silly mistake 🙂
Want results from more Discord servers?
Add your server