Author Topic: HashTables and isempty and null pointers  (Read 5337 times)

jporkkahtc

  • Senior Community Member
  • Posts: 2620
  • Hero Points: 210
  • Text
HashTables and isempty and null pointers
« on: September 06, 2016, 01:45:51 AM »

Can you explain how Slick manages hashtables and arrays?




How do you ask if a hashtable has a specific entry without creating the entry?
What is the difference between a "null" object and an "_isempty" object?




&s_nameIndex:["Two"]
returns a pointer to the "Two" element - fine.
But, what about when you look up elements that don't exist?

 
&s_nameIndex:["Four"]

In the debugger, when this statement is executed s_nameIndex grows from 3 to 4 elements.
The new element is shown as "null", yet what is the thing returned by taking the address of a "null" object?
Even though the debugger shows the entry in the hashtable as null, when I reference it it seems like an object because the pointer doesn't compare= to null, but it _isempty() is true.




Whereas this statement

 
    NameIndex *x = &s_nameIndex._el("Four"); x->m_name="newName"; 

Apparently allocates a NameIndex - as the entry in the hashtable changes from null to a valid NameIndex.


See the attached code.

« Last Edit: September 06, 2016, 06:31:21 PM by jporkkahtc »

Bamsen

  • Community Member
  • Posts: 66
  • Hero Points: 8
Re: HashTables and isempty and null pointers
« Reply #1 on: September 06, 2016, 07:35:47 AM »
From one of my scripts:
Code: [Select]
if (Linjer:[sTemp] == null) { Linjer:[sTemp]=AddValue; } else { Linjer:[sTemp]+=AddValue; }
This creates the hash variable with a specific value if it does not exist.
If it does exist, it adds the value to the existing one.

jporkkahtc

  • Senior Community Member
  • Posts: 2620
  • Hero Points: 210
  • Text
Re: HashTables and isempty and null pointers
« Reply #2 on: September 06, 2016, 06:43:33 PM »
Yes, but when you do that it creates new entries in the hash table.
Just doing this:
if (Linjer:["foo"] == null)
will insert a null "foo" entry and increase the size of the hashtable by 1.


Clark

  • SlickEdit Team Member
  • Senior Community Member
  • *
  • Posts: 6937
  • Hero Points: 531
Re: HashTables and isempty and null pointers
« Reply #3 on: September 07, 2016, 03:26:09 AM »
To avoid adding entries to a hash table, use the _indexin() method.

if (hashtab._indexin('abc') && hashtab:['abc']==null) ...