最適なキャッシュ


14

メモリリクエストのシーケンスとキャッシュサイズが与えられます。キャッシュ置換戦略では、キャッシュミスの数をできるだけ少なくする必要があります。

最適な戦略は、Beladyのアルゴリズムです。これは、必要に応じて使用できます。


キャッシングシステムは次のように機能します。キャッシュは空から始まります。メモリ要求が入ります。要求がキャッシュ内のデータの一部を要求する場合、すべてが順調です。そうでない場合、キャッシュミスが発生します。この時点で、将来使用するために要求されたデータをキャッシュに挿入できます。キャッシュがいっぱいで、新しいデータを挿入する場合は、以前にキャッシュにあったデータを削除する必要があります。キャッシュ内だけではないデータを挿入することはできません。

あなたの目標は、特定のメモリリクエストシーケンスとキャッシュサイズについて、キャッシュミスの最小数を見つけることです。


キャッシュサイズ、正の整数、およびトークンのリストであるメモリ要求シーケンスが提供されます。これらのトークンは、少なくとも256個の異なるトークンが可能な限り、どのような種類のトークンでもかまいません(バイトは問題ありませんが、ブールはできません)。たとえば、int、string、listはすべて問題ありません。必要に応じて説明を求めてください。


テストケース:

3
[5, 0, 1, 2, 0, 3, 1, 2, 5, 2]

6

これを実現する代替ポリシーについては、ウィキペディアを参照してください。

2
[0, 1, 2, 0, 1, 0, 1]

3

2キャッシュへの追加を避けるだけです。

3
[0, 1, 2, 1, 4, 3, 1, 0, 2, 3, 4, 5, 0, 2, 3, 4]

9

これを達成する1枚の方法は、追い出したことがないことである02し、立ち退か1その最後の使用後は、できるだけ早く。


得点:これはコードゴルフです。少ないバイトが勝ちます。


リストに少なくとも2つのトークンが含まれていると想定できますか?
アーナウルド

@Arnauldノーと言いますが、解決策が1つしかない場合、答えはもちろん常に1です。
isaacg

回答:


4

JavaScript(ES6)、128バイト

入力をとして受け取ります(size)(list)

s=>a=>a.map((x,i)=>c.includes(x)?0:c[e++,[x,...c].map(m=(x,j)=>(k=[...a,x].indexOf(x,i+1))<m||(p=j,m=k)),i<s?i:p-1]=x,e=c=[])&&e

オンラインでお試しください!

コメント済み

これは、Beladyのアルゴリズムの実装です。

s => a =>                      // s = cache size; a[] = token list
  a.map((x, i) =>              // for each token x at position i in a[]:
    c.includes(x) ?            //   if x is currently stored in the cache:
      0                        //     do nothing
    :                          //   else:
      c[                       //     update the cache:
        e++,                   //       increment the number of errors (cache misses)
        [x, ...c]              //       we want to find which value among x and all current
                               //       cache values will be needed for the longest time in
                               //       the future (or not needed anymore at all)
        .map(m =               //       initialize m to a non-numeric value
                 (x, j) =>     //       for each x at position j in this array:
          ( k = [...a, x]      //         k = position of x in the array made of all values
            .indexOf(x, i + 1) //         of a[] followed by x, starting at i + 1
          ) < m                //         if it's greater than or equal to m, or m is
          || (p = j, m = k)    //         still non-numeric: set p to j and m to k
        ),                     //       end of inner map()
        i < s ?                //       if i is less than the cache size:
          i                    //         just fill the cache by using the next cache slot
        :                      //       else:
          p - 1                //         use the slot that was found above
                               //         special case: if p = 0, x was the best candidate
                               //         and we're going to store it at c[-1], which is
                               //         simply ignored (it will not trigger c.includes(x))
      ] = x,                   //     store x at this position
      e = c = []               //     start with e = [] (coerced to 0) and c = []
  ) && e                       // end of outer map; return e

4

Perl 5、193バイト

sub g{
  my($i,$m,$s,@a,%c)=(-1,0,@_);
  for(@a){
    $i++;
    next if $c{$_}++ || ++$m && keys%c <= $s;
    my($x,$d);
    for $k (sort keys %c){  #find which to delete, the one furtherst away
      my $n=0;
      ++$n && /^$k$/ && last for @a[$i+1..$#a];
      ($x,$d)=($n,$k) if $n>$x
    }
    delete $c{$d}
  }
  $m
}

オンラインでお試しください!

print g(3,  5, 0, 1, 2, 0, 3, 1, 2, 5, 2),"\n";                     # 6
print g(2,  0, 1, 2, 0, 1, 0, 1),"\n";                              # 3
print g(3,  0, 1, 2, 1, 4, 3, 1, 0, 2, 3, 4, 5, 0, 2, 3, 4),"\n";   # 9

インデント、改行、スペース、コメントなしの193バイト:

sub g{my($i,$m,$s,@a,%c)=(-1,0,@_);for(@a){$i++;next if$c{$_}++||++$m&&keys%c<=$s;my($x,$d);for$k(sort keys%c){my$n=0;++$n&&/^$k$/&&last for@a[$i+1..$#a];($x,$d)=($n,$k)if$n>$x}delete$c{$d}}$m}


1

Haskell、82バイト

f n|let(d:t)#c=1-sum[1|elem d c]+minimum[t#take n e|e<-scanr(:)(d:c)c];_#_=0=(#[])

オンラインでお試しください!

説明

力ずくで動作:すべてのキャッシュ戦略が試行され、最良の結果が返されます。

f n            Define a function f on argument n (cache size) and a list (implicit).
 |let(d:t)#c=  Define binary helper function #.
               Arguments are list with head d (current data) and tail t (remaining data), and list c (cache).
 1-            It returns 1 minus
 sum[1|        1 if
 elem d c]+    d is in the cache, plus
 minimum[      minimum of
 t#            recursive calls to # with list t
 take n e|     and cache being the first n values of e, where
 e<-           e is drawn from
 scanr(:)  c]  the prefixes of c
 (d:c)         with d and c tacked to the end.
 ;_#_=0        If the first list is empty, return 0.
 =(#[])        f then calls # with the list argument and empty cache.

弊社のサイトを使用することにより、あなたは弊社のクッキーポリシーおよびプライバシーポリシーを読み、理解したものとみなされます。
Licensed under cc by-sa 3.0 with attribution required.