Hi guys,
Is there a reason there is no hasValue procedure in the tables module? I have this code:
var
uniqueObjects = newSeq[myObject]()
objectTable = newTable[int, myObject]()
for item in allObjects:
if not(item in uniqueObjects):
uniqueObjects.add(item)
objectTable[] = mySeqToTableProc(uniqueObjects)
But is there a way to write it like so:
var
objectTable = newTable[int, myObject]()
counter = 0
for item in allObjects:
if not(objectTable.hasValue(item)):
objectTable[counter] = item
counter += 1
Thanks
Are you sure that you are doing it properly at all?
If your keys are ints, then you may use a plain array to store the value objects, you may not need the hash table.
if not(item in uniqueObjects):
This should be very slow, as in op does a linear search each time in whole seq.
And finally, a hasValue() would have to do a linear search also, which is very slow of course.
Maybe you can tell us what you want to archive? There are many ways -- maybe see countTable or this thread: https://forum.nim-lang.org/t/2611#16168
Tables have contains proc, as well as arrays, seqs, openarrays, and other data structures.
if myTable.contains(myKey) or mySeq.contains(myKey):
That said, there's a in and notin operators in nim which translate to the underlying contains call. So you can implement contains proc for your own data structure and in and notin will just work for it.
if myKey in myTable and myKey notin mySeq:
That said, contains proc for array-like structures is linear complexity. For big datasets you likely want to use HashSet instead.