Archive for the 'NSIndexSet' Category

2015/01/14 NSMutableIndexSetの初期化、作成

NSMutableIndexSetの初期化、データ作成のサンプルです。

NSMutableIndexSetの作成について、初期化の仕方がいまひとつ分かりませんでしたが、Shane Stanleyから「こうやればいいよ」と教えてもらったScript(そのまんま)です。

AppleScript名:NSMutableIndexSetの初期化、作成
–By Shane Stanley
use AppleScript version “2.4″
use scripting additions
use framework “Foundation”
use framework “ASObjCExtras”

set anIndexSet to current application’s NSMutableIndexSet’s indexSet() –initialize
anIndexSet’s addIndexesInRange:(current application’s NSMakeRange(3, 20)) –make range

set aList to (current application’s SMSFord’s arrayWithIndexSet:anIndexSet) as list
–>  {3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22}

★Click Here to Open This Script 

2014/12/30 NSIndexSetに値を入れる、取り出す

ユニークな整数値のみをソートされた状態で保持するNSIndexSetを作成し、値を取り出すAppleScriptです。

NSIndexSetは値の重複を許さず、整数値のみ保持し、値をソートされた状態で保持するクラスで・・・AppleScriptから作成、値の追加、値の取り出しを行います。

正確にいえば、ここで扱っているのはNSIndexSetではなくNSMutableIndexSetです。

v2の方は、ASObjCExtras.frameworkの機能を利用して、一括でNSIndexSetから値を取り出しています。

AppleのReferenceを読むと、NSMutableIndexSetは10.9までしかinit()できないと書いてありますが・・・できているような、、、

AppleScript名:indexSetに値を入れる、取り出す
– Created 2014-12-30 by Takaaki Naganoya
– 2014 Piyomaru Software
use AppleScript version “2.4″
use scripting additions
use framework “Foundation”
use framework “ASObjCExtras”

set aindexSet to current application’s NSMutableIndexSet’s alloc()’s init()

repeat 100 times
  set aRandom to random number from 1 to 100
  
aindexSet’s addIndex:aRandom
end repeat

set aList to {}
set aInd to aindexSet’s firstIndex()

repeat
  if aInd = current application’s NSNotFound then exit repeat
  
set the end of aList to aInd as integer
  
set bInd to aindexSet’s indexGreaterThanIndex:aInd
  
copy bInd to aInd
end repeat

aList
–> {2, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 23, 24, 25, 28, 29, 30, 31, 32, 33, 34, 37, 38, 39, 40, 43, 52, 53, 56, 57, 59, 61, 63, 67, 70, 74, 76, 78, 79, 80, 81, 83, 84, 85, 87, 88, 89, 90, 91, 92, 94, 96, 97}

★Click Here to Open This Script 

AppleScript名:indexSetに値を入れる、取り出す v2
– Created 2014-12-30 by Takaaki Naganoya
– 2014 Piyomaru Software
use AppleScript version “2.4″
use scripting additions
use framework “Foundation”
use framework “ASObjCExtras”

set aindexSet to current application’s NSMutableIndexSet’s alloc()’s init()

repeat 100 times
  set aRandom to random number from 1 to 100
  
aindexSet’s addIndex:aRandom
end repeat

set aList to (current application’s SMSFord’s arrayWithIndexSet:aindexSet) as list

aList
–> {2, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 23, 24, 25, 28, 29, 30, 31, 32, 33, 34, 37, 38, 39, 40, 43, 52, 53, 56, 57, 59, 61, 63, 67, 70, 74, 76, 78, 79, 80, 81, 83, 84, 85, 87, 88, 89, 90, 91, 92, 94, 96, 97}

★Click Here to Open This Script 

AppleScript名:Listに値を入れる、取り出す(通常ASで書いた場合)
– Created 2014-12-31 by Takaaki Naganoya
– 2014 Piyomaru Software
use AppleScript version “2.4″
use scripting additions

set aIndexSet to {}

repeat 100 times
  set aRandom to random number from 1 to 100
  
  
–ユニークな値のみをストアし、つねに昇順ソートされた状態を維持する
  
if aRandom is not in aIndexSet then –ユニーク判定
    set the end of aIndexSet to aRandom
    
set aIndexSet to shellSortAscending(aIndexSet) of me –昇順ソート
  end if
  
end repeat

aIndexSet
–> {1, 3, 4, 5, 6, 7, 8, 9, 11, 12, 14, 16, 17, 18, 19, 20, 21, 23, 26, 27, 28, 29, 30, 31, 34, 36, 37, 39, 40, 41, 43, 47, 49, 53, 55, 57, 58, 60, 63, 65, 66, 68, 71, 72, 73, 74, 76, 77, 80, 81, 82, 83, 84, 86, 87, 88, 91, 92, 95}

–1D Listの昇順ソート
on shellSortAscending(aSortList)
  script oBj
    property list : aSortList
  end script
  
set len to count oBj’s list’s items
  
set gap to 1
  
repeat while (gap len)
    set gap to ((gap * 3) + 1)
  end repeat
  
repeat while (gap > 0)
    set gap to (gap div 3)
    
if (gap < len) then
      repeat with i from gap to (len - 1)
        set temp to oBj’s list’s item (i + 1)
        
set j to i
        
repeat while ((j gap) and (oBj’s list’s item (j - gap + 1) > temp))
          set oBj’s list’s item (j + 1) to oBj’s list’s item (j - gap + 1)
          
set j to j - gap
        end repeat
        
set oBj’s list’s item (j + 1) to temp
      end repeat
    end if
  end repeat
  
return oBj’s list
end shellSortAscending

★Click Here to Open This Script