上下文:作为作业的一部分,我需要考虑新的缓存替换算法。有许多这样的算法,如最近最少使用,先入先出,但我不允许使用任何这些。所以我想到了一种根据索引工作的新算法。索引将从0开始并继续直到n-1,其中n是缓存的大小。
如果缓存未满,我将项目放在索引0,1,2,...,n-1直到它已满。缓存已满后,我只是替换缓存块的索引(th)索引处的块(在本例中表示为数组)。
问题: 在看到代码之后,你会发现它非常简单,因为我只是添加元素到数组的键和值以及增加数组的大小。如果数组已满,我将使用新项替换index(th)元素并增加索引,以便在即将到来的迭代中替换下一个块/索引
但问题是,在插入项目时,相同的元素被放在index = 0处,我很难弄清楚原因。任何帮助将受到高度赞赏。
总之,
//This is the faulty output
/*
*****---Executing print function---*****
Block 0 Memory Address: 10010 Value: 10
Block 1 Memory Address: 10010 Value: 10
Block 2 Memory Address: 10010 Value: 10
Block 3 Memory Address: 10010 Value: 10
*/
//While it should have been
/*
Block 0 Memory Address: 10010 Value: 10
Block 1 Memory Address: 10011 Value: 11
Block 2 Memory Address: 10100 Value: 12
Block 3 Memory Address: 10101 Value: 13
*/
代码:
#include <iostream>
using namespace std;
//We will have 2 primary functions. get(key) and set(key, value).
//get is used to retreive the value if key exists in cache. Otherwise it will return -1.
//set is used to set (key,value) by replacing the block. if value is already existent it should not do anything.
//Using this struct, we will add key value and a data structure for holding a pair of key and value.
typedef struct
{
int key, value; //to store address and data
}cache_set;
class algorithm_implement
{
public:
cache_set* cache;//array to hold information
int capacity; //number of blocks per set
int size; //total current elements in the cache
int index;
algorithm_implement(int total_size)
{
cache = new cache_set[capacity];
size = 0;
capacity = total_size;
index = 0;
}
int get (int mem_address)
{
//if the cache already has the address in it then retreive the value and do nothing. Else return -1
for (int i = 0; i < capacity; i++ )
{
if (cache[i].key == mem_address)
{
int req_val = cache[i].value;
return req_val;
}
}
return -1;
}
void set(int mem_address, int data)
{
for (int i = 0; i< capacity; i++){
if (cache[i].key == mem_address)
{
cout << "This memory address is already present in the cache. No replacement necessary."<<endl<<endl;
return;
}
//if item is not present and blocks are full then put key and value at index(th) position and increment the index. Don't let index be greater than capacity.
if (size == capacity)
{
index = index % capacity;
cache[index].key = mem_address;
cache[index].value = data;
index ++;
}
//if item is not present and some blocks are empty then put key and value at the index of size and increment size until cache is full.
else if (size < capacity)
{
cache[size].key = mem_address;
cache[size].value = data;
size ++;
}
}
}
void print()
{
cout << "*****---Executing print function---*****"<<endl<<endl;
for (int i = 0; i< capacity; i++)
{
cout << "Block "<< i << " Memory Address: "<<cache[i].key << " Value: "<<cache[i].value<<endl;
}
}
};
int main(){
int num_blocks = 4;
algorithm_implement index_based = *new algorithm_implement(num_blocks);
index_based.set(10010, 10);
index_based.set(10011, 11);
index_based.set(10100, 12);
index_based.set(10101, 13);
//Trying to put pre-existing element in the cache.
index_based.set(10010, 10);
//Printing the elements present in the cache
index_based.print();
}
//This is the output
/*
*****---Executing print function---*****
Block 0 Memory Address: 10010 Value: 10
Block 1 Memory Address: 10010 Value: 10
Block 2 Memory Address: 10010 Value: 10
Block 3 Memory Address: 10010 Value: 10
*/
//The output should have been
/*
Block 0 Memory Address: 10010 Value: 10
Block 1 Memory Address: 10011 Value: 11
Block 2 Memory Address: 10100 Value: 12
Block 3 Memory Address: 10101 Value: 13
*/