dict containing sets?
I'm having issuesto have a dict whose values are sets
first, Set doesn't implement copyinit so I had to wrap it
but now I get that
what's the simplest way to build a dict whose values are sets? can I operate on the sets?
here's the code if needed
first, Set doesn't implement copyinit so I had to wrap it
but now I get that
rag[current_label]rag[current_label] is a "function that might raise in a context that cannot"what's the simplest way to build a dict whose values are sets? can I operate on the sets?
here's the code if needed
struct CopyableSet[T: KeyElement](CollectionElement): # we need this because set is not copyable :S
var set: Set[T]
fn __init__(inout self: Self):
self.set = Set[T]()
fn __copyinit__(inout self: Self, existing: Self):
self.set = Set[T]()
self.set._data = existing.set._data
fn __moveinit__(inout self: Self, owned other: Self):
self.set = other.set^
fn create_rag(indices: FlatTensor[DType.uint16]) -> Dict[Int, CopyableSet[Int]]:
neighbor_offsets = List[List[Int]](
List(1, 0), List(-1, 0), List(0, 1), List(0, -1)
)
rag = Dict[Int, CopyableSet[Int]]()
max_label, _ = max[DType.uint16](indices, 0, len(indices.buffer))
for label in range(1, max_label + 1):
rag[label] = CopyableSet[Int]()
for row in range(S):
for col in range(S):
current_label = indices.buffer[rowcol_to_ind(row, col)].__int__()
if current_label == 0:
print('tremendous mistake!')
for i in range(len(neighbor_offsets)):
nrow = row + neighbor_offsets[i][0]
ncol = col + neighbor_offsets[i][1]
if 0 <= nrow and nrow < S and 0 <= ncol and ncol < S:
neighbor_label = indices.buffer[rowcol_to_ind(nrow, ncol)]
if neighbor_label != current_label:
aux = rag[current_label] # this line fails
aux.set.add(neighbor_label.__int__())
rag[current_label] = aux
return ragstruct CopyableSet[T: KeyElement](CollectionElement): # we need this because set is not copyable :S
var set: Set[T]
fn __init__(inout self: Self):
self.set = Set[T]()
fn __copyinit__(inout self: Self, existing: Self):
self.set = Set[T]()
self.set._data = existing.set._data
fn __moveinit__(inout self: Self, owned other: Self):
self.set = other.set^
fn create_rag(indices: FlatTensor[DType.uint16]) -> Dict[Int, CopyableSet[Int]]:
neighbor_offsets = List[List[Int]](
List(1, 0), List(-1, 0), List(0, 1), List(0, -1)
)
rag = Dict[Int, CopyableSet[Int]]()
max_label, _ = max[DType.uint16](indices, 0, len(indices.buffer))
for label in range(1, max_label + 1):
rag[label] = CopyableSet[Int]()
for row in range(S):
for col in range(S):
current_label = indices.buffer[rowcol_to_ind(row, col)].__int__()
if current_label == 0:
print('tremendous mistake!')
for i in range(len(neighbor_offsets)):
nrow = row + neighbor_offsets[i][0]
ncol = col + neighbor_offsets[i][1]
if 0 <= nrow and nrow < S and 0 <= ncol and ncol < S:
neighbor_label = indices.buffer[rowcol_to_ind(nrow, ncol)]
if neighbor_label != current_label:
aux = rag[current_label] # this line fails
aux.set.add(neighbor_label.__int__())
rag[current_label] = aux
return rag