Here's my solution. Not doubt it is ugly and slow. I look forward to
you guys showing me how to do it correctly.
emptyHash =: ,~ ,. < 'not found'
hashSet =: dyad define
'key val' =. y
pos =. x hashGetInd key
if. pos = 1{ $x
do. x ,"(1,0) key ; val
else. ((< key) pos} (0{x)) ,: (< val) pos} (1{x)
end.
)
hashGetInd =: dyad define
keylist =. > 0{ x
len =. # 0{ keylist
if. len > #y
do. key =. y , ((len - #y) # ' ')
else. key =. len {. y
end.
keylist i. key
)
hashGet =: dyad define
pos =. x hashGetInd y
if. pos = 1{ $x
do. > 0 { 1{ x
else. > pos { 1{ x
end.
)
hashDel =: dyad define
pos =. x hashGetInd y
if. pos < 1{ $x
do. (<<< pos) {"1 x
end.
)
Example:
test =: emptyHash
test
┌─────────┐
│not found│
├─────────┤
│not found│
└─────────┘
test1 =: test hashSet 'a' ; 13
test1
┌─────────┬──┐
│not found│a │
├─────────┼──┤
│not found│13│
└─────────┴──┘
test2 =: test1 hashSet 'a' ; 'Aye'
test2
┌─────────┬───┐
│not found│a │
├─────────┼───┤
│not found│Aye│
└─────────┴───┘
test3 =: test2 hashSet 'bb' ; 17
test3
┌─────────┬───┬──┐
│not found│a │bb│
├─────────┼───┼──┤
│not found│Aye│17│
└─────────┴───┴──┘
test3 hashGet 'a'
Aye
test3 hashGet 'bb'
17
test4 =: test3 hashDel 'bb'
test4
┌─────────┬───┐
│not found│a │
├─────────┼───┤
│not found│Aye│
└─────────┴───┘
test4 hashGet 'bb'
not found
test4 hashGet 'a'
Aye
On 12/03/2017 04:25 PM, Henry Rich wrote:
Perhaps we shouldn't try to make a full datatype for associative
arrays, but just a set of functions (foreigns, or (m h.) ) that do
what's needed.
We would need to decide what's needed.
hashtable =: keys create values
indexes =: hashtable lookup keys (like i.)
values =: hashtable fetch indexes (like {)
hashtable =: hashtable add keys;values
hashtable =: hashtable delete indexes
That would not be the hardest thing to implement; perhaps we should
start a strawman in Interpreter/Requests.
----------------------------------------------------------------------
For information about J forums see http://www.jsoftware.com/forums.htm