Archive for the 'NSMapTable' Category

2017/01/03 NSMapTableのじっけん

どうしてもNSMapTableを作って呼び出す必要のある部品があって、仕方なくNSMapTableについて調査。「NSMutableDictionaryとほぼ同じだがメモリー上の管理方式が異なる」という以上の説明がなく、なかなか強敵です。

しかも、NSDictionary/NSMutableDictionaryから変換するメソッドなどもないため、なかなか作るのもめんどくさいと来ています。

NSMapTable自体を操作するメソッドも少ないので、AppleScript側にとってあまりメリットはなさそうですが、とりあえずNSDictionary/NSMutableDictionaryとの間で相互変換できるようにしてみました。

AppleScript名:NSMapTableのじっけん
– Created 2017-01-02 by Takaaki Naganoya
– 2017 Piyomaru Software
use AppleScript version “2.4″
use scripting additions
use framework “Foundation”

–AppleScriptではStrongでもWeakでも関係ない???
set aMapTable to current application’s NSMapTable’s mapTableWithKeyOptions:(current application’s NSMapTableStrongMemory) valueOptions:(current application’s NSMapTableStrongMemory)

–set aMapTable to current application’s NSMapTable’s mapTableWithKeyOptions:(current application’s NSMapTableWeakMemory) valueOptions:(current application’s NSMapTableWeakMemory)

aMapTable’s setObject:“1″ forKey:“foo1″
aMapTable’s setObject:“2″ forKey:“foo2″
aMapTable’s |count|()
–> 2

aMapTable’s objectEnumerator()’s allObjects() as list –objects
–>  {”2″, “1″}

aMapTable’s keyEnumerator()’s allObjects() as list –keys
–>  {”foo2″, “foo1″}

aMapTable’s dictionaryRepresentation() as record –NSMapTable to NSDictionary to record
–>  {foo:”1″, foo2:”2″}

–以下はあまり関係ない?
–aMapTable’s keyPointerFunctions()
–>  (NSConcretePointerFunctions)

–aMapTable’s valuePointerFunctions()
–>  (NSConcretePointerFunctions)

★Click Here to Open This Script 

record –> NSMutableDictionary –> NSMapTable と変換するサブルーチンも作ってみました。

AppleScript名:NSMapTableのじっけん(NSDictionary to NSMapTable)
– Created 2017-01-02 by Takaaki Naganoya
– 2017 Piyomaru Software
use AppleScript version “2.4″
use scripting additions
use framework “Foundation”

set aRec to {foo1:1, foo2:2, foo3:3, foo4:4, foo5:5}
set aDict to current application’s NSMutableDictionary’s dictionaryWithDictionary:aRec
set aMap to convertDictToMap(aDict) of me
(*
–>  (NSConcreteMapTable) NSMapTable {
[1] foo2 -> 2
[5] foo4 -> 4
[7] foo5 -> 5
[11] foo3 -> 3
[14] foo1 -> 1
}
*)


–Verify
set bDict to aMap’s dictionaryRepresentation() as record
–>  {foo1:1, foo4:4, foo2:2, foo5:5, foo3:3}

on convertDictToMap(aDict)
  set aMapTable to current application’s NSMapTable’s mapTableWithKeyOptions:(current application’s NSMapTableStrongMemory) valueOptions:(current application’s NSMapTableStrongMemory)
  
  set keyList to aDict’s allKeys() as list
  
set valList to aDict’s allValues() as list
  
set aLen to length of keyList
  
  repeat with i from 1 to aLen
    set aKey to contents of item i of keyList
    
set aVal to contents of item i of valList
    (
aMapTable’s setObject:aVal forKey:aKey)
  end repeat
  
  return aMapTable
end convertDictToMap

★Click Here to Open This Script 

recordから直接NSMapTableに変換するものも作っておきました。

AppleScript名:NSMapTableのじっけん(record to NSMapTable)
– Created 2017-01-02 by Takaaki Naganoya
– 2017 Piyomaru Software
use AppleScript version "2.4"
use scripting additions
use framework "Foundation"

set aRec to {foo1:1, foo2:2, foo3:3, foo4:4, foo5:5}
set aMap to convertRecToMap(aRec) of me
(*
–>  (NSConcreteMapTable) NSMapTable {
[1] foo2 -> 2
[5] foo4 -> 4
[7] foo5 -> 5
[11] foo3 -> 3
[14] foo1 -> 1
}
*)

–Verify
set bDict to aMap’s dictionaryRepresentation() as record
–>  {foo1:1, foo4:4, foo2:2, foo5:5, foo3:3}

on convertRecToMap(aRec as record)
  set aDict to current application’s NSMutableDictionary’s dictionaryWithDictionary:aRec
  
  
set aMapTable to current application’s NSMapTable’s mapTableWithKeyOptions:(current application’s NSMapTableStrongMemory) valueOptions:(current application’s NSMapTableStrongMemory)
  
  
set keyList to aDict’s allKeys() as list
  
set valList to aDict’s allValues() as list
  
set aLen to length of keyList
  
  
repeat with i from 1 to aLen
    set aKey to contents of item i of keyList
    
set aVal to contents of item i of valList
    (
aMapTable’s setObject:aVal forKey:aKey)
  end repeat
  
  
return aMapTable
end convertRecToMap

★Click Here to Open This Script