Initial commit
This commit is contained in:
commit
d3bac575d4
698 changed files with 180798 additions and 0 deletions
267
dat/cache/difffile-large/10/CacheHandler10_0.dn
vendored
Normal file
267
dat/cache/difffile-large/10/CacheHandler10_0.dn
vendored
Normal file
|
@ -0,0 +1,267 @@
|
|||
/*
|
||||
* EmerGen(e)tic 0.8
|
||||
* Copyright © 2017 Ben Goldsworthy (rumperuu)
|
||||
*
|
||||
* A program to research the utility of genetic algorithms, applied to
|
||||
* 'emergent' sytems.
|
||||
*
|
||||
* This file is part of EmerGen(e)tic.
|
||||
*
|
||||
* IMPROVEMENT(S): License (probably GNU GPLv3 tbh) goes here.
|
||||
*/
|
||||
|
||||
/**
|
||||
** This program serves as the base for each mutated CacheHandler,
|
||||
** providing the functionality that is common to all of them.
|
||||
**/
|
||||
|
||||
/**
|
||||
** @author Ben Goldsworthy (rumperuu) <bgoldsworthy96 @ gmail.com>
|
||||
** @version 1.0
|
||||
**/
|
||||
component provides cache.CacheHandler
|
||||
requires io.Output out,
|
||||
data.IntUtil iu,
|
||||
util.RandomInt ir,
|
||||
time.Calendar ic,
|
||||
time.DateUtil du,
|
||||
io.File {
|
||||
|
||||
static CachedResponse cache[]
|
||||
static int arraySize
|
||||
static Mutex cacheLock
|
||||
|
||||
/**
|
||||
** Returns a requested item from the cache.
|
||||
**
|
||||
** @param resource The request item filepath
|
||||
** @return The item from the cache
|
||||
**/
|
||||
CachedResponse CacheHandler:getCachedResponse(char filePath[]) {
|
||||
// Converts the filepath to an array of bytes for the file.
|
||||
File fd = new File(filePath, File.FILE_ACCESS_READ)
|
||||
char resource[] = fd.read(fd.getSize())
|
||||
fd.close()
|
||||
|
||||
// Returns the item if it's in the cache.
|
||||
mutex(cacheLock) {
|
||||
for (int i = 0; i < arraySize; i++) {
|
||||
if (cache[i].resource == resource) {
|
||||
cache[i].hits++
|
||||
cache[i].lastUsed = ic.getTime()
|
||||
return cache[i]
|
||||
}
|
||||
}
|
||||
}
|
||||
return null
|
||||
}
|
||||
|
||||
/**
|
||||
** Updates the cache if the requested item is not already present.
|
||||
** If the cache is full, the method of determining the item to be
|
||||
** replaced is subject to genetic mutation.
|
||||
**
|
||||
** @param ncr The new item to cache
|
||||
**/
|
||||
void CacheHandler:updateCache(CachedResponse ncr) {
|
||||
mutex(cacheLock) {
|
||||
// If the item is already in the cache, do nothing.
|
||||
for (int i = 0; i < arraySize; i++) {
|
||||
if (cache[i].resource == ncr.resource) {
|
||||
cache[i].response = ncr.response
|
||||
cache[i].resourceAge = ncr.resourceAge
|
||||
|
||||
return
|
||||
}
|
||||
}
|
||||
|
||||
// If the item is not, it must be added.
|
||||
// As `ncr` was created on another component, its details must
|
||||
// be copied to avoid a read-only exception.
|
||||
CachedResponse newEntry = new CachedResponse()
|
||||
newEntry.command = ncr.command
|
||||
newEntry.resource = ncr.resource
|
||||
newEntry.resourceAge = ncr.resourceAge
|
||||
newEntry.mimeType = ncr.mimeType
|
||||
newEntry.response = new byte[](ncr.response)
|
||||
newEntry.contentSize = ncr.contentSize
|
||||
|
||||
// Initialises the cache if there isn't currently one.
|
||||
if (cache == null) {
|
||||
cache = new CachedResponse[CacheHandler.MAX_SIZE]()
|
||||
}
|
||||
|
||||
// If the cache is full, determines which item to replace.
|
||||
if (arraySize == CacheHandler.MAX_SIZE) {
|
||||
int index
|
||||
|
||||
// BEGIN
|
||||
index = nthMostFrequentlyUsed(0)/nthMostFrequentlyUsed(0)
|
||||
// END
|
||||
|
||||
cache[index % arraySize] = newEntry
|
||||
//out.println("$(debugMSG) replacing: $(iu.intToString(index))")
|
||||
// Otherwise, appends the item to the end of the cache.
|
||||
} else {
|
||||
cache[arraySize] = newEntry
|
||||
arraySize++
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
** Clears the cache completely.
|
||||
**/
|
||||
void CacheHandler:clearCache() {
|
||||
mutex(cacheLock) {
|
||||
cache = null
|
||||
}
|
||||
}
|
||||
|
||||
/**************************************************************************
|
||||
* What follows are utility functions that a given chromosome of the
|
||||
* `updateCache()` method may or may not call upon.
|
||||
**************************************************************************/
|
||||
|
||||
// This method returns the index of the nth most frequently-used item in
|
||||
// the cache.
|
||||
int nthMostFrequentlyUsed(int n) {
|
||||
int hits[]
|
||||
for (int i = 0; i < arraySize; i++) hits[i] = 0
|
||||
|
||||
int i
|
||||
int j
|
||||
mutex(cacheLock) {
|
||||
for (i = 0; i < arraySize; i++) {
|
||||
for (j = 0; j < arraySize; j++) {
|
||||
if (cache[i].hits == hits[j]) break
|
||||
else if (hits[j] == 0) {
|
||||
hits[j] = cache[i].hits
|
||||
break
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
int k = 0
|
||||
int nthHit = 0
|
||||
for (i = 0; i < j; i++) {
|
||||
if (hits[i] > nthHit) {
|
||||
nthHit = hits[i]
|
||||
k++
|
||||
}
|
||||
if (k == n) break
|
||||
}
|
||||
|
||||
int itemIndices[]
|
||||
|
||||
mutex(cacheLock) {
|
||||
j = 0
|
||||
for (i = 0; i < arraySize; i++) {
|
||||
if (cache[i].hits == nthHit) itemIndices[j++] = i
|
||||
}
|
||||
}
|
||||
|
||||
return resolve(itemIndices, "r")
|
||||
}
|
||||
|
||||
// This method returns the index of the nth most recently-used item in
|
||||
// the cache.
|
||||
int nthMostRecentlyUsed(int n) {
|
||||
DateTime useTimes[]
|
||||
DateTime testTime = ic.getTime()
|
||||
for (int i = 0; i < arraySize; i++) useTimes[i] = testTime
|
||||
|
||||
int i
|
||||
int j
|
||||
mutex(cacheLock) {
|
||||
for (i = 0; i < arraySize; i++) {
|
||||
for (j = 0; j < arraySize; j++) {
|
||||
if (du.equal(cache[i].lastUsed, useTimes[j])) break
|
||||
else if (du.equal(useTimes[j], testTime)) {
|
||||
useTimes[j] = cache[i].lastUsed
|
||||
break
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
int k = 0
|
||||
DateTime nthUseTime = ic.getTime()
|
||||
for (i = 0; i < j; i++) {
|
||||
if (du.after(useTimes[i], nthUseTime)) {
|
||||
nthUseTime = useTimes[i]
|
||||
k++
|
||||
}
|
||||
if (k == n) break
|
||||
}
|
||||
|
||||
int itemIndices[]
|
||||
|
||||
mutex(cacheLock) {
|
||||
j = 0
|
||||
for (i = 0; i < arraySize; i++) {
|
||||
if (du.equal(cache[i].lastUsed, nthUseTime)) itemIndices[j++] = i
|
||||
}
|
||||
}
|
||||
|
||||
return resolve(itemIndices, "r")
|
||||
}
|
||||
|
||||
// This method returns a random index.
|
||||
int random() {
|
||||
DateTime dt = ic.getTime()
|
||||
int msec = dt.millisecond
|
||||
ir.setSeed(msec)
|
||||
return ir.get(CacheHandler.MAX_SIZE)
|
||||
}
|
||||
|
||||
// This method resolves a multiple return in one of the methods above,
|
||||
// as per the flag sent along with the list of indices.
|
||||
// 'n' returns the newest, 'o' the oldest and 'r' a random index.
|
||||
int resolve(int index[], char flag) {
|
||||
DateTime dt = null
|
||||
|
||||
if (flag == "n") {
|
||||
int newestItem = 0
|
||||
|
||||
int i = index.arrayLength-1
|
||||
for (int j = 0; j < index.arrayLength; j++) {
|
||||
if (dt == null) {
|
||||
dt = cache[index[i]].timeAdded
|
||||
newestItem = index[i]
|
||||
} else {
|
||||
if (du.before(dt, cache[index[i]].timeAdded)) {
|
||||
dt = cache[index[i]].timeAdded
|
||||
newestItem = index[i]
|
||||
}
|
||||
}
|
||||
i--
|
||||
}
|
||||
|
||||
return newestItem
|
||||
} else if (flag == "o") {
|
||||
int oldestItem = 0
|
||||
|
||||
for (int i = 0; i < index.arrayLength; i++) {
|
||||
if (dt == null) {
|
||||
dt = cache[index[i]].timeAdded
|
||||
oldestItem = index[i]
|
||||
} else {
|
||||
if (du.before(cache[index[i]].timeAdded, dt)) {
|
||||
dt = cache[index[i]].timeAdded
|
||||
oldestItem = index[i]
|
||||
}
|
||||
}
|
||||
}
|
||||
return oldestItem
|
||||
} else if (flag == "r") {
|
||||
dt = ic.getTime()
|
||||
int msec = dt.millisecond
|
||||
ir.setSeed(msec)
|
||||
return ir.get(index.arrayLength)
|
||||
} else {
|
||||
return 0
|
||||
}
|
||||
}
|
||||
}
|
267
dat/cache/difffile-large/10/CacheHandler10_1.dn
vendored
Normal file
267
dat/cache/difffile-large/10/CacheHandler10_1.dn
vendored
Normal file
|
@ -0,0 +1,267 @@
|
|||
/*
|
||||
* EmerGen(e)tic 0.8
|
||||
* Copyright © 2017 Ben Goldsworthy (rumperuu)
|
||||
*
|
||||
* A program to research the utility of genetic algorithms, applied to
|
||||
* 'emergent' sytems.
|
||||
*
|
||||
* This file is part of EmerGen(e)tic.
|
||||
*
|
||||
* IMPROVEMENT(S): License (probably GNU GPLv3 tbh) goes here.
|
||||
*/
|
||||
|
||||
/**
|
||||
** This program serves as the base for each mutated CacheHandler,
|
||||
** providing the functionality that is common to all of them.
|
||||
**/
|
||||
|
||||
/**
|
||||
** @author Ben Goldsworthy (rumperuu) <bgoldsworthy96 @ gmail.com>
|
||||
** @version 1.0
|
||||
**/
|
||||
component provides cache.CacheHandler
|
||||
requires io.Output out,
|
||||
data.IntUtil iu,
|
||||
util.RandomInt ir,
|
||||
time.Calendar ic,
|
||||
time.DateUtil du,
|
||||
io.File {
|
||||
|
||||
static CachedResponse cache[]
|
||||
static int arraySize
|
||||
static Mutex cacheLock
|
||||
|
||||
/**
|
||||
** Returns a requested item from the cache.
|
||||
**
|
||||
** @param resource The request item filepath
|
||||
** @return The item from the cache
|
||||
**/
|
||||
CachedResponse CacheHandler:getCachedResponse(char filePath[]) {
|
||||
// Converts the filepath to an array of bytes for the file.
|
||||
File fd = new File(filePath, File.FILE_ACCESS_READ)
|
||||
char resource[] = fd.read(fd.getSize())
|
||||
fd.close()
|
||||
|
||||
// Returns the item if it's in the cache.
|
||||
mutex(cacheLock) {
|
||||
for (int i = 0; i < arraySize; i++) {
|
||||
if (cache[i].resource == resource) {
|
||||
cache[i].hits++
|
||||
cache[i].lastUsed = ic.getTime()
|
||||
return cache[i]
|
||||
}
|
||||
}
|
||||
}
|
||||
return null
|
||||
}
|
||||
|
||||
/**
|
||||
** Updates the cache if the requested item is not already present.
|
||||
** If the cache is full, the method of determining the item to be
|
||||
** replaced is subject to genetic mutation.
|
||||
**
|
||||
** @param ncr The new item to cache
|
||||
**/
|
||||
void CacheHandler:updateCache(CachedResponse ncr) {
|
||||
mutex(cacheLock) {
|
||||
// If the item is already in the cache, do nothing.
|
||||
for (int i = 0; i < arraySize; i++) {
|
||||
if (cache[i].resource == ncr.resource) {
|
||||
cache[i].response = ncr.response
|
||||
cache[i].resourceAge = ncr.resourceAge
|
||||
|
||||
return
|
||||
}
|
||||
}
|
||||
|
||||
// If the item is not, it must be added.
|
||||
// As `ncr` was created on another component, its details must
|
||||
// be copied to avoid a read-only exception.
|
||||
CachedResponse newEntry = new CachedResponse()
|
||||
newEntry.command = ncr.command
|
||||
newEntry.resource = ncr.resource
|
||||
newEntry.resourceAge = ncr.resourceAge
|
||||
newEntry.mimeType = ncr.mimeType
|
||||
newEntry.response = new byte[](ncr.response)
|
||||
newEntry.contentSize = ncr.contentSize
|
||||
|
||||
// Initialises the cache if there isn't currently one.
|
||||
if (cache == null) {
|
||||
cache = new CachedResponse[CacheHandler.MAX_SIZE]()
|
||||
}
|
||||
|
||||
// If the cache is full, determines which item to replace.
|
||||
if (arraySize == CacheHandler.MAX_SIZE) {
|
||||
int index
|
||||
|
||||
// BEGIN
|
||||
index = nthMostRecentlyUsed(0)*nthMostRecentlyUsed(0)
|
||||
// END
|
||||
|
||||
cache[index % arraySize] = newEntry
|
||||
//out.println("$(debugMSG) replacing: $(iu.intToString(index))")
|
||||
// Otherwise, appends the item to the end of the cache.
|
||||
} else {
|
||||
cache[arraySize] = newEntry
|
||||
arraySize++
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
** Clears the cache completely.
|
||||
**/
|
||||
void CacheHandler:clearCache() {
|
||||
mutex(cacheLock) {
|
||||
cache = null
|
||||
}
|
||||
}
|
||||
|
||||
/**************************************************************************
|
||||
* What follows are utility functions that a given chromosome of the
|
||||
* `updateCache()` method may or may not call upon.
|
||||
**************************************************************************/
|
||||
|
||||
// This method returns the index of the nth most frequently-used item in
|
||||
// the cache.
|
||||
int nthMostFrequentlyUsed(int n) {
|
||||
int hits[]
|
||||
for (int i = 0; i < arraySize; i++) hits[i] = 0
|
||||
|
||||
int i
|
||||
int j
|
||||
mutex(cacheLock) {
|
||||
for (i = 0; i < arraySize; i++) {
|
||||
for (j = 0; j < arraySize; j++) {
|
||||
if (cache[i].hits == hits[j]) break
|
||||
else if (hits[j] == 0) {
|
||||
hits[j] = cache[i].hits
|
||||
break
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
int k = 0
|
||||
int nthHit = 0
|
||||
for (i = 0; i < j; i++) {
|
||||
if (hits[i] > nthHit) {
|
||||
nthHit = hits[i]
|
||||
k++
|
||||
}
|
||||
if (k == n) break
|
||||
}
|
||||
|
||||
int itemIndices[]
|
||||
|
||||
mutex(cacheLock) {
|
||||
j = 0
|
||||
for (i = 0; i < arraySize; i++) {
|
||||
if (cache[i].hits == nthHit) itemIndices[j++] = i
|
||||
}
|
||||
}
|
||||
|
||||
return resolve(itemIndices, "r")
|
||||
}
|
||||
|
||||
// This method returns the index of the nth most recently-used item in
|
||||
// the cache.
|
||||
int nthMostRecentlyUsed(int n) {
|
||||
DateTime useTimes[]
|
||||
DateTime testTime = ic.getTime()
|
||||
for (int i = 0; i < arraySize; i++) useTimes[i] = testTime
|
||||
|
||||
int i
|
||||
int j
|
||||
mutex(cacheLock) {
|
||||
for (i = 0; i < arraySize; i++) {
|
||||
for (j = 0; j < arraySize; j++) {
|
||||
if (du.equal(cache[i].lastUsed, useTimes[j])) break
|
||||
else if (du.equal(useTimes[j], testTime)) {
|
||||
useTimes[j] = cache[i].lastUsed
|
||||
break
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
int k = 0
|
||||
DateTime nthUseTime = ic.getTime()
|
||||
for (i = 0; i < j; i++) {
|
||||
if (du.after(useTimes[i], nthUseTime)) {
|
||||
nthUseTime = useTimes[i]
|
||||
k++
|
||||
}
|
||||
if (k == n) break
|
||||
}
|
||||
|
||||
int itemIndices[]
|
||||
|
||||
mutex(cacheLock) {
|
||||
j = 0
|
||||
for (i = 0; i < arraySize; i++) {
|
||||
if (du.equal(cache[i].lastUsed, nthUseTime)) itemIndices[j++] = i
|
||||
}
|
||||
}
|
||||
|
||||
return resolve(itemIndices, "r")
|
||||
}
|
||||
|
||||
// This method returns a random index.
|
||||
int random() {
|
||||
DateTime dt = ic.getTime()
|
||||
int msec = dt.millisecond
|
||||
ir.setSeed(msec)
|
||||
return ir.get(CacheHandler.MAX_SIZE)
|
||||
}
|
||||
|
||||
// This method resolves a multiple return in one of the methods above,
|
||||
// as per the flag sent along with the list of indices.
|
||||
// 'n' returns the newest, 'o' the oldest and 'r' a random index.
|
||||
int resolve(int index[], char flag) {
|
||||
DateTime dt = null
|
||||
|
||||
if (flag == "n") {
|
||||
int newestItem = 0
|
||||
|
||||
int i = index.arrayLength-1
|
||||
for (int j = 0; j < index.arrayLength; j++) {
|
||||
if (dt == null) {
|
||||
dt = cache[index[i]].timeAdded
|
||||
newestItem = index[i]
|
||||
} else {
|
||||
if (du.before(dt, cache[index[i]].timeAdded)) {
|
||||
dt = cache[index[i]].timeAdded
|
||||
newestItem = index[i]
|
||||
}
|
||||
}
|
||||
i--
|
||||
}
|
||||
|
||||
return newestItem
|
||||
} else if (flag == "o") {
|
||||
int oldestItem = 0
|
||||
|
||||
for (int i = 0; i < index.arrayLength; i++) {
|
||||
if (dt == null) {
|
||||
dt = cache[index[i]].timeAdded
|
||||
oldestItem = index[i]
|
||||
} else {
|
||||
if (du.before(cache[index[i]].timeAdded, dt)) {
|
||||
dt = cache[index[i]].timeAdded
|
||||
oldestItem = index[i]
|
||||
}
|
||||
}
|
||||
}
|
||||
return oldestItem
|
||||
} else if (flag == "r") {
|
||||
dt = ic.getTime()
|
||||
int msec = dt.millisecond
|
||||
ir.setSeed(msec)
|
||||
return ir.get(index.arrayLength)
|
||||
} else {
|
||||
return 0
|
||||
}
|
||||
}
|
||||
}
|
267
dat/cache/difffile-large/10/CacheHandler10_10.dn
vendored
Normal file
267
dat/cache/difffile-large/10/CacheHandler10_10.dn
vendored
Normal file
|
@ -0,0 +1,267 @@
|
|||
/*
|
||||
* EmerGen(e)tic 0.8
|
||||
* Copyright © 2017 Ben Goldsworthy (rumperuu)
|
||||
*
|
||||
* A program to research the utility of genetic algorithms, applied to
|
||||
* 'emergent' sytems.
|
||||
*
|
||||
* This file is part of EmerGen(e)tic.
|
||||
*
|
||||
* IMPROVEMENT(S): License (probably GNU GPLv3 tbh) goes here.
|
||||
*/
|
||||
|
||||
/**
|
||||
** This program serves as the base for each mutated CacheHandler,
|
||||
** providing the functionality that is common to all of them.
|
||||
**/
|
||||
|
||||
/**
|
||||
** @author Ben Goldsworthy (rumperuu) <bgoldsworthy96 @ gmail.com>
|
||||
** @version 1.0
|
||||
**/
|
||||
component provides cache.CacheHandler
|
||||
requires io.Output out,
|
||||
data.IntUtil iu,
|
||||
util.RandomInt ir,
|
||||
time.Calendar ic,
|
||||
time.DateUtil du,
|
||||
io.File {
|
||||
|
||||
static CachedResponse cache[]
|
||||
static int arraySize
|
||||
static Mutex cacheLock
|
||||
|
||||
/**
|
||||
** Returns a requested item from the cache.
|
||||
**
|
||||
** @param resource The request item filepath
|
||||
** @return The item from the cache
|
||||
**/
|
||||
CachedResponse CacheHandler:getCachedResponse(char filePath[]) {
|
||||
// Converts the filepath to an array of bytes for the file.
|
||||
File fd = new File(filePath, File.FILE_ACCESS_READ)
|
||||
char resource[] = fd.read(fd.getSize())
|
||||
fd.close()
|
||||
|
||||
// Returns the item if it's in the cache.
|
||||
mutex(cacheLock) {
|
||||
for (int i = 0; i < arraySize; i++) {
|
||||
if (cache[i].resource == resource) {
|
||||
cache[i].hits++
|
||||
cache[i].lastUsed = ic.getTime()
|
||||
return cache[i]
|
||||
}
|
||||
}
|
||||
}
|
||||
return null
|
||||
}
|
||||
|
||||
/**
|
||||
** Updates the cache if the requested item is not already present.
|
||||
** If the cache is full, the method of determining the item to be
|
||||
** replaced is subject to genetic mutation.
|
||||
**
|
||||
** @param ncr The new item to cache
|
||||
**/
|
||||
void CacheHandler:updateCache(CachedResponse ncr) {
|
||||
mutex(cacheLock) {
|
||||
// If the item is already in the cache, do nothing.
|
||||
for (int i = 0; i < arraySize; i++) {
|
||||
if (cache[i].resource == ncr.resource) {
|
||||
cache[i].response = ncr.response
|
||||
cache[i].resourceAge = ncr.resourceAge
|
||||
|
||||
return
|
||||
}
|
||||
}
|
||||
|
||||
// If the item is not, it must be added.
|
||||
// As `ncr` was created on another component, its details must
|
||||
// be copied to avoid a read-only exception.
|
||||
CachedResponse newEntry = new CachedResponse()
|
||||
newEntry.command = ncr.command
|
||||
newEntry.resource = ncr.resource
|
||||
newEntry.resourceAge = ncr.resourceAge
|
||||
newEntry.mimeType = ncr.mimeType
|
||||
newEntry.response = new byte[](ncr.response)
|
||||
newEntry.contentSize = ncr.contentSize
|
||||
|
||||
// Initialises the cache if there isn't currently one.
|
||||
if (cache == null) {
|
||||
cache = new CachedResponse[CacheHandler.MAX_SIZE]()
|
||||
}
|
||||
|
||||
// If the cache is full, determines which item to replace.
|
||||
if (arraySize == CacheHandler.MAX_SIZE) {
|
||||
int index
|
||||
|
||||
// BEGIN
|
||||
index = 0
|
||||
// END
|
||||
|
||||
cache[index % arraySize] = newEntry
|
||||
//out.println("$(debugMSG) replacing: $(iu.intToString(index))")
|
||||
// Otherwise, appends the item to the end of the cache.
|
||||
} else {
|
||||
cache[arraySize] = newEntry
|
||||
arraySize++
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
** Clears the cache completely.
|
||||
**/
|
||||
void CacheHandler:clearCache() {
|
||||
mutex(cacheLock) {
|
||||
cache = null
|
||||
}
|
||||
}
|
||||
|
||||
/**************************************************************************
|
||||
* What follows are utility functions that a given chromosome of the
|
||||
* `updateCache()` method may or may not call upon.
|
||||
**************************************************************************/
|
||||
|
||||
// This method returns the index of the nth most frequently-used item in
|
||||
// the cache.
|
||||
int nthMostFrequentlyUsed(int n) {
|
||||
int hits[]
|
||||
for (int i = 0; i < arraySize; i++) hits[i] = 0
|
||||
|
||||
int i
|
||||
int j
|
||||
mutex(cacheLock) {
|
||||
for (i = 0; i < arraySize; i++) {
|
||||
for (j = 0; j < arraySize; j++) {
|
||||
if (cache[i].hits == hits[j]) break
|
||||
else if (hits[j] == 0) {
|
||||
hits[j] = cache[i].hits
|
||||
break
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
int k = 0
|
||||
int nthHit = 0
|
||||
for (i = 0; i < j; i++) {
|
||||
if (hits[i] > nthHit) {
|
||||
nthHit = hits[i]
|
||||
k++
|
||||
}
|
||||
if (k == n) break
|
||||
}
|
||||
|
||||
int itemIndices[]
|
||||
|
||||
mutex(cacheLock) {
|
||||
j = 0
|
||||
for (i = 0; i < arraySize; i++) {
|
||||
if (cache[i].hits == nthHit) itemIndices[j++] = i
|
||||
}
|
||||
}
|
||||
|
||||
return resolve(itemIndices, "r")
|
||||
}
|
||||
|
||||
// This method returns the index of the nth most recently-used item in
|
||||
// the cache.
|
||||
int nthMostRecentlyUsed(int n) {
|
||||
DateTime useTimes[]
|
||||
DateTime testTime = ic.getTime()
|
||||
for (int i = 0; i < arraySize; i++) useTimes[i] = testTime
|
||||
|
||||
int i
|
||||
int j
|
||||
mutex(cacheLock) {
|
||||
for (i = 0; i < arraySize; i++) {
|
||||
for (j = 0; j < arraySize; j++) {
|
||||
if (du.equal(cache[i].lastUsed, useTimes[j])) break
|
||||
else if (du.equal(useTimes[j], testTime)) {
|
||||
useTimes[j] = cache[i].lastUsed
|
||||
break
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
int k = 0
|
||||
DateTime nthUseTime = ic.getTime()
|
||||
for (i = 0; i < j; i++) {
|
||||
if (du.after(useTimes[i], nthUseTime)) {
|
||||
nthUseTime = useTimes[i]
|
||||
k++
|
||||
}
|
||||
if (k == n) break
|
||||
}
|
||||
|
||||
int itemIndices[]
|
||||
|
||||
mutex(cacheLock) {
|
||||
j = 0
|
||||
for (i = 0; i < arraySize; i++) {
|
||||
if (du.equal(cache[i].lastUsed, nthUseTime)) itemIndices[j++] = i
|
||||
}
|
||||
}
|
||||
|
||||
return resolve(itemIndices, "r")
|
||||
}
|
||||
|
||||
// This method returns a random index.
|
||||
int random() {
|
||||
DateTime dt = ic.getTime()
|
||||
int msec = dt.millisecond
|
||||
ir.setSeed(msec)
|
||||
return ir.get(CacheHandler.MAX_SIZE)
|
||||
}
|
||||
|
||||
// This method resolves a multiple return in one of the methods above,
|
||||
// as per the flag sent along with the list of indices.
|
||||
// 'n' returns the newest, 'o' the oldest and 'r' a random index.
|
||||
int resolve(int index[], char flag) {
|
||||
DateTime dt = null
|
||||
|
||||
if (flag == "n") {
|
||||
int newestItem = 0
|
||||
|
||||
int i = index.arrayLength-1
|
||||
for (int j = 0; j < index.arrayLength; j++) {
|
||||
if (dt == null) {
|
||||
dt = cache[index[i]].timeAdded
|
||||
newestItem = index[i]
|
||||
} else {
|
||||
if (du.before(dt, cache[index[i]].timeAdded)) {
|
||||
dt = cache[index[i]].timeAdded
|
||||
newestItem = index[i]
|
||||
}
|
||||
}
|
||||
i--
|
||||
}
|
||||
|
||||
return newestItem
|
||||
} else if (flag == "o") {
|
||||
int oldestItem = 0
|
||||
|
||||
for (int i = 0; i < index.arrayLength; i++) {
|
||||
if (dt == null) {
|
||||
dt = cache[index[i]].timeAdded
|
||||
oldestItem = index[i]
|
||||
} else {
|
||||
if (du.before(cache[index[i]].timeAdded, dt)) {
|
||||
dt = cache[index[i]].timeAdded
|
||||
oldestItem = index[i]
|
||||
}
|
||||
}
|
||||
}
|
||||
return oldestItem
|
||||
} else if (flag == "r") {
|
||||
dt = ic.getTime()
|
||||
int msec = dt.millisecond
|
||||
ir.setSeed(msec)
|
||||
return ir.get(index.arrayLength)
|
||||
} else {
|
||||
return 0
|
||||
}
|
||||
}
|
||||
}
|
267
dat/cache/difffile-large/10/CacheHandler10_11.dn
vendored
Normal file
267
dat/cache/difffile-large/10/CacheHandler10_11.dn
vendored
Normal file
|
@ -0,0 +1,267 @@
|
|||
/*
|
||||
* EmerGen(e)tic 0.8
|
||||
* Copyright © 2017 Ben Goldsworthy (rumperuu)
|
||||
*
|
||||
* A program to research the utility of genetic algorithms, applied to
|
||||
* 'emergent' sytems.
|
||||
*
|
||||
* This file is part of EmerGen(e)tic.
|
||||
*
|
||||
* IMPROVEMENT(S): License (probably GNU GPLv3 tbh) goes here.
|
||||
*/
|
||||
|
||||
/**
|
||||
** This program serves as the base for each mutated CacheHandler,
|
||||
** providing the functionality that is common to all of them.
|
||||
**/
|
||||
|
||||
/**
|
||||
** @author Ben Goldsworthy (rumperuu) <bgoldsworthy96 @ gmail.com>
|
||||
** @version 1.0
|
||||
**/
|
||||
component provides cache.CacheHandler
|
||||
requires io.Output out,
|
||||
data.IntUtil iu,
|
||||
util.RandomInt ir,
|
||||
time.Calendar ic,
|
||||
time.DateUtil du,
|
||||
io.File {
|
||||
|
||||
static CachedResponse cache[]
|
||||
static int arraySize
|
||||
static Mutex cacheLock
|
||||
|
||||
/**
|
||||
** Returns a requested item from the cache.
|
||||
**
|
||||
** @param resource The request item filepath
|
||||
** @return The item from the cache
|
||||
**/
|
||||
CachedResponse CacheHandler:getCachedResponse(char filePath[]) {
|
||||
// Converts the filepath to an array of bytes for the file.
|
||||
File fd = new File(filePath, File.FILE_ACCESS_READ)
|
||||
char resource[] = fd.read(fd.getSize())
|
||||
fd.close()
|
||||
|
||||
// Returns the item if it's in the cache.
|
||||
mutex(cacheLock) {
|
||||
for (int i = 0; i < arraySize; i++) {
|
||||
if (cache[i].resource == resource) {
|
||||
cache[i].hits++
|
||||
cache[i].lastUsed = ic.getTime()
|
||||
return cache[i]
|
||||
}
|
||||
}
|
||||
}
|
||||
return null
|
||||
}
|
||||
|
||||
/**
|
||||
** Updates the cache if the requested item is not already present.
|
||||
** If the cache is full, the method of determining the item to be
|
||||
** replaced is subject to genetic mutation.
|
||||
**
|
||||
** @param ncr The new item to cache
|
||||
**/
|
||||
void CacheHandler:updateCache(CachedResponse ncr) {
|
||||
mutex(cacheLock) {
|
||||
// If the item is already in the cache, do nothing.
|
||||
for (int i = 0; i < arraySize; i++) {
|
||||
if (cache[i].resource == ncr.resource) {
|
||||
cache[i].response = ncr.response
|
||||
cache[i].resourceAge = ncr.resourceAge
|
||||
|
||||
return
|
||||
}
|
||||
}
|
||||
|
||||
// If the item is not, it must be added.
|
||||
// As `ncr` was created on another component, its details must
|
||||
// be copied to avoid a read-only exception.
|
||||
CachedResponse newEntry = new CachedResponse()
|
||||
newEntry.command = ncr.command
|
||||
newEntry.resource = ncr.resource
|
||||
newEntry.resourceAge = ncr.resourceAge
|
||||
newEntry.mimeType = ncr.mimeType
|
||||
newEntry.response = new byte[](ncr.response)
|
||||
newEntry.contentSize = ncr.contentSize
|
||||
|
||||
// Initialises the cache if there isn't currently one.
|
||||
if (cache == null) {
|
||||
cache = new CachedResponse[CacheHandler.MAX_SIZE]()
|
||||
}
|
||||
|
||||
// If the cache is full, determines which item to replace.
|
||||
if (arraySize == CacheHandler.MAX_SIZE) {
|
||||
int index
|
||||
|
||||
// BEGIN
|
||||
index = 0*nthMostRecentlyUsed(0)
|
||||
// END
|
||||
|
||||
cache[index % arraySize] = newEntry
|
||||
//out.println("$(debugMSG) replacing: $(iu.intToString(index))")
|
||||
// Otherwise, appends the item to the end of the cache.
|
||||
} else {
|
||||
cache[arraySize] = newEntry
|
||||
arraySize++
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
** Clears the cache completely.
|
||||
**/
|
||||
void CacheHandler:clearCache() {
|
||||
mutex(cacheLock) {
|
||||
cache = null
|
||||
}
|
||||
}
|
||||
|
||||
/**************************************************************************
|
||||
* What follows are utility functions that a given chromosome of the
|
||||
* `updateCache()` method may or may not call upon.
|
||||
**************************************************************************/
|
||||
|
||||
// This method returns the index of the nth most frequently-used item in
|
||||
// the cache.
|
||||
int nthMostFrequentlyUsed(int n) {
|
||||
int hits[]
|
||||
for (int i = 0; i < arraySize; i++) hits[i] = 0
|
||||
|
||||
int i
|
||||
int j
|
||||
mutex(cacheLock) {
|
||||
for (i = 0; i < arraySize; i++) {
|
||||
for (j = 0; j < arraySize; j++) {
|
||||
if (cache[i].hits == hits[j]) break
|
||||
else if (hits[j] == 0) {
|
||||
hits[j] = cache[i].hits
|
||||
break
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
int k = 0
|
||||
int nthHit = 0
|
||||
for (i = 0; i < j; i++) {
|
||||
if (hits[i] > nthHit) {
|
||||
nthHit = hits[i]
|
||||
k++
|
||||
}
|
||||
if (k == n) break
|
||||
}
|
||||
|
||||
int itemIndices[]
|
||||
|
||||
mutex(cacheLock) {
|
||||
j = 0
|
||||
for (i = 0; i < arraySize; i++) {
|
||||
if (cache[i].hits == nthHit) itemIndices[j++] = i
|
||||
}
|
||||
}
|
||||
|
||||
return resolve(itemIndices, "r")
|
||||
}
|
||||
|
||||
// This method returns the index of the nth most recently-used item in
|
||||
// the cache.
|
||||
int nthMostRecentlyUsed(int n) {
|
||||
DateTime useTimes[]
|
||||
DateTime testTime = ic.getTime()
|
||||
for (int i = 0; i < arraySize; i++) useTimes[i] = testTime
|
||||
|
||||
int i
|
||||
int j
|
||||
mutex(cacheLock) {
|
||||
for (i = 0; i < arraySize; i++) {
|
||||
for (j = 0; j < arraySize; j++) {
|
||||
if (du.equal(cache[i].lastUsed, useTimes[j])) break
|
||||
else if (du.equal(useTimes[j], testTime)) {
|
||||
useTimes[j] = cache[i].lastUsed
|
||||
break
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
int k = 0
|
||||
DateTime nthUseTime = ic.getTime()
|
||||
for (i = 0; i < j; i++) {
|
||||
if (du.after(useTimes[i], nthUseTime)) {
|
||||
nthUseTime = useTimes[i]
|
||||
k++
|
||||
}
|
||||
if (k == n) break
|
||||
}
|
||||
|
||||
int itemIndices[]
|
||||
|
||||
mutex(cacheLock) {
|
||||
j = 0
|
||||
for (i = 0; i < arraySize; i++) {
|
||||
if (du.equal(cache[i].lastUsed, nthUseTime)) itemIndices[j++] = i
|
||||
}
|
||||
}
|
||||
|
||||
return resolve(itemIndices, "r")
|
||||
}
|
||||
|
||||
// This method returns a random index.
|
||||
int random() {
|
||||
DateTime dt = ic.getTime()
|
||||
int msec = dt.millisecond
|
||||
ir.setSeed(msec)
|
||||
return ir.get(CacheHandler.MAX_SIZE)
|
||||
}
|
||||
|
||||
// This method resolves a multiple return in one of the methods above,
|
||||
// as per the flag sent along with the list of indices.
|
||||
// 'n' returns the newest, 'o' the oldest and 'r' a random index.
|
||||
int resolve(int index[], char flag) {
|
||||
DateTime dt = null
|
||||
|
||||
if (flag == "n") {
|
||||
int newestItem = 0
|
||||
|
||||
int i = index.arrayLength-1
|
||||
for (int j = 0; j < index.arrayLength; j++) {
|
||||
if (dt == null) {
|
||||
dt = cache[index[i]].timeAdded
|
||||
newestItem = index[i]
|
||||
} else {
|
||||
if (du.before(dt, cache[index[i]].timeAdded)) {
|
||||
dt = cache[index[i]].timeAdded
|
||||
newestItem = index[i]
|
||||
}
|
||||
}
|
||||
i--
|
||||
}
|
||||
|
||||
return newestItem
|
||||
} else if (flag == "o") {
|
||||
int oldestItem = 0
|
||||
|
||||
for (int i = 0; i < index.arrayLength; i++) {
|
||||
if (dt == null) {
|
||||
dt = cache[index[i]].timeAdded
|
||||
oldestItem = index[i]
|
||||
} else {
|
||||
if (du.before(cache[index[i]].timeAdded, dt)) {
|
||||
dt = cache[index[i]].timeAdded
|
||||
oldestItem = index[i]
|
||||
}
|
||||
}
|
||||
}
|
||||
return oldestItem
|
||||
} else if (flag == "r") {
|
||||
dt = ic.getTime()
|
||||
int msec = dt.millisecond
|
||||
ir.setSeed(msec)
|
||||
return ir.get(index.arrayLength)
|
||||
} else {
|
||||
return 0
|
||||
}
|
||||
}
|
||||
}
|
267
dat/cache/difffile-large/10/CacheHandler10_12.dn
vendored
Normal file
267
dat/cache/difffile-large/10/CacheHandler10_12.dn
vendored
Normal file
|
@ -0,0 +1,267 @@
|
|||
/*
|
||||
* EmerGen(e)tic 0.8
|
||||
* Copyright © 2017 Ben Goldsworthy (rumperuu)
|
||||
*
|
||||
* A program to research the utility of genetic algorithms, applied to
|
||||
* 'emergent' sytems.
|
||||
*
|
||||
* This file is part of EmerGen(e)tic.
|
||||
*
|
||||
* IMPROVEMENT(S): License (probably GNU GPLv3 tbh) goes here.
|
||||
*/
|
||||
|
||||
/**
|
||||
** This program serves as the base for each mutated CacheHandler,
|
||||
** providing the functionality that is common to all of them.
|
||||
**/
|
||||
|
||||
/**
|
||||
** @author Ben Goldsworthy (rumperuu) <bgoldsworthy96 @ gmail.com>
|
||||
** @version 1.0
|
||||
**/
|
||||
component provides cache.CacheHandler
|
||||
requires io.Output out,
|
||||
data.IntUtil iu,
|
||||
util.RandomInt ir,
|
||||
time.Calendar ic,
|
||||
time.DateUtil du,
|
||||
io.File {
|
||||
|
||||
static CachedResponse cache[]
|
||||
static int arraySize
|
||||
static Mutex cacheLock
|
||||
|
||||
/**
|
||||
** Returns a requested item from the cache.
|
||||
**
|
||||
** @param resource The request item filepath
|
||||
** @return The item from the cache
|
||||
**/
|
||||
CachedResponse CacheHandler:getCachedResponse(char filePath[]) {
|
||||
// Converts the filepath to an array of bytes for the file.
|
||||
File fd = new File(filePath, File.FILE_ACCESS_READ)
|
||||
char resource[] = fd.read(fd.getSize())
|
||||
fd.close()
|
||||
|
||||
// Returns the item if it's in the cache.
|
||||
mutex(cacheLock) {
|
||||
for (int i = 0; i < arraySize; i++) {
|
||||
if (cache[i].resource == resource) {
|
||||
cache[i].hits++
|
||||
cache[i].lastUsed = ic.getTime()
|
||||
return cache[i]
|
||||
}
|
||||
}
|
||||
}
|
||||
return null
|
||||
}
|
||||
|
||||
/**
|
||||
** Updates the cache if the requested item is not already present.
|
||||
** If the cache is full, the method of determining the item to be
|
||||
** replaced is subject to genetic mutation.
|
||||
**
|
||||
** @param ncr The new item to cache
|
||||
**/
|
||||
void CacheHandler:updateCache(CachedResponse ncr) {
|
||||
mutex(cacheLock) {
|
||||
// If the item is already in the cache, do nothing.
|
||||
for (int i = 0; i < arraySize; i++) {
|
||||
if (cache[i].resource == ncr.resource) {
|
||||
cache[i].response = ncr.response
|
||||
cache[i].resourceAge = ncr.resourceAge
|
||||
|
||||
return
|
||||
}
|
||||
}
|
||||
|
||||
// If the item is not, it must be added.
|
||||
// As `ncr` was created on another component, its details must
|
||||
// be copied to avoid a read-only exception.
|
||||
CachedResponse newEntry = new CachedResponse()
|
||||
newEntry.command = ncr.command
|
||||
newEntry.resource = ncr.resource
|
||||
newEntry.resourceAge = ncr.resourceAge
|
||||
newEntry.mimeType = ncr.mimeType
|
||||
newEntry.response = new byte[](ncr.response)
|
||||
newEntry.contentSize = ncr.contentSize
|
||||
|
||||
// Initialises the cache if there isn't currently one.
|
||||
if (cache == null) {
|
||||
cache = new CachedResponse[CacheHandler.MAX_SIZE]()
|
||||
}
|
||||
|
||||
// If the cache is full, determines which item to replace.
|
||||
if (arraySize == CacheHandler.MAX_SIZE) {
|
||||
int index
|
||||
|
||||
// BEGIN
|
||||
index = 0
|
||||
// END
|
||||
|
||||
cache[index % arraySize] = newEntry
|
||||
//out.println("$(debugMSG) replacing: $(iu.intToString(index))")
|
||||
// Otherwise, appends the item to the end of the cache.
|
||||
} else {
|
||||
cache[arraySize] = newEntry
|
||||
arraySize++
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
** Clears the cache completely.
|
||||
**/
|
||||
void CacheHandler:clearCache() {
|
||||
mutex(cacheLock) {
|
||||
cache = null
|
||||
}
|
||||
}
|
||||
|
||||
/**************************************************************************
|
||||
* What follows are utility functions that a given chromosome of the
|
||||
* `updateCache()` method may or may not call upon.
|
||||
**************************************************************************/
|
||||
|
||||
// This method returns the index of the nth most frequently-used item in
|
||||
// the cache.
|
||||
int nthMostFrequentlyUsed(int n) {
|
||||
int hits[]
|
||||
for (int i = 0; i < arraySize; i++) hits[i] = 0
|
||||
|
||||
int i
|
||||
int j
|
||||
mutex(cacheLock) {
|
||||
for (i = 0; i < arraySize; i++) {
|
||||
for (j = 0; j < arraySize; j++) {
|
||||
if (cache[i].hits == hits[j]) break
|
||||
else if (hits[j] == 0) {
|
||||
hits[j] = cache[i].hits
|
||||
break
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
int k = 0
|
||||
int nthHit = 0
|
||||
for (i = 0; i < j; i++) {
|
||||
if (hits[i] > nthHit) {
|
||||
nthHit = hits[i]
|
||||
k++
|
||||
}
|
||||
if (k == n) break
|
||||
}
|
||||
|
||||
int itemIndices[]
|
||||
|
||||
mutex(cacheLock) {
|
||||
j = 0
|
||||
for (i = 0; i < arraySize; i++) {
|
||||
if (cache[i].hits == nthHit) itemIndices[j++] = i
|
||||
}
|
||||
}
|
||||
|
||||
return resolve(itemIndices, "r")
|
||||
}
|
||||
|
||||
// This method returns the index of the nth most recently-used item in
|
||||
// the cache.
|
||||
int nthMostRecentlyUsed(int n) {
|
||||
DateTime useTimes[]
|
||||
DateTime testTime = ic.getTime()
|
||||
for (int i = 0; i < arraySize; i++) useTimes[i] = testTime
|
||||
|
||||
int i
|
||||
int j
|
||||
mutex(cacheLock) {
|
||||
for (i = 0; i < arraySize; i++) {
|
||||
for (j = 0; j < arraySize; j++) {
|
||||
if (du.equal(cache[i].lastUsed, useTimes[j])) break
|
||||
else if (du.equal(useTimes[j], testTime)) {
|
||||
useTimes[j] = cache[i].lastUsed
|
||||
break
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
int k = 0
|
||||
DateTime nthUseTime = ic.getTime()
|
||||
for (i = 0; i < j; i++) {
|
||||
if (du.after(useTimes[i], nthUseTime)) {
|
||||
nthUseTime = useTimes[i]
|
||||
k++
|
||||
}
|
||||
if (k == n) break
|
||||
}
|
||||
|
||||
int itemIndices[]
|
||||
|
||||
mutex(cacheLock) {
|
||||
j = 0
|
||||
for (i = 0; i < arraySize; i++) {
|
||||
if (du.equal(cache[i].lastUsed, nthUseTime)) itemIndices[j++] = i
|
||||
}
|
||||
}
|
||||
|
||||
return resolve(itemIndices, "r")
|
||||
}
|
||||
|
||||
// This method returns a random index.
|
||||
int random() {
|
||||
DateTime dt = ic.getTime()
|
||||
int msec = dt.millisecond
|
||||
ir.setSeed(msec)
|
||||
return ir.get(CacheHandler.MAX_SIZE)
|
||||
}
|
||||
|
||||
// This method resolves a multiple return in one of the methods above,
|
||||
// as per the flag sent along with the list of indices.
|
||||
// 'n' returns the newest, 'o' the oldest and 'r' a random index.
|
||||
int resolve(int index[], char flag) {
|
||||
DateTime dt = null
|
||||
|
||||
if (flag == "n") {
|
||||
int newestItem = 0
|
||||
|
||||
int i = index.arrayLength-1
|
||||
for (int j = 0; j < index.arrayLength; j++) {
|
||||
if (dt == null) {
|
||||
dt = cache[index[i]].timeAdded
|
||||
newestItem = index[i]
|
||||
} else {
|
||||
if (du.before(dt, cache[index[i]].timeAdded)) {
|
||||
dt = cache[index[i]].timeAdded
|
||||
newestItem = index[i]
|
||||
}
|
||||
}
|
||||
i--
|
||||
}
|
||||
|
||||
return newestItem
|
||||
} else if (flag == "o") {
|
||||
int oldestItem = 0
|
||||
|
||||
for (int i = 0; i < index.arrayLength; i++) {
|
||||
if (dt == null) {
|
||||
dt = cache[index[i]].timeAdded
|
||||
oldestItem = index[i]
|
||||
} else {
|
||||
if (du.before(cache[index[i]].timeAdded, dt)) {
|
||||
dt = cache[index[i]].timeAdded
|
||||
oldestItem = index[i]
|
||||
}
|
||||
}
|
||||
}
|
||||
return oldestItem
|
||||
} else if (flag == "r") {
|
||||
dt = ic.getTime()
|
||||
int msec = dt.millisecond
|
||||
ir.setSeed(msec)
|
||||
return ir.get(index.arrayLength)
|
||||
} else {
|
||||
return 0
|
||||
}
|
||||
}
|
||||
}
|
267
dat/cache/difffile-large/10/CacheHandler10_13.dn
vendored
Normal file
267
dat/cache/difffile-large/10/CacheHandler10_13.dn
vendored
Normal file
|
@ -0,0 +1,267 @@
|
|||
/*
|
||||
* EmerGen(e)tic 0.8
|
||||
* Copyright © 2017 Ben Goldsworthy (rumperuu)
|
||||
*
|
||||
* A program to research the utility of genetic algorithms, applied to
|
||||
* 'emergent' sytems.
|
||||
*
|
||||
* This file is part of EmerGen(e)tic.
|
||||
*
|
||||
* IMPROVEMENT(S): License (probably GNU GPLv3 tbh) goes here.
|
||||
*/
|
||||
|
||||
/**
|
||||
** This program serves as the base for each mutated CacheHandler,
|
||||
** providing the functionality that is common to all of them.
|
||||
**/
|
||||
|
||||
/**
|
||||
** @author Ben Goldsworthy (rumperuu) <bgoldsworthy96 @ gmail.com>
|
||||
** @version 1.0
|
||||
**/
|
||||
component provides cache.CacheHandler
|
||||
requires io.Output out,
|
||||
data.IntUtil iu,
|
||||
util.RandomInt ir,
|
||||
time.Calendar ic,
|
||||
time.DateUtil du,
|
||||
io.File {
|
||||
|
||||
static CachedResponse cache[]
|
||||
static int arraySize
|
||||
static Mutex cacheLock
|
||||
|
||||
/**
|
||||
** Returns a requested item from the cache.
|
||||
**
|
||||
** @param resource The request item filepath
|
||||
** @return The item from the cache
|
||||
**/
|
||||
CachedResponse CacheHandler:getCachedResponse(char filePath[]) {
|
||||
// Converts the filepath to an array of bytes for the file.
|
||||
File fd = new File(filePath, File.FILE_ACCESS_READ)
|
||||
char resource[] = fd.read(fd.getSize())
|
||||
fd.close()
|
||||
|
||||
// Returns the item if it's in the cache.
|
||||
mutex(cacheLock) {
|
||||
for (int i = 0; i < arraySize; i++) {
|
||||
if (cache[i].resource == resource) {
|
||||
cache[i].hits++
|
||||
cache[i].lastUsed = ic.getTime()
|
||||
return cache[i]
|
||||
}
|
||||
}
|
||||
}
|
||||
return null
|
||||
}
|
||||
|
||||
/**
|
||||
** Updates the cache if the requested item is not already present.
|
||||
** If the cache is full, the method of determining the item to be
|
||||
** replaced is subject to genetic mutation.
|
||||
**
|
||||
** @param ncr The new item to cache
|
||||
**/
|
||||
void CacheHandler:updateCache(CachedResponse ncr) {
|
||||
mutex(cacheLock) {
|
||||
// If the item is already in the cache, do nothing.
|
||||
for (int i = 0; i < arraySize; i++) {
|
||||
if (cache[i].resource == ncr.resource) {
|
||||
cache[i].response = ncr.response
|
||||
cache[i].resourceAge = ncr.resourceAge
|
||||
|
||||
return
|
||||
}
|
||||
}
|
||||
|
||||
// If the item is not, it must be added.
|
||||
// As `ncr` was created on another component, its details must
|
||||
// be copied to avoid a read-only exception.
|
||||
CachedResponse newEntry = new CachedResponse()
|
||||
newEntry.command = ncr.command
|
||||
newEntry.resource = ncr.resource
|
||||
newEntry.resourceAge = ncr.resourceAge
|
||||
newEntry.mimeType = ncr.mimeType
|
||||
newEntry.response = new byte[](ncr.response)
|
||||
newEntry.contentSize = ncr.contentSize
|
||||
|
||||
// Initialises the cache if there isn't currently one.
|
||||
if (cache == null) {
|
||||
cache = new CachedResponse[CacheHandler.MAX_SIZE]()
|
||||
}
|
||||
|
||||
// If the cache is full, determines which item to replace.
|
||||
if (arraySize == CacheHandler.MAX_SIZE) {
|
||||
int index
|
||||
|
||||
// BEGIN
|
||||
index = 0
|
||||
// END
|
||||
|
||||
cache[index % arraySize] = newEntry
|
||||
//out.println("$(debugMSG) replacing: $(iu.intToString(index))")
|
||||
// Otherwise, appends the item to the end of the cache.
|
||||
} else {
|
||||
cache[arraySize] = newEntry
|
||||
arraySize++
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
** Clears the cache completely.
|
||||
**/
|
||||
void CacheHandler:clearCache() {
|
||||
mutex(cacheLock) {
|
||||
cache = null
|
||||
}
|
||||
}
|
||||
|
||||
/**************************************************************************
|
||||
* What follows are utility functions that a given chromosome of the
|
||||
* `updateCache()` method may or may not call upon.
|
||||
**************************************************************************/
|
||||
|
||||
// This method returns the index of the nth most frequently-used item in
|
||||
// the cache.
|
||||
int nthMostFrequentlyUsed(int n) {
|
||||
int hits[]
|
||||
for (int i = 0; i < arraySize; i++) hits[i] = 0
|
||||
|
||||
int i
|
||||
int j
|
||||
mutex(cacheLock) {
|
||||
for (i = 0; i < arraySize; i++) {
|
||||
for (j = 0; j < arraySize; j++) {
|
||||
if (cache[i].hits == hits[j]) break
|
||||
else if (hits[j] == 0) {
|
||||
hits[j] = cache[i].hits
|
||||
break
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
int k = 0
|
||||
int nthHit = 0
|
||||
for (i = 0; i < j; i++) {
|
||||
if (hits[i] > nthHit) {
|
||||
nthHit = hits[i]
|
||||
k++
|
||||
}
|
||||
if (k == n) break
|
||||
}
|
||||
|
||||
int itemIndices[]
|
||||
|
||||
mutex(cacheLock) {
|
||||
j = 0
|
||||
for (i = 0; i < arraySize; i++) {
|
||||
if (cache[i].hits == nthHit) itemIndices[j++] = i
|
||||
}
|
||||
}
|
||||
|
||||
return resolve(itemIndices, "r")
|
||||
}
|
||||
|
||||
// This method returns the index of the nth most recently-used item in
|
||||
// the cache.
|
||||
int nthMostRecentlyUsed(int n) {
|
||||
DateTime useTimes[]
|
||||
DateTime testTime = ic.getTime()
|
||||
for (int i = 0; i < arraySize; i++) useTimes[i] = testTime
|
||||
|
||||
int i
|
||||
int j
|
||||
mutex(cacheLock) {
|
||||
for (i = 0; i < arraySize; i++) {
|
||||
for (j = 0; j < arraySize; j++) {
|
||||
if (du.equal(cache[i].lastUsed, useTimes[j])) break
|
||||
else if (du.equal(useTimes[j], testTime)) {
|
||||
useTimes[j] = cache[i].lastUsed
|
||||
break
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
int k = 0
|
||||
DateTime nthUseTime = ic.getTime()
|
||||
for (i = 0; i < j; i++) {
|
||||
if (du.after(useTimes[i], nthUseTime)) {
|
||||
nthUseTime = useTimes[i]
|
||||
k++
|
||||
}
|
||||
if (k == n) break
|
||||
}
|
||||
|
||||
int itemIndices[]
|
||||
|
||||
mutex(cacheLock) {
|
||||
j = 0
|
||||
for (i = 0; i < arraySize; i++) {
|
||||
if (du.equal(cache[i].lastUsed, nthUseTime)) itemIndices[j++] = i
|
||||
}
|
||||
}
|
||||
|
||||
return resolve(itemIndices, "r")
|
||||
}
|
||||
|
||||
// This method returns a random index.
|
||||
int random() {
|
||||
DateTime dt = ic.getTime()
|
||||
int msec = dt.millisecond
|
||||
ir.setSeed(msec)
|
||||
return ir.get(CacheHandler.MAX_SIZE)
|
||||
}
|
||||
|
||||
// This method resolves a multiple return in one of the methods above,
|
||||
// as per the flag sent along with the list of indices.
|
||||
// 'n' returns the newest, 'o' the oldest and 'r' a random index.
|
||||
int resolve(int index[], char flag) {
|
||||
DateTime dt = null
|
||||
|
||||
if (flag == "n") {
|
||||
int newestItem = 0
|
||||
|
||||
int i = index.arrayLength-1
|
||||
for (int j = 0; j < index.arrayLength; j++) {
|
||||
if (dt == null) {
|
||||
dt = cache[index[i]].timeAdded
|
||||
newestItem = index[i]
|
||||
} else {
|
||||
if (du.before(dt, cache[index[i]].timeAdded)) {
|
||||
dt = cache[index[i]].timeAdded
|
||||
newestItem = index[i]
|
||||
}
|
||||
}
|
||||
i--
|
||||
}
|
||||
|
||||
return newestItem
|
||||
} else if (flag == "o") {
|
||||
int oldestItem = 0
|
||||
|
||||
for (int i = 0; i < index.arrayLength; i++) {
|
||||
if (dt == null) {
|
||||
dt = cache[index[i]].timeAdded
|
||||
oldestItem = index[i]
|
||||
} else {
|
||||
if (du.before(cache[index[i]].timeAdded, dt)) {
|
||||
dt = cache[index[i]].timeAdded
|
||||
oldestItem = index[i]
|
||||
}
|
||||
}
|
||||
}
|
||||
return oldestItem
|
||||
} else if (flag == "r") {
|
||||
dt = ic.getTime()
|
||||
int msec = dt.millisecond
|
||||
ir.setSeed(msec)
|
||||
return ir.get(index.arrayLength)
|
||||
} else {
|
||||
return 0
|
||||
}
|
||||
}
|
||||
}
|
267
dat/cache/difffile-large/10/CacheHandler10_14.dn
vendored
Normal file
267
dat/cache/difffile-large/10/CacheHandler10_14.dn
vendored
Normal file
|
@ -0,0 +1,267 @@
|
|||
/*
|
||||
* EmerGen(e)tic 0.8
|
||||
* Copyright © 2017 Ben Goldsworthy (rumperuu)
|
||||
*
|
||||
* A program to research the utility of genetic algorithms, applied to
|
||||
* 'emergent' sytems.
|
||||
*
|
||||
* This file is part of EmerGen(e)tic.
|
||||
*
|
||||
* IMPROVEMENT(S): License (probably GNU GPLv3 tbh) goes here.
|
||||
*/
|
||||
|
||||
/**
|
||||
** This program serves as the base for each mutated CacheHandler,
|
||||
** providing the functionality that is common to all of them.
|
||||
**/
|
||||
|
||||
/**
|
||||
** @author Ben Goldsworthy (rumperuu) <bgoldsworthy96 @ gmail.com>
|
||||
** @version 1.0
|
||||
**/
|
||||
component provides cache.CacheHandler
|
||||
requires io.Output out,
|
||||
data.IntUtil iu,
|
||||
util.RandomInt ir,
|
||||
time.Calendar ic,
|
||||
time.DateUtil du,
|
||||
io.File {
|
||||
|
||||
static CachedResponse cache[]
|
||||
static int arraySize
|
||||
static Mutex cacheLock
|
||||
|
||||
/**
|
||||
** Returns a requested item from the cache.
|
||||
**
|
||||
** @param resource The request item filepath
|
||||
** @return The item from the cache
|
||||
**/
|
||||
CachedResponse CacheHandler:getCachedResponse(char filePath[]) {
|
||||
// Converts the filepath to an array of bytes for the file.
|
||||
File fd = new File(filePath, File.FILE_ACCESS_READ)
|
||||
char resource[] = fd.read(fd.getSize())
|
||||
fd.close()
|
||||
|
||||
// Returns the item if it's in the cache.
|
||||
mutex(cacheLock) {
|
||||
for (int i = 0; i < arraySize; i++) {
|
||||
if (cache[i].resource == resource) {
|
||||
cache[i].hits++
|
||||
cache[i].lastUsed = ic.getTime()
|
||||
return cache[i]
|
||||
}
|
||||
}
|
||||
}
|
||||
return null
|
||||
}
|
||||
|
||||
/**
|
||||
** Updates the cache if the requested item is not already present.
|
||||
** If the cache is full, the method of determining the item to be
|
||||
** replaced is subject to genetic mutation.
|
||||
**
|
||||
** @param ncr The new item to cache
|
||||
**/
|
||||
void CacheHandler:updateCache(CachedResponse ncr) {
|
||||
mutex(cacheLock) {
|
||||
// If the item is already in the cache, do nothing.
|
||||
for (int i = 0; i < arraySize; i++) {
|
||||
if (cache[i].resource == ncr.resource) {
|
||||
cache[i].response = ncr.response
|
||||
cache[i].resourceAge = ncr.resourceAge
|
||||
|
||||
return
|
||||
}
|
||||
}
|
||||
|
||||
// If the item is not, it must be added.
|
||||
// As `ncr` was created on another component, its details must
|
||||
// be copied to avoid a read-only exception.
|
||||
CachedResponse newEntry = new CachedResponse()
|
||||
newEntry.command = ncr.command
|
||||
newEntry.resource = ncr.resource
|
||||
newEntry.resourceAge = ncr.resourceAge
|
||||
newEntry.mimeType = ncr.mimeType
|
||||
newEntry.response = new byte[](ncr.response)
|
||||
newEntry.contentSize = ncr.contentSize
|
||||
|
||||
// Initialises the cache if there isn't currently one.
|
||||
if (cache == null) {
|
||||
cache = new CachedResponse[CacheHandler.MAX_SIZE]()
|
||||
}
|
||||
|
||||
// If the cache is full, determines which item to replace.
|
||||
if (arraySize == CacheHandler.MAX_SIZE) {
|
||||
int index
|
||||
|
||||
// BEGIN
|
||||
index = 0*nthMostRecentlyUsed(0)
|
||||
// END
|
||||
|
||||
cache[index % arraySize] = newEntry
|
||||
//out.println("$(debugMSG) replacing: $(iu.intToString(index))")
|
||||
// Otherwise, appends the item to the end of the cache.
|
||||
} else {
|
||||
cache[arraySize] = newEntry
|
||||
arraySize++
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
** Clears the cache completely.
|
||||
**/
|
||||
void CacheHandler:clearCache() {
|
||||
mutex(cacheLock) {
|
||||
cache = null
|
||||
}
|
||||
}
|
||||
|
||||
/**************************************************************************
|
||||
* What follows are utility functions that a given chromosome of the
|
||||
* `updateCache()` method may or may not call upon.
|
||||
**************************************************************************/
|
||||
|
||||
// This method returns the index of the nth most frequently-used item in
|
||||
// the cache.
|
||||
int nthMostFrequentlyUsed(int n) {
|
||||
int hits[]
|
||||
for (int i = 0; i < arraySize; i++) hits[i] = 0
|
||||
|
||||
int i
|
||||
int j
|
||||
mutex(cacheLock) {
|
||||
for (i = 0; i < arraySize; i++) {
|
||||
for (j = 0; j < arraySize; j++) {
|
||||
if (cache[i].hits == hits[j]) break
|
||||
else if (hits[j] == 0) {
|
||||
hits[j] = cache[i].hits
|
||||
break
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
int k = 0
|
||||
int nthHit = 0
|
||||
for (i = 0; i < j; i++) {
|
||||
if (hits[i] > nthHit) {
|
||||
nthHit = hits[i]
|
||||
k++
|
||||
}
|
||||
if (k == n) break
|
||||
}
|
||||
|
||||
int itemIndices[]
|
||||
|
||||
mutex(cacheLock) {
|
||||
j = 0
|
||||
for (i = 0; i < arraySize; i++) {
|
||||
if (cache[i].hits == nthHit) itemIndices[j++] = i
|
||||
}
|
||||
}
|
||||
|
||||
return resolve(itemIndices, "r")
|
||||
}
|
||||
|
||||
// This method returns the index of the nth most recently-used item in
|
||||
// the cache.
|
||||
int nthMostRecentlyUsed(int n) {
|
||||
DateTime useTimes[]
|
||||
DateTime testTime = ic.getTime()
|
||||
for (int i = 0; i < arraySize; i++) useTimes[i] = testTime
|
||||
|
||||
int i
|
||||
int j
|
||||
mutex(cacheLock) {
|
||||
for (i = 0; i < arraySize; i++) {
|
||||
for (j = 0; j < arraySize; j++) {
|
||||
if (du.equal(cache[i].lastUsed, useTimes[j])) break
|
||||
else if (du.equal(useTimes[j], testTime)) {
|
||||
useTimes[j] = cache[i].lastUsed
|
||||
break
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
int k = 0
|
||||
DateTime nthUseTime = ic.getTime()
|
||||
for (i = 0; i < j; i++) {
|
||||
if (du.after(useTimes[i], nthUseTime)) {
|
||||
nthUseTime = useTimes[i]
|
||||
k++
|
||||
}
|
||||
if (k == n) break
|
||||
}
|
||||
|
||||
int itemIndices[]
|
||||
|
||||
mutex(cacheLock) {
|
||||
j = 0
|
||||
for (i = 0; i < arraySize; i++) {
|
||||
if (du.equal(cache[i].lastUsed, nthUseTime)) itemIndices[j++] = i
|
||||
}
|
||||
}
|
||||
|
||||
return resolve(itemIndices, "r")
|
||||
}
|
||||
|
||||
// This method returns a random index.
|
||||
int random() {
|
||||
DateTime dt = ic.getTime()
|
||||
int msec = dt.millisecond
|
||||
ir.setSeed(msec)
|
||||
return ir.get(CacheHandler.MAX_SIZE)
|
||||
}
|
||||
|
||||
// This method resolves a multiple return in one of the methods above,
|
||||
// as per the flag sent along with the list of indices.
|
||||
// 'n' returns the newest, 'o' the oldest and 'r' a random index.
|
||||
int resolve(int index[], char flag) {
|
||||
DateTime dt = null
|
||||
|
||||
if (flag == "n") {
|
||||
int newestItem = 0
|
||||
|
||||
int i = index.arrayLength-1
|
||||
for (int j = 0; j < index.arrayLength; j++) {
|
||||
if (dt == null) {
|
||||
dt = cache[index[i]].timeAdded
|
||||
newestItem = index[i]
|
||||
} else {
|
||||
if (du.before(dt, cache[index[i]].timeAdded)) {
|
||||
dt = cache[index[i]].timeAdded
|
||||
newestItem = index[i]
|
||||
}
|
||||
}
|
||||
i--
|
||||
}
|
||||
|
||||
return newestItem
|
||||
} else if (flag == "o") {
|
||||
int oldestItem = 0
|
||||
|
||||
for (int i = 0; i < index.arrayLength; i++) {
|
||||
if (dt == null) {
|
||||
dt = cache[index[i]].timeAdded
|
||||
oldestItem = index[i]
|
||||
} else {
|
||||
if (du.before(cache[index[i]].timeAdded, dt)) {
|
||||
dt = cache[index[i]].timeAdded
|
||||
oldestItem = index[i]
|
||||
}
|
||||
}
|
||||
}
|
||||
return oldestItem
|
||||
} else if (flag == "r") {
|
||||
dt = ic.getTime()
|
||||
int msec = dt.millisecond
|
||||
ir.setSeed(msec)
|
||||
return ir.get(index.arrayLength)
|
||||
} else {
|
||||
return 0
|
||||
}
|
||||
}
|
||||
}
|
267
dat/cache/difffile-large/10/CacheHandler10_15.dn
vendored
Normal file
267
dat/cache/difffile-large/10/CacheHandler10_15.dn
vendored
Normal file
|
@ -0,0 +1,267 @@
|
|||
/*
|
||||
* EmerGen(e)tic 0.8
|
||||
* Copyright © 2017 Ben Goldsworthy (rumperuu)
|
||||
*
|
||||
* A program to research the utility of genetic algorithms, applied to
|
||||
* 'emergent' sytems.
|
||||
*
|
||||
* This file is part of EmerGen(e)tic.
|
||||
*
|
||||
* IMPROVEMENT(S): License (probably GNU GPLv3 tbh) goes here.
|
||||
*/
|
||||
|
||||
/**
|
||||
** This program serves as the base for each mutated CacheHandler,
|
||||
** providing the functionality that is common to all of them.
|
||||
**/
|
||||
|
||||
/**
|
||||
** @author Ben Goldsworthy (rumperuu) <bgoldsworthy96 @ gmail.com>
|
||||
** @version 1.0
|
||||
**/
|
||||
component provides cache.CacheHandler
|
||||
requires io.Output out,
|
||||
data.IntUtil iu,
|
||||
util.RandomInt ir,
|
||||
time.Calendar ic,
|
||||
time.DateUtil du,
|
||||
io.File {
|
||||
|
||||
static CachedResponse cache[]
|
||||
static int arraySize
|
||||
static Mutex cacheLock
|
||||
|
||||
/**
|
||||
** Returns a requested item from the cache.
|
||||
**
|
||||
** @param resource The request item filepath
|
||||
** @return The item from the cache
|
||||
**/
|
||||
CachedResponse CacheHandler:getCachedResponse(char filePath[]) {
|
||||
// Converts the filepath to an array of bytes for the file.
|
||||
File fd = new File(filePath, File.FILE_ACCESS_READ)
|
||||
char resource[] = fd.read(fd.getSize())
|
||||
fd.close()
|
||||
|
||||
// Returns the item if it's in the cache.
|
||||
mutex(cacheLock) {
|
||||
for (int i = 0; i < arraySize; i++) {
|
||||
if (cache[i].resource == resource) {
|
||||
cache[i].hits++
|
||||
cache[i].lastUsed = ic.getTime()
|
||||
return cache[i]
|
||||
}
|
||||
}
|
||||
}
|
||||
return null
|
||||
}
|
||||
|
||||
/**
|
||||
** Updates the cache if the requested item is not already present.
|
||||
** If the cache is full, the method of determining the item to be
|
||||
** replaced is subject to genetic mutation.
|
||||
**
|
||||
** @param ncr The new item to cache
|
||||
**/
|
||||
void CacheHandler:updateCache(CachedResponse ncr) {
|
||||
mutex(cacheLock) {
|
||||
// If the item is already in the cache, do nothing.
|
||||
for (int i = 0; i < arraySize; i++) {
|
||||
if (cache[i].resource == ncr.resource) {
|
||||
cache[i].response = ncr.response
|
||||
cache[i].resourceAge = ncr.resourceAge
|
||||
|
||||
return
|
||||
}
|
||||
}
|
||||
|
||||
// If the item is not, it must be added.
|
||||
// As `ncr` was created on another component, its details must
|
||||
// be copied to avoid a read-only exception.
|
||||
CachedResponse newEntry = new CachedResponse()
|
||||
newEntry.command = ncr.command
|
||||
newEntry.resource = ncr.resource
|
||||
newEntry.resourceAge = ncr.resourceAge
|
||||
newEntry.mimeType = ncr.mimeType
|
||||
newEntry.response = new byte[](ncr.response)
|
||||
newEntry.contentSize = ncr.contentSize
|
||||
|
||||
// Initialises the cache if there isn't currently one.
|
||||
if (cache == null) {
|
||||
cache = new CachedResponse[CacheHandler.MAX_SIZE]()
|
||||
}
|
||||
|
||||
// If the cache is full, determines which item to replace.
|
||||
if (arraySize == CacheHandler.MAX_SIZE) {
|
||||
int index
|
||||
|
||||
// BEGIN
|
||||
index = 0
|
||||
// END
|
||||
|
||||
cache[index % arraySize] = newEntry
|
||||
//out.println("$(debugMSG) replacing: $(iu.intToString(index))")
|
||||
// Otherwise, appends the item to the end of the cache.
|
||||
} else {
|
||||
cache[arraySize] = newEntry
|
||||
arraySize++
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
** Clears the cache completely.
|
||||
**/
|
||||
void CacheHandler:clearCache() {
|
||||
mutex(cacheLock) {
|
||||
cache = null
|
||||
}
|
||||
}
|
||||
|
||||
/**************************************************************************
|
||||
* What follows are utility functions that a given chromosome of the
|
||||
* `updateCache()` method may or may not call upon.
|
||||
**************************************************************************/
|
||||
|
||||
// This method returns the index of the nth most frequently-used item in
|
||||
// the cache.
|
||||
int nthMostFrequentlyUsed(int n) {
|
||||
int hits[]
|
||||
for (int i = 0; i < arraySize; i++) hits[i] = 0
|
||||
|
||||
int i
|
||||
int j
|
||||
mutex(cacheLock) {
|
||||
for (i = 0; i < arraySize; i++) {
|
||||
for (j = 0; j < arraySize; j++) {
|
||||
if (cache[i].hits == hits[j]) break
|
||||
else if (hits[j] == 0) {
|
||||
hits[j] = cache[i].hits
|
||||
break
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
int k = 0
|
||||
int nthHit = 0
|
||||
for (i = 0; i < j; i++) {
|
||||
if (hits[i] > nthHit) {
|
||||
nthHit = hits[i]
|
||||
k++
|
||||
}
|
||||
if (k == n) break
|
||||
}
|
||||
|
||||
int itemIndices[]
|
||||
|
||||
mutex(cacheLock) {
|
||||
j = 0
|
||||
for (i = 0; i < arraySize; i++) {
|
||||
if (cache[i].hits == nthHit) itemIndices[j++] = i
|
||||
}
|
||||
}
|
||||
|
||||
return resolve(itemIndices, "r")
|
||||
}
|
||||
|
||||
// This method returns the index of the nth most recently-used item in
|
||||
// the cache.
|
||||
int nthMostRecentlyUsed(int n) {
|
||||
DateTime useTimes[]
|
||||
DateTime testTime = ic.getTime()
|
||||
for (int i = 0; i < arraySize; i++) useTimes[i] = testTime
|
||||
|
||||
int i
|
||||
int j
|
||||
mutex(cacheLock) {
|
||||
for (i = 0; i < arraySize; i++) {
|
||||
for (j = 0; j < arraySize; j++) {
|
||||
if (du.equal(cache[i].lastUsed, useTimes[j])) break
|
||||
else if (du.equal(useTimes[j], testTime)) {
|
||||
useTimes[j] = cache[i].lastUsed
|
||||
break
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
int k = 0
|
||||
DateTime nthUseTime = ic.getTime()
|
||||
for (i = 0; i < j; i++) {
|
||||
if (du.after(useTimes[i], nthUseTime)) {
|
||||
nthUseTime = useTimes[i]
|
||||
k++
|
||||
}
|
||||
if (k == n) break
|
||||
}
|
||||
|
||||
int itemIndices[]
|
||||
|
||||
mutex(cacheLock) {
|
||||
j = 0
|
||||
for (i = 0; i < arraySize; i++) {
|
||||
if (du.equal(cache[i].lastUsed, nthUseTime)) itemIndices[j++] = i
|
||||
}
|
||||
}
|
||||
|
||||
return resolve(itemIndices, "r")
|
||||
}
|
||||
|
||||
// This method returns a random index.
|
||||
int random() {
|
||||
DateTime dt = ic.getTime()
|
||||
int msec = dt.millisecond
|
||||
ir.setSeed(msec)
|
||||
return ir.get(CacheHandler.MAX_SIZE)
|
||||
}
|
||||
|
||||
// This method resolves a multiple return in one of the methods above,
|
||||
// as per the flag sent along with the list of indices.
|
||||
// 'n' returns the newest, 'o' the oldest and 'r' a random index.
|
||||
int resolve(int index[], char flag) {
|
||||
DateTime dt = null
|
||||
|
||||
if (flag == "n") {
|
||||
int newestItem = 0
|
||||
|
||||
int i = index.arrayLength-1
|
||||
for (int j = 0; j < index.arrayLength; j++) {
|
||||
if (dt == null) {
|
||||
dt = cache[index[i]].timeAdded
|
||||
newestItem = index[i]
|
||||
} else {
|
||||
if (du.before(dt, cache[index[i]].timeAdded)) {
|
||||
dt = cache[index[i]].timeAdded
|
||||
newestItem = index[i]
|
||||
}
|
||||
}
|
||||
i--
|
||||
}
|
||||
|
||||
return newestItem
|
||||
} else if (flag == "o") {
|
||||
int oldestItem = 0
|
||||
|
||||
for (int i = 0; i < index.arrayLength; i++) {
|
||||
if (dt == null) {
|
||||
dt = cache[index[i]].timeAdded
|
||||
oldestItem = index[i]
|
||||
} else {
|
||||
if (du.before(cache[index[i]].timeAdded, dt)) {
|
||||
dt = cache[index[i]].timeAdded
|
||||
oldestItem = index[i]
|
||||
}
|
||||
}
|
||||
}
|
||||
return oldestItem
|
||||
} else if (flag == "r") {
|
||||
dt = ic.getTime()
|
||||
int msec = dt.millisecond
|
||||
ir.setSeed(msec)
|
||||
return ir.get(index.arrayLength)
|
||||
} else {
|
||||
return 0
|
||||
}
|
||||
}
|
||||
}
|
267
dat/cache/difffile-large/10/CacheHandler10_16.dn
vendored
Normal file
267
dat/cache/difffile-large/10/CacheHandler10_16.dn
vendored
Normal file
|
@ -0,0 +1,267 @@
|
|||
/*
|
||||
* EmerGen(e)tic 0.8
|
||||
* Copyright © 2017 Ben Goldsworthy (rumperuu)
|
||||
*
|
||||
* A program to research the utility of genetic algorithms, applied to
|
||||
* 'emergent' sytems.
|
||||
*
|
||||
* This file is part of EmerGen(e)tic.
|
||||
*
|
||||
* IMPROVEMENT(S): License (probably GNU GPLv3 tbh) goes here.
|
||||
*/
|
||||
|
||||
/**
|
||||
** This program serves as the base for each mutated CacheHandler,
|
||||
** providing the functionality that is common to all of them.
|
||||
**/
|
||||
|
||||
/**
|
||||
** @author Ben Goldsworthy (rumperuu) <bgoldsworthy96 @ gmail.com>
|
||||
** @version 1.0
|
||||
**/
|
||||
component provides cache.CacheHandler
|
||||
requires io.Output out,
|
||||
data.IntUtil iu,
|
||||
util.RandomInt ir,
|
||||
time.Calendar ic,
|
||||
time.DateUtil du,
|
||||
io.File {
|
||||
|
||||
static CachedResponse cache[]
|
||||
static int arraySize
|
||||
static Mutex cacheLock
|
||||
|
||||
/**
|
||||
** Returns a requested item from the cache.
|
||||
**
|
||||
** @param resource The request item filepath
|
||||
** @return The item from the cache
|
||||
**/
|
||||
CachedResponse CacheHandler:getCachedResponse(char filePath[]) {
|
||||
// Converts the filepath to an array of bytes for the file.
|
||||
File fd = new File(filePath, File.FILE_ACCESS_READ)
|
||||
char resource[] = fd.read(fd.getSize())
|
||||
fd.close()
|
||||
|
||||
// Returns the item if it's in the cache.
|
||||
mutex(cacheLock) {
|
||||
for (int i = 0; i < arraySize; i++) {
|
||||
if (cache[i].resource == resource) {
|
||||
cache[i].hits++
|
||||
cache[i].lastUsed = ic.getTime()
|
||||
return cache[i]
|
||||
}
|
||||
}
|
||||
}
|
||||
return null
|
||||
}
|
||||
|
||||
/**
|
||||
** Updates the cache if the requested item is not already present.
|
||||
** If the cache is full, the method of determining the item to be
|
||||
** replaced is subject to genetic mutation.
|
||||
**
|
||||
** @param ncr The new item to cache
|
||||
**/
|
||||
void CacheHandler:updateCache(CachedResponse ncr) {
|
||||
mutex(cacheLock) {
|
||||
// If the item is already in the cache, do nothing.
|
||||
for (int i = 0; i < arraySize; i++) {
|
||||
if (cache[i].resource == ncr.resource) {
|
||||
cache[i].response = ncr.response
|
||||
cache[i].resourceAge = ncr.resourceAge
|
||||
|
||||
return
|
||||
}
|
||||
}
|
||||
|
||||
// If the item is not, it must be added.
|
||||
// As `ncr` was created on another component, its details must
|
||||
// be copied to avoid a read-only exception.
|
||||
CachedResponse newEntry = new CachedResponse()
|
||||
newEntry.command = ncr.command
|
||||
newEntry.resource = ncr.resource
|
||||
newEntry.resourceAge = ncr.resourceAge
|
||||
newEntry.mimeType = ncr.mimeType
|
||||
newEntry.response = new byte[](ncr.response)
|
||||
newEntry.contentSize = ncr.contentSize
|
||||
|
||||
// Initialises the cache if there isn't currently one.
|
||||
if (cache == null) {
|
||||
cache = new CachedResponse[CacheHandler.MAX_SIZE]()
|
||||
}
|
||||
|
||||
// If the cache is full, determines which item to replace.
|
||||
if (arraySize == CacheHandler.MAX_SIZE) {
|
||||
int index
|
||||
|
||||
// BEGIN
|
||||
index = nthMostRecentlyUsed(0)/19
|
||||
// END
|
||||
|
||||
cache[index % arraySize] = newEntry
|
||||
//out.println("$(debugMSG) replacing: $(iu.intToString(index))")
|
||||
// Otherwise, appends the item to the end of the cache.
|
||||
} else {
|
||||
cache[arraySize] = newEntry
|
||||
arraySize++
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
** Clears the cache completely.
|
||||
**/
|
||||
void CacheHandler:clearCache() {
|
||||
mutex(cacheLock) {
|
||||
cache = null
|
||||
}
|
||||
}
|
||||
|
||||
/**************************************************************************
|
||||
* What follows are utility functions that a given chromosome of the
|
||||
* `updateCache()` method may or may not call upon.
|
||||
**************************************************************************/
|
||||
|
||||
// This method returns the index of the nth most frequently-used item in
|
||||
// the cache.
|
||||
int nthMostFrequentlyUsed(int n) {
|
||||
int hits[]
|
||||
for (int i = 0; i < arraySize; i++) hits[i] = 0
|
||||
|
||||
int i
|
||||
int j
|
||||
mutex(cacheLock) {
|
||||
for (i = 0; i < arraySize; i++) {
|
||||
for (j = 0; j < arraySize; j++) {
|
||||
if (cache[i].hits == hits[j]) break
|
||||
else if (hits[j] == 0) {
|
||||
hits[j] = cache[i].hits
|
||||
break
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
int k = 0
|
||||
int nthHit = 0
|
||||
for (i = 0; i < j; i++) {
|
||||
if (hits[i] > nthHit) {
|
||||
nthHit = hits[i]
|
||||
k++
|
||||
}
|
||||
if (k == n) break
|
||||
}
|
||||
|
||||
int itemIndices[]
|
||||
|
||||
mutex(cacheLock) {
|
||||
j = 0
|
||||
for (i = 0; i < arraySize; i++) {
|
||||
if (cache[i].hits == nthHit) itemIndices[j++] = i
|
||||
}
|
||||
}
|
||||
|
||||
return resolve(itemIndices, "r")
|
||||
}
|
||||
|
||||
// This method returns the index of the nth most recently-used item in
|
||||
// the cache.
|
||||
int nthMostRecentlyUsed(int n) {
|
||||
DateTime useTimes[]
|
||||
DateTime testTime = ic.getTime()
|
||||
for (int i = 0; i < arraySize; i++) useTimes[i] = testTime
|
||||
|
||||
int i
|
||||
int j
|
||||
mutex(cacheLock) {
|
||||
for (i = 0; i < arraySize; i++) {
|
||||
for (j = 0; j < arraySize; j++) {
|
||||
if (du.equal(cache[i].lastUsed, useTimes[j])) break
|
||||
else if (du.equal(useTimes[j], testTime)) {
|
||||
useTimes[j] = cache[i].lastUsed
|
||||
break
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
int k = 0
|
||||
DateTime nthUseTime = ic.getTime()
|
||||
for (i = 0; i < j; i++) {
|
||||
if (du.after(useTimes[i], nthUseTime)) {
|
||||
nthUseTime = useTimes[i]
|
||||
k++
|
||||
}
|
||||
if (k == n) break
|
||||
}
|
||||
|
||||
int itemIndices[]
|
||||
|
||||
mutex(cacheLock) {
|
||||
j = 0
|
||||
for (i = 0; i < arraySize; i++) {
|
||||
if (du.equal(cache[i].lastUsed, nthUseTime)) itemIndices[j++] = i
|
||||
}
|
||||
}
|
||||
|
||||
return resolve(itemIndices, "r")
|
||||
}
|
||||
|
||||
// This method returns a random index.
|
||||
int random() {
|
||||
DateTime dt = ic.getTime()
|
||||
int msec = dt.millisecond
|
||||
ir.setSeed(msec)
|
||||
return ir.get(CacheHandler.MAX_SIZE)
|
||||
}
|
||||
|
||||
// This method resolves a multiple return in one of the methods above,
|
||||
// as per the flag sent along with the list of indices.
|
||||
// 'n' returns the newest, 'o' the oldest and 'r' a random index.
|
||||
int resolve(int index[], char flag) {
|
||||
DateTime dt = null
|
||||
|
||||
if (flag == "n") {
|
||||
int newestItem = 0
|
||||
|
||||
int i = index.arrayLength-1
|
||||
for (int j = 0; j < index.arrayLength; j++) {
|
||||
if (dt == null) {
|
||||
dt = cache[index[i]].timeAdded
|
||||
newestItem = index[i]
|
||||
} else {
|
||||
if (du.before(dt, cache[index[i]].timeAdded)) {
|
||||
dt = cache[index[i]].timeAdded
|
||||
newestItem = index[i]
|
||||
}
|
||||
}
|
||||
i--
|
||||
}
|
||||
|
||||
return newestItem
|
||||
} else if (flag == "o") {
|
||||
int oldestItem = 0
|
||||
|
||||
for (int i = 0; i < index.arrayLength; i++) {
|
||||
if (dt == null) {
|
||||
dt = cache[index[i]].timeAdded
|
||||
oldestItem = index[i]
|
||||
} else {
|
||||
if (du.before(cache[index[i]].timeAdded, dt)) {
|
||||
dt = cache[index[i]].timeAdded
|
||||
oldestItem = index[i]
|
||||
}
|
||||
}
|
||||
}
|
||||
return oldestItem
|
||||
} else if (flag == "r") {
|
||||
dt = ic.getTime()
|
||||
int msec = dt.millisecond
|
||||
ir.setSeed(msec)
|
||||
return ir.get(index.arrayLength)
|
||||
} else {
|
||||
return 0
|
||||
}
|
||||
}
|
||||
}
|
267
dat/cache/difffile-large/10/CacheHandler10_17.dn
vendored
Normal file
267
dat/cache/difffile-large/10/CacheHandler10_17.dn
vendored
Normal file
|
@ -0,0 +1,267 @@
|
|||
/*
|
||||
* EmerGen(e)tic 0.8
|
||||
* Copyright © 2017 Ben Goldsworthy (rumperuu)
|
||||
*
|
||||
* A program to research the utility of genetic algorithms, applied to
|
||||
* 'emergent' sytems.
|
||||
*
|
||||
* This file is part of EmerGen(e)tic.
|
||||
*
|
||||
* IMPROVEMENT(S): License (probably GNU GPLv3 tbh) goes here.
|
||||
*/
|
||||
|
||||
/**
|
||||
** This program serves as the base for each mutated CacheHandler,
|
||||
** providing the functionality that is common to all of them.
|
||||
**/
|
||||
|
||||
/**
|
||||
** @author Ben Goldsworthy (rumperuu) <bgoldsworthy96 @ gmail.com>
|
||||
** @version 1.0
|
||||
**/
|
||||
component provides cache.CacheHandler
|
||||
requires io.Output out,
|
||||
data.IntUtil iu,
|
||||
util.RandomInt ir,
|
||||
time.Calendar ic,
|
||||
time.DateUtil du,
|
||||
io.File {
|
||||
|
||||
static CachedResponse cache[]
|
||||
static int arraySize
|
||||
static Mutex cacheLock
|
||||
|
||||
/**
|
||||
** Returns a requested item from the cache.
|
||||
**
|
||||
** @param resource The request item filepath
|
||||
** @return The item from the cache
|
||||
**/
|
||||
CachedResponse CacheHandler:getCachedResponse(char filePath[]) {
|
||||
// Converts the filepath to an array of bytes for the file.
|
||||
File fd = new File(filePath, File.FILE_ACCESS_READ)
|
||||
char resource[] = fd.read(fd.getSize())
|
||||
fd.close()
|
||||
|
||||
// Returns the item if it's in the cache.
|
||||
mutex(cacheLock) {
|
||||
for (int i = 0; i < arraySize; i++) {
|
||||
if (cache[i].resource == resource) {
|
||||
cache[i].hits++
|
||||
cache[i].lastUsed = ic.getTime()
|
||||
return cache[i]
|
||||
}
|
||||
}
|
||||
}
|
||||
return null
|
||||
}
|
||||
|
||||
/**
|
||||
** Updates the cache if the requested item is not already present.
|
||||
** If the cache is full, the method of determining the item to be
|
||||
** replaced is subject to genetic mutation.
|
||||
**
|
||||
** @param ncr The new item to cache
|
||||
**/
|
||||
void CacheHandler:updateCache(CachedResponse ncr) {
|
||||
mutex(cacheLock) {
|
||||
// If the item is already in the cache, do nothing.
|
||||
for (int i = 0; i < arraySize; i++) {
|
||||
if (cache[i].resource == ncr.resource) {
|
||||
cache[i].response = ncr.response
|
||||
cache[i].resourceAge = ncr.resourceAge
|
||||
|
||||
return
|
||||
}
|
||||
}
|
||||
|
||||
// If the item is not, it must be added.
|
||||
// As `ncr` was created on another component, its details must
|
||||
// be copied to avoid a read-only exception.
|
||||
CachedResponse newEntry = new CachedResponse()
|
||||
newEntry.command = ncr.command
|
||||
newEntry.resource = ncr.resource
|
||||
newEntry.resourceAge = ncr.resourceAge
|
||||
newEntry.mimeType = ncr.mimeType
|
||||
newEntry.response = new byte[](ncr.response)
|
||||
newEntry.contentSize = ncr.contentSize
|
||||
|
||||
// Initialises the cache if there isn't currently one.
|
||||
if (cache == null) {
|
||||
cache = new CachedResponse[CacheHandler.MAX_SIZE]()
|
||||
}
|
||||
|
||||
// If the cache is full, determines which item to replace.
|
||||
if (arraySize == CacheHandler.MAX_SIZE) {
|
||||
int index
|
||||
|
||||
// BEGIN
|
||||
index = 0
|
||||
// END
|
||||
|
||||
cache[index % arraySize] = newEntry
|
||||
//out.println("$(debugMSG) replacing: $(iu.intToString(index))")
|
||||
// Otherwise, appends the item to the end of the cache.
|
||||
} else {
|
||||
cache[arraySize] = newEntry
|
||||
arraySize++
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
** Clears the cache completely.
|
||||
**/
|
||||
void CacheHandler:clearCache() {
|
||||
mutex(cacheLock) {
|
||||
cache = null
|
||||
}
|
||||
}
|
||||
|
||||
/**************************************************************************
|
||||
* What follows are utility functions that a given chromosome of the
|
||||
* `updateCache()` method may or may not call upon.
|
||||
**************************************************************************/
|
||||
|
||||
// This method returns the index of the nth most frequently-used item in
|
||||
// the cache.
|
||||
int nthMostFrequentlyUsed(int n) {
|
||||
int hits[]
|
||||
for (int i = 0; i < arraySize; i++) hits[i] = 0
|
||||
|
||||
int i
|
||||
int j
|
||||
mutex(cacheLock) {
|
||||
for (i = 0; i < arraySize; i++) {
|
||||
for (j = 0; j < arraySize; j++) {
|
||||
if (cache[i].hits == hits[j]) break
|
||||
else if (hits[j] == 0) {
|
||||
hits[j] = cache[i].hits
|
||||
break
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
int k = 0
|
||||
int nthHit = 0
|
||||
for (i = 0; i < j; i++) {
|
||||
if (hits[i] > nthHit) {
|
||||
nthHit = hits[i]
|
||||
k++
|
||||
}
|
||||
if (k == n) break
|
||||
}
|
||||
|
||||
int itemIndices[]
|
||||
|
||||
mutex(cacheLock) {
|
||||
j = 0
|
||||
for (i = 0; i < arraySize; i++) {
|
||||
if (cache[i].hits == nthHit) itemIndices[j++] = i
|
||||
}
|
||||
}
|
||||
|
||||
return resolve(itemIndices, "r")
|
||||
}
|
||||
|
||||
// This method returns the index of the nth most recently-used item in
|
||||
// the cache.
|
||||
int nthMostRecentlyUsed(int n) {
|
||||
DateTime useTimes[]
|
||||
DateTime testTime = ic.getTime()
|
||||
for (int i = 0; i < arraySize; i++) useTimes[i] = testTime
|
||||
|
||||
int i
|
||||
int j
|
||||
mutex(cacheLock) {
|
||||
for (i = 0; i < arraySize; i++) {
|
||||
for (j = 0; j < arraySize; j++) {
|
||||
if (du.equal(cache[i].lastUsed, useTimes[j])) break
|
||||
else if (du.equal(useTimes[j], testTime)) {
|
||||
useTimes[j] = cache[i].lastUsed
|
||||
break
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
int k = 0
|
||||
DateTime nthUseTime = ic.getTime()
|
||||
for (i = 0; i < j; i++) {
|
||||
if (du.after(useTimes[i], nthUseTime)) {
|
||||
nthUseTime = useTimes[i]
|
||||
k++
|
||||
}
|
||||
if (k == n) break
|
||||
}
|
||||
|
||||
int itemIndices[]
|
||||
|
||||
mutex(cacheLock) {
|
||||
j = 0
|
||||
for (i = 0; i < arraySize; i++) {
|
||||
if (du.equal(cache[i].lastUsed, nthUseTime)) itemIndices[j++] = i
|
||||
}
|
||||
}
|
||||
|
||||
return resolve(itemIndices, "r")
|
||||
}
|
||||
|
||||
// This method returns a random index.
|
||||
int random() {
|
||||
DateTime dt = ic.getTime()
|
||||
int msec = dt.millisecond
|
||||
ir.setSeed(msec)
|
||||
return ir.get(CacheHandler.MAX_SIZE)
|
||||
}
|
||||
|
||||
// This method resolves a multiple return in one of the methods above,
|
||||
// as per the flag sent along with the list of indices.
|
||||
// 'n' returns the newest, 'o' the oldest and 'r' a random index.
|
||||
int resolve(int index[], char flag) {
|
||||
DateTime dt = null
|
||||
|
||||
if (flag == "n") {
|
||||
int newestItem = 0
|
||||
|
||||
int i = index.arrayLength-1
|
||||
for (int j = 0; j < index.arrayLength; j++) {
|
||||
if (dt == null) {
|
||||
dt = cache[index[i]].timeAdded
|
||||
newestItem = index[i]
|
||||
} else {
|
||||
if (du.before(dt, cache[index[i]].timeAdded)) {
|
||||
dt = cache[index[i]].timeAdded
|
||||
newestItem = index[i]
|
||||
}
|
||||
}
|
||||
i--
|
||||
}
|
||||
|
||||
return newestItem
|
||||
} else if (flag == "o") {
|
||||
int oldestItem = 0
|
||||
|
||||
for (int i = 0; i < index.arrayLength; i++) {
|
||||
if (dt == null) {
|
||||
dt = cache[index[i]].timeAdded
|
||||
oldestItem = index[i]
|
||||
} else {
|
||||
if (du.before(cache[index[i]].timeAdded, dt)) {
|
||||
dt = cache[index[i]].timeAdded
|
||||
oldestItem = index[i]
|
||||
}
|
||||
}
|
||||
}
|
||||
return oldestItem
|
||||
} else if (flag == "r") {
|
||||
dt = ic.getTime()
|
||||
int msec = dt.millisecond
|
||||
ir.setSeed(msec)
|
||||
return ir.get(index.arrayLength)
|
||||
} else {
|
||||
return 0
|
||||
}
|
||||
}
|
||||
}
|
267
dat/cache/difffile-large/10/CacheHandler10_18.dn
vendored
Normal file
267
dat/cache/difffile-large/10/CacheHandler10_18.dn
vendored
Normal file
|
@ -0,0 +1,267 @@
|
|||
/*
|
||||
* EmerGen(e)tic 0.8
|
||||
* Copyright © 2017 Ben Goldsworthy (rumperuu)
|
||||
*
|
||||
* A program to research the utility of genetic algorithms, applied to
|
||||
* 'emergent' sytems.
|
||||
*
|
||||
* This file is part of EmerGen(e)tic.
|
||||
*
|
||||
* IMPROVEMENT(S): License (probably GNU GPLv3 tbh) goes here.
|
||||
*/
|
||||
|
||||
/**
|
||||
** This program serves as the base for each mutated CacheHandler,
|
||||
** providing the functionality that is common to all of them.
|
||||
**/
|
||||
|
||||
/**
|
||||
** @author Ben Goldsworthy (rumperuu) <bgoldsworthy96 @ gmail.com>
|
||||
** @version 1.0
|
||||
**/
|
||||
component provides cache.CacheHandler
|
||||
requires io.Output out,
|
||||
data.IntUtil iu,
|
||||
util.RandomInt ir,
|
||||
time.Calendar ic,
|
||||
time.DateUtil du,
|
||||
io.File {
|
||||
|
||||
static CachedResponse cache[]
|
||||
static int arraySize
|
||||
static Mutex cacheLock
|
||||
|
||||
/**
|
||||
** Returns a requested item from the cache.
|
||||
**
|
||||
** @param resource The request item filepath
|
||||
** @return The item from the cache
|
||||
**/
|
||||
CachedResponse CacheHandler:getCachedResponse(char filePath[]) {
|
||||
// Converts the filepath to an array of bytes for the file.
|
||||
File fd = new File(filePath, File.FILE_ACCESS_READ)
|
||||
char resource[] = fd.read(fd.getSize())
|
||||
fd.close()
|
||||
|
||||
// Returns the item if it's in the cache.
|
||||
mutex(cacheLock) {
|
||||
for (int i = 0; i < arraySize; i++) {
|
||||
if (cache[i].resource == resource) {
|
||||
cache[i].hits++
|
||||
cache[i].lastUsed = ic.getTime()
|
||||
return cache[i]
|
||||
}
|
||||
}
|
||||
}
|
||||
return null
|
||||
}
|
||||
|
||||
/**
|
||||
** Updates the cache if the requested item is not already present.
|
||||
** If the cache is full, the method of determining the item to be
|
||||
** replaced is subject to genetic mutation.
|
||||
**
|
||||
** @param ncr The new item to cache
|
||||
**/
|
||||
void CacheHandler:updateCache(CachedResponse ncr) {
|
||||
mutex(cacheLock) {
|
||||
// If the item is already in the cache, do nothing.
|
||||
for (int i = 0; i < arraySize; i++) {
|
||||
if (cache[i].resource == ncr.resource) {
|
||||
cache[i].response = ncr.response
|
||||
cache[i].resourceAge = ncr.resourceAge
|
||||
|
||||
return
|
||||
}
|
||||
}
|
||||
|
||||
// If the item is not, it must be added.
|
||||
// As `ncr` was created on another component, its details must
|
||||
// be copied to avoid a read-only exception.
|
||||
CachedResponse newEntry = new CachedResponse()
|
||||
newEntry.command = ncr.command
|
||||
newEntry.resource = ncr.resource
|
||||
newEntry.resourceAge = ncr.resourceAge
|
||||
newEntry.mimeType = ncr.mimeType
|
||||
newEntry.response = new byte[](ncr.response)
|
||||
newEntry.contentSize = ncr.contentSize
|
||||
|
||||
// Initialises the cache if there isn't currently one.
|
||||
if (cache == null) {
|
||||
cache = new CachedResponse[CacheHandler.MAX_SIZE]()
|
||||
}
|
||||
|
||||
// If the cache is full, determines which item to replace.
|
||||
if (arraySize == CacheHandler.MAX_SIZE) {
|
||||
int index
|
||||
|
||||
// BEGIN
|
||||
index = 0+nthMostRecentlyUsed(0)
|
||||
// END
|
||||
|
||||
cache[index % arraySize] = newEntry
|
||||
//out.println("$(debugMSG) replacing: $(iu.intToString(index))")
|
||||
// Otherwise, appends the item to the end of the cache.
|
||||
} else {
|
||||
cache[arraySize] = newEntry
|
||||
arraySize++
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
** Clears the cache completely.
|
||||
**/
|
||||
void CacheHandler:clearCache() {
|
||||
mutex(cacheLock) {
|
||||
cache = null
|
||||
}
|
||||
}
|
||||
|
||||
/**************************************************************************
|
||||
* What follows are utility functions that a given chromosome of the
|
||||
* `updateCache()` method may or may not call upon.
|
||||
**************************************************************************/
|
||||
|
||||
// This method returns the index of the nth most frequently-used item in
|
||||
// the cache.
|
||||
int nthMostFrequentlyUsed(int n) {
|
||||
int hits[]
|
||||
for (int i = 0; i < arraySize; i++) hits[i] = 0
|
||||
|
||||
int i
|
||||
int j
|
||||
mutex(cacheLock) {
|
||||
for (i = 0; i < arraySize; i++) {
|
||||
for (j = 0; j < arraySize; j++) {
|
||||
if (cache[i].hits == hits[j]) break
|
||||
else if (hits[j] == 0) {
|
||||
hits[j] = cache[i].hits
|
||||
break
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
int k = 0
|
||||
int nthHit = 0
|
||||
for (i = 0; i < j; i++) {
|
||||
if (hits[i] > nthHit) {
|
||||
nthHit = hits[i]
|
||||
k++
|
||||
}
|
||||
if (k == n) break
|
||||
}
|
||||
|
||||
int itemIndices[]
|
||||
|
||||
mutex(cacheLock) {
|
||||
j = 0
|
||||
for (i = 0; i < arraySize; i++) {
|
||||
if (cache[i].hits == nthHit) itemIndices[j++] = i
|
||||
}
|
||||
}
|
||||
|
||||
return resolve(itemIndices, "r")
|
||||
}
|
||||
|
||||
// This method returns the index of the nth most recently-used item in
|
||||
// the cache.
|
||||
int nthMostRecentlyUsed(int n) {
|
||||
DateTime useTimes[]
|
||||
DateTime testTime = ic.getTime()
|
||||
for (int i = 0; i < arraySize; i++) useTimes[i] = testTime
|
||||
|
||||
int i
|
||||
int j
|
||||
mutex(cacheLock) {
|
||||
for (i = 0; i < arraySize; i++) {
|
||||
for (j = 0; j < arraySize; j++) {
|
||||
if (du.equal(cache[i].lastUsed, useTimes[j])) break
|
||||
else if (du.equal(useTimes[j], testTime)) {
|
||||
useTimes[j] = cache[i].lastUsed
|
||||
break
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
int k = 0
|
||||
DateTime nthUseTime = ic.getTime()
|
||||
for (i = 0; i < j; i++) {
|
||||
if (du.after(useTimes[i], nthUseTime)) {
|
||||
nthUseTime = useTimes[i]
|
||||
k++
|
||||
}
|
||||
if (k == n) break
|
||||
}
|
||||
|
||||
int itemIndices[]
|
||||
|
||||
mutex(cacheLock) {
|
||||
j = 0
|
||||
for (i = 0; i < arraySize; i++) {
|
||||
if (du.equal(cache[i].lastUsed, nthUseTime)) itemIndices[j++] = i
|
||||
}
|
||||
}
|
||||
|
||||
return resolve(itemIndices, "r")
|
||||
}
|
||||
|
||||
// This method returns a random index.
|
||||
int random() {
|
||||
DateTime dt = ic.getTime()
|
||||
int msec = dt.millisecond
|
||||
ir.setSeed(msec)
|
||||
return ir.get(CacheHandler.MAX_SIZE)
|
||||
}
|
||||
|
||||
// This method resolves a multiple return in one of the methods above,
|
||||
// as per the flag sent along with the list of indices.
|
||||
// 'n' returns the newest, 'o' the oldest and 'r' a random index.
|
||||
int resolve(int index[], char flag) {
|
||||
DateTime dt = null
|
||||
|
||||
if (flag == "n") {
|
||||
int newestItem = 0
|
||||
|
||||
int i = index.arrayLength-1
|
||||
for (int j = 0; j < index.arrayLength; j++) {
|
||||
if (dt == null) {
|
||||
dt = cache[index[i]].timeAdded
|
||||
newestItem = index[i]
|
||||
} else {
|
||||
if (du.before(dt, cache[index[i]].timeAdded)) {
|
||||
dt = cache[index[i]].timeAdded
|
||||
newestItem = index[i]
|
||||
}
|
||||
}
|
||||
i--
|
||||
}
|
||||
|
||||
return newestItem
|
||||
} else if (flag == "o") {
|
||||
int oldestItem = 0
|
||||
|
||||
for (int i = 0; i < index.arrayLength; i++) {
|
||||
if (dt == null) {
|
||||
dt = cache[index[i]].timeAdded
|
||||
oldestItem = index[i]
|
||||
} else {
|
||||
if (du.before(cache[index[i]].timeAdded, dt)) {
|
||||
dt = cache[index[i]].timeAdded
|
||||
oldestItem = index[i]
|
||||
}
|
||||
}
|
||||
}
|
||||
return oldestItem
|
||||
} else if (flag == "r") {
|
||||
dt = ic.getTime()
|
||||
int msec = dt.millisecond
|
||||
ir.setSeed(msec)
|
||||
return ir.get(index.arrayLength)
|
||||
} else {
|
||||
return 0
|
||||
}
|
||||
}
|
||||
}
|
267
dat/cache/difffile-large/10/CacheHandler10_19.dn
vendored
Normal file
267
dat/cache/difffile-large/10/CacheHandler10_19.dn
vendored
Normal file
|
@ -0,0 +1,267 @@
|
|||
/*
|
||||
* EmerGen(e)tic 0.8
|
||||
* Copyright © 2017 Ben Goldsworthy (rumperuu)
|
||||
*
|
||||
* A program to research the utility of genetic algorithms, applied to
|
||||
* 'emergent' sytems.
|
||||
*
|
||||
* This file is part of EmerGen(e)tic.
|
||||
*
|
||||
* IMPROVEMENT(S): License (probably GNU GPLv3 tbh) goes here.
|
||||
*/
|
||||
|
||||
/**
|
||||
** This program serves as the base for each mutated CacheHandler,
|
||||
** providing the functionality that is common to all of them.
|
||||
**/
|
||||
|
||||
/**
|
||||
** @author Ben Goldsworthy (rumperuu) <bgoldsworthy96 @ gmail.com>
|
||||
** @version 1.0
|
||||
**/
|
||||
component provides cache.CacheHandler
|
||||
requires io.Output out,
|
||||
data.IntUtil iu,
|
||||
util.RandomInt ir,
|
||||
time.Calendar ic,
|
||||
time.DateUtil du,
|
||||
io.File {
|
||||
|
||||
static CachedResponse cache[]
|
||||
static int arraySize
|
||||
static Mutex cacheLock
|
||||
|
||||
/**
|
||||
** Returns a requested item from the cache.
|
||||
**
|
||||
** @param resource The request item filepath
|
||||
** @return The item from the cache
|
||||
**/
|
||||
CachedResponse CacheHandler:getCachedResponse(char filePath[]) {
|
||||
// Converts the filepath to an array of bytes for the file.
|
||||
File fd = new File(filePath, File.FILE_ACCESS_READ)
|
||||
char resource[] = fd.read(fd.getSize())
|
||||
fd.close()
|
||||
|
||||
// Returns the item if it's in the cache.
|
||||
mutex(cacheLock) {
|
||||
for (int i = 0; i < arraySize; i++) {
|
||||
if (cache[i].resource == resource) {
|
||||
cache[i].hits++
|
||||
cache[i].lastUsed = ic.getTime()
|
||||
return cache[i]
|
||||
}
|
||||
}
|
||||
}
|
||||
return null
|
||||
}
|
||||
|
||||
/**
|
||||
** Updates the cache if the requested item is not already present.
|
||||
** If the cache is full, the method of determining the item to be
|
||||
** replaced is subject to genetic mutation.
|
||||
**
|
||||
** @param ncr The new item to cache
|
||||
**/
|
||||
void CacheHandler:updateCache(CachedResponse ncr) {
|
||||
mutex(cacheLock) {
|
||||
// If the item is already in the cache, do nothing.
|
||||
for (int i = 0; i < arraySize; i++) {
|
||||
if (cache[i].resource == ncr.resource) {
|
||||
cache[i].response = ncr.response
|
||||
cache[i].resourceAge = ncr.resourceAge
|
||||
|
||||
return
|
||||
}
|
||||
}
|
||||
|
||||
// If the item is not, it must be added.
|
||||
// As `ncr` was created on another component, its details must
|
||||
// be copied to avoid a read-only exception.
|
||||
CachedResponse newEntry = new CachedResponse()
|
||||
newEntry.command = ncr.command
|
||||
newEntry.resource = ncr.resource
|
||||
newEntry.resourceAge = ncr.resourceAge
|
||||
newEntry.mimeType = ncr.mimeType
|
||||
newEntry.response = new byte[](ncr.response)
|
||||
newEntry.contentSize = ncr.contentSize
|
||||
|
||||
// Initialises the cache if there isn't currently one.
|
||||
if (cache == null) {
|
||||
cache = new CachedResponse[CacheHandler.MAX_SIZE]()
|
||||
}
|
||||
|
||||
// If the cache is full, determines which item to replace.
|
||||
if (arraySize == CacheHandler.MAX_SIZE) {
|
||||
int index
|
||||
|
||||
// BEGIN
|
||||
index = 12-nthMostRecentlyUsed(0)
|
||||
// END
|
||||
|
||||
cache[index % arraySize] = newEntry
|
||||
//out.println("$(debugMSG) replacing: $(iu.intToString(index))")
|
||||
// Otherwise, appends the item to the end of the cache.
|
||||
} else {
|
||||
cache[arraySize] = newEntry
|
||||
arraySize++
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
** Clears the cache completely.
|
||||
**/
|
||||
void CacheHandler:clearCache() {
|
||||
mutex(cacheLock) {
|
||||
cache = null
|
||||
}
|
||||
}
|
||||
|
||||
/**************************************************************************
|
||||
* What follows are utility functions that a given chromosome of the
|
||||
* `updateCache()` method may or may not call upon.
|
||||
**************************************************************************/
|
||||
|
||||
// This method returns the index of the nth most frequently-used item in
|
||||
// the cache.
|
||||
int nthMostFrequentlyUsed(int n) {
|
||||
int hits[]
|
||||
for (int i = 0; i < arraySize; i++) hits[i] = 0
|
||||
|
||||
int i
|
||||
int j
|
||||
mutex(cacheLock) {
|
||||
for (i = 0; i < arraySize; i++) {
|
||||
for (j = 0; j < arraySize; j++) {
|
||||
if (cache[i].hits == hits[j]) break
|
||||
else if (hits[j] == 0) {
|
||||
hits[j] = cache[i].hits
|
||||
break
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
int k = 0
|
||||
int nthHit = 0
|
||||
for (i = 0; i < j; i++) {
|
||||
if (hits[i] > nthHit) {
|
||||
nthHit = hits[i]
|
||||
k++
|
||||
}
|
||||
if (k == n) break
|
||||
}
|
||||
|
||||
int itemIndices[]
|
||||
|
||||
mutex(cacheLock) {
|
||||
j = 0
|
||||
for (i = 0; i < arraySize; i++) {
|
||||
if (cache[i].hits == nthHit) itemIndices[j++] = i
|
||||
}
|
||||
}
|
||||
|
||||
return resolve(itemIndices, "r")
|
||||
}
|
||||
|
||||
// This method returns the index of the nth most recently-used item in
|
||||
// the cache.
|
||||
int nthMostRecentlyUsed(int n) {
|
||||
DateTime useTimes[]
|
||||
DateTime testTime = ic.getTime()
|
||||
for (int i = 0; i < arraySize; i++) useTimes[i] = testTime
|
||||
|
||||
int i
|
||||
int j
|
||||
mutex(cacheLock) {
|
||||
for (i = 0; i < arraySize; i++) {
|
||||
for (j = 0; j < arraySize; j++) {
|
||||
if (du.equal(cache[i].lastUsed, useTimes[j])) break
|
||||
else if (du.equal(useTimes[j], testTime)) {
|
||||
useTimes[j] = cache[i].lastUsed
|
||||
break
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
int k = 0
|
||||
DateTime nthUseTime = ic.getTime()
|
||||
for (i = 0; i < j; i++) {
|
||||
if (du.after(useTimes[i], nthUseTime)) {
|
||||
nthUseTime = useTimes[i]
|
||||
k++
|
||||
}
|
||||
if (k == n) break
|
||||
}
|
||||
|
||||
int itemIndices[]
|
||||
|
||||
mutex(cacheLock) {
|
||||
j = 0
|
||||
for (i = 0; i < arraySize; i++) {
|
||||
if (du.equal(cache[i].lastUsed, nthUseTime)) itemIndices[j++] = i
|
||||
}
|
||||
}
|
||||
|
||||
return resolve(itemIndices, "r")
|
||||
}
|
||||
|
||||
// This method returns a random index.
|
||||
int random() {
|
||||
DateTime dt = ic.getTime()
|
||||
int msec = dt.millisecond
|
||||
ir.setSeed(msec)
|
||||
return ir.get(CacheHandler.MAX_SIZE)
|
||||
}
|
||||
|
||||
// This method resolves a multiple return in one of the methods above,
|
||||
// as per the flag sent along with the list of indices.
|
||||
// 'n' returns the newest, 'o' the oldest and 'r' a random index.
|
||||
int resolve(int index[], char flag) {
|
||||
DateTime dt = null
|
||||
|
||||
if (flag == "n") {
|
||||
int newestItem = 0
|
||||
|
||||
int i = index.arrayLength-1
|
||||
for (int j = 0; j < index.arrayLength; j++) {
|
||||
if (dt == null) {
|
||||
dt = cache[index[i]].timeAdded
|
||||
newestItem = index[i]
|
||||
} else {
|
||||
if (du.before(dt, cache[index[i]].timeAdded)) {
|
||||
dt = cache[index[i]].timeAdded
|
||||
newestItem = index[i]
|
||||
}
|
||||
}
|
||||
i--
|
||||
}
|
||||
|
||||
return newestItem
|
||||
} else if (flag == "o") {
|
||||
int oldestItem = 0
|
||||
|
||||
for (int i = 0; i < index.arrayLength; i++) {
|
||||
if (dt == null) {
|
||||
dt = cache[index[i]].timeAdded
|
||||
oldestItem = index[i]
|
||||
} else {
|
||||
if (du.before(cache[index[i]].timeAdded, dt)) {
|
||||
dt = cache[index[i]].timeAdded
|
||||
oldestItem = index[i]
|
||||
}
|
||||
}
|
||||
}
|
||||
return oldestItem
|
||||
} else if (flag == "r") {
|
||||
dt = ic.getTime()
|
||||
int msec = dt.millisecond
|
||||
ir.setSeed(msec)
|
||||
return ir.get(index.arrayLength)
|
||||
} else {
|
||||
return 0
|
||||
}
|
||||
}
|
||||
}
|
267
dat/cache/difffile-large/10/CacheHandler10_2.dn
vendored
Normal file
267
dat/cache/difffile-large/10/CacheHandler10_2.dn
vendored
Normal file
|
@ -0,0 +1,267 @@
|
|||
/*
|
||||
* EmerGen(e)tic 0.8
|
||||
* Copyright © 2017 Ben Goldsworthy (rumperuu)
|
||||
*
|
||||
* A program to research the utility of genetic algorithms, applied to
|
||||
* 'emergent' sytems.
|
||||
*
|
||||
* This file is part of EmerGen(e)tic.
|
||||
*
|
||||
* IMPROVEMENT(S): License (probably GNU GPLv3 tbh) goes here.
|
||||
*/
|
||||
|
||||
/**
|
||||
** This program serves as the base for each mutated CacheHandler,
|
||||
** providing the functionality that is common to all of them.
|
||||
**/
|
||||
|
||||
/**
|
||||
** @author Ben Goldsworthy (rumperuu) <bgoldsworthy96 @ gmail.com>
|
||||
** @version 1.0
|
||||
**/
|
||||
component provides cache.CacheHandler
|
||||
requires io.Output out,
|
||||
data.IntUtil iu,
|
||||
util.RandomInt ir,
|
||||
time.Calendar ic,
|
||||
time.DateUtil du,
|
||||
io.File {
|
||||
|
||||
static CachedResponse cache[]
|
||||
static int arraySize
|
||||
static Mutex cacheLock
|
||||
|
||||
/**
|
||||
** Returns a requested item from the cache.
|
||||
**
|
||||
** @param resource The request item filepath
|
||||
** @return The item from the cache
|
||||
**/
|
||||
CachedResponse CacheHandler:getCachedResponse(char filePath[]) {
|
||||
// Converts the filepath to an array of bytes for the file.
|
||||
File fd = new File(filePath, File.FILE_ACCESS_READ)
|
||||
char resource[] = fd.read(fd.getSize())
|
||||
fd.close()
|
||||
|
||||
// Returns the item if it's in the cache.
|
||||
mutex(cacheLock) {
|
||||
for (int i = 0; i < arraySize; i++) {
|
||||
if (cache[i].resource == resource) {
|
||||
cache[i].hits++
|
||||
cache[i].lastUsed = ic.getTime()
|
||||
return cache[i]
|
||||
}
|
||||
}
|
||||
}
|
||||
return null
|
||||
}
|
||||
|
||||
/**
|
||||
** Updates the cache if the requested item is not already present.
|
||||
** If the cache is full, the method of determining the item to be
|
||||
** replaced is subject to genetic mutation.
|
||||
**
|
||||
** @param ncr The new item to cache
|
||||
**/
|
||||
void CacheHandler:updateCache(CachedResponse ncr) {
|
||||
mutex(cacheLock) {
|
||||
// If the item is already in the cache, do nothing.
|
||||
for (int i = 0; i < arraySize; i++) {
|
||||
if (cache[i].resource == ncr.resource) {
|
||||
cache[i].response = ncr.response
|
||||
cache[i].resourceAge = ncr.resourceAge
|
||||
|
||||
return
|
||||
}
|
||||
}
|
||||
|
||||
// If the item is not, it must be added.
|
||||
// As `ncr` was created on another component, its details must
|
||||
// be copied to avoid a read-only exception.
|
||||
CachedResponse newEntry = new CachedResponse()
|
||||
newEntry.command = ncr.command
|
||||
newEntry.resource = ncr.resource
|
||||
newEntry.resourceAge = ncr.resourceAge
|
||||
newEntry.mimeType = ncr.mimeType
|
||||
newEntry.response = new byte[](ncr.response)
|
||||
newEntry.contentSize = ncr.contentSize
|
||||
|
||||
// Initialises the cache if there isn't currently one.
|
||||
if (cache == null) {
|
||||
cache = new CachedResponse[CacheHandler.MAX_SIZE]()
|
||||
}
|
||||
|
||||
// If the cache is full, determines which item to replace.
|
||||
if (arraySize == CacheHandler.MAX_SIZE) {
|
||||
int index
|
||||
|
||||
// BEGIN
|
||||
index = 0-nthMostRecentlyUsed(0)
|
||||
// END
|
||||
|
||||
cache[index % arraySize] = newEntry
|
||||
//out.println("$(debugMSG) replacing: $(iu.intToString(index))")
|
||||
// Otherwise, appends the item to the end of the cache.
|
||||
} else {
|
||||
cache[arraySize] = newEntry
|
||||
arraySize++
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
** Clears the cache completely.
|
||||
**/
|
||||
void CacheHandler:clearCache() {
|
||||
mutex(cacheLock) {
|
||||
cache = null
|
||||
}
|
||||
}
|
||||
|
||||
/**************************************************************************
|
||||
* What follows are utility functions that a given chromosome of the
|
||||
* `updateCache()` method may or may not call upon.
|
||||
**************************************************************************/
|
||||
|
||||
// This method returns the index of the nth most frequently-used item in
|
||||
// the cache.
|
||||
int nthMostFrequentlyUsed(int n) {
|
||||
int hits[]
|
||||
for (int i = 0; i < arraySize; i++) hits[i] = 0
|
||||
|
||||
int i
|
||||
int j
|
||||
mutex(cacheLock) {
|
||||
for (i = 0; i < arraySize; i++) {
|
||||
for (j = 0; j < arraySize; j++) {
|
||||
if (cache[i].hits == hits[j]) break
|
||||
else if (hits[j] == 0) {
|
||||
hits[j] = cache[i].hits
|
||||
break
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
int k = 0
|
||||
int nthHit = 0
|
||||
for (i = 0; i < j; i++) {
|
||||
if (hits[i] > nthHit) {
|
||||
nthHit = hits[i]
|
||||
k++
|
||||
}
|
||||
if (k == n) break
|
||||
}
|
||||
|
||||
int itemIndices[]
|
||||
|
||||
mutex(cacheLock) {
|
||||
j = 0
|
||||
for (i = 0; i < arraySize; i++) {
|
||||
if (cache[i].hits == nthHit) itemIndices[j++] = i
|
||||
}
|
||||
}
|
||||
|
||||
return resolve(itemIndices, "r")
|
||||
}
|
||||
|
||||
// This method returns the index of the nth most recently-used item in
|
||||
// the cache.
|
||||
int nthMostRecentlyUsed(int n) {
|
||||
DateTime useTimes[]
|
||||
DateTime testTime = ic.getTime()
|
||||
for (int i = 0; i < arraySize; i++) useTimes[i] = testTime
|
||||
|
||||
int i
|
||||
int j
|
||||
mutex(cacheLock) {
|
||||
for (i = 0; i < arraySize; i++) {
|
||||
for (j = 0; j < arraySize; j++) {
|
||||
if (du.equal(cache[i].lastUsed, useTimes[j])) break
|
||||
else if (du.equal(useTimes[j], testTime)) {
|
||||
useTimes[j] = cache[i].lastUsed
|
||||
break
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
int k = 0
|
||||
DateTime nthUseTime = ic.getTime()
|
||||
for (i = 0; i < j; i++) {
|
||||
if (du.after(useTimes[i], nthUseTime)) {
|
||||
nthUseTime = useTimes[i]
|
||||
k++
|
||||
}
|
||||
if (k == n) break
|
||||
}
|
||||
|
||||
int itemIndices[]
|
||||
|
||||
mutex(cacheLock) {
|
||||
j = 0
|
||||
for (i = 0; i < arraySize; i++) {
|
||||
if (du.equal(cache[i].lastUsed, nthUseTime)) itemIndices[j++] = i
|
||||
}
|
||||
}
|
||||
|
||||
return resolve(itemIndices, "r")
|
||||
}
|
||||
|
||||
// This method returns a random index.
|
||||
int random() {
|
||||
DateTime dt = ic.getTime()
|
||||
int msec = dt.millisecond
|
||||
ir.setSeed(msec)
|
||||
return ir.get(CacheHandler.MAX_SIZE)
|
||||
}
|
||||
|
||||
// This method resolves a multiple return in one of the methods above,
|
||||
// as per the flag sent along with the list of indices.
|
||||
// 'n' returns the newest, 'o' the oldest and 'r' a random index.
|
||||
int resolve(int index[], char flag) {
|
||||
DateTime dt = null
|
||||
|
||||
if (flag == "n") {
|
||||
int newestItem = 0
|
||||
|
||||
int i = index.arrayLength-1
|
||||
for (int j = 0; j < index.arrayLength; j++) {
|
||||
if (dt == null) {
|
||||
dt = cache[index[i]].timeAdded
|
||||
newestItem = index[i]
|
||||
} else {
|
||||
if (du.before(dt, cache[index[i]].timeAdded)) {
|
||||
dt = cache[index[i]].timeAdded
|
||||
newestItem = index[i]
|
||||
}
|
||||
}
|
||||
i--
|
||||
}
|
||||
|
||||
return newestItem
|
||||
} else if (flag == "o") {
|
||||
int oldestItem = 0
|
||||
|
||||
for (int i = 0; i < index.arrayLength; i++) {
|
||||
if (dt == null) {
|
||||
dt = cache[index[i]].timeAdded
|
||||
oldestItem = index[i]
|
||||
} else {
|
||||
if (du.before(cache[index[i]].timeAdded, dt)) {
|
||||
dt = cache[index[i]].timeAdded
|
||||
oldestItem = index[i]
|
||||
}
|
||||
}
|
||||
}
|
||||
return oldestItem
|
||||
} else if (flag == "r") {
|
||||
dt = ic.getTime()
|
||||
int msec = dt.millisecond
|
||||
ir.setSeed(msec)
|
||||
return ir.get(index.arrayLength)
|
||||
} else {
|
||||
return 0
|
||||
}
|
||||
}
|
||||
}
|
267
dat/cache/difffile-large/10/CacheHandler10_20.dn
vendored
Normal file
267
dat/cache/difffile-large/10/CacheHandler10_20.dn
vendored
Normal file
|
@ -0,0 +1,267 @@
|
|||
/*
|
||||
* EmerGen(e)tic 0.8
|
||||
* Copyright © 2017 Ben Goldsworthy (rumperuu)
|
||||
*
|
||||
* A program to research the utility of genetic algorithms, applied to
|
||||
* 'emergent' sytems.
|
||||
*
|
||||
* This file is part of EmerGen(e)tic.
|
||||
*
|
||||
* IMPROVEMENT(S): License (probably GNU GPLv3 tbh) goes here.
|
||||
*/
|
||||
|
||||
/**
|
||||
** This program serves as the base for each mutated CacheHandler,
|
||||
** providing the functionality that is common to all of them.
|
||||
**/
|
||||
|
||||
/**
|
||||
** @author Ben Goldsworthy (rumperuu) <bgoldsworthy96 @ gmail.com>
|
||||
** @version 1.0
|
||||
**/
|
||||
component provides cache.CacheHandler
|
||||
requires io.Output out,
|
||||
data.IntUtil iu,
|
||||
util.RandomInt ir,
|
||||
time.Calendar ic,
|
||||
time.DateUtil du,
|
||||
io.File {
|
||||
|
||||
static CachedResponse cache[]
|
||||
static int arraySize
|
||||
static Mutex cacheLock
|
||||
|
||||
/**
|
||||
** Returns a requested item from the cache.
|
||||
**
|
||||
** @param resource The request item filepath
|
||||
** @return The item from the cache
|
||||
**/
|
||||
CachedResponse CacheHandler:getCachedResponse(char filePath[]) {
|
||||
// Converts the filepath to an array of bytes for the file.
|
||||
File fd = new File(filePath, File.FILE_ACCESS_READ)
|
||||
char resource[] = fd.read(fd.getSize())
|
||||
fd.close()
|
||||
|
||||
// Returns the item if it's in the cache.
|
||||
mutex(cacheLock) {
|
||||
for (int i = 0; i < arraySize; i++) {
|
||||
if (cache[i].resource == resource) {
|
||||
cache[i].hits++
|
||||
cache[i].lastUsed = ic.getTime()
|
||||
return cache[i]
|
||||
}
|
||||
}
|
||||
}
|
||||
return null
|
||||
}
|
||||
|
||||
/**
|
||||
** Updates the cache if the requested item is not already present.
|
||||
** If the cache is full, the method of determining the item to be
|
||||
** replaced is subject to genetic mutation.
|
||||
**
|
||||
** @param ncr The new item to cache
|
||||
**/
|
||||
void CacheHandler:updateCache(CachedResponse ncr) {
|
||||
mutex(cacheLock) {
|
||||
// If the item is already in the cache, do nothing.
|
||||
for (int i = 0; i < arraySize; i++) {
|
||||
if (cache[i].resource == ncr.resource) {
|
||||
cache[i].response = ncr.response
|
||||
cache[i].resourceAge = ncr.resourceAge
|
||||
|
||||
return
|
||||
}
|
||||
}
|
||||
|
||||
// If the item is not, it must be added.
|
||||
// As `ncr` was created on another component, its details must
|
||||
// be copied to avoid a read-only exception.
|
||||
CachedResponse newEntry = new CachedResponse()
|
||||
newEntry.command = ncr.command
|
||||
newEntry.resource = ncr.resource
|
||||
newEntry.resourceAge = ncr.resourceAge
|
||||
newEntry.mimeType = ncr.mimeType
|
||||
newEntry.response = new byte[](ncr.response)
|
||||
newEntry.contentSize = ncr.contentSize
|
||||
|
||||
// Initialises the cache if there isn't currently one.
|
||||
if (cache == null) {
|
||||
cache = new CachedResponse[CacheHandler.MAX_SIZE]()
|
||||
}
|
||||
|
||||
// If the cache is full, determines which item to replace.
|
||||
if (arraySize == CacheHandler.MAX_SIZE) {
|
||||
int index
|
||||
|
||||
// BEGIN
|
||||
index = 0
|
||||
// END
|
||||
|
||||
cache[index % arraySize] = newEntry
|
||||
//out.println("$(debugMSG) replacing: $(iu.intToString(index))")
|
||||
// Otherwise, appends the item to the end of the cache.
|
||||
} else {
|
||||
cache[arraySize] = newEntry
|
||||
arraySize++
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
** Clears the cache completely.
|
||||
**/
|
||||
void CacheHandler:clearCache() {
|
||||
mutex(cacheLock) {
|
||||
cache = null
|
||||
}
|
||||
}
|
||||
|
||||
/**************************************************************************
|
||||
* What follows are utility functions that a given chromosome of the
|
||||
* `updateCache()` method may or may not call upon.
|
||||
**************************************************************************/
|
||||
|
||||
// This method returns the index of the nth most frequently-used item in
|
||||
// the cache.
|
||||
int nthMostFrequentlyUsed(int n) {
|
||||
int hits[]
|
||||
for (int i = 0; i < arraySize; i++) hits[i] = 0
|
||||
|
||||
int i
|
||||
int j
|
||||
mutex(cacheLock) {
|
||||
for (i = 0; i < arraySize; i++) {
|
||||
for (j = 0; j < arraySize; j++) {
|
||||
if (cache[i].hits == hits[j]) break
|
||||
else if (hits[j] == 0) {
|
||||
hits[j] = cache[i].hits
|
||||
break
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
int k = 0
|
||||
int nthHit = 0
|
||||
for (i = 0; i < j; i++) {
|
||||
if (hits[i] > nthHit) {
|
||||
nthHit = hits[i]
|
||||
k++
|
||||
}
|
||||
if (k == n) break
|
||||
}
|
||||
|
||||
int itemIndices[]
|
||||
|
||||
mutex(cacheLock) {
|
||||
j = 0
|
||||
for (i = 0; i < arraySize; i++) {
|
||||
if (cache[i].hits == nthHit) itemIndices[j++] = i
|
||||
}
|
||||
}
|
||||
|
||||
return resolve(itemIndices, "r")
|
||||
}
|
||||
|
||||
// This method returns the index of the nth most recently-used item in
|
||||
// the cache.
|
||||
int nthMostRecentlyUsed(int n) {
|
||||
DateTime useTimes[]
|
||||
DateTime testTime = ic.getTime()
|
||||
for (int i = 0; i < arraySize; i++) useTimes[i] = testTime
|
||||
|
||||
int i
|
||||
int j
|
||||
mutex(cacheLock) {
|
||||
for (i = 0; i < arraySize; i++) {
|
||||
for (j = 0; j < arraySize; j++) {
|
||||
if (du.equal(cache[i].lastUsed, useTimes[j])) break
|
||||
else if (du.equal(useTimes[j], testTime)) {
|
||||
useTimes[j] = cache[i].lastUsed
|
||||
break
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
int k = 0
|
||||
DateTime nthUseTime = ic.getTime()
|
||||
for (i = 0; i < j; i++) {
|
||||
if (du.after(useTimes[i], nthUseTime)) {
|
||||
nthUseTime = useTimes[i]
|
||||
k++
|
||||
}
|
||||
if (k == n) break
|
||||
}
|
||||
|
||||
int itemIndices[]
|
||||
|
||||
mutex(cacheLock) {
|
||||
j = 0
|
||||
for (i = 0; i < arraySize; i++) {
|
||||
if (du.equal(cache[i].lastUsed, nthUseTime)) itemIndices[j++] = i
|
||||
}
|
||||
}
|
||||
|
||||
return resolve(itemIndices, "r")
|
||||
}
|
||||
|
||||
// This method returns a random index.
|
||||
int random() {
|
||||
DateTime dt = ic.getTime()
|
||||
int msec = dt.millisecond
|
||||
ir.setSeed(msec)
|
||||
return ir.get(CacheHandler.MAX_SIZE)
|
||||
}
|
||||
|
||||
// This method resolves a multiple return in one of the methods above,
|
||||
// as per the flag sent along with the list of indices.
|
||||
// 'n' returns the newest, 'o' the oldest and 'r' a random index.
|
||||
int resolve(int index[], char flag) {
|
||||
DateTime dt = null
|
||||
|
||||
if (flag == "n") {
|
||||
int newestItem = 0
|
||||
|
||||
int i = index.arrayLength-1
|
||||
for (int j = 0; j < index.arrayLength; j++) {
|
||||
if (dt == null) {
|
||||
dt = cache[index[i]].timeAdded
|
||||
newestItem = index[i]
|
||||
} else {
|
||||
if (du.before(dt, cache[index[i]].timeAdded)) {
|
||||
dt = cache[index[i]].timeAdded
|
||||
newestItem = index[i]
|
||||
}
|
||||
}
|
||||
i--
|
||||
}
|
||||
|
||||
return newestItem
|
||||
} else if (flag == "o") {
|
||||
int oldestItem = 0
|
||||
|
||||
for (int i = 0; i < index.arrayLength; i++) {
|
||||
if (dt == null) {
|
||||
dt = cache[index[i]].timeAdded
|
||||
oldestItem = index[i]
|
||||
} else {
|
||||
if (du.before(cache[index[i]].timeAdded, dt)) {
|
||||
dt = cache[index[i]].timeAdded
|
||||
oldestItem = index[i]
|
||||
}
|
||||
}
|
||||
}
|
||||
return oldestItem
|
||||
} else if (flag == "r") {
|
||||
dt = ic.getTime()
|
||||
int msec = dt.millisecond
|
||||
ir.setSeed(msec)
|
||||
return ir.get(index.arrayLength)
|
||||
} else {
|
||||
return 0
|
||||
}
|
||||
}
|
||||
}
|
267
dat/cache/difffile-large/10/CacheHandler10_21.dn
vendored
Normal file
267
dat/cache/difffile-large/10/CacheHandler10_21.dn
vendored
Normal file
|
@ -0,0 +1,267 @@
|
|||
/*
|
||||
* EmerGen(e)tic 0.8
|
||||
* Copyright © 2017 Ben Goldsworthy (rumperuu)
|
||||
*
|
||||
* A program to research the utility of genetic algorithms, applied to
|
||||
* 'emergent' sytems.
|
||||
*
|
||||
* This file is part of EmerGen(e)tic.
|
||||
*
|
||||
* IMPROVEMENT(S): License (probably GNU GPLv3 tbh) goes here.
|
||||
*/
|
||||
|
||||
/**
|
||||
** This program serves as the base for each mutated CacheHandler,
|
||||
** providing the functionality that is common to all of them.
|
||||
**/
|
||||
|
||||
/**
|
||||
** @author Ben Goldsworthy (rumperuu) <bgoldsworthy96 @ gmail.com>
|
||||
** @version 1.0
|
||||
**/
|
||||
component provides cache.CacheHandler
|
||||
requires io.Output out,
|
||||
data.IntUtil iu,
|
||||
util.RandomInt ir,
|
||||
time.Calendar ic,
|
||||
time.DateUtil du,
|
||||
io.File {
|
||||
|
||||
static CachedResponse cache[]
|
||||
static int arraySize
|
||||
static Mutex cacheLock
|
||||
|
||||
/**
|
||||
** Returns a requested item from the cache.
|
||||
**
|
||||
** @param resource The request item filepath
|
||||
** @return The item from the cache
|
||||
**/
|
||||
CachedResponse CacheHandler:getCachedResponse(char filePath[]) {
|
||||
// Converts the filepath to an array of bytes for the file.
|
||||
File fd = new File(filePath, File.FILE_ACCESS_READ)
|
||||
char resource[] = fd.read(fd.getSize())
|
||||
fd.close()
|
||||
|
||||
// Returns the item if it's in the cache.
|
||||
mutex(cacheLock) {
|
||||
for (int i = 0; i < arraySize; i++) {
|
||||
if (cache[i].resource == resource) {
|
||||
cache[i].hits++
|
||||
cache[i].lastUsed = ic.getTime()
|
||||
return cache[i]
|
||||
}
|
||||
}
|
||||
}
|
||||
return null
|
||||
}
|
||||
|
||||
/**
|
||||
** Updates the cache if the requested item is not already present.
|
||||
** If the cache is full, the method of determining the item to be
|
||||
** replaced is subject to genetic mutation.
|
||||
**
|
||||
** @param ncr The new item to cache
|
||||
**/
|
||||
void CacheHandler:updateCache(CachedResponse ncr) {
|
||||
mutex(cacheLock) {
|
||||
// If the item is already in the cache, do nothing.
|
||||
for (int i = 0; i < arraySize; i++) {
|
||||
if (cache[i].resource == ncr.resource) {
|
||||
cache[i].response = ncr.response
|
||||
cache[i].resourceAge = ncr.resourceAge
|
||||
|
||||
return
|
||||
}
|
||||
}
|
||||
|
||||
// If the item is not, it must be added.
|
||||
// As `ncr` was created on another component, its details must
|
||||
// be copied to avoid a read-only exception.
|
||||
CachedResponse newEntry = new CachedResponse()
|
||||
newEntry.command = ncr.command
|
||||
newEntry.resource = ncr.resource
|
||||
newEntry.resourceAge = ncr.resourceAge
|
||||
newEntry.mimeType = ncr.mimeType
|
||||
newEntry.response = new byte[](ncr.response)
|
||||
newEntry.contentSize = ncr.contentSize
|
||||
|
||||
// Initialises the cache if there isn't currently one.
|
||||
if (cache == null) {
|
||||
cache = new CachedResponse[CacheHandler.MAX_SIZE]()
|
||||
}
|
||||
|
||||
// If the cache is full, determines which item to replace.
|
||||
if (arraySize == CacheHandler.MAX_SIZE) {
|
||||
int index
|
||||
|
||||
// BEGIN
|
||||
index = 0
|
||||
// END
|
||||
|
||||
cache[index % arraySize] = newEntry
|
||||
//out.println("$(debugMSG) replacing: $(iu.intToString(index))")
|
||||
// Otherwise, appends the item to the end of the cache.
|
||||
} else {
|
||||
cache[arraySize] = newEntry
|
||||
arraySize++
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
** Clears the cache completely.
|
||||
**/
|
||||
void CacheHandler:clearCache() {
|
||||
mutex(cacheLock) {
|
||||
cache = null
|
||||
}
|
||||
}
|
||||
|
||||
/**************************************************************************
|
||||
* What follows are utility functions that a given chromosome of the
|
||||
* `updateCache()` method may or may not call upon.
|
||||
**************************************************************************/
|
||||
|
||||
// This method returns the index of the nth most frequently-used item in
|
||||
// the cache.
|
||||
int nthMostFrequentlyUsed(int n) {
|
||||
int hits[]
|
||||
for (int i = 0; i < arraySize; i++) hits[i] = 0
|
||||
|
||||
int i
|
||||
int j
|
||||
mutex(cacheLock) {
|
||||
for (i = 0; i < arraySize; i++) {
|
||||
for (j = 0; j < arraySize; j++) {
|
||||
if (cache[i].hits == hits[j]) break
|
||||
else if (hits[j] == 0) {
|
||||
hits[j] = cache[i].hits
|
||||
break
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
int k = 0
|
||||
int nthHit = 0
|
||||
for (i = 0; i < j; i++) {
|
||||
if (hits[i] > nthHit) {
|
||||
nthHit = hits[i]
|
||||
k++
|
||||
}
|
||||
if (k == n) break
|
||||
}
|
||||
|
||||
int itemIndices[]
|
||||
|
||||
mutex(cacheLock) {
|
||||
j = 0
|
||||
for (i = 0; i < arraySize; i++) {
|
||||
if (cache[i].hits == nthHit) itemIndices[j++] = i
|
||||
}
|
||||
}
|
||||
|
||||
return resolve(itemIndices, "r")
|
||||
}
|
||||
|
||||
// This method returns the index of the nth most recently-used item in
|
||||
// the cache.
|
||||
int nthMostRecentlyUsed(int n) {
|
||||
DateTime useTimes[]
|
||||
DateTime testTime = ic.getTime()
|
||||
for (int i = 0; i < arraySize; i++) useTimes[i] = testTime
|
||||
|
||||
int i
|
||||
int j
|
||||
mutex(cacheLock) {
|
||||
for (i = 0; i < arraySize; i++) {
|
||||
for (j = 0; j < arraySize; j++) {
|
||||
if (du.equal(cache[i].lastUsed, useTimes[j])) break
|
||||
else if (du.equal(useTimes[j], testTime)) {
|
||||
useTimes[j] = cache[i].lastUsed
|
||||
break
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
int k = 0
|
||||
DateTime nthUseTime = ic.getTime()
|
||||
for (i = 0; i < j; i++) {
|
||||
if (du.after(useTimes[i], nthUseTime)) {
|
||||
nthUseTime = useTimes[i]
|
||||
k++
|
||||
}
|
||||
if (k == n) break
|
||||
}
|
||||
|
||||
int itemIndices[]
|
||||
|
||||
mutex(cacheLock) {
|
||||
j = 0
|
||||
for (i = 0; i < arraySize; i++) {
|
||||
if (du.equal(cache[i].lastUsed, nthUseTime)) itemIndices[j++] = i
|
||||
}
|
||||
}
|
||||
|
||||
return resolve(itemIndices, "r")
|
||||
}
|
||||
|
||||
// This method returns a random index.
|
||||
int random() {
|
||||
DateTime dt = ic.getTime()
|
||||
int msec = dt.millisecond
|
||||
ir.setSeed(msec)
|
||||
return ir.get(CacheHandler.MAX_SIZE)
|
||||
}
|
||||
|
||||
// This method resolves a multiple return in one of the methods above,
|
||||
// as per the flag sent along with the list of indices.
|
||||
// 'n' returns the newest, 'o' the oldest and 'r' a random index.
|
||||
int resolve(int index[], char flag) {
|
||||
DateTime dt = null
|
||||
|
||||
if (flag == "n") {
|
||||
int newestItem = 0
|
||||
|
||||
int i = index.arrayLength-1
|
||||
for (int j = 0; j < index.arrayLength; j++) {
|
||||
if (dt == null) {
|
||||
dt = cache[index[i]].timeAdded
|
||||
newestItem = index[i]
|
||||
} else {
|
||||
if (du.before(dt, cache[index[i]].timeAdded)) {
|
||||
dt = cache[index[i]].timeAdded
|
||||
newestItem = index[i]
|
||||
}
|
||||
}
|
||||
i--
|
||||
}
|
||||
|
||||
return newestItem
|
||||
} else if (flag == "o") {
|
||||
int oldestItem = 0
|
||||
|
||||
for (int i = 0; i < index.arrayLength; i++) {
|
||||
if (dt == null) {
|
||||
dt = cache[index[i]].timeAdded
|
||||
oldestItem = index[i]
|
||||
} else {
|
||||
if (du.before(cache[index[i]].timeAdded, dt)) {
|
||||
dt = cache[index[i]].timeAdded
|
||||
oldestItem = index[i]
|
||||
}
|
||||
}
|
||||
}
|
||||
return oldestItem
|
||||
} else if (flag == "r") {
|
||||
dt = ic.getTime()
|
||||
int msec = dt.millisecond
|
||||
ir.setSeed(msec)
|
||||
return ir.get(index.arrayLength)
|
||||
} else {
|
||||
return 0
|
||||
}
|
||||
}
|
||||
}
|
267
dat/cache/difffile-large/10/CacheHandler10_22.dn
vendored
Normal file
267
dat/cache/difffile-large/10/CacheHandler10_22.dn
vendored
Normal file
|
@ -0,0 +1,267 @@
|
|||
/*
|
||||
* EmerGen(e)tic 0.8
|
||||
* Copyright © 2017 Ben Goldsworthy (rumperuu)
|
||||
*
|
||||
* A program to research the utility of genetic algorithms, applied to
|
||||
* 'emergent' sytems.
|
||||
*
|
||||
* This file is part of EmerGen(e)tic.
|
||||
*
|
||||
* IMPROVEMENT(S): License (probably GNU GPLv3 tbh) goes here.
|
||||
*/
|
||||
|
||||
/**
|
||||
** This program serves as the base for each mutated CacheHandler,
|
||||
** providing the functionality that is common to all of them.
|
||||
**/
|
||||
|
||||
/**
|
||||
** @author Ben Goldsworthy (rumperuu) <bgoldsworthy96 @ gmail.com>
|
||||
** @version 1.0
|
||||
**/
|
||||
component provides cache.CacheHandler
|
||||
requires io.Output out,
|
||||
data.IntUtil iu,
|
||||
util.RandomInt ir,
|
||||
time.Calendar ic,
|
||||
time.DateUtil du,
|
||||
io.File {
|
||||
|
||||
static CachedResponse cache[]
|
||||
static int arraySize
|
||||
static Mutex cacheLock
|
||||
|
||||
/**
|
||||
** Returns a requested item from the cache.
|
||||
**
|
||||
** @param resource The request item filepath
|
||||
** @return The item from the cache
|
||||
**/
|
||||
CachedResponse CacheHandler:getCachedResponse(char filePath[]) {
|
||||
// Converts the filepath to an array of bytes for the file.
|
||||
File fd = new File(filePath, File.FILE_ACCESS_READ)
|
||||
char resource[] = fd.read(fd.getSize())
|
||||
fd.close()
|
||||
|
||||
// Returns the item if it's in the cache.
|
||||
mutex(cacheLock) {
|
||||
for (int i = 0; i < arraySize; i++) {
|
||||
if (cache[i].resource == resource) {
|
||||
cache[i].hits++
|
||||
cache[i].lastUsed = ic.getTime()
|
||||
return cache[i]
|
||||
}
|
||||
}
|
||||
}
|
||||
return null
|
||||
}
|
||||
|
||||
/**
|
||||
** Updates the cache if the requested item is not already present.
|
||||
** If the cache is full, the method of determining the item to be
|
||||
** replaced is subject to genetic mutation.
|
||||
**
|
||||
** @param ncr The new item to cache
|
||||
**/
|
||||
void CacheHandler:updateCache(CachedResponse ncr) {
|
||||
mutex(cacheLock) {
|
||||
// If the item is already in the cache, do nothing.
|
||||
for (int i = 0; i < arraySize; i++) {
|
||||
if (cache[i].resource == ncr.resource) {
|
||||
cache[i].response = ncr.response
|
||||
cache[i].resourceAge = ncr.resourceAge
|
||||
|
||||
return
|
||||
}
|
||||
}
|
||||
|
||||
// If the item is not, it must be added.
|
||||
// As `ncr` was created on another component, its details must
|
||||
// be copied to avoid a read-only exception.
|
||||
CachedResponse newEntry = new CachedResponse()
|
||||
newEntry.command = ncr.command
|
||||
newEntry.resource = ncr.resource
|
||||
newEntry.resourceAge = ncr.resourceAge
|
||||
newEntry.mimeType = ncr.mimeType
|
||||
newEntry.response = new byte[](ncr.response)
|
||||
newEntry.contentSize = ncr.contentSize
|
||||
|
||||
// Initialises the cache if there isn't currently one.
|
||||
if (cache == null) {
|
||||
cache = new CachedResponse[CacheHandler.MAX_SIZE]()
|
||||
}
|
||||
|
||||
// If the cache is full, determines which item to replace.
|
||||
if (arraySize == CacheHandler.MAX_SIZE) {
|
||||
int index
|
||||
|
||||
// BEGIN
|
||||
index = 0
|
||||
// END
|
||||
|
||||
cache[index % arraySize] = newEntry
|
||||
//out.println("$(debugMSG) replacing: $(iu.intToString(index))")
|
||||
// Otherwise, appends the item to the end of the cache.
|
||||
} else {
|
||||
cache[arraySize] = newEntry
|
||||
arraySize++
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
** Clears the cache completely.
|
||||
**/
|
||||
void CacheHandler:clearCache() {
|
||||
mutex(cacheLock) {
|
||||
cache = null
|
||||
}
|
||||
}
|
||||
|
||||
/**************************************************************************
|
||||
* What follows are utility functions that a given chromosome of the
|
||||
* `updateCache()` method may or may not call upon.
|
||||
**************************************************************************/
|
||||
|
||||
// This method returns the index of the nth most frequently-used item in
|
||||
// the cache.
|
||||
int nthMostFrequentlyUsed(int n) {
|
||||
int hits[]
|
||||
for (int i = 0; i < arraySize; i++) hits[i] = 0
|
||||
|
||||
int i
|
||||
int j
|
||||
mutex(cacheLock) {
|
||||
for (i = 0; i < arraySize; i++) {
|
||||
for (j = 0; j < arraySize; j++) {
|
||||
if (cache[i].hits == hits[j]) break
|
||||
else if (hits[j] == 0) {
|
||||
hits[j] = cache[i].hits
|
||||
break
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
int k = 0
|
||||
int nthHit = 0
|
||||
for (i = 0; i < j; i++) {
|
||||
if (hits[i] > nthHit) {
|
||||
nthHit = hits[i]
|
||||
k++
|
||||
}
|
||||
if (k == n) break
|
||||
}
|
||||
|
||||
int itemIndices[]
|
||||
|
||||
mutex(cacheLock) {
|
||||
j = 0
|
||||
for (i = 0; i < arraySize; i++) {
|
||||
if (cache[i].hits == nthHit) itemIndices[j++] = i
|
||||
}
|
||||
}
|
||||
|
||||
return resolve(itemIndices, "r")
|
||||
}
|
||||
|
||||
// This method returns the index of the nth most recently-used item in
|
||||
// the cache.
|
||||
int nthMostRecentlyUsed(int n) {
|
||||
DateTime useTimes[]
|
||||
DateTime testTime = ic.getTime()
|
||||
for (int i = 0; i < arraySize; i++) useTimes[i] = testTime
|
||||
|
||||
int i
|
||||
int j
|
||||
mutex(cacheLock) {
|
||||
for (i = 0; i < arraySize; i++) {
|
||||
for (j = 0; j < arraySize; j++) {
|
||||
if (du.equal(cache[i].lastUsed, useTimes[j])) break
|
||||
else if (du.equal(useTimes[j], testTime)) {
|
||||
useTimes[j] = cache[i].lastUsed
|
||||
break
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
int k = 0
|
||||
DateTime nthUseTime = ic.getTime()
|
||||
for (i = 0; i < j; i++) {
|
||||
if (du.after(useTimes[i], nthUseTime)) {
|
||||
nthUseTime = useTimes[i]
|
||||
k++
|
||||
}
|
||||
if (k == n) break
|
||||
}
|
||||
|
||||
int itemIndices[]
|
||||
|
||||
mutex(cacheLock) {
|
||||
j = 0
|
||||
for (i = 0; i < arraySize; i++) {
|
||||
if (du.equal(cache[i].lastUsed, nthUseTime)) itemIndices[j++] = i
|
||||
}
|
||||
}
|
||||
|
||||
return resolve(itemIndices, "r")
|
||||
}
|
||||
|
||||
// This method returns a random index.
|
||||
int random() {
|
||||
DateTime dt = ic.getTime()
|
||||
int msec = dt.millisecond
|
||||
ir.setSeed(msec)
|
||||
return ir.get(CacheHandler.MAX_SIZE)
|
||||
}
|
||||
|
||||
// This method resolves a multiple return in one of the methods above,
|
||||
// as per the flag sent along with the list of indices.
|
||||
// 'n' returns the newest, 'o' the oldest and 'r' a random index.
|
||||
int resolve(int index[], char flag) {
|
||||
DateTime dt = null
|
||||
|
||||
if (flag == "n") {
|
||||
int newestItem = 0
|
||||
|
||||
int i = index.arrayLength-1
|
||||
for (int j = 0; j < index.arrayLength; j++) {
|
||||
if (dt == null) {
|
||||
dt = cache[index[i]].timeAdded
|
||||
newestItem = index[i]
|
||||
} else {
|
||||
if (du.before(dt, cache[index[i]].timeAdded)) {
|
||||
dt = cache[index[i]].timeAdded
|
||||
newestItem = index[i]
|
||||
}
|
||||
}
|
||||
i--
|
||||
}
|
||||
|
||||
return newestItem
|
||||
} else if (flag == "o") {
|
||||
int oldestItem = 0
|
||||
|
||||
for (int i = 0; i < index.arrayLength; i++) {
|
||||
if (dt == null) {
|
||||
dt = cache[index[i]].timeAdded
|
||||
oldestItem = index[i]
|
||||
} else {
|
||||
if (du.before(cache[index[i]].timeAdded, dt)) {
|
||||
dt = cache[index[i]].timeAdded
|
||||
oldestItem = index[i]
|
||||
}
|
||||
}
|
||||
}
|
||||
return oldestItem
|
||||
} else if (flag == "r") {
|
||||
dt = ic.getTime()
|
||||
int msec = dt.millisecond
|
||||
ir.setSeed(msec)
|
||||
return ir.get(index.arrayLength)
|
||||
} else {
|
||||
return 0
|
||||
}
|
||||
}
|
||||
}
|
267
dat/cache/difffile-large/10/CacheHandler10_23.dn
vendored
Normal file
267
dat/cache/difffile-large/10/CacheHandler10_23.dn
vendored
Normal file
|
@ -0,0 +1,267 @@
|
|||
/*
|
||||
* EmerGen(e)tic 0.8
|
||||
* Copyright © 2017 Ben Goldsworthy (rumperuu)
|
||||
*
|
||||
* A program to research the utility of genetic algorithms, applied to
|
||||
* 'emergent' sytems.
|
||||
*
|
||||
* This file is part of EmerGen(e)tic.
|
||||
*
|
||||
* IMPROVEMENT(S): License (probably GNU GPLv3 tbh) goes here.
|
||||
*/
|
||||
|
||||
/**
|
||||
** This program serves as the base for each mutated CacheHandler,
|
||||
** providing the functionality that is common to all of them.
|
||||
**/
|
||||
|
||||
/**
|
||||
** @author Ben Goldsworthy (rumperuu) <bgoldsworthy96 @ gmail.com>
|
||||
** @version 1.0
|
||||
**/
|
||||
component provides cache.CacheHandler
|
||||
requires io.Output out,
|
||||
data.IntUtil iu,
|
||||
util.RandomInt ir,
|
||||
time.Calendar ic,
|
||||
time.DateUtil du,
|
||||
io.File {
|
||||
|
||||
static CachedResponse cache[]
|
||||
static int arraySize
|
||||
static Mutex cacheLock
|
||||
|
||||
/**
|
||||
** Returns a requested item from the cache.
|
||||
**
|
||||
** @param resource The request item filepath
|
||||
** @return The item from the cache
|
||||
**/
|
||||
CachedResponse CacheHandler:getCachedResponse(char filePath[]) {
|
||||
// Converts the filepath to an array of bytes for the file.
|
||||
File fd = new File(filePath, File.FILE_ACCESS_READ)
|
||||
char resource[] = fd.read(fd.getSize())
|
||||
fd.close()
|
||||
|
||||
// Returns the item if it's in the cache.
|
||||
mutex(cacheLock) {
|
||||
for (int i = 0; i < arraySize; i++) {
|
||||
if (cache[i].resource == resource) {
|
||||
cache[i].hits++
|
||||
cache[i].lastUsed = ic.getTime()
|
||||
return cache[i]
|
||||
}
|
||||
}
|
||||
}
|
||||
return null
|
||||
}
|
||||
|
||||
/**
|
||||
** Updates the cache if the requested item is not already present.
|
||||
** If the cache is full, the method of determining the item to be
|
||||
** replaced is subject to genetic mutation.
|
||||
**
|
||||
** @param ncr The new item to cache
|
||||
**/
|
||||
void CacheHandler:updateCache(CachedResponse ncr) {
|
||||
mutex(cacheLock) {
|
||||
// If the item is already in the cache, do nothing.
|
||||
for (int i = 0; i < arraySize; i++) {
|
||||
if (cache[i].resource == ncr.resource) {
|
||||
cache[i].response = ncr.response
|
||||
cache[i].resourceAge = ncr.resourceAge
|
||||
|
||||
return
|
||||
}
|
||||
}
|
||||
|
||||
// If the item is not, it must be added.
|
||||
// As `ncr` was created on another component, its details must
|
||||
// be copied to avoid a read-only exception.
|
||||
CachedResponse newEntry = new CachedResponse()
|
||||
newEntry.command = ncr.command
|
||||
newEntry.resource = ncr.resource
|
||||
newEntry.resourceAge = ncr.resourceAge
|
||||
newEntry.mimeType = ncr.mimeType
|
||||
newEntry.response = new byte[](ncr.response)
|
||||
newEntry.contentSize = ncr.contentSize
|
||||
|
||||
// Initialises the cache if there isn't currently one.
|
||||
if (cache == null) {
|
||||
cache = new CachedResponse[CacheHandler.MAX_SIZE]()
|
||||
}
|
||||
|
||||
// If the cache is full, determines which item to replace.
|
||||
if (arraySize == CacheHandler.MAX_SIZE) {
|
||||
int index
|
||||
|
||||
// BEGIN
|
||||
index = 0
|
||||
// END
|
||||
|
||||
cache[index % arraySize] = newEntry
|
||||
//out.println("$(debugMSG) replacing: $(iu.intToString(index))")
|
||||
// Otherwise, appends the item to the end of the cache.
|
||||
} else {
|
||||
cache[arraySize] = newEntry
|
||||
arraySize++
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
** Clears the cache completely.
|
||||
**/
|
||||
void CacheHandler:clearCache() {
|
||||
mutex(cacheLock) {
|
||||
cache = null
|
||||
}
|
||||
}
|
||||
|
||||
/**************************************************************************
|
||||
* What follows are utility functions that a given chromosome of the
|
||||
* `updateCache()` method may or may not call upon.
|
||||
**************************************************************************/
|
||||
|
||||
// This method returns the index of the nth most frequently-used item in
|
||||
// the cache.
|
||||
int nthMostFrequentlyUsed(int n) {
|
||||
int hits[]
|
||||
for (int i = 0; i < arraySize; i++) hits[i] = 0
|
||||
|
||||
int i
|
||||
int j
|
||||
mutex(cacheLock) {
|
||||
for (i = 0; i < arraySize; i++) {
|
||||
for (j = 0; j < arraySize; j++) {
|
||||
if (cache[i].hits == hits[j]) break
|
||||
else if (hits[j] == 0) {
|
||||
hits[j] = cache[i].hits
|
||||
break
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
int k = 0
|
||||
int nthHit = 0
|
||||
for (i = 0; i < j; i++) {
|
||||
if (hits[i] > nthHit) {
|
||||
nthHit = hits[i]
|
||||
k++
|
||||
}
|
||||
if (k == n) break
|
||||
}
|
||||
|
||||
int itemIndices[]
|
||||
|
||||
mutex(cacheLock) {
|
||||
j = 0
|
||||
for (i = 0; i < arraySize; i++) {
|
||||
if (cache[i].hits == nthHit) itemIndices[j++] = i
|
||||
}
|
||||
}
|
||||
|
||||
return resolve(itemIndices, "r")
|
||||
}
|
||||
|
||||
// This method returns the index of the nth most recently-used item in
|
||||
// the cache.
|
||||
int nthMostRecentlyUsed(int n) {
|
||||
DateTime useTimes[]
|
||||
DateTime testTime = ic.getTime()
|
||||
for (int i = 0; i < arraySize; i++) useTimes[i] = testTime
|
||||
|
||||
int i
|
||||
int j
|
||||
mutex(cacheLock) {
|
||||
for (i = 0; i < arraySize; i++) {
|
||||
for (j = 0; j < arraySize; j++) {
|
||||
if (du.equal(cache[i].lastUsed, useTimes[j])) break
|
||||
else if (du.equal(useTimes[j], testTime)) {
|
||||
useTimes[j] = cache[i].lastUsed
|
||||
break
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
int k = 0
|
||||
DateTime nthUseTime = ic.getTime()
|
||||
for (i = 0; i < j; i++) {
|
||||
if (du.after(useTimes[i], nthUseTime)) {
|
||||
nthUseTime = useTimes[i]
|
||||
k++
|
||||
}
|
||||
if (k == n) break
|
||||
}
|
||||
|
||||
int itemIndices[]
|
||||
|
||||
mutex(cacheLock) {
|
||||
j = 0
|
||||
for (i = 0; i < arraySize; i++) {
|
||||
if (du.equal(cache[i].lastUsed, nthUseTime)) itemIndices[j++] = i
|
||||
}
|
||||
}
|
||||
|
||||
return resolve(itemIndices, "r")
|
||||
}
|
||||
|
||||
// This method returns a random index.
|
||||
int random() {
|
||||
DateTime dt = ic.getTime()
|
||||
int msec = dt.millisecond
|
||||
ir.setSeed(msec)
|
||||
return ir.get(CacheHandler.MAX_SIZE)
|
||||
}
|
||||
|
||||
// This method resolves a multiple return in one of the methods above,
|
||||
// as per the flag sent along with the list of indices.
|
||||
// 'n' returns the newest, 'o' the oldest and 'r' a random index.
|
||||
int resolve(int index[], char flag) {
|
||||
DateTime dt = null
|
||||
|
||||
if (flag == "n") {
|
||||
int newestItem = 0
|
||||
|
||||
int i = index.arrayLength-1
|
||||
for (int j = 0; j < index.arrayLength; j++) {
|
||||
if (dt == null) {
|
||||
dt = cache[index[i]].timeAdded
|
||||
newestItem = index[i]
|
||||
} else {
|
||||
if (du.before(dt, cache[index[i]].timeAdded)) {
|
||||
dt = cache[index[i]].timeAdded
|
||||
newestItem = index[i]
|
||||
}
|
||||
}
|
||||
i--
|
||||
}
|
||||
|
||||
return newestItem
|
||||
} else if (flag == "o") {
|
||||
int oldestItem = 0
|
||||
|
||||
for (int i = 0; i < index.arrayLength; i++) {
|
||||
if (dt == null) {
|
||||
dt = cache[index[i]].timeAdded
|
||||
oldestItem = index[i]
|
||||
} else {
|
||||
if (du.before(cache[index[i]].timeAdded, dt)) {
|
||||
dt = cache[index[i]].timeAdded
|
||||
oldestItem = index[i]
|
||||
}
|
||||
}
|
||||
}
|
||||
return oldestItem
|
||||
} else if (flag == "r") {
|
||||
dt = ic.getTime()
|
||||
int msec = dt.millisecond
|
||||
ir.setSeed(msec)
|
||||
return ir.get(index.arrayLength)
|
||||
} else {
|
||||
return 0
|
||||
}
|
||||
}
|
||||
}
|
267
dat/cache/difffile-large/10/CacheHandler10_24.dn
vendored
Normal file
267
dat/cache/difffile-large/10/CacheHandler10_24.dn
vendored
Normal file
|
@ -0,0 +1,267 @@
|
|||
/*
|
||||
* EmerGen(e)tic 0.8
|
||||
* Copyright © 2017 Ben Goldsworthy (rumperuu)
|
||||
*
|
||||
* A program to research the utility of genetic algorithms, applied to
|
||||
* 'emergent' sytems.
|
||||
*
|
||||
* This file is part of EmerGen(e)tic.
|
||||
*
|
||||
* IMPROVEMENT(S): License (probably GNU GPLv3 tbh) goes here.
|
||||
*/
|
||||
|
||||
/**
|
||||
** This program serves as the base for each mutated CacheHandler,
|
||||
** providing the functionality that is common to all of them.
|
||||
**/
|
||||
|
||||
/**
|
||||
** @author Ben Goldsworthy (rumperuu) <bgoldsworthy96 @ gmail.com>
|
||||
** @version 1.0
|
||||
**/
|
||||
component provides cache.CacheHandler
|
||||
requires io.Output out,
|
||||
data.IntUtil iu,
|
||||
util.RandomInt ir,
|
||||
time.Calendar ic,
|
||||
time.DateUtil du,
|
||||
io.File {
|
||||
|
||||
static CachedResponse cache[]
|
||||
static int arraySize
|
||||
static Mutex cacheLock
|
||||
|
||||
/**
|
||||
** Returns a requested item from the cache.
|
||||
**
|
||||
** @param resource The request item filepath
|
||||
** @return The item from the cache
|
||||
**/
|
||||
CachedResponse CacheHandler:getCachedResponse(char filePath[]) {
|
||||
// Converts the filepath to an array of bytes for the file.
|
||||
File fd = new File(filePath, File.FILE_ACCESS_READ)
|
||||
char resource[] = fd.read(fd.getSize())
|
||||
fd.close()
|
||||
|
||||
// Returns the item if it's in the cache.
|
||||
mutex(cacheLock) {
|
||||
for (int i = 0; i < arraySize; i++) {
|
||||
if (cache[i].resource == resource) {
|
||||
cache[i].hits++
|
||||
cache[i].lastUsed = ic.getTime()
|
||||
return cache[i]
|
||||
}
|
||||
}
|
||||
}
|
||||
return null
|
||||
}
|
||||
|
||||
/**
|
||||
** Updates the cache if the requested item is not already present.
|
||||
** If the cache is full, the method of determining the item to be
|
||||
** replaced is subject to genetic mutation.
|
||||
**
|
||||
** @param ncr The new item to cache
|
||||
**/
|
||||
void CacheHandler:updateCache(CachedResponse ncr) {
|
||||
mutex(cacheLock) {
|
||||
// If the item is already in the cache, do nothing.
|
||||
for (int i = 0; i < arraySize; i++) {
|
||||
if (cache[i].resource == ncr.resource) {
|
||||
cache[i].response = ncr.response
|
||||
cache[i].resourceAge = ncr.resourceAge
|
||||
|
||||
return
|
||||
}
|
||||
}
|
||||
|
||||
// If the item is not, it must be added.
|
||||
// As `ncr` was created on another component, its details must
|
||||
// be copied to avoid a read-only exception.
|
||||
CachedResponse newEntry = new CachedResponse()
|
||||
newEntry.command = ncr.command
|
||||
newEntry.resource = ncr.resource
|
||||
newEntry.resourceAge = ncr.resourceAge
|
||||
newEntry.mimeType = ncr.mimeType
|
||||
newEntry.response = new byte[](ncr.response)
|
||||
newEntry.contentSize = ncr.contentSize
|
||||
|
||||
// Initialises the cache if there isn't currently one.
|
||||
if (cache == null) {
|
||||
cache = new CachedResponse[CacheHandler.MAX_SIZE]()
|
||||
}
|
||||
|
||||
// If the cache is full, determines which item to replace.
|
||||
if (arraySize == CacheHandler.MAX_SIZE) {
|
||||
int index
|
||||
|
||||
// BEGIN
|
||||
index = 0/nthMostRecentlyUsed(0)
|
||||
// END
|
||||
|
||||
cache[index % arraySize] = newEntry
|
||||
//out.println("$(debugMSG) replacing: $(iu.intToString(index))")
|
||||
// Otherwise, appends the item to the end of the cache.
|
||||
} else {
|
||||
cache[arraySize] = newEntry
|
||||
arraySize++
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
** Clears the cache completely.
|
||||
**/
|
||||
void CacheHandler:clearCache() {
|
||||
mutex(cacheLock) {
|
||||
cache = null
|
||||
}
|
||||
}
|
||||
|
||||
/**************************************************************************
|
||||
* What follows are utility functions that a given chromosome of the
|
||||
* `updateCache()` method may or may not call upon.
|
||||
**************************************************************************/
|
||||
|
||||
// This method returns the index of the nth most frequently-used item in
|
||||
// the cache.
|
||||
int nthMostFrequentlyUsed(int n) {
|
||||
int hits[]
|
||||
for (int i = 0; i < arraySize; i++) hits[i] = 0
|
||||
|
||||
int i
|
||||
int j
|
||||
mutex(cacheLock) {
|
||||
for (i = 0; i < arraySize; i++) {
|
||||
for (j = 0; j < arraySize; j++) {
|
||||
if (cache[i].hits == hits[j]) break
|
||||
else if (hits[j] == 0) {
|
||||
hits[j] = cache[i].hits
|
||||
break
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
int k = 0
|
||||
int nthHit = 0
|
||||
for (i = 0; i < j; i++) {
|
||||
if (hits[i] > nthHit) {
|
||||
nthHit = hits[i]
|
||||
k++
|
||||
}
|
||||
if (k == n) break
|
||||
}
|
||||
|
||||
int itemIndices[]
|
||||
|
||||
mutex(cacheLock) {
|
||||
j = 0
|
||||
for (i = 0; i < arraySize; i++) {
|
||||
if (cache[i].hits == nthHit) itemIndices[j++] = i
|
||||
}
|
||||
}
|
||||
|
||||
return resolve(itemIndices, "r")
|
||||
}
|
||||
|
||||
// This method returns the index of the nth most recently-used item in
|
||||
// the cache.
|
||||
int nthMostRecentlyUsed(int n) {
|
||||
DateTime useTimes[]
|
||||
DateTime testTime = ic.getTime()
|
||||
for (int i = 0; i < arraySize; i++) useTimes[i] = testTime
|
||||
|
||||
int i
|
||||
int j
|
||||
mutex(cacheLock) {
|
||||
for (i = 0; i < arraySize; i++) {
|
||||
for (j = 0; j < arraySize; j++) {
|
||||
if (du.equal(cache[i].lastUsed, useTimes[j])) break
|
||||
else if (du.equal(useTimes[j], testTime)) {
|
||||
useTimes[j] = cache[i].lastUsed
|
||||
break
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
int k = 0
|
||||
DateTime nthUseTime = ic.getTime()
|
||||
for (i = 0; i < j; i++) {
|
||||
if (du.after(useTimes[i], nthUseTime)) {
|
||||
nthUseTime = useTimes[i]
|
||||
k++
|
||||
}
|
||||
if (k == n) break
|
||||
}
|
||||
|
||||
int itemIndices[]
|
||||
|
||||
mutex(cacheLock) {
|
||||
j = 0
|
||||
for (i = 0; i < arraySize; i++) {
|
||||
if (du.equal(cache[i].lastUsed, nthUseTime)) itemIndices[j++] = i
|
||||
}
|
||||
}
|
||||
|
||||
return resolve(itemIndices, "r")
|
||||
}
|
||||
|
||||
// This method returns a random index.
|
||||
int random() {
|
||||
DateTime dt = ic.getTime()
|
||||
int msec = dt.millisecond
|
||||
ir.setSeed(msec)
|
||||
return ir.get(CacheHandler.MAX_SIZE)
|
||||
}
|
||||
|
||||
// This method resolves a multiple return in one of the methods above,
|
||||
// as per the flag sent along with the list of indices.
|
||||
// 'n' returns the newest, 'o' the oldest and 'r' a random index.
|
||||
int resolve(int index[], char flag) {
|
||||
DateTime dt = null
|
||||
|
||||
if (flag == "n") {
|
||||
int newestItem = 0
|
||||
|
||||
int i = index.arrayLength-1
|
||||
for (int j = 0; j < index.arrayLength; j++) {
|
||||
if (dt == null) {
|
||||
dt = cache[index[i]].timeAdded
|
||||
newestItem = index[i]
|
||||
} else {
|
||||
if (du.before(dt, cache[index[i]].timeAdded)) {
|
||||
dt = cache[index[i]].timeAdded
|
||||
newestItem = index[i]
|
||||
}
|
||||
}
|
||||
i--
|
||||
}
|
||||
|
||||
return newestItem
|
||||
} else if (flag == "o") {
|
||||
int oldestItem = 0
|
||||
|
||||
for (int i = 0; i < index.arrayLength; i++) {
|
||||
if (dt == null) {
|
||||
dt = cache[index[i]].timeAdded
|
||||
oldestItem = index[i]
|
||||
} else {
|
||||
if (du.before(cache[index[i]].timeAdded, dt)) {
|
||||
dt = cache[index[i]].timeAdded
|
||||
oldestItem = index[i]
|
||||
}
|
||||
}
|
||||
}
|
||||
return oldestItem
|
||||
} else if (flag == "r") {
|
||||
dt = ic.getTime()
|
||||
int msec = dt.millisecond
|
||||
ir.setSeed(msec)
|
||||
return ir.get(index.arrayLength)
|
||||
} else {
|
||||
return 0
|
||||
}
|
||||
}
|
||||
}
|
267
dat/cache/difffile-large/10/CacheHandler10_25.dn
vendored
Normal file
267
dat/cache/difffile-large/10/CacheHandler10_25.dn
vendored
Normal file
|
@ -0,0 +1,267 @@
|
|||
/*
|
||||
* EmerGen(e)tic 0.8
|
||||
* Copyright © 2017 Ben Goldsworthy (rumperuu)
|
||||
*
|
||||
* A program to research the utility of genetic algorithms, applied to
|
||||
* 'emergent' sytems.
|
||||
*
|
||||
* This file is part of EmerGen(e)tic.
|
||||
*
|
||||
* IMPROVEMENT(S): License (probably GNU GPLv3 tbh) goes here.
|
||||
*/
|
||||
|
||||
/**
|
||||
** This program serves as the base for each mutated CacheHandler,
|
||||
** providing the functionality that is common to all of them.
|
||||
**/
|
||||
|
||||
/**
|
||||
** @author Ben Goldsworthy (rumperuu) <bgoldsworthy96 @ gmail.com>
|
||||
** @version 1.0
|
||||
**/
|
||||
component provides cache.CacheHandler
|
||||
requires io.Output out,
|
||||
data.IntUtil iu,
|
||||
util.RandomInt ir,
|
||||
time.Calendar ic,
|
||||
time.DateUtil du,
|
||||
io.File {
|
||||
|
||||
static CachedResponse cache[]
|
||||
static int arraySize
|
||||
static Mutex cacheLock
|
||||
|
||||
/**
|
||||
** Returns a requested item from the cache.
|
||||
**
|
||||
** @param resource The request item filepath
|
||||
** @return The item from the cache
|
||||
**/
|
||||
CachedResponse CacheHandler:getCachedResponse(char filePath[]) {
|
||||
// Converts the filepath to an array of bytes for the file.
|
||||
File fd = new File(filePath, File.FILE_ACCESS_READ)
|
||||
char resource[] = fd.read(fd.getSize())
|
||||
fd.close()
|
||||
|
||||
// Returns the item if it's in the cache.
|
||||
mutex(cacheLock) {
|
||||
for (int i = 0; i < arraySize; i++) {
|
||||
if (cache[i].resource == resource) {
|
||||
cache[i].hits++
|
||||
cache[i].lastUsed = ic.getTime()
|
||||
return cache[i]
|
||||
}
|
||||
}
|
||||
}
|
||||
return null
|
||||
}
|
||||
|
||||
/**
|
||||
** Updates the cache if the requested item is not already present.
|
||||
** If the cache is full, the method of determining the item to be
|
||||
** replaced is subject to genetic mutation.
|
||||
**
|
||||
** @param ncr The new item to cache
|
||||
**/
|
||||
void CacheHandler:updateCache(CachedResponse ncr) {
|
||||
mutex(cacheLock) {
|
||||
// If the item is already in the cache, do nothing.
|
||||
for (int i = 0; i < arraySize; i++) {
|
||||
if (cache[i].resource == ncr.resource) {
|
||||
cache[i].response = ncr.response
|
||||
cache[i].resourceAge = ncr.resourceAge
|
||||
|
||||
return
|
||||
}
|
||||
}
|
||||
|
||||
// If the item is not, it must be added.
|
||||
// As `ncr` was created on another component, its details must
|
||||
// be copied to avoid a read-only exception.
|
||||
CachedResponse newEntry = new CachedResponse()
|
||||
newEntry.command = ncr.command
|
||||
newEntry.resource = ncr.resource
|
||||
newEntry.resourceAge = ncr.resourceAge
|
||||
newEntry.mimeType = ncr.mimeType
|
||||
newEntry.response = new byte[](ncr.response)
|
||||
newEntry.contentSize = ncr.contentSize
|
||||
|
||||
// Initialises the cache if there isn't currently one.
|
||||
if (cache == null) {
|
||||
cache = new CachedResponse[CacheHandler.MAX_SIZE]()
|
||||
}
|
||||
|
||||
// If the cache is full, determines which item to replace.
|
||||
if (arraySize == CacheHandler.MAX_SIZE) {
|
||||
int index
|
||||
|
||||
// BEGIN
|
||||
index = nthMostFrequentlyUsed(0)/nthMostFrequentlyUsed(0)
|
||||
// END
|
||||
|
||||
cache[index % arraySize] = newEntry
|
||||
//out.println("$(debugMSG) replacing: $(iu.intToString(index))")
|
||||
// Otherwise, appends the item to the end of the cache.
|
||||
} else {
|
||||
cache[arraySize] = newEntry
|
||||
arraySize++
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
** Clears the cache completely.
|
||||
**/
|
||||
void CacheHandler:clearCache() {
|
||||
mutex(cacheLock) {
|
||||
cache = null
|
||||
}
|
||||
}
|
||||
|
||||
/**************************************************************************
|
||||
* What follows are utility functions that a given chromosome of the
|
||||
* `updateCache()` method may or may not call upon.
|
||||
**************************************************************************/
|
||||
|
||||
// This method returns the index of the nth most frequently-used item in
|
||||
// the cache.
|
||||
int nthMostFrequentlyUsed(int n) {
|
||||
int hits[]
|
||||
for (int i = 0; i < arraySize; i++) hits[i] = 0
|
||||
|
||||
int i
|
||||
int j
|
||||
mutex(cacheLock) {
|
||||
for (i = 0; i < arraySize; i++) {
|
||||
for (j = 0; j < arraySize; j++) {
|
||||
if (cache[i].hits == hits[j]) break
|
||||
else if (hits[j] == 0) {
|
||||
hits[j] = cache[i].hits
|
||||
break
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
int k = 0
|
||||
int nthHit = 0
|
||||
for (i = 0; i < j; i++) {
|
||||
if (hits[i] > nthHit) {
|
||||
nthHit = hits[i]
|
||||
k++
|
||||
}
|
||||
if (k == n) break
|
||||
}
|
||||
|
||||
int itemIndices[]
|
||||
|
||||
mutex(cacheLock) {
|
||||
j = 0
|
||||
for (i = 0; i < arraySize; i++) {
|
||||
if (cache[i].hits == nthHit) itemIndices[j++] = i
|
||||
}
|
||||
}
|
||||
|
||||
return resolve(itemIndices, "r")
|
||||
}
|
||||
|
||||
// This method returns the index of the nth most recently-used item in
|
||||
// the cache.
|
||||
int nthMostRecentlyUsed(int n) {
|
||||
DateTime useTimes[]
|
||||
DateTime testTime = ic.getTime()
|
||||
for (int i = 0; i < arraySize; i++) useTimes[i] = testTime
|
||||
|
||||
int i
|
||||
int j
|
||||
mutex(cacheLock) {
|
||||
for (i = 0; i < arraySize; i++) {
|
||||
for (j = 0; j < arraySize; j++) {
|
||||
if (du.equal(cache[i].lastUsed, useTimes[j])) break
|
||||
else if (du.equal(useTimes[j], testTime)) {
|
||||
useTimes[j] = cache[i].lastUsed
|
||||
break
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
int k = 0
|
||||
DateTime nthUseTime = ic.getTime()
|
||||
for (i = 0; i < j; i++) {
|
||||
if (du.after(useTimes[i], nthUseTime)) {
|
||||
nthUseTime = useTimes[i]
|
||||
k++
|
||||
}
|
||||
if (k == n) break
|
||||
}
|
||||
|
||||
int itemIndices[]
|
||||
|
||||
mutex(cacheLock) {
|
||||
j = 0
|
||||
for (i = 0; i < arraySize; i++) {
|
||||
if (du.equal(cache[i].lastUsed, nthUseTime)) itemIndices[j++] = i
|
||||
}
|
||||
}
|
||||
|
||||
return resolve(itemIndices, "r")
|
||||
}
|
||||
|
||||
// This method returns a random index.
|
||||
int random() {
|
||||
DateTime dt = ic.getTime()
|
||||
int msec = dt.millisecond
|
||||
ir.setSeed(msec)
|
||||
return ir.get(CacheHandler.MAX_SIZE)
|
||||
}
|
||||
|
||||
// This method resolves a multiple return in one of the methods above,
|
||||
// as per the flag sent along with the list of indices.
|
||||
// 'n' returns the newest, 'o' the oldest and 'r' a random index.
|
||||
int resolve(int index[], char flag) {
|
||||
DateTime dt = null
|
||||
|
||||
if (flag == "n") {
|
||||
int newestItem = 0
|
||||
|
||||
int i = index.arrayLength-1
|
||||
for (int j = 0; j < index.arrayLength; j++) {
|
||||
if (dt == null) {
|
||||
dt = cache[index[i]].timeAdded
|
||||
newestItem = index[i]
|
||||
} else {
|
||||
if (du.before(dt, cache[index[i]].timeAdded)) {
|
||||
dt = cache[index[i]].timeAdded
|
||||
newestItem = index[i]
|
||||
}
|
||||
}
|
||||
i--
|
||||
}
|
||||
|
||||
return newestItem
|
||||
} else if (flag == "o") {
|
||||
int oldestItem = 0
|
||||
|
||||
for (int i = 0; i < index.arrayLength; i++) {
|
||||
if (dt == null) {
|
||||
dt = cache[index[i]].timeAdded
|
||||
oldestItem = index[i]
|
||||
} else {
|
||||
if (du.before(cache[index[i]].timeAdded, dt)) {
|
||||
dt = cache[index[i]].timeAdded
|
||||
oldestItem = index[i]
|
||||
}
|
||||
}
|
||||
}
|
||||
return oldestItem
|
||||
} else if (flag == "r") {
|
||||
dt = ic.getTime()
|
||||
int msec = dt.millisecond
|
||||
ir.setSeed(msec)
|
||||
return ir.get(index.arrayLength)
|
||||
} else {
|
||||
return 0
|
||||
}
|
||||
}
|
||||
}
|
267
dat/cache/difffile-large/10/CacheHandler10_26.dn
vendored
Normal file
267
dat/cache/difffile-large/10/CacheHandler10_26.dn
vendored
Normal file
|
@ -0,0 +1,267 @@
|
|||
/*
|
||||
* EmerGen(e)tic 0.8
|
||||
* Copyright © 2017 Ben Goldsworthy (rumperuu)
|
||||
*
|
||||
* A program to research the utility of genetic algorithms, applied to
|
||||
* 'emergent' sytems.
|
||||
*
|
||||
* This file is part of EmerGen(e)tic.
|
||||
*
|
||||
* IMPROVEMENT(S): License (probably GNU GPLv3 tbh) goes here.
|
||||
*/
|
||||
|
||||
/**
|
||||
** This program serves as the base for each mutated CacheHandler,
|
||||
** providing the functionality that is common to all of them.
|
||||
**/
|
||||
|
||||
/**
|
||||
** @author Ben Goldsworthy (rumperuu) <bgoldsworthy96 @ gmail.com>
|
||||
** @version 1.0
|
||||
**/
|
||||
component provides cache.CacheHandler
|
||||
requires io.Output out,
|
||||
data.IntUtil iu,
|
||||
util.RandomInt ir,
|
||||
time.Calendar ic,
|
||||
time.DateUtil du,
|
||||
io.File {
|
||||
|
||||
static CachedResponse cache[]
|
||||
static int arraySize
|
||||
static Mutex cacheLock
|
||||
|
||||
/**
|
||||
** Returns a requested item from the cache.
|
||||
**
|
||||
** @param resource The request item filepath
|
||||
** @return The item from the cache
|
||||
**/
|
||||
CachedResponse CacheHandler:getCachedResponse(char filePath[]) {
|
||||
// Converts the filepath to an array of bytes for the file.
|
||||
File fd = new File(filePath, File.FILE_ACCESS_READ)
|
||||
char resource[] = fd.read(fd.getSize())
|
||||
fd.close()
|
||||
|
||||
// Returns the item if it's in the cache.
|
||||
mutex(cacheLock) {
|
||||
for (int i = 0; i < arraySize; i++) {
|
||||
if (cache[i].resource == resource) {
|
||||
cache[i].hits++
|
||||
cache[i].lastUsed = ic.getTime()
|
||||
return cache[i]
|
||||
}
|
||||
}
|
||||
}
|
||||
return null
|
||||
}
|
||||
|
||||
/**
|
||||
** Updates the cache if the requested item is not already present.
|
||||
** If the cache is full, the method of determining the item to be
|
||||
** replaced is subject to genetic mutation.
|
||||
**
|
||||
** @param ncr The new item to cache
|
||||
**/
|
||||
void CacheHandler:updateCache(CachedResponse ncr) {
|
||||
mutex(cacheLock) {
|
||||
// If the item is already in the cache, do nothing.
|
||||
for (int i = 0; i < arraySize; i++) {
|
||||
if (cache[i].resource == ncr.resource) {
|
||||
cache[i].response = ncr.response
|
||||
cache[i].resourceAge = ncr.resourceAge
|
||||
|
||||
return
|
||||
}
|
||||
}
|
||||
|
||||
// If the item is not, it must be added.
|
||||
// As `ncr` was created on another component, its details must
|
||||
// be copied to avoid a read-only exception.
|
||||
CachedResponse newEntry = new CachedResponse()
|
||||
newEntry.command = ncr.command
|
||||
newEntry.resource = ncr.resource
|
||||
newEntry.resourceAge = ncr.resourceAge
|
||||
newEntry.mimeType = ncr.mimeType
|
||||
newEntry.response = new byte[](ncr.response)
|
||||
newEntry.contentSize = ncr.contentSize
|
||||
|
||||
// Initialises the cache if there isn't currently one.
|
||||
if (cache == null) {
|
||||
cache = new CachedResponse[CacheHandler.MAX_SIZE]()
|
||||
}
|
||||
|
||||
// If the cache is full, determines which item to replace.
|
||||
if (arraySize == CacheHandler.MAX_SIZE) {
|
||||
int index
|
||||
|
||||
// BEGIN
|
||||
index = 0-2
|
||||
// END
|
||||
|
||||
cache[index % arraySize] = newEntry
|
||||
//out.println("$(debugMSG) replacing: $(iu.intToString(index))")
|
||||
// Otherwise, appends the item to the end of the cache.
|
||||
} else {
|
||||
cache[arraySize] = newEntry
|
||||
arraySize++
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
** Clears the cache completely.
|
||||
**/
|
||||
void CacheHandler:clearCache() {
|
||||
mutex(cacheLock) {
|
||||
cache = null
|
||||
}
|
||||
}
|
||||
|
||||
/**************************************************************************
|
||||
* What follows are utility functions that a given chromosome of the
|
||||
* `updateCache()` method may or may not call upon.
|
||||
**************************************************************************/
|
||||
|
||||
// This method returns the index of the nth most frequently-used item in
|
||||
// the cache.
|
||||
int nthMostFrequentlyUsed(int n) {
|
||||
int hits[]
|
||||
for (int i = 0; i < arraySize; i++) hits[i] = 0
|
||||
|
||||
int i
|
||||
int j
|
||||
mutex(cacheLock) {
|
||||
for (i = 0; i < arraySize; i++) {
|
||||
for (j = 0; j < arraySize; j++) {
|
||||
if (cache[i].hits == hits[j]) break
|
||||
else if (hits[j] == 0) {
|
||||
hits[j] = cache[i].hits
|
||||
break
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
int k = 0
|
||||
int nthHit = 0
|
||||
for (i = 0; i < j; i++) {
|
||||
if (hits[i] > nthHit) {
|
||||
nthHit = hits[i]
|
||||
k++
|
||||
}
|
||||
if (k == n) break
|
||||
}
|
||||
|
||||
int itemIndices[]
|
||||
|
||||
mutex(cacheLock) {
|
||||
j = 0
|
||||
for (i = 0; i < arraySize; i++) {
|
||||
if (cache[i].hits == nthHit) itemIndices[j++] = i
|
||||
}
|
||||
}
|
||||
|
||||
return resolve(itemIndices, "r")
|
||||
}
|
||||
|
||||
// This method returns the index of the nth most recently-used item in
|
||||
// the cache.
|
||||
int nthMostRecentlyUsed(int n) {
|
||||
DateTime useTimes[]
|
||||
DateTime testTime = ic.getTime()
|
||||
for (int i = 0; i < arraySize; i++) useTimes[i] = testTime
|
||||
|
||||
int i
|
||||
int j
|
||||
mutex(cacheLock) {
|
||||
for (i = 0; i < arraySize; i++) {
|
||||
for (j = 0; j < arraySize; j++) {
|
||||
if (du.equal(cache[i].lastUsed, useTimes[j])) break
|
||||
else if (du.equal(useTimes[j], testTime)) {
|
||||
useTimes[j] = cache[i].lastUsed
|
||||
break
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
int k = 0
|
||||
DateTime nthUseTime = ic.getTime()
|
||||
for (i = 0; i < j; i++) {
|
||||
if (du.after(useTimes[i], nthUseTime)) {
|
||||
nthUseTime = useTimes[i]
|
||||
k++
|
||||
}
|
||||
if (k == n) break
|
||||
}
|
||||
|
||||
int itemIndices[]
|
||||
|
||||
mutex(cacheLock) {
|
||||
j = 0
|
||||
for (i = 0; i < arraySize; i++) {
|
||||
if (du.equal(cache[i].lastUsed, nthUseTime)) itemIndices[j++] = i
|
||||
}
|
||||
}
|
||||
|
||||
return resolve(itemIndices, "r")
|
||||
}
|
||||
|
||||
// This method returns a random index.
|
||||
int random() {
|
||||
DateTime dt = ic.getTime()
|
||||
int msec = dt.millisecond
|
||||
ir.setSeed(msec)
|
||||
return ir.get(CacheHandler.MAX_SIZE)
|
||||
}
|
||||
|
||||
// This method resolves a multiple return in one of the methods above,
|
||||
// as per the flag sent along with the list of indices.
|
||||
// 'n' returns the newest, 'o' the oldest and 'r' a random index.
|
||||
int resolve(int index[], char flag) {
|
||||
DateTime dt = null
|
||||
|
||||
if (flag == "n") {
|
||||
int newestItem = 0
|
||||
|
||||
int i = index.arrayLength-1
|
||||
for (int j = 0; j < index.arrayLength; j++) {
|
||||
if (dt == null) {
|
||||
dt = cache[index[i]].timeAdded
|
||||
newestItem = index[i]
|
||||
} else {
|
||||
if (du.before(dt, cache[index[i]].timeAdded)) {
|
||||
dt = cache[index[i]].timeAdded
|
||||
newestItem = index[i]
|
||||
}
|
||||
}
|
||||
i--
|
||||
}
|
||||
|
||||
return newestItem
|
||||
} else if (flag == "o") {
|
||||
int oldestItem = 0
|
||||
|
||||
for (int i = 0; i < index.arrayLength; i++) {
|
||||
if (dt == null) {
|
||||
dt = cache[index[i]].timeAdded
|
||||
oldestItem = index[i]
|
||||
} else {
|
||||
if (du.before(cache[index[i]].timeAdded, dt)) {
|
||||
dt = cache[index[i]].timeAdded
|
||||
oldestItem = index[i]
|
||||
}
|
||||
}
|
||||
}
|
||||
return oldestItem
|
||||
} else if (flag == "r") {
|
||||
dt = ic.getTime()
|
||||
int msec = dt.millisecond
|
||||
ir.setSeed(msec)
|
||||
return ir.get(index.arrayLength)
|
||||
} else {
|
||||
return 0
|
||||
}
|
||||
}
|
||||
}
|
267
dat/cache/difffile-large/10/CacheHandler10_27.dn
vendored
Normal file
267
dat/cache/difffile-large/10/CacheHandler10_27.dn
vendored
Normal file
|
@ -0,0 +1,267 @@
|
|||
/*
|
||||
* EmerGen(e)tic 0.8
|
||||
* Copyright © 2017 Ben Goldsworthy (rumperuu)
|
||||
*
|
||||
* A program to research the utility of genetic algorithms, applied to
|
||||
* 'emergent' sytems.
|
||||
*
|
||||
* This file is part of EmerGen(e)tic.
|
||||
*
|
||||
* IMPROVEMENT(S): License (probably GNU GPLv3 tbh) goes here.
|
||||
*/
|
||||
|
||||
/**
|
||||
** This program serves as the base for each mutated CacheHandler,
|
||||
** providing the functionality that is common to all of them.
|
||||
**/
|
||||
|
||||
/**
|
||||
** @author Ben Goldsworthy (rumperuu) <bgoldsworthy96 @ gmail.com>
|
||||
** @version 1.0
|
||||
**/
|
||||
component provides cache.CacheHandler
|
||||
requires io.Output out,
|
||||
data.IntUtil iu,
|
||||
util.RandomInt ir,
|
||||
time.Calendar ic,
|
||||
time.DateUtil du,
|
||||
io.File {
|
||||
|
||||
static CachedResponse cache[]
|
||||
static int arraySize
|
||||
static Mutex cacheLock
|
||||
|
||||
/**
|
||||
** Returns a requested item from the cache.
|
||||
**
|
||||
** @param resource The request item filepath
|
||||
** @return The item from the cache
|
||||
**/
|
||||
CachedResponse CacheHandler:getCachedResponse(char filePath[]) {
|
||||
// Converts the filepath to an array of bytes for the file.
|
||||
File fd = new File(filePath, File.FILE_ACCESS_READ)
|
||||
char resource[] = fd.read(fd.getSize())
|
||||
fd.close()
|
||||
|
||||
// Returns the item if it's in the cache.
|
||||
mutex(cacheLock) {
|
||||
for (int i = 0; i < arraySize; i++) {
|
||||
if (cache[i].resource == resource) {
|
||||
cache[i].hits++
|
||||
cache[i].lastUsed = ic.getTime()
|
||||
return cache[i]
|
||||
}
|
||||
}
|
||||
}
|
||||
return null
|
||||
}
|
||||
|
||||
/**
|
||||
** Updates the cache if the requested item is not already present.
|
||||
** If the cache is full, the method of determining the item to be
|
||||
** replaced is subject to genetic mutation.
|
||||
**
|
||||
** @param ncr The new item to cache
|
||||
**/
|
||||
void CacheHandler:updateCache(CachedResponse ncr) {
|
||||
mutex(cacheLock) {
|
||||
// If the item is already in the cache, do nothing.
|
||||
for (int i = 0; i < arraySize; i++) {
|
||||
if (cache[i].resource == ncr.resource) {
|
||||
cache[i].response = ncr.response
|
||||
cache[i].resourceAge = ncr.resourceAge
|
||||
|
||||
return
|
||||
}
|
||||
}
|
||||
|
||||
// If the item is not, it must be added.
|
||||
// As `ncr` was created on another component, its details must
|
||||
// be copied to avoid a read-only exception.
|
||||
CachedResponse newEntry = new CachedResponse()
|
||||
newEntry.command = ncr.command
|
||||
newEntry.resource = ncr.resource
|
||||
newEntry.resourceAge = ncr.resourceAge
|
||||
newEntry.mimeType = ncr.mimeType
|
||||
newEntry.response = new byte[](ncr.response)
|
||||
newEntry.contentSize = ncr.contentSize
|
||||
|
||||
// Initialises the cache if there isn't currently one.
|
||||
if (cache == null) {
|
||||
cache = new CachedResponse[CacheHandler.MAX_SIZE]()
|
||||
}
|
||||
|
||||
// If the cache is full, determines which item to replace.
|
||||
if (arraySize == CacheHandler.MAX_SIZE) {
|
||||
int index
|
||||
|
||||
// BEGIN
|
||||
index = nthMostRecentlyUsed(0)-nthMostFrequentlyUsed(0)
|
||||
// END
|
||||
|
||||
cache[index % arraySize] = newEntry
|
||||
//out.println("$(debugMSG) replacing: $(iu.intToString(index))")
|
||||
// Otherwise, appends the item to the end of the cache.
|
||||
} else {
|
||||
cache[arraySize] = newEntry
|
||||
arraySize++
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
** Clears the cache completely.
|
||||
**/
|
||||
void CacheHandler:clearCache() {
|
||||
mutex(cacheLock) {
|
||||
cache = null
|
||||
}
|
||||
}
|
||||
|
||||
/**************************************************************************
|
||||
* What follows are utility functions that a given chromosome of the
|
||||
* `updateCache()` method may or may not call upon.
|
||||
**************************************************************************/
|
||||
|
||||
// This method returns the index of the nth most frequently-used item in
|
||||
// the cache.
|
||||
int nthMostFrequentlyUsed(int n) {
|
||||
int hits[]
|
||||
for (int i = 0; i < arraySize; i++) hits[i] = 0
|
||||
|
||||
int i
|
||||
int j
|
||||
mutex(cacheLock) {
|
||||
for (i = 0; i < arraySize; i++) {
|
||||
for (j = 0; j < arraySize; j++) {
|
||||
if (cache[i].hits == hits[j]) break
|
||||
else if (hits[j] == 0) {
|
||||
hits[j] = cache[i].hits
|
||||
break
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
int k = 0
|
||||
int nthHit = 0
|
||||
for (i = 0; i < j; i++) {
|
||||
if (hits[i] > nthHit) {
|
||||
nthHit = hits[i]
|
||||
k++
|
||||
}
|
||||
if (k == n) break
|
||||
}
|
||||
|
||||
int itemIndices[]
|
||||
|
||||
mutex(cacheLock) {
|
||||
j = 0
|
||||
for (i = 0; i < arraySize; i++) {
|
||||
if (cache[i].hits == nthHit) itemIndices[j++] = i
|
||||
}
|
||||
}
|
||||
|
||||
return resolve(itemIndices, "r")
|
||||
}
|
||||
|
||||
// This method returns the index of the nth most recently-used item in
|
||||
// the cache.
|
||||
int nthMostRecentlyUsed(int n) {
|
||||
DateTime useTimes[]
|
||||
DateTime testTime = ic.getTime()
|
||||
for (int i = 0; i < arraySize; i++) useTimes[i] = testTime
|
||||
|
||||
int i
|
||||
int j
|
||||
mutex(cacheLock) {
|
||||
for (i = 0; i < arraySize; i++) {
|
||||
for (j = 0; j < arraySize; j++) {
|
||||
if (du.equal(cache[i].lastUsed, useTimes[j])) break
|
||||
else if (du.equal(useTimes[j], testTime)) {
|
||||
useTimes[j] = cache[i].lastUsed
|
||||
break
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
int k = 0
|
||||
DateTime nthUseTime = ic.getTime()
|
||||
for (i = 0; i < j; i++) {
|
||||
if (du.after(useTimes[i], nthUseTime)) {
|
||||
nthUseTime = useTimes[i]
|
||||
k++
|
||||
}
|
||||
if (k == n) break
|
||||
}
|
||||
|
||||
int itemIndices[]
|
||||
|
||||
mutex(cacheLock) {
|
||||
j = 0
|
||||
for (i = 0; i < arraySize; i++) {
|
||||
if (du.equal(cache[i].lastUsed, nthUseTime)) itemIndices[j++] = i
|
||||
}
|
||||
}
|
||||
|
||||
return resolve(itemIndices, "r")
|
||||
}
|
||||
|
||||
// This method returns a random index.
|
||||
int random() {
|
||||
DateTime dt = ic.getTime()
|
||||
int msec = dt.millisecond
|
||||
ir.setSeed(msec)
|
||||
return ir.get(CacheHandler.MAX_SIZE)
|
||||
}
|
||||
|
||||
// This method resolves a multiple return in one of the methods above,
|
||||
// as per the flag sent along with the list of indices.
|
||||
// 'n' returns the newest, 'o' the oldest and 'r' a random index.
|
||||
int resolve(int index[], char flag) {
|
||||
DateTime dt = null
|
||||
|
||||
if (flag == "n") {
|
||||
int newestItem = 0
|
||||
|
||||
int i = index.arrayLength-1
|
||||
for (int j = 0; j < index.arrayLength; j++) {
|
||||
if (dt == null) {
|
||||
dt = cache[index[i]].timeAdded
|
||||
newestItem = index[i]
|
||||
} else {
|
||||
if (du.before(dt, cache[index[i]].timeAdded)) {
|
||||
dt = cache[index[i]].timeAdded
|
||||
newestItem = index[i]
|
||||
}
|
||||
}
|
||||
i--
|
||||
}
|
||||
|
||||
return newestItem
|
||||
} else if (flag == "o") {
|
||||
int oldestItem = 0
|
||||
|
||||
for (int i = 0; i < index.arrayLength; i++) {
|
||||
if (dt == null) {
|
||||
dt = cache[index[i]].timeAdded
|
||||
oldestItem = index[i]
|
||||
} else {
|
||||
if (du.before(cache[index[i]].timeAdded, dt)) {
|
||||
dt = cache[index[i]].timeAdded
|
||||
oldestItem = index[i]
|
||||
}
|
||||
}
|
||||
}
|
||||
return oldestItem
|
||||
} else if (flag == "r") {
|
||||
dt = ic.getTime()
|
||||
int msec = dt.millisecond
|
||||
ir.setSeed(msec)
|
||||
return ir.get(index.arrayLength)
|
||||
} else {
|
||||
return 0
|
||||
}
|
||||
}
|
||||
}
|
267
dat/cache/difffile-large/10/CacheHandler10_28.dn
vendored
Normal file
267
dat/cache/difffile-large/10/CacheHandler10_28.dn
vendored
Normal file
|
@ -0,0 +1,267 @@
|
|||
/*
|
||||
* EmerGen(e)tic 0.8
|
||||
* Copyright © 2017 Ben Goldsworthy (rumperuu)
|
||||
*
|
||||
* A program to research the utility of genetic algorithms, applied to
|
||||
* 'emergent' sytems.
|
||||
*
|
||||
* This file is part of EmerGen(e)tic.
|
||||
*
|
||||
* IMPROVEMENT(S): License (probably GNU GPLv3 tbh) goes here.
|
||||
*/
|
||||
|
||||
/**
|
||||
** This program serves as the base for each mutated CacheHandler,
|
||||
** providing the functionality that is common to all of them.
|
||||
**/
|
||||
|
||||
/**
|
||||
** @author Ben Goldsworthy (rumperuu) <bgoldsworthy96 @ gmail.com>
|
||||
** @version 1.0
|
||||
**/
|
||||
component provides cache.CacheHandler
|
||||
requires io.Output out,
|
||||
data.IntUtil iu,
|
||||
util.RandomInt ir,
|
||||
time.Calendar ic,
|
||||
time.DateUtil du,
|
||||
io.File {
|
||||
|
||||
static CachedResponse cache[]
|
||||
static int arraySize
|
||||
static Mutex cacheLock
|
||||
|
||||
/**
|
||||
** Returns a requested item from the cache.
|
||||
**
|
||||
** @param resource The request item filepath
|
||||
** @return The item from the cache
|
||||
**/
|
||||
CachedResponse CacheHandler:getCachedResponse(char filePath[]) {
|
||||
// Converts the filepath to an array of bytes for the file.
|
||||
File fd = new File(filePath, File.FILE_ACCESS_READ)
|
||||
char resource[] = fd.read(fd.getSize())
|
||||
fd.close()
|
||||
|
||||
// Returns the item if it's in the cache.
|
||||
mutex(cacheLock) {
|
||||
for (int i = 0; i < arraySize; i++) {
|
||||
if (cache[i].resource == resource) {
|
||||
cache[i].hits++
|
||||
cache[i].lastUsed = ic.getTime()
|
||||
return cache[i]
|
||||
}
|
||||
}
|
||||
}
|
||||
return null
|
||||
}
|
||||
|
||||
/**
|
||||
** Updates the cache if the requested item is not already present.
|
||||
** If the cache is full, the method of determining the item to be
|
||||
** replaced is subject to genetic mutation.
|
||||
**
|
||||
** @param ncr The new item to cache
|
||||
**/
|
||||
void CacheHandler:updateCache(CachedResponse ncr) {
|
||||
mutex(cacheLock) {
|
||||
// If the item is already in the cache, do nothing.
|
||||
for (int i = 0; i < arraySize; i++) {
|
||||
if (cache[i].resource == ncr.resource) {
|
||||
cache[i].response = ncr.response
|
||||
cache[i].resourceAge = ncr.resourceAge
|
||||
|
||||
return
|
||||
}
|
||||
}
|
||||
|
||||
// If the item is not, it must be added.
|
||||
// As `ncr` was created on another component, its details must
|
||||
// be copied to avoid a read-only exception.
|
||||
CachedResponse newEntry = new CachedResponse()
|
||||
newEntry.command = ncr.command
|
||||
newEntry.resource = ncr.resource
|
||||
newEntry.resourceAge = ncr.resourceAge
|
||||
newEntry.mimeType = ncr.mimeType
|
||||
newEntry.response = new byte[](ncr.response)
|
||||
newEntry.contentSize = ncr.contentSize
|
||||
|
||||
// Initialises the cache if there isn't currently one.
|
||||
if (cache == null) {
|
||||
cache = new CachedResponse[CacheHandler.MAX_SIZE]()
|
||||
}
|
||||
|
||||
// If the cache is full, determines which item to replace.
|
||||
if (arraySize == CacheHandler.MAX_SIZE) {
|
||||
int index
|
||||
|
||||
// BEGIN
|
||||
index = nthMostFrequentlyUsed(0)-nthMostRecentlyUsed(0)
|
||||
// END
|
||||
|
||||
cache[index % arraySize] = newEntry
|
||||
//out.println("$(debugMSG) replacing: $(iu.intToString(index))")
|
||||
// Otherwise, appends the item to the end of the cache.
|
||||
} else {
|
||||
cache[arraySize] = newEntry
|
||||
arraySize++
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
** Clears the cache completely.
|
||||
**/
|
||||
void CacheHandler:clearCache() {
|
||||
mutex(cacheLock) {
|
||||
cache = null
|
||||
}
|
||||
}
|
||||
|
||||
/**************************************************************************
|
||||
* What follows are utility functions that a given chromosome of the
|
||||
* `updateCache()` method may or may not call upon.
|
||||
**************************************************************************/
|
||||
|
||||
// This method returns the index of the nth most frequently-used item in
|
||||
// the cache.
|
||||
int nthMostFrequentlyUsed(int n) {
|
||||
int hits[]
|
||||
for (int i = 0; i < arraySize; i++) hits[i] = 0
|
||||
|
||||
int i
|
||||
int j
|
||||
mutex(cacheLock) {
|
||||
for (i = 0; i < arraySize; i++) {
|
||||
for (j = 0; j < arraySize; j++) {
|
||||
if (cache[i].hits == hits[j]) break
|
||||
else if (hits[j] == 0) {
|
||||
hits[j] = cache[i].hits
|
||||
break
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
int k = 0
|
||||
int nthHit = 0
|
||||
for (i = 0; i < j; i++) {
|
||||
if (hits[i] > nthHit) {
|
||||
nthHit = hits[i]
|
||||
k++
|
||||
}
|
||||
if (k == n) break
|
||||
}
|
||||
|
||||
int itemIndices[]
|
||||
|
||||
mutex(cacheLock) {
|
||||
j = 0
|
||||
for (i = 0; i < arraySize; i++) {
|
||||
if (cache[i].hits == nthHit) itemIndices[j++] = i
|
||||
}
|
||||
}
|
||||
|
||||
return resolve(itemIndices, "r")
|
||||
}
|
||||
|
||||
// This method returns the index of the nth most recently-used item in
|
||||
// the cache.
|
||||
int nthMostRecentlyUsed(int n) {
|
||||
DateTime useTimes[]
|
||||
DateTime testTime = ic.getTime()
|
||||
for (int i = 0; i < arraySize; i++) useTimes[i] = testTime
|
||||
|
||||
int i
|
||||
int j
|
||||
mutex(cacheLock) {
|
||||
for (i = 0; i < arraySize; i++) {
|
||||
for (j = 0; j < arraySize; j++) {
|
||||
if (du.equal(cache[i].lastUsed, useTimes[j])) break
|
||||
else if (du.equal(useTimes[j], testTime)) {
|
||||
useTimes[j] = cache[i].lastUsed
|
||||
break
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
int k = 0
|
||||
DateTime nthUseTime = ic.getTime()
|
||||
for (i = 0; i < j; i++) {
|
||||
if (du.after(useTimes[i], nthUseTime)) {
|
||||
nthUseTime = useTimes[i]
|
||||
k++
|
||||
}
|
||||
if (k == n) break
|
||||
}
|
||||
|
||||
int itemIndices[]
|
||||
|
||||
mutex(cacheLock) {
|
||||
j = 0
|
||||
for (i = 0; i < arraySize; i++) {
|
||||
if (du.equal(cache[i].lastUsed, nthUseTime)) itemIndices[j++] = i
|
||||
}
|
||||
}
|
||||
|
||||
return resolve(itemIndices, "r")
|
||||
}
|
||||
|
||||
// This method returns a random index.
|
||||
int random() {
|
||||
DateTime dt = ic.getTime()
|
||||
int msec = dt.millisecond
|
||||
ir.setSeed(msec)
|
||||
return ir.get(CacheHandler.MAX_SIZE)
|
||||
}
|
||||
|
||||
// This method resolves a multiple return in one of the methods above,
|
||||
// as per the flag sent along with the list of indices.
|
||||
// 'n' returns the newest, 'o' the oldest and 'r' a random index.
|
||||
int resolve(int index[], char flag) {
|
||||
DateTime dt = null
|
||||
|
||||
if (flag == "n") {
|
||||
int newestItem = 0
|
||||
|
||||
int i = index.arrayLength-1
|
||||
for (int j = 0; j < index.arrayLength; j++) {
|
||||
if (dt == null) {
|
||||
dt = cache[index[i]].timeAdded
|
||||
newestItem = index[i]
|
||||
} else {
|
||||
if (du.before(dt, cache[index[i]].timeAdded)) {
|
||||
dt = cache[index[i]].timeAdded
|
||||
newestItem = index[i]
|
||||
}
|
||||
}
|
||||
i--
|
||||
}
|
||||
|
||||
return newestItem
|
||||
} else if (flag == "o") {
|
||||
int oldestItem = 0
|
||||
|
||||
for (int i = 0; i < index.arrayLength; i++) {
|
||||
if (dt == null) {
|
||||
dt = cache[index[i]].timeAdded
|
||||
oldestItem = index[i]
|
||||
} else {
|
||||
if (du.before(cache[index[i]].timeAdded, dt)) {
|
||||
dt = cache[index[i]].timeAdded
|
||||
oldestItem = index[i]
|
||||
}
|
||||
}
|
||||
}
|
||||
return oldestItem
|
||||
} else if (flag == "r") {
|
||||
dt = ic.getTime()
|
||||
int msec = dt.millisecond
|
||||
ir.setSeed(msec)
|
||||
return ir.get(index.arrayLength)
|
||||
} else {
|
||||
return 0
|
||||
}
|
||||
}
|
||||
}
|
267
dat/cache/difffile-large/10/CacheHandler10_29.dn
vendored
Normal file
267
dat/cache/difffile-large/10/CacheHandler10_29.dn
vendored
Normal file
|
@ -0,0 +1,267 @@
|
|||
/*
|
||||
* EmerGen(e)tic 0.8
|
||||
* Copyright © 2017 Ben Goldsworthy (rumperuu)
|
||||
*
|
||||
* A program to research the utility of genetic algorithms, applied to
|
||||
* 'emergent' sytems.
|
||||
*
|
||||
* This file is part of EmerGen(e)tic.
|
||||
*
|
||||
* IMPROVEMENT(S): License (probably GNU GPLv3 tbh) goes here.
|
||||
*/
|
||||
|
||||
/**
|
||||
** This program serves as the base for each mutated CacheHandler,
|
||||
** providing the functionality that is common to all of them.
|
||||
**/
|
||||
|
||||
/**
|
||||
** @author Ben Goldsworthy (rumperuu) <bgoldsworthy96 @ gmail.com>
|
||||
** @version 1.0
|
||||
**/
|
||||
component provides cache.CacheHandler
|
||||
requires io.Output out,
|
||||
data.IntUtil iu,
|
||||
util.RandomInt ir,
|
||||
time.Calendar ic,
|
||||
time.DateUtil du,
|
||||
io.File {
|
||||
|
||||
static CachedResponse cache[]
|
||||
static int arraySize
|
||||
static Mutex cacheLock
|
||||
|
||||
/**
|
||||
** Returns a requested item from the cache.
|
||||
**
|
||||
** @param resource The request item filepath
|
||||
** @return The item from the cache
|
||||
**/
|
||||
CachedResponse CacheHandler:getCachedResponse(char filePath[]) {
|
||||
// Converts the filepath to an array of bytes for the file.
|
||||
File fd = new File(filePath, File.FILE_ACCESS_READ)
|
||||
char resource[] = fd.read(fd.getSize())
|
||||
fd.close()
|
||||
|
||||
// Returns the item if it's in the cache.
|
||||
mutex(cacheLock) {
|
||||
for (int i = 0; i < arraySize; i++) {
|
||||
if (cache[i].resource == resource) {
|
||||
cache[i].hits++
|
||||
cache[i].lastUsed = ic.getTime()
|
||||
return cache[i]
|
||||
}
|
||||
}
|
||||
}
|
||||
return null
|
||||
}
|
||||
|
||||
/**
|
||||
** Updates the cache if the requested item is not already present.
|
||||
** If the cache is full, the method of determining the item to be
|
||||
** replaced is subject to genetic mutation.
|
||||
**
|
||||
** @param ncr The new item to cache
|
||||
**/
|
||||
void CacheHandler:updateCache(CachedResponse ncr) {
|
||||
mutex(cacheLock) {
|
||||
// If the item is already in the cache, do nothing.
|
||||
for (int i = 0; i < arraySize; i++) {
|
||||
if (cache[i].resource == ncr.resource) {
|
||||
cache[i].response = ncr.response
|
||||
cache[i].resourceAge = ncr.resourceAge
|
||||
|
||||
return
|
||||
}
|
||||
}
|
||||
|
||||
// If the item is not, it must be added.
|
||||
// As `ncr` was created on another component, its details must
|
||||
// be copied to avoid a read-only exception.
|
||||
CachedResponse newEntry = new CachedResponse()
|
||||
newEntry.command = ncr.command
|
||||
newEntry.resource = ncr.resource
|
||||
newEntry.resourceAge = ncr.resourceAge
|
||||
newEntry.mimeType = ncr.mimeType
|
||||
newEntry.response = new byte[](ncr.response)
|
||||
newEntry.contentSize = ncr.contentSize
|
||||
|
||||
// Initialises the cache if there isn't currently one.
|
||||
if (cache == null) {
|
||||
cache = new CachedResponse[CacheHandler.MAX_SIZE]()
|
||||
}
|
||||
|
||||
// If the cache is full, determines which item to replace.
|
||||
if (arraySize == CacheHandler.MAX_SIZE) {
|
||||
int index
|
||||
|
||||
// BEGIN
|
||||
index = 0+nthMostFrequentlyUsed(0)
|
||||
// END
|
||||
|
||||
cache[index % arraySize] = newEntry
|
||||
//out.println("$(debugMSG) replacing: $(iu.intToString(index))")
|
||||
// Otherwise, appends the item to the end of the cache.
|
||||
} else {
|
||||
cache[arraySize] = newEntry
|
||||
arraySize++
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
** Clears the cache completely.
|
||||
**/
|
||||
void CacheHandler:clearCache() {
|
||||
mutex(cacheLock) {
|
||||
cache = null
|
||||
}
|
||||
}
|
||||
|
||||
/**************************************************************************
|
||||
* What follows are utility functions that a given chromosome of the
|
||||
* `updateCache()` method may or may not call upon.
|
||||
**************************************************************************/
|
||||
|
||||
// This method returns the index of the nth most frequently-used item in
|
||||
// the cache.
|
||||
int nthMostFrequentlyUsed(int n) {
|
||||
int hits[]
|
||||
for (int i = 0; i < arraySize; i++) hits[i] = 0
|
||||
|
||||
int i
|
||||
int j
|
||||
mutex(cacheLock) {
|
||||
for (i = 0; i < arraySize; i++) {
|
||||
for (j = 0; j < arraySize; j++) {
|
||||
if (cache[i].hits == hits[j]) break
|
||||
else if (hits[j] == 0) {
|
||||
hits[j] = cache[i].hits
|
||||
break
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
int k = 0
|
||||
int nthHit = 0
|
||||
for (i = 0; i < j; i++) {
|
||||
if (hits[i] > nthHit) {
|
||||
nthHit = hits[i]
|
||||
k++
|
||||
}
|
||||
if (k == n) break
|
||||
}
|
||||
|
||||
int itemIndices[]
|
||||
|
||||
mutex(cacheLock) {
|
||||
j = 0
|
||||
for (i = 0; i < arraySize; i++) {
|
||||
if (cache[i].hits == nthHit) itemIndices[j++] = i
|
||||
}
|
||||
}
|
||||
|
||||
return resolve(itemIndices, "r")
|
||||
}
|
||||
|
||||
// This method returns the index of the nth most recently-used item in
|
||||
// the cache.
|
||||
int nthMostRecentlyUsed(int n) {
|
||||
DateTime useTimes[]
|
||||
DateTime testTime = ic.getTime()
|
||||
for (int i = 0; i < arraySize; i++) useTimes[i] = testTime
|
||||
|
||||
int i
|
||||
int j
|
||||
mutex(cacheLock) {
|
||||
for (i = 0; i < arraySize; i++) {
|
||||
for (j = 0; j < arraySize; j++) {
|
||||
if (du.equal(cache[i].lastUsed, useTimes[j])) break
|
||||
else if (du.equal(useTimes[j], testTime)) {
|
||||
useTimes[j] = cache[i].lastUsed
|
||||
break
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
int k = 0
|
||||
DateTime nthUseTime = ic.getTime()
|
||||
for (i = 0; i < j; i++) {
|
||||
if (du.after(useTimes[i], nthUseTime)) {
|
||||
nthUseTime = useTimes[i]
|
||||
k++
|
||||
}
|
||||
if (k == n) break
|
||||
}
|
||||
|
||||
int itemIndices[]
|
||||
|
||||
mutex(cacheLock) {
|
||||
j = 0
|
||||
for (i = 0; i < arraySize; i++) {
|
||||
if (du.equal(cache[i].lastUsed, nthUseTime)) itemIndices[j++] = i
|
||||
}
|
||||
}
|
||||
|
||||
return resolve(itemIndices, "r")
|
||||
}
|
||||
|
||||
// This method returns a random index.
|
||||
int random() {
|
||||
DateTime dt = ic.getTime()
|
||||
int msec = dt.millisecond
|
||||
ir.setSeed(msec)
|
||||
return ir.get(CacheHandler.MAX_SIZE)
|
||||
}
|
||||
|
||||
// This method resolves a multiple return in one of the methods above,
|
||||
// as per the flag sent along with the list of indices.
|
||||
// 'n' returns the newest, 'o' the oldest and 'r' a random index.
|
||||
int resolve(int index[], char flag) {
|
||||
DateTime dt = null
|
||||
|
||||
if (flag == "n") {
|
||||
int newestItem = 0
|
||||
|
||||
int i = index.arrayLength-1
|
||||
for (int j = 0; j < index.arrayLength; j++) {
|
||||
if (dt == null) {
|
||||
dt = cache[index[i]].timeAdded
|
||||
newestItem = index[i]
|
||||
} else {
|
||||
if (du.before(dt, cache[index[i]].timeAdded)) {
|
||||
dt = cache[index[i]].timeAdded
|
||||
newestItem = index[i]
|
||||
}
|
||||
}
|
||||
i--
|
||||
}
|
||||
|
||||
return newestItem
|
||||
} else if (flag == "o") {
|
||||
int oldestItem = 0
|
||||
|
||||
for (int i = 0; i < index.arrayLength; i++) {
|
||||
if (dt == null) {
|
||||
dt = cache[index[i]].timeAdded
|
||||
oldestItem = index[i]
|
||||
} else {
|
||||
if (du.before(cache[index[i]].timeAdded, dt)) {
|
||||
dt = cache[index[i]].timeAdded
|
||||
oldestItem = index[i]
|
||||
}
|
||||
}
|
||||
}
|
||||
return oldestItem
|
||||
} else if (flag == "r") {
|
||||
dt = ic.getTime()
|
||||
int msec = dt.millisecond
|
||||
ir.setSeed(msec)
|
||||
return ir.get(index.arrayLength)
|
||||
} else {
|
||||
return 0
|
||||
}
|
||||
}
|
||||
}
|
267
dat/cache/difffile-large/10/CacheHandler10_3.dn
vendored
Normal file
267
dat/cache/difffile-large/10/CacheHandler10_3.dn
vendored
Normal file
|
@ -0,0 +1,267 @@
|
|||
/*
|
||||
* EmerGen(e)tic 0.8
|
||||
* Copyright © 2017 Ben Goldsworthy (rumperuu)
|
||||
*
|
||||
* A program to research the utility of genetic algorithms, applied to
|
||||
* 'emergent' sytems.
|
||||
*
|
||||
* This file is part of EmerGen(e)tic.
|
||||
*
|
||||
* IMPROVEMENT(S): License (probably GNU GPLv3 tbh) goes here.
|
||||
*/
|
||||
|
||||
/**
|
||||
** This program serves as the base for each mutated CacheHandler,
|
||||
** providing the functionality that is common to all of them.
|
||||
**/
|
||||
|
||||
/**
|
||||
** @author Ben Goldsworthy (rumperuu) <bgoldsworthy96 @ gmail.com>
|
||||
** @version 1.0
|
||||
**/
|
||||
component provides cache.CacheHandler
|
||||
requires io.Output out,
|
||||
data.IntUtil iu,
|
||||
util.RandomInt ir,
|
||||
time.Calendar ic,
|
||||
time.DateUtil du,
|
||||
io.File {
|
||||
|
||||
static CachedResponse cache[]
|
||||
static int arraySize
|
||||
static Mutex cacheLock
|
||||
|
||||
/**
|
||||
** Returns a requested item from the cache.
|
||||
**
|
||||
** @param resource The request item filepath
|
||||
** @return The item from the cache
|
||||
**/
|
||||
CachedResponse CacheHandler:getCachedResponse(char filePath[]) {
|
||||
// Converts the filepath to an array of bytes for the file.
|
||||
File fd = new File(filePath, File.FILE_ACCESS_READ)
|
||||
char resource[] = fd.read(fd.getSize())
|
||||
fd.close()
|
||||
|
||||
// Returns the item if it's in the cache.
|
||||
mutex(cacheLock) {
|
||||
for (int i = 0; i < arraySize; i++) {
|
||||
if (cache[i].resource == resource) {
|
||||
cache[i].hits++
|
||||
cache[i].lastUsed = ic.getTime()
|
||||
return cache[i]
|
||||
}
|
||||
}
|
||||
}
|
||||
return null
|
||||
}
|
||||
|
||||
/**
|
||||
** Updates the cache if the requested item is not already present.
|
||||
** If the cache is full, the method of determining the item to be
|
||||
** replaced is subject to genetic mutation.
|
||||
**
|
||||
** @param ncr The new item to cache
|
||||
**/
|
||||
void CacheHandler:updateCache(CachedResponse ncr) {
|
||||
mutex(cacheLock) {
|
||||
// If the item is already in the cache, do nothing.
|
||||
for (int i = 0; i < arraySize; i++) {
|
||||
if (cache[i].resource == ncr.resource) {
|
||||
cache[i].response = ncr.response
|
||||
cache[i].resourceAge = ncr.resourceAge
|
||||
|
||||
return
|
||||
}
|
||||
}
|
||||
|
||||
// If the item is not, it must be added.
|
||||
// As `ncr` was created on another component, its details must
|
||||
// be copied to avoid a read-only exception.
|
||||
CachedResponse newEntry = new CachedResponse()
|
||||
newEntry.command = ncr.command
|
||||
newEntry.resource = ncr.resource
|
||||
newEntry.resourceAge = ncr.resourceAge
|
||||
newEntry.mimeType = ncr.mimeType
|
||||
newEntry.response = new byte[](ncr.response)
|
||||
newEntry.contentSize = ncr.contentSize
|
||||
|
||||
// Initialises the cache if there isn't currently one.
|
||||
if (cache == null) {
|
||||
cache = new CachedResponse[CacheHandler.MAX_SIZE]()
|
||||
}
|
||||
|
||||
// If the cache is full, determines which item to replace.
|
||||
if (arraySize == CacheHandler.MAX_SIZE) {
|
||||
int index
|
||||
|
||||
// BEGIN
|
||||
index = nthMostRecentlyUsed(0)*nthMostRecentlyUsed(0)
|
||||
// END
|
||||
|
||||
cache[index % arraySize] = newEntry
|
||||
//out.println("$(debugMSG) replacing: $(iu.intToString(index))")
|
||||
// Otherwise, appends the item to the end of the cache.
|
||||
} else {
|
||||
cache[arraySize] = newEntry
|
||||
arraySize++
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
** Clears the cache completely.
|
||||
**/
|
||||
void CacheHandler:clearCache() {
|
||||
mutex(cacheLock) {
|
||||
cache = null
|
||||
}
|
||||
}
|
||||
|
||||
/**************************************************************************
|
||||
* What follows are utility functions that a given chromosome of the
|
||||
* `updateCache()` method may or may not call upon.
|
||||
**************************************************************************/
|
||||
|
||||
// This method returns the index of the nth most frequently-used item in
|
||||
// the cache.
|
||||
int nthMostFrequentlyUsed(int n) {
|
||||
int hits[]
|
||||
for (int i = 0; i < arraySize; i++) hits[i] = 0
|
||||
|
||||
int i
|
||||
int j
|
||||
mutex(cacheLock) {
|
||||
for (i = 0; i < arraySize; i++) {
|
||||
for (j = 0; j < arraySize; j++) {
|
||||
if (cache[i].hits == hits[j]) break
|
||||
else if (hits[j] == 0) {
|
||||
hits[j] = cache[i].hits
|
||||
break
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
int k = 0
|
||||
int nthHit = 0
|
||||
for (i = 0; i < j; i++) {
|
||||
if (hits[i] > nthHit) {
|
||||
nthHit = hits[i]
|
||||
k++
|
||||
}
|
||||
if (k == n) break
|
||||
}
|
||||
|
||||
int itemIndices[]
|
||||
|
||||
mutex(cacheLock) {
|
||||
j = 0
|
||||
for (i = 0; i < arraySize; i++) {
|
||||
if (cache[i].hits == nthHit) itemIndices[j++] = i
|
||||
}
|
||||
}
|
||||
|
||||
return resolve(itemIndices, "r")
|
||||
}
|
||||
|
||||
// This method returns the index of the nth most recently-used item in
|
||||
// the cache.
|
||||
int nthMostRecentlyUsed(int n) {
|
||||
DateTime useTimes[]
|
||||
DateTime testTime = ic.getTime()
|
||||
for (int i = 0; i < arraySize; i++) useTimes[i] = testTime
|
||||
|
||||
int i
|
||||
int j
|
||||
mutex(cacheLock) {
|
||||
for (i = 0; i < arraySize; i++) {
|
||||
for (j = 0; j < arraySize; j++) {
|
||||
if (du.equal(cache[i].lastUsed, useTimes[j])) break
|
||||
else if (du.equal(useTimes[j], testTime)) {
|
||||
useTimes[j] = cache[i].lastUsed
|
||||
break
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
int k = 0
|
||||
DateTime nthUseTime = ic.getTime()
|
||||
for (i = 0; i < j; i++) {
|
||||
if (du.after(useTimes[i], nthUseTime)) {
|
||||
nthUseTime = useTimes[i]
|
||||
k++
|
||||
}
|
||||
if (k == n) break
|
||||
}
|
||||
|
||||
int itemIndices[]
|
||||
|
||||
mutex(cacheLock) {
|
||||
j = 0
|
||||
for (i = 0; i < arraySize; i++) {
|
||||
if (du.equal(cache[i].lastUsed, nthUseTime)) itemIndices[j++] = i
|
||||
}
|
||||
}
|
||||
|
||||
return resolve(itemIndices, "r")
|
||||
}
|
||||
|
||||
// This method returns a random index.
|
||||
int random() {
|
||||
DateTime dt = ic.getTime()
|
||||
int msec = dt.millisecond
|
||||
ir.setSeed(msec)
|
||||
return ir.get(CacheHandler.MAX_SIZE)
|
||||
}
|
||||
|
||||
// This method resolves a multiple return in one of the methods above,
|
||||
// as per the flag sent along with the list of indices.
|
||||
// 'n' returns the newest, 'o' the oldest and 'r' a random index.
|
||||
int resolve(int index[], char flag) {
|
||||
DateTime dt = null
|
||||
|
||||
if (flag == "n") {
|
||||
int newestItem = 0
|
||||
|
||||
int i = index.arrayLength-1
|
||||
for (int j = 0; j < index.arrayLength; j++) {
|
||||
if (dt == null) {
|
||||
dt = cache[index[i]].timeAdded
|
||||
newestItem = index[i]
|
||||
} else {
|
||||
if (du.before(dt, cache[index[i]].timeAdded)) {
|
||||
dt = cache[index[i]].timeAdded
|
||||
newestItem = index[i]
|
||||
}
|
||||
}
|
||||
i--
|
||||
}
|
||||
|
||||
return newestItem
|
||||
} else if (flag == "o") {
|
||||
int oldestItem = 0
|
||||
|
||||
for (int i = 0; i < index.arrayLength; i++) {
|
||||
if (dt == null) {
|
||||
dt = cache[index[i]].timeAdded
|
||||
oldestItem = index[i]
|
||||
} else {
|
||||
if (du.before(cache[index[i]].timeAdded, dt)) {
|
||||
dt = cache[index[i]].timeAdded
|
||||
oldestItem = index[i]
|
||||
}
|
||||
}
|
||||
}
|
||||
return oldestItem
|
||||
} else if (flag == "r") {
|
||||
dt = ic.getTime()
|
||||
int msec = dt.millisecond
|
||||
ir.setSeed(msec)
|
||||
return ir.get(index.arrayLength)
|
||||
} else {
|
||||
return 0
|
||||
}
|
||||
}
|
||||
}
|
267
dat/cache/difffile-large/10/CacheHandler10_30.dn
vendored
Normal file
267
dat/cache/difffile-large/10/CacheHandler10_30.dn
vendored
Normal file
|
@ -0,0 +1,267 @@
|
|||
/*
|
||||
* EmerGen(e)tic 0.8
|
||||
* Copyright © 2017 Ben Goldsworthy (rumperuu)
|
||||
*
|
||||
* A program to research the utility of genetic algorithms, applied to
|
||||
* 'emergent' sytems.
|
||||
*
|
||||
* This file is part of EmerGen(e)tic.
|
||||
*
|
||||
* IMPROVEMENT(S): License (probably GNU GPLv3 tbh) goes here.
|
||||
*/
|
||||
|
||||
/**
|
||||
** This program serves as the base for each mutated CacheHandler,
|
||||
** providing the functionality that is common to all of them.
|
||||
**/
|
||||
|
||||
/**
|
||||
** @author Ben Goldsworthy (rumperuu) <bgoldsworthy96 @ gmail.com>
|
||||
** @version 1.0
|
||||
**/
|
||||
component provides cache.CacheHandler
|
||||
requires io.Output out,
|
||||
data.IntUtil iu,
|
||||
util.RandomInt ir,
|
||||
time.Calendar ic,
|
||||
time.DateUtil du,
|
||||
io.File {
|
||||
|
||||
static CachedResponse cache[]
|
||||
static int arraySize
|
||||
static Mutex cacheLock
|
||||
|
||||
/**
|
||||
** Returns a requested item from the cache.
|
||||
**
|
||||
** @param resource The request item filepath
|
||||
** @return The item from the cache
|
||||
**/
|
||||
CachedResponse CacheHandler:getCachedResponse(char filePath[]) {
|
||||
// Converts the filepath to an array of bytes for the file.
|
||||
File fd = new File(filePath, File.FILE_ACCESS_READ)
|
||||
char resource[] = fd.read(fd.getSize())
|
||||
fd.close()
|
||||
|
||||
// Returns the item if it's in the cache.
|
||||
mutex(cacheLock) {
|
||||
for (int i = 0; i < arraySize; i++) {
|
||||
if (cache[i].resource == resource) {
|
||||
cache[i].hits++
|
||||
cache[i].lastUsed = ic.getTime()
|
||||
return cache[i]
|
||||
}
|
||||
}
|
||||
}
|
||||
return null
|
||||
}
|
||||
|
||||
/**
|
||||
** Updates the cache if the requested item is not already present.
|
||||
** If the cache is full, the method of determining the item to be
|
||||
** replaced is subject to genetic mutation.
|
||||
**
|
||||
** @param ncr The new item to cache
|
||||
**/
|
||||
void CacheHandler:updateCache(CachedResponse ncr) {
|
||||
mutex(cacheLock) {
|
||||
// If the item is already in the cache, do nothing.
|
||||
for (int i = 0; i < arraySize; i++) {
|
||||
if (cache[i].resource == ncr.resource) {
|
||||
cache[i].response = ncr.response
|
||||
cache[i].resourceAge = ncr.resourceAge
|
||||
|
||||
return
|
||||
}
|
||||
}
|
||||
|
||||
// If the item is not, it must be added.
|
||||
// As `ncr` was created on another component, its details must
|
||||
// be copied to avoid a read-only exception.
|
||||
CachedResponse newEntry = new CachedResponse()
|
||||
newEntry.command = ncr.command
|
||||
newEntry.resource = ncr.resource
|
||||
newEntry.resourceAge = ncr.resourceAge
|
||||
newEntry.mimeType = ncr.mimeType
|
||||
newEntry.response = new byte[](ncr.response)
|
||||
newEntry.contentSize = ncr.contentSize
|
||||
|
||||
// Initialises the cache if there isn't currently one.
|
||||
if (cache == null) {
|
||||
cache = new CachedResponse[CacheHandler.MAX_SIZE]()
|
||||
}
|
||||
|
||||
// If the cache is full, determines which item to replace.
|
||||
if (arraySize == CacheHandler.MAX_SIZE) {
|
||||
int index
|
||||
|
||||
// BEGIN
|
||||
index = nthMostRecentlyUsed(0)-nthMostFrequentlyUsed(0)
|
||||
// END
|
||||
|
||||
cache[index % arraySize] = newEntry
|
||||
//out.println("$(debugMSG) replacing: $(iu.intToString(index))")
|
||||
// Otherwise, appends the item to the end of the cache.
|
||||
} else {
|
||||
cache[arraySize] = newEntry
|
||||
arraySize++
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
** Clears the cache completely.
|
||||
**/
|
||||
void CacheHandler:clearCache() {
|
||||
mutex(cacheLock) {
|
||||
cache = null
|
||||
}
|
||||
}
|
||||
|
||||
/**************************************************************************
|
||||
* What follows are utility functions that a given chromosome of the
|
||||
* `updateCache()` method may or may not call upon.
|
||||
**************************************************************************/
|
||||
|
||||
// This method returns the index of the nth most frequently-used item in
|
||||
// the cache.
|
||||
int nthMostFrequentlyUsed(int n) {
|
||||
int hits[]
|
||||
for (int i = 0; i < arraySize; i++) hits[i] = 0
|
||||
|
||||
int i
|
||||
int j
|
||||
mutex(cacheLock) {
|
||||
for (i = 0; i < arraySize; i++) {
|
||||
for (j = 0; j < arraySize; j++) {
|
||||
if (cache[i].hits == hits[j]) break
|
||||
else if (hits[j] == 0) {
|
||||
hits[j] = cache[i].hits
|
||||
break
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
int k = 0
|
||||
int nthHit = 0
|
||||
for (i = 0; i < j; i++) {
|
||||
if (hits[i] > nthHit) {
|
||||
nthHit = hits[i]
|
||||
k++
|
||||
}
|
||||
if (k == n) break
|
||||
}
|
||||
|
||||
int itemIndices[]
|
||||
|
||||
mutex(cacheLock) {
|
||||
j = 0
|
||||
for (i = 0; i < arraySize; i++) {
|
||||
if (cache[i].hits == nthHit) itemIndices[j++] = i
|
||||
}
|
||||
}
|
||||
|
||||
return resolve(itemIndices, "r")
|
||||
}
|
||||
|
||||
// This method returns the index of the nth most recently-used item in
|
||||
// the cache.
|
||||
int nthMostRecentlyUsed(int n) {
|
||||
DateTime useTimes[]
|
||||
DateTime testTime = ic.getTime()
|
||||
for (int i = 0; i < arraySize; i++) useTimes[i] = testTime
|
||||
|
||||
int i
|
||||
int j
|
||||
mutex(cacheLock) {
|
||||
for (i = 0; i < arraySize; i++) {
|
||||
for (j = 0; j < arraySize; j++) {
|
||||
if (du.equal(cache[i].lastUsed, useTimes[j])) break
|
||||
else if (du.equal(useTimes[j], testTime)) {
|
||||
useTimes[j] = cache[i].lastUsed
|
||||
break
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
int k = 0
|
||||
DateTime nthUseTime = ic.getTime()
|
||||
for (i = 0; i < j; i++) {
|
||||
if (du.after(useTimes[i], nthUseTime)) {
|
||||
nthUseTime = useTimes[i]
|
||||
k++
|
||||
}
|
||||
if (k == n) break
|
||||
}
|
||||
|
||||
int itemIndices[]
|
||||
|
||||
mutex(cacheLock) {
|
||||
j = 0
|
||||
for (i = 0; i < arraySize; i++) {
|
||||
if (du.equal(cache[i].lastUsed, nthUseTime)) itemIndices[j++] = i
|
||||
}
|
||||
}
|
||||
|
||||
return resolve(itemIndices, "r")
|
||||
}
|
||||
|
||||
// This method returns a random index.
|
||||
int random() {
|
||||
DateTime dt = ic.getTime()
|
||||
int msec = dt.millisecond
|
||||
ir.setSeed(msec)
|
||||
return ir.get(CacheHandler.MAX_SIZE)
|
||||
}
|
||||
|
||||
// This method resolves a multiple return in one of the methods above,
|
||||
// as per the flag sent along with the list of indices.
|
||||
// 'n' returns the newest, 'o' the oldest and 'r' a random index.
|
||||
int resolve(int index[], char flag) {
|
||||
DateTime dt = null
|
||||
|
||||
if (flag == "n") {
|
||||
int newestItem = 0
|
||||
|
||||
int i = index.arrayLength-1
|
||||
for (int j = 0; j < index.arrayLength; j++) {
|
||||
if (dt == null) {
|
||||
dt = cache[index[i]].timeAdded
|
||||
newestItem = index[i]
|
||||
} else {
|
||||
if (du.before(dt, cache[index[i]].timeAdded)) {
|
||||
dt = cache[index[i]].timeAdded
|
||||
newestItem = index[i]
|
||||
}
|
||||
}
|
||||
i--
|
||||
}
|
||||
|
||||
return newestItem
|
||||
} else if (flag == "o") {
|
||||
int oldestItem = 0
|
||||
|
||||
for (int i = 0; i < index.arrayLength; i++) {
|
||||
if (dt == null) {
|
||||
dt = cache[index[i]].timeAdded
|
||||
oldestItem = index[i]
|
||||
} else {
|
||||
if (du.before(cache[index[i]].timeAdded, dt)) {
|
||||
dt = cache[index[i]].timeAdded
|
||||
oldestItem = index[i]
|
||||
}
|
||||
}
|
||||
}
|
||||
return oldestItem
|
||||
} else if (flag == "r") {
|
||||
dt = ic.getTime()
|
||||
int msec = dt.millisecond
|
||||
ir.setSeed(msec)
|
||||
return ir.get(index.arrayLength)
|
||||
} else {
|
||||
return 0
|
||||
}
|
||||
}
|
||||
}
|
267
dat/cache/difffile-large/10/CacheHandler10_31.dn
vendored
Normal file
267
dat/cache/difffile-large/10/CacheHandler10_31.dn
vendored
Normal file
|
@ -0,0 +1,267 @@
|
|||
/*
|
||||
* EmerGen(e)tic 0.8
|
||||
* Copyright © 2017 Ben Goldsworthy (rumperuu)
|
||||
*
|
||||
* A program to research the utility of genetic algorithms, applied to
|
||||
* 'emergent' sytems.
|
||||
*
|
||||
* This file is part of EmerGen(e)tic.
|
||||
*
|
||||
* IMPROVEMENT(S): License (probably GNU GPLv3 tbh) goes here.
|
||||
*/
|
||||
|
||||
/**
|
||||
** This program serves as the base for each mutated CacheHandler,
|
||||
** providing the functionality that is common to all of them.
|
||||
**/
|
||||
|
||||
/**
|
||||
** @author Ben Goldsworthy (rumperuu) <bgoldsworthy96 @ gmail.com>
|
||||
** @version 1.0
|
||||
**/
|
||||
component provides cache.CacheHandler
|
||||
requires io.Output out,
|
||||
data.IntUtil iu,
|
||||
util.RandomInt ir,
|
||||
time.Calendar ic,
|
||||
time.DateUtil du,
|
||||
io.File {
|
||||
|
||||
static CachedResponse cache[]
|
||||
static int arraySize
|
||||
static Mutex cacheLock
|
||||
|
||||
/**
|
||||
** Returns a requested item from the cache.
|
||||
**
|
||||
** @param resource The request item filepath
|
||||
** @return The item from the cache
|
||||
**/
|
||||
CachedResponse CacheHandler:getCachedResponse(char filePath[]) {
|
||||
// Converts the filepath to an array of bytes for the file.
|
||||
File fd = new File(filePath, File.FILE_ACCESS_READ)
|
||||
char resource[] = fd.read(fd.getSize())
|
||||
fd.close()
|
||||
|
||||
// Returns the item if it's in the cache.
|
||||
mutex(cacheLock) {
|
||||
for (int i = 0; i < arraySize; i++) {
|
||||
if (cache[i].resource == resource) {
|
||||
cache[i].hits++
|
||||
cache[i].lastUsed = ic.getTime()
|
||||
return cache[i]
|
||||
}
|
||||
}
|
||||
}
|
||||
return null
|
||||
}
|
||||
|
||||
/**
|
||||
** Updates the cache if the requested item is not already present.
|
||||
** If the cache is full, the method of determining the item to be
|
||||
** replaced is subject to genetic mutation.
|
||||
**
|
||||
** @param ncr The new item to cache
|
||||
**/
|
||||
void CacheHandler:updateCache(CachedResponse ncr) {
|
||||
mutex(cacheLock) {
|
||||
// If the item is already in the cache, do nothing.
|
||||
for (int i = 0; i < arraySize; i++) {
|
||||
if (cache[i].resource == ncr.resource) {
|
||||
cache[i].response = ncr.response
|
||||
cache[i].resourceAge = ncr.resourceAge
|
||||
|
||||
return
|
||||
}
|
||||
}
|
||||
|
||||
// If the item is not, it must be added.
|
||||
// As `ncr` was created on another component, its details must
|
||||
// be copied to avoid a read-only exception.
|
||||
CachedResponse newEntry = new CachedResponse()
|
||||
newEntry.command = ncr.command
|
||||
newEntry.resource = ncr.resource
|
||||
newEntry.resourceAge = ncr.resourceAge
|
||||
newEntry.mimeType = ncr.mimeType
|
||||
newEntry.response = new byte[](ncr.response)
|
||||
newEntry.contentSize = ncr.contentSize
|
||||
|
||||
// Initialises the cache if there isn't currently one.
|
||||
if (cache == null) {
|
||||
cache = new CachedResponse[CacheHandler.MAX_SIZE]()
|
||||
}
|
||||
|
||||
// If the cache is full, determines which item to replace.
|
||||
if (arraySize == CacheHandler.MAX_SIZE) {
|
||||
int index
|
||||
|
||||
// BEGIN
|
||||
index = 0/random()
|
||||
// END
|
||||
|
||||
cache[index % arraySize] = newEntry
|
||||
//out.println("$(debugMSG) replacing: $(iu.intToString(index))")
|
||||
// Otherwise, appends the item to the end of the cache.
|
||||
} else {
|
||||
cache[arraySize] = newEntry
|
||||
arraySize++
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
** Clears the cache completely.
|
||||
**/
|
||||
void CacheHandler:clearCache() {
|
||||
mutex(cacheLock) {
|
||||
cache = null
|
||||
}
|
||||
}
|
||||
|
||||
/**************************************************************************
|
||||
* What follows are utility functions that a given chromosome of the
|
||||
* `updateCache()` method may or may not call upon.
|
||||
**************************************************************************/
|
||||
|
||||
// This method returns the index of the nth most frequently-used item in
|
||||
// the cache.
|
||||
int nthMostFrequentlyUsed(int n) {
|
||||
int hits[]
|
||||
for (int i = 0; i < arraySize; i++) hits[i] = 0
|
||||
|
||||
int i
|
||||
int j
|
||||
mutex(cacheLock) {
|
||||
for (i = 0; i < arraySize; i++) {
|
||||
for (j = 0; j < arraySize; j++) {
|
||||
if (cache[i].hits == hits[j]) break
|
||||
else if (hits[j] == 0) {
|
||||
hits[j] = cache[i].hits
|
||||
break
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
int k = 0
|
||||
int nthHit = 0
|
||||
for (i = 0; i < j; i++) {
|
||||
if (hits[i] > nthHit) {
|
||||
nthHit = hits[i]
|
||||
k++
|
||||
}
|
||||
if (k == n) break
|
||||
}
|
||||
|
||||
int itemIndices[]
|
||||
|
||||
mutex(cacheLock) {
|
||||
j = 0
|
||||
for (i = 0; i < arraySize; i++) {
|
||||
if (cache[i].hits == nthHit) itemIndices[j++] = i
|
||||
}
|
||||
}
|
||||
|
||||
return resolve(itemIndices, "r")
|
||||
}
|
||||
|
||||
// This method returns the index of the nth most recently-used item in
|
||||
// the cache.
|
||||
int nthMostRecentlyUsed(int n) {
|
||||
DateTime useTimes[]
|
||||
DateTime testTime = ic.getTime()
|
||||
for (int i = 0; i < arraySize; i++) useTimes[i] = testTime
|
||||
|
||||
int i
|
||||
int j
|
||||
mutex(cacheLock) {
|
||||
for (i = 0; i < arraySize; i++) {
|
||||
for (j = 0; j < arraySize; j++) {
|
||||
if (du.equal(cache[i].lastUsed, useTimes[j])) break
|
||||
else if (du.equal(useTimes[j], testTime)) {
|
||||
useTimes[j] = cache[i].lastUsed
|
||||
break
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
int k = 0
|
||||
DateTime nthUseTime = ic.getTime()
|
||||
for (i = 0; i < j; i++) {
|
||||
if (du.after(useTimes[i], nthUseTime)) {
|
||||
nthUseTime = useTimes[i]
|
||||
k++
|
||||
}
|
||||
if (k == n) break
|
||||
}
|
||||
|
||||
int itemIndices[]
|
||||
|
||||
mutex(cacheLock) {
|
||||
j = 0
|
||||
for (i = 0; i < arraySize; i++) {
|
||||
if (du.equal(cache[i].lastUsed, nthUseTime)) itemIndices[j++] = i
|
||||
}
|
||||
}
|
||||
|
||||
return resolve(itemIndices, "r")
|
||||
}
|
||||
|
||||
// This method returns a random index.
|
||||
int random() {
|
||||
DateTime dt = ic.getTime()
|
||||
int msec = dt.millisecond
|
||||
ir.setSeed(msec)
|
||||
return ir.get(CacheHandler.MAX_SIZE)
|
||||
}
|
||||
|
||||
// This method resolves a multiple return in one of the methods above,
|
||||
// as per the flag sent along with the list of indices.
|
||||
// 'n' returns the newest, 'o' the oldest and 'r' a random index.
|
||||
int resolve(int index[], char flag) {
|
||||
DateTime dt = null
|
||||
|
||||
if (flag == "n") {
|
||||
int newestItem = 0
|
||||
|
||||
int i = index.arrayLength-1
|
||||
for (int j = 0; j < index.arrayLength; j++) {
|
||||
if (dt == null) {
|
||||
dt = cache[index[i]].timeAdded
|
||||
newestItem = index[i]
|
||||
} else {
|
||||
if (du.before(dt, cache[index[i]].timeAdded)) {
|
||||
dt = cache[index[i]].timeAdded
|
||||
newestItem = index[i]
|
||||
}
|
||||
}
|
||||
i--
|
||||
}
|
||||
|
||||
return newestItem
|
||||
} else if (flag == "o") {
|
||||
int oldestItem = 0
|
||||
|
||||
for (int i = 0; i < index.arrayLength; i++) {
|
||||
if (dt == null) {
|
||||
dt = cache[index[i]].timeAdded
|
||||
oldestItem = index[i]
|
||||
} else {
|
||||
if (du.before(cache[index[i]].timeAdded, dt)) {
|
||||
dt = cache[index[i]].timeAdded
|
||||
oldestItem = index[i]
|
||||
}
|
||||
}
|
||||
}
|
||||
return oldestItem
|
||||
} else if (flag == "r") {
|
||||
dt = ic.getTime()
|
||||
int msec = dt.millisecond
|
||||
ir.setSeed(msec)
|
||||
return ir.get(index.arrayLength)
|
||||
} else {
|
||||
return 0
|
||||
}
|
||||
}
|
||||
}
|
267
dat/cache/difffile-large/10/CacheHandler10_32.dn
vendored
Normal file
267
dat/cache/difffile-large/10/CacheHandler10_32.dn
vendored
Normal file
|
@ -0,0 +1,267 @@
|
|||
/*
|
||||
* EmerGen(e)tic 0.8
|
||||
* Copyright © 2017 Ben Goldsworthy (rumperuu)
|
||||
*
|
||||
* A program to research the utility of genetic algorithms, applied to
|
||||
* 'emergent' sytems.
|
||||
*
|
||||
* This file is part of EmerGen(e)tic.
|
||||
*
|
||||
* IMPROVEMENT(S): License (probably GNU GPLv3 tbh) goes here.
|
||||
*/
|
||||
|
||||
/**
|
||||
** This program serves as the base for each mutated CacheHandler,
|
||||
** providing the functionality that is common to all of them.
|
||||
**/
|
||||
|
||||
/**
|
||||
** @author Ben Goldsworthy (rumperuu) <bgoldsworthy96 @ gmail.com>
|
||||
** @version 1.0
|
||||
**/
|
||||
component provides cache.CacheHandler
|
||||
requires io.Output out,
|
||||
data.IntUtil iu,
|
||||
util.RandomInt ir,
|
||||
time.Calendar ic,
|
||||
time.DateUtil du,
|
||||
io.File {
|
||||
|
||||
static CachedResponse cache[]
|
||||
static int arraySize
|
||||
static Mutex cacheLock
|
||||
|
||||
/**
|
||||
** Returns a requested item from the cache.
|
||||
**
|
||||
** @param resource The request item filepath
|
||||
** @return The item from the cache
|
||||
**/
|
||||
CachedResponse CacheHandler:getCachedResponse(char filePath[]) {
|
||||
// Converts the filepath to an array of bytes for the file.
|
||||
File fd = new File(filePath, File.FILE_ACCESS_READ)
|
||||
char resource[] = fd.read(fd.getSize())
|
||||
fd.close()
|
||||
|
||||
// Returns the item if it's in the cache.
|
||||
mutex(cacheLock) {
|
||||
for (int i = 0; i < arraySize; i++) {
|
||||
if (cache[i].resource == resource) {
|
||||
cache[i].hits++
|
||||
cache[i].lastUsed = ic.getTime()
|
||||
return cache[i]
|
||||
}
|
||||
}
|
||||
}
|
||||
return null
|
||||
}
|
||||
|
||||
/**
|
||||
** Updates the cache if the requested item is not already present.
|
||||
** If the cache is full, the method of determining the item to be
|
||||
** replaced is subject to genetic mutation.
|
||||
**
|
||||
** @param ncr The new item to cache
|
||||
**/
|
||||
void CacheHandler:updateCache(CachedResponse ncr) {
|
||||
mutex(cacheLock) {
|
||||
// If the item is already in the cache, do nothing.
|
||||
for (int i = 0; i < arraySize; i++) {
|
||||
if (cache[i].resource == ncr.resource) {
|
||||
cache[i].response = ncr.response
|
||||
cache[i].resourceAge = ncr.resourceAge
|
||||
|
||||
return
|
||||
}
|
||||
}
|
||||
|
||||
// If the item is not, it must be added.
|
||||
// As `ncr` was created on another component, its details must
|
||||
// be copied to avoid a read-only exception.
|
||||
CachedResponse newEntry = new CachedResponse()
|
||||
newEntry.command = ncr.command
|
||||
newEntry.resource = ncr.resource
|
||||
newEntry.resourceAge = ncr.resourceAge
|
||||
newEntry.mimeType = ncr.mimeType
|
||||
newEntry.response = new byte[](ncr.response)
|
||||
newEntry.contentSize = ncr.contentSize
|
||||
|
||||
// Initialises the cache if there isn't currently one.
|
||||
if (cache == null) {
|
||||
cache = new CachedResponse[CacheHandler.MAX_SIZE]()
|
||||
}
|
||||
|
||||
// If the cache is full, determines which item to replace.
|
||||
if (arraySize == CacheHandler.MAX_SIZE) {
|
||||
int index
|
||||
|
||||
// BEGIN
|
||||
index = 0-nthMostFrequentlyUsed(0)
|
||||
// END
|
||||
|
||||
cache[index % arraySize] = newEntry
|
||||
//out.println("$(debugMSG) replacing: $(iu.intToString(index))")
|
||||
// Otherwise, appends the item to the end of the cache.
|
||||
} else {
|
||||
cache[arraySize] = newEntry
|
||||
arraySize++
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
** Clears the cache completely.
|
||||
**/
|
||||
void CacheHandler:clearCache() {
|
||||
mutex(cacheLock) {
|
||||
cache = null
|
||||
}
|
||||
}
|
||||
|
||||
/**************************************************************************
|
||||
* What follows are utility functions that a given chromosome of the
|
||||
* `updateCache()` method may or may not call upon.
|
||||
**************************************************************************/
|
||||
|
||||
// This method returns the index of the nth most frequently-used item in
|
||||
// the cache.
|
||||
int nthMostFrequentlyUsed(int n) {
|
||||
int hits[]
|
||||
for (int i = 0; i < arraySize; i++) hits[i] = 0
|
||||
|
||||
int i
|
||||
int j
|
||||
mutex(cacheLock) {
|
||||
for (i = 0; i < arraySize; i++) {
|
||||
for (j = 0; j < arraySize; j++) {
|
||||
if (cache[i].hits == hits[j]) break
|
||||
else if (hits[j] == 0) {
|
||||
hits[j] = cache[i].hits
|
||||
break
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
int k = 0
|
||||
int nthHit = 0
|
||||
for (i = 0; i < j; i++) {
|
||||
if (hits[i] > nthHit) {
|
||||
nthHit = hits[i]
|
||||
k++
|
||||
}
|
||||
if (k == n) break
|
||||
}
|
||||
|
||||
int itemIndices[]
|
||||
|
||||
mutex(cacheLock) {
|
||||
j = 0
|
||||
for (i = 0; i < arraySize; i++) {
|
||||
if (cache[i].hits == nthHit) itemIndices[j++] = i
|
||||
}
|
||||
}
|
||||
|
||||
return resolve(itemIndices, "r")
|
||||
}
|
||||
|
||||
// This method returns the index of the nth most recently-used item in
|
||||
// the cache.
|
||||
int nthMostRecentlyUsed(int n) {
|
||||
DateTime useTimes[]
|
||||
DateTime testTime = ic.getTime()
|
||||
for (int i = 0; i < arraySize; i++) useTimes[i] = testTime
|
||||
|
||||
int i
|
||||
int j
|
||||
mutex(cacheLock) {
|
||||
for (i = 0; i < arraySize; i++) {
|
||||
for (j = 0; j < arraySize; j++) {
|
||||
if (du.equal(cache[i].lastUsed, useTimes[j])) break
|
||||
else if (du.equal(useTimes[j], testTime)) {
|
||||
useTimes[j] = cache[i].lastUsed
|
||||
break
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
int k = 0
|
||||
DateTime nthUseTime = ic.getTime()
|
||||
for (i = 0; i < j; i++) {
|
||||
if (du.after(useTimes[i], nthUseTime)) {
|
||||
nthUseTime = useTimes[i]
|
||||
k++
|
||||
}
|
||||
if (k == n) break
|
||||
}
|
||||
|
||||
int itemIndices[]
|
||||
|
||||
mutex(cacheLock) {
|
||||
j = 0
|
||||
for (i = 0; i < arraySize; i++) {
|
||||
if (du.equal(cache[i].lastUsed, nthUseTime)) itemIndices[j++] = i
|
||||
}
|
||||
}
|
||||
|
||||
return resolve(itemIndices, "r")
|
||||
}
|
||||
|
||||
// This method returns a random index.
|
||||
int random() {
|
||||
DateTime dt = ic.getTime()
|
||||
int msec = dt.millisecond
|
||||
ir.setSeed(msec)
|
||||
return ir.get(CacheHandler.MAX_SIZE)
|
||||
}
|
||||
|
||||
// This method resolves a multiple return in one of the methods above,
|
||||
// as per the flag sent along with the list of indices.
|
||||
// 'n' returns the newest, 'o' the oldest and 'r' a random index.
|
||||
int resolve(int index[], char flag) {
|
||||
DateTime dt = null
|
||||
|
||||
if (flag == "n") {
|
||||
int newestItem = 0
|
||||
|
||||
int i = index.arrayLength-1
|
||||
for (int j = 0; j < index.arrayLength; j++) {
|
||||
if (dt == null) {
|
||||
dt = cache[index[i]].timeAdded
|
||||
newestItem = index[i]
|
||||
} else {
|
||||
if (du.before(dt, cache[index[i]].timeAdded)) {
|
||||
dt = cache[index[i]].timeAdded
|
||||
newestItem = index[i]
|
||||
}
|
||||
}
|
||||
i--
|
||||
}
|
||||
|
||||
return newestItem
|
||||
} else if (flag == "o") {
|
||||
int oldestItem = 0
|
||||
|
||||
for (int i = 0; i < index.arrayLength; i++) {
|
||||
if (dt == null) {
|
||||
dt = cache[index[i]].timeAdded
|
||||
oldestItem = index[i]
|
||||
} else {
|
||||
if (du.before(cache[index[i]].timeAdded, dt)) {
|
||||
dt = cache[index[i]].timeAdded
|
||||
oldestItem = index[i]
|
||||
}
|
||||
}
|
||||
}
|
||||
return oldestItem
|
||||
} else if (flag == "r") {
|
||||
dt = ic.getTime()
|
||||
int msec = dt.millisecond
|
||||
ir.setSeed(msec)
|
||||
return ir.get(index.arrayLength)
|
||||
} else {
|
||||
return 0
|
||||
}
|
||||
}
|
||||
}
|
267
dat/cache/difffile-large/10/CacheHandler10_33.dn
vendored
Normal file
267
dat/cache/difffile-large/10/CacheHandler10_33.dn
vendored
Normal file
|
@ -0,0 +1,267 @@
|
|||
/*
|
||||
* EmerGen(e)tic 0.8
|
||||
* Copyright © 2017 Ben Goldsworthy (rumperuu)
|
||||
*
|
||||
* A program to research the utility of genetic algorithms, applied to
|
||||
* 'emergent' sytems.
|
||||
*
|
||||
* This file is part of EmerGen(e)tic.
|
||||
*
|
||||
* IMPROVEMENT(S): License (probably GNU GPLv3 tbh) goes here.
|
||||
*/
|
||||
|
||||
/**
|
||||
** This program serves as the base for each mutated CacheHandler,
|
||||
** providing the functionality that is common to all of them.
|
||||
**/
|
||||
|
||||
/**
|
||||
** @author Ben Goldsworthy (rumperuu) <bgoldsworthy96 @ gmail.com>
|
||||
** @version 1.0
|
||||
**/
|
||||
component provides cache.CacheHandler
|
||||
requires io.Output out,
|
||||
data.IntUtil iu,
|
||||
util.RandomInt ir,
|
||||
time.Calendar ic,
|
||||
time.DateUtil du,
|
||||
io.File {
|
||||
|
||||
static CachedResponse cache[]
|
||||
static int arraySize
|
||||
static Mutex cacheLock
|
||||
|
||||
/**
|
||||
** Returns a requested item from the cache.
|
||||
**
|
||||
** @param resource The request item filepath
|
||||
** @return The item from the cache
|
||||
**/
|
||||
CachedResponse CacheHandler:getCachedResponse(char filePath[]) {
|
||||
// Converts the filepath to an array of bytes for the file.
|
||||
File fd = new File(filePath, File.FILE_ACCESS_READ)
|
||||
char resource[] = fd.read(fd.getSize())
|
||||
fd.close()
|
||||
|
||||
// Returns the item if it's in the cache.
|
||||
mutex(cacheLock) {
|
||||
for (int i = 0; i < arraySize; i++) {
|
||||
if (cache[i].resource == resource) {
|
||||
cache[i].hits++
|
||||
cache[i].lastUsed = ic.getTime()
|
||||
return cache[i]
|
||||
}
|
||||
}
|
||||
}
|
||||
return null
|
||||
}
|
||||
|
||||
/**
|
||||
** Updates the cache if the requested item is not already present.
|
||||
** If the cache is full, the method of determining the item to be
|
||||
** replaced is subject to genetic mutation.
|
||||
**
|
||||
** @param ncr The new item to cache
|
||||
**/
|
||||
void CacheHandler:updateCache(CachedResponse ncr) {
|
||||
mutex(cacheLock) {
|
||||
// If the item is already in the cache, do nothing.
|
||||
for (int i = 0; i < arraySize; i++) {
|
||||
if (cache[i].resource == ncr.resource) {
|
||||
cache[i].response = ncr.response
|
||||
cache[i].resourceAge = ncr.resourceAge
|
||||
|
||||
return
|
||||
}
|
||||
}
|
||||
|
||||
// If the item is not, it must be added.
|
||||
// As `ncr` was created on another component, its details must
|
||||
// be copied to avoid a read-only exception.
|
||||
CachedResponse newEntry = new CachedResponse()
|
||||
newEntry.command = ncr.command
|
||||
newEntry.resource = ncr.resource
|
||||
newEntry.resourceAge = ncr.resourceAge
|
||||
newEntry.mimeType = ncr.mimeType
|
||||
newEntry.response = new byte[](ncr.response)
|
||||
newEntry.contentSize = ncr.contentSize
|
||||
|
||||
// Initialises the cache if there isn't currently one.
|
||||
if (cache == null) {
|
||||
cache = new CachedResponse[CacheHandler.MAX_SIZE]()
|
||||
}
|
||||
|
||||
// If the cache is full, determines which item to replace.
|
||||
if (arraySize == CacheHandler.MAX_SIZE) {
|
||||
int index
|
||||
|
||||
// BEGIN
|
||||
index = 0
|
||||
// END
|
||||
|
||||
cache[index % arraySize] = newEntry
|
||||
//out.println("$(debugMSG) replacing: $(iu.intToString(index))")
|
||||
// Otherwise, appends the item to the end of the cache.
|
||||
} else {
|
||||
cache[arraySize] = newEntry
|
||||
arraySize++
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
** Clears the cache completely.
|
||||
**/
|
||||
void CacheHandler:clearCache() {
|
||||
mutex(cacheLock) {
|
||||
cache = null
|
||||
}
|
||||
}
|
||||
|
||||
/**************************************************************************
|
||||
* What follows are utility functions that a given chromosome of the
|
||||
* `updateCache()` method may or may not call upon.
|
||||
**************************************************************************/
|
||||
|
||||
// This method returns the index of the nth most frequently-used item in
|
||||
// the cache.
|
||||
int nthMostFrequentlyUsed(int n) {
|
||||
int hits[]
|
||||
for (int i = 0; i < arraySize; i++) hits[i] = 0
|
||||
|
||||
int i
|
||||
int j
|
||||
mutex(cacheLock) {
|
||||
for (i = 0; i < arraySize; i++) {
|
||||
for (j = 0; j < arraySize; j++) {
|
||||
if (cache[i].hits == hits[j]) break
|
||||
else if (hits[j] == 0) {
|
||||
hits[j] = cache[i].hits
|
||||
break
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
int k = 0
|
||||
int nthHit = 0
|
||||
for (i = 0; i < j; i++) {
|
||||
if (hits[i] > nthHit) {
|
||||
nthHit = hits[i]
|
||||
k++
|
||||
}
|
||||
if (k == n) break
|
||||
}
|
||||
|
||||
int itemIndices[]
|
||||
|
||||
mutex(cacheLock) {
|
||||
j = 0
|
||||
for (i = 0; i < arraySize; i++) {
|
||||
if (cache[i].hits == nthHit) itemIndices[j++] = i
|
||||
}
|
||||
}
|
||||
|
||||
return resolve(itemIndices, "r")
|
||||
}
|
||||
|
||||
// This method returns the index of the nth most recently-used item in
|
||||
// the cache.
|
||||
int nthMostRecentlyUsed(int n) {
|
||||
DateTime useTimes[]
|
||||
DateTime testTime = ic.getTime()
|
||||
for (int i = 0; i < arraySize; i++) useTimes[i] = testTime
|
||||
|
||||
int i
|
||||
int j
|
||||
mutex(cacheLock) {
|
||||
for (i = 0; i < arraySize; i++) {
|
||||
for (j = 0; j < arraySize; j++) {
|
||||
if (du.equal(cache[i].lastUsed, useTimes[j])) break
|
||||
else if (du.equal(useTimes[j], testTime)) {
|
||||
useTimes[j] = cache[i].lastUsed
|
||||
break
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
int k = 0
|
||||
DateTime nthUseTime = ic.getTime()
|
||||
for (i = 0; i < j; i++) {
|
||||
if (du.after(useTimes[i], nthUseTime)) {
|
||||
nthUseTime = useTimes[i]
|
||||
k++
|
||||
}
|
||||
if (k == n) break
|
||||
}
|
||||
|
||||
int itemIndices[]
|
||||
|
||||
mutex(cacheLock) {
|
||||
j = 0
|
||||
for (i = 0; i < arraySize; i++) {
|
||||
if (du.equal(cache[i].lastUsed, nthUseTime)) itemIndices[j++] = i
|
||||
}
|
||||
}
|
||||
|
||||
return resolve(itemIndices, "r")
|
||||
}
|
||||
|
||||
// This method returns a random index.
|
||||
int random() {
|
||||
DateTime dt = ic.getTime()
|
||||
int msec = dt.millisecond
|
||||
ir.setSeed(msec)
|
||||
return ir.get(CacheHandler.MAX_SIZE)
|
||||
}
|
||||
|
||||
// This method resolves a multiple return in one of the methods above,
|
||||
// as per the flag sent along with the list of indices.
|
||||
// 'n' returns the newest, 'o' the oldest and 'r' a random index.
|
||||
int resolve(int index[], char flag) {
|
||||
DateTime dt = null
|
||||
|
||||
if (flag == "n") {
|
||||
int newestItem = 0
|
||||
|
||||
int i = index.arrayLength-1
|
||||
for (int j = 0; j < index.arrayLength; j++) {
|
||||
if (dt == null) {
|
||||
dt = cache[index[i]].timeAdded
|
||||
newestItem = index[i]
|
||||
} else {
|
||||
if (du.before(dt, cache[index[i]].timeAdded)) {
|
||||
dt = cache[index[i]].timeAdded
|
||||
newestItem = index[i]
|
||||
}
|
||||
}
|
||||
i--
|
||||
}
|
||||
|
||||
return newestItem
|
||||
} else if (flag == "o") {
|
||||
int oldestItem = 0
|
||||
|
||||
for (int i = 0; i < index.arrayLength; i++) {
|
||||
if (dt == null) {
|
||||
dt = cache[index[i]].timeAdded
|
||||
oldestItem = index[i]
|
||||
} else {
|
||||
if (du.before(cache[index[i]].timeAdded, dt)) {
|
||||
dt = cache[index[i]].timeAdded
|
||||
oldestItem = index[i]
|
||||
}
|
||||
}
|
||||
}
|
||||
return oldestItem
|
||||
} else if (flag == "r") {
|
||||
dt = ic.getTime()
|
||||
int msec = dt.millisecond
|
||||
ir.setSeed(msec)
|
||||
return ir.get(index.arrayLength)
|
||||
} else {
|
||||
return 0
|
||||
}
|
||||
}
|
||||
}
|
267
dat/cache/difffile-large/10/CacheHandler10_34.dn
vendored
Normal file
267
dat/cache/difffile-large/10/CacheHandler10_34.dn
vendored
Normal file
|
@ -0,0 +1,267 @@
|
|||
/*
|
||||
* EmerGen(e)tic 0.8
|
||||
* Copyright © 2017 Ben Goldsworthy (rumperuu)
|
||||
*
|
||||
* A program to research the utility of genetic algorithms, applied to
|
||||
* 'emergent' sytems.
|
||||
*
|
||||
* This file is part of EmerGen(e)tic.
|
||||
*
|
||||
* IMPROVEMENT(S): License (probably GNU GPLv3 tbh) goes here.
|
||||
*/
|
||||
|
||||
/**
|
||||
** This program serves as the base for each mutated CacheHandler,
|
||||
** providing the functionality that is common to all of them.
|
||||
**/
|
||||
|
||||
/**
|
||||
** @author Ben Goldsworthy (rumperuu) <bgoldsworthy96 @ gmail.com>
|
||||
** @version 1.0
|
||||
**/
|
||||
component provides cache.CacheHandler
|
||||
requires io.Output out,
|
||||
data.IntUtil iu,
|
||||
util.RandomInt ir,
|
||||
time.Calendar ic,
|
||||
time.DateUtil du,
|
||||
io.File {
|
||||
|
||||
static CachedResponse cache[]
|
||||
static int arraySize
|
||||
static Mutex cacheLock
|
||||
|
||||
/**
|
||||
** Returns a requested item from the cache.
|
||||
**
|
||||
** @param resource The request item filepath
|
||||
** @return The item from the cache
|
||||
**/
|
||||
CachedResponse CacheHandler:getCachedResponse(char filePath[]) {
|
||||
// Converts the filepath to an array of bytes for the file.
|
||||
File fd = new File(filePath, File.FILE_ACCESS_READ)
|
||||
char resource[] = fd.read(fd.getSize())
|
||||
fd.close()
|
||||
|
||||
// Returns the item if it's in the cache.
|
||||
mutex(cacheLock) {
|
||||
for (int i = 0; i < arraySize; i++) {
|
||||
if (cache[i].resource == resource) {
|
||||
cache[i].hits++
|
||||
cache[i].lastUsed = ic.getTime()
|
||||
return cache[i]
|
||||
}
|
||||
}
|
||||
}
|
||||
return null
|
||||
}
|
||||
|
||||
/**
|
||||
** Updates the cache if the requested item is not already present.
|
||||
** If the cache is full, the method of determining the item to be
|
||||
** replaced is subject to genetic mutation.
|
||||
**
|
||||
** @param ncr The new item to cache
|
||||
**/
|
||||
void CacheHandler:updateCache(CachedResponse ncr) {
|
||||
mutex(cacheLock) {
|
||||
// If the item is already in the cache, do nothing.
|
||||
for (int i = 0; i < arraySize; i++) {
|
||||
if (cache[i].resource == ncr.resource) {
|
||||
cache[i].response = ncr.response
|
||||
cache[i].resourceAge = ncr.resourceAge
|
||||
|
||||
return
|
||||
}
|
||||
}
|
||||
|
||||
// If the item is not, it must be added.
|
||||
// As `ncr` was created on another component, its details must
|
||||
// be copied to avoid a read-only exception.
|
||||
CachedResponse newEntry = new CachedResponse()
|
||||
newEntry.command = ncr.command
|
||||
newEntry.resource = ncr.resource
|
||||
newEntry.resourceAge = ncr.resourceAge
|
||||
newEntry.mimeType = ncr.mimeType
|
||||
newEntry.response = new byte[](ncr.response)
|
||||
newEntry.contentSize = ncr.contentSize
|
||||
|
||||
// Initialises the cache if there isn't currently one.
|
||||
if (cache == null) {
|
||||
cache = new CachedResponse[CacheHandler.MAX_SIZE]()
|
||||
}
|
||||
|
||||
// If the cache is full, determines which item to replace.
|
||||
if (arraySize == CacheHandler.MAX_SIZE) {
|
||||
int index
|
||||
|
||||
// BEGIN
|
||||
index = 0/nthMostRecentlyUsed(0)
|
||||
// END
|
||||
|
||||
cache[index % arraySize] = newEntry
|
||||
//out.println("$(debugMSG) replacing: $(iu.intToString(index))")
|
||||
// Otherwise, appends the item to the end of the cache.
|
||||
} else {
|
||||
cache[arraySize] = newEntry
|
||||
arraySize++
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
** Clears the cache completely.
|
||||
**/
|
||||
void CacheHandler:clearCache() {
|
||||
mutex(cacheLock) {
|
||||
cache = null
|
||||
}
|
||||
}
|
||||
|
||||
/**************************************************************************
|
||||
* What follows are utility functions that a given chromosome of the
|
||||
* `updateCache()` method may or may not call upon.
|
||||
**************************************************************************/
|
||||
|
||||
// This method returns the index of the nth most frequently-used item in
|
||||
// the cache.
|
||||
int nthMostFrequentlyUsed(int n) {
|
||||
int hits[]
|
||||
for (int i = 0; i < arraySize; i++) hits[i] = 0
|
||||
|
||||
int i
|
||||
int j
|
||||
mutex(cacheLock) {
|
||||
for (i = 0; i < arraySize; i++) {
|
||||
for (j = 0; j < arraySize; j++) {
|
||||
if (cache[i].hits == hits[j]) break
|
||||
else if (hits[j] == 0) {
|
||||
hits[j] = cache[i].hits
|
||||
break
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
int k = 0
|
||||
int nthHit = 0
|
||||
for (i = 0; i < j; i++) {
|
||||
if (hits[i] > nthHit) {
|
||||
nthHit = hits[i]
|
||||
k++
|
||||
}
|
||||
if (k == n) break
|
||||
}
|
||||
|
||||
int itemIndices[]
|
||||
|
||||
mutex(cacheLock) {
|
||||
j = 0
|
||||
for (i = 0; i < arraySize; i++) {
|
||||
if (cache[i].hits == nthHit) itemIndices[j++] = i
|
||||
}
|
||||
}
|
||||
|
||||
return resolve(itemIndices, "r")
|
||||
}
|
||||
|
||||
// This method returns the index of the nth most recently-used item in
|
||||
// the cache.
|
||||
int nthMostRecentlyUsed(int n) {
|
||||
DateTime useTimes[]
|
||||
DateTime testTime = ic.getTime()
|
||||
for (int i = 0; i < arraySize; i++) useTimes[i] = testTime
|
||||
|
||||
int i
|
||||
int j
|
||||
mutex(cacheLock) {
|
||||
for (i = 0; i < arraySize; i++) {
|
||||
for (j = 0; j < arraySize; j++) {
|
||||
if (du.equal(cache[i].lastUsed, useTimes[j])) break
|
||||
else if (du.equal(useTimes[j], testTime)) {
|
||||
useTimes[j] = cache[i].lastUsed
|
||||
break
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
int k = 0
|
||||
DateTime nthUseTime = ic.getTime()
|
||||
for (i = 0; i < j; i++) {
|
||||
if (du.after(useTimes[i], nthUseTime)) {
|
||||
nthUseTime = useTimes[i]
|
||||
k++
|
||||
}
|
||||
if (k == n) break
|
||||
}
|
||||
|
||||
int itemIndices[]
|
||||
|
||||
mutex(cacheLock) {
|
||||
j = 0
|
||||
for (i = 0; i < arraySize; i++) {
|
||||
if (du.equal(cache[i].lastUsed, nthUseTime)) itemIndices[j++] = i
|
||||
}
|
||||
}
|
||||
|
||||
return resolve(itemIndices, "r")
|
||||
}
|
||||
|
||||
// This method returns a random index.
|
||||
int random() {
|
||||
DateTime dt = ic.getTime()
|
||||
int msec = dt.millisecond
|
||||
ir.setSeed(msec)
|
||||
return ir.get(CacheHandler.MAX_SIZE)
|
||||
}
|
||||
|
||||
// This method resolves a multiple return in one of the methods above,
|
||||
// as per the flag sent along with the list of indices.
|
||||
// 'n' returns the newest, 'o' the oldest and 'r' a random index.
|
||||
int resolve(int index[], char flag) {
|
||||
DateTime dt = null
|
||||
|
||||
if (flag == "n") {
|
||||
int newestItem = 0
|
||||
|
||||
int i = index.arrayLength-1
|
||||
for (int j = 0; j < index.arrayLength; j++) {
|
||||
if (dt == null) {
|
||||
dt = cache[index[i]].timeAdded
|
||||
newestItem = index[i]
|
||||
} else {
|
||||
if (du.before(dt, cache[index[i]].timeAdded)) {
|
||||
dt = cache[index[i]].timeAdded
|
||||
newestItem = index[i]
|
||||
}
|
||||
}
|
||||
i--
|
||||
}
|
||||
|
||||
return newestItem
|
||||
} else if (flag == "o") {
|
||||
int oldestItem = 0
|
||||
|
||||
for (int i = 0; i < index.arrayLength; i++) {
|
||||
if (dt == null) {
|
||||
dt = cache[index[i]].timeAdded
|
||||
oldestItem = index[i]
|
||||
} else {
|
||||
if (du.before(cache[index[i]].timeAdded, dt)) {
|
||||
dt = cache[index[i]].timeAdded
|
||||
oldestItem = index[i]
|
||||
}
|
||||
}
|
||||
}
|
||||
return oldestItem
|
||||
} else if (flag == "r") {
|
||||
dt = ic.getTime()
|
||||
int msec = dt.millisecond
|
||||
ir.setSeed(msec)
|
||||
return ir.get(index.arrayLength)
|
||||
} else {
|
||||
return 0
|
||||
}
|
||||
}
|
||||
}
|
267
dat/cache/difffile-large/10/CacheHandler10_4.dn
vendored
Normal file
267
dat/cache/difffile-large/10/CacheHandler10_4.dn
vendored
Normal file
|
@ -0,0 +1,267 @@
|
|||
/*
|
||||
* EmerGen(e)tic 0.8
|
||||
* Copyright © 2017 Ben Goldsworthy (rumperuu)
|
||||
*
|
||||
* A program to research the utility of genetic algorithms, applied to
|
||||
* 'emergent' sytems.
|
||||
*
|
||||
* This file is part of EmerGen(e)tic.
|
||||
*
|
||||
* IMPROVEMENT(S): License (probably GNU GPLv3 tbh) goes here.
|
||||
*/
|
||||
|
||||
/**
|
||||
** This program serves as the base for each mutated CacheHandler,
|
||||
** providing the functionality that is common to all of them.
|
||||
**/
|
||||
|
||||
/**
|
||||
** @author Ben Goldsworthy (rumperuu) <bgoldsworthy96 @ gmail.com>
|
||||
** @version 1.0
|
||||
**/
|
||||
component provides cache.CacheHandler
|
||||
requires io.Output out,
|
||||
data.IntUtil iu,
|
||||
util.RandomInt ir,
|
||||
time.Calendar ic,
|
||||
time.DateUtil du,
|
||||
io.File {
|
||||
|
||||
static CachedResponse cache[]
|
||||
static int arraySize
|
||||
static Mutex cacheLock
|
||||
|
||||
/**
|
||||
** Returns a requested item from the cache.
|
||||
**
|
||||
** @param resource The request item filepath
|
||||
** @return The item from the cache
|
||||
**/
|
||||
CachedResponse CacheHandler:getCachedResponse(char filePath[]) {
|
||||
// Converts the filepath to an array of bytes for the file.
|
||||
File fd = new File(filePath, File.FILE_ACCESS_READ)
|
||||
char resource[] = fd.read(fd.getSize())
|
||||
fd.close()
|
||||
|
||||
// Returns the item if it's in the cache.
|
||||
mutex(cacheLock) {
|
||||
for (int i = 0; i < arraySize; i++) {
|
||||
if (cache[i].resource == resource) {
|
||||
cache[i].hits++
|
||||
cache[i].lastUsed = ic.getTime()
|
||||
return cache[i]
|
||||
}
|
||||
}
|
||||
}
|
||||
return null
|
||||
}
|
||||
|
||||
/**
|
||||
** Updates the cache if the requested item is not already present.
|
||||
** If the cache is full, the method of determining the item to be
|
||||
** replaced is subject to genetic mutation.
|
||||
**
|
||||
** @param ncr The new item to cache
|
||||
**/
|
||||
void CacheHandler:updateCache(CachedResponse ncr) {
|
||||
mutex(cacheLock) {
|
||||
// If the item is already in the cache, do nothing.
|
||||
for (int i = 0; i < arraySize; i++) {
|
||||
if (cache[i].resource == ncr.resource) {
|
||||
cache[i].response = ncr.response
|
||||
cache[i].resourceAge = ncr.resourceAge
|
||||
|
||||
return
|
||||
}
|
||||
}
|
||||
|
||||
// If the item is not, it must be added.
|
||||
// As `ncr` was created on another component, its details must
|
||||
// be copied to avoid a read-only exception.
|
||||
CachedResponse newEntry = new CachedResponse()
|
||||
newEntry.command = ncr.command
|
||||
newEntry.resource = ncr.resource
|
||||
newEntry.resourceAge = ncr.resourceAge
|
||||
newEntry.mimeType = ncr.mimeType
|
||||
newEntry.response = new byte[](ncr.response)
|
||||
newEntry.contentSize = ncr.contentSize
|
||||
|
||||
// Initialises the cache if there isn't currently one.
|
||||
if (cache == null) {
|
||||
cache = new CachedResponse[CacheHandler.MAX_SIZE]()
|
||||
}
|
||||
|
||||
// If the cache is full, determines which item to replace.
|
||||
if (arraySize == CacheHandler.MAX_SIZE) {
|
||||
int index
|
||||
|
||||
// BEGIN
|
||||
index = 0
|
||||
// END
|
||||
|
||||
cache[index % arraySize] = newEntry
|
||||
//out.println("$(debugMSG) replacing: $(iu.intToString(index))")
|
||||
// Otherwise, appends the item to the end of the cache.
|
||||
} else {
|
||||
cache[arraySize] = newEntry
|
||||
arraySize++
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
** Clears the cache completely.
|
||||
**/
|
||||
void CacheHandler:clearCache() {
|
||||
mutex(cacheLock) {
|
||||
cache = null
|
||||
}
|
||||
}
|
||||
|
||||
/**************************************************************************
|
||||
* What follows are utility functions that a given chromosome of the
|
||||
* `updateCache()` method may or may not call upon.
|
||||
**************************************************************************/
|
||||
|
||||
// This method returns the index of the nth most frequently-used item in
|
||||
// the cache.
|
||||
int nthMostFrequentlyUsed(int n) {
|
||||
int hits[]
|
||||
for (int i = 0; i < arraySize; i++) hits[i] = 0
|
||||
|
||||
int i
|
||||
int j
|
||||
mutex(cacheLock) {
|
||||
for (i = 0; i < arraySize; i++) {
|
||||
for (j = 0; j < arraySize; j++) {
|
||||
if (cache[i].hits == hits[j]) break
|
||||
else if (hits[j] == 0) {
|
||||
hits[j] = cache[i].hits
|
||||
break
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
int k = 0
|
||||
int nthHit = 0
|
||||
for (i = 0; i < j; i++) {
|
||||
if (hits[i] > nthHit) {
|
||||
nthHit = hits[i]
|
||||
k++
|
||||
}
|
||||
if (k == n) break
|
||||
}
|
||||
|
||||
int itemIndices[]
|
||||
|
||||
mutex(cacheLock) {
|
||||
j = 0
|
||||
for (i = 0; i < arraySize; i++) {
|
||||
if (cache[i].hits == nthHit) itemIndices[j++] = i
|
||||
}
|
||||
}
|
||||
|
||||
return resolve(itemIndices, "r")
|
||||
}
|
||||
|
||||
// This method returns the index of the nth most recently-used item in
|
||||
// the cache.
|
||||
int nthMostRecentlyUsed(int n) {
|
||||
DateTime useTimes[]
|
||||
DateTime testTime = ic.getTime()
|
||||
for (int i = 0; i < arraySize; i++) useTimes[i] = testTime
|
||||
|
||||
int i
|
||||
int j
|
||||
mutex(cacheLock) {
|
||||
for (i = 0; i < arraySize; i++) {
|
||||
for (j = 0; j < arraySize; j++) {
|
||||
if (du.equal(cache[i].lastUsed, useTimes[j])) break
|
||||
else if (du.equal(useTimes[j], testTime)) {
|
||||
useTimes[j] = cache[i].lastUsed
|
||||
break
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
int k = 0
|
||||
DateTime nthUseTime = ic.getTime()
|
||||
for (i = 0; i < j; i++) {
|
||||
if (du.after(useTimes[i], nthUseTime)) {
|
||||
nthUseTime = useTimes[i]
|
||||
k++
|
||||
}
|
||||
if (k == n) break
|
||||
}
|
||||
|
||||
int itemIndices[]
|
||||
|
||||
mutex(cacheLock) {
|
||||
j = 0
|
||||
for (i = 0; i < arraySize; i++) {
|
||||
if (du.equal(cache[i].lastUsed, nthUseTime)) itemIndices[j++] = i
|
||||
}
|
||||
}
|
||||
|
||||
return resolve(itemIndices, "r")
|
||||
}
|
||||
|
||||
// This method returns a random index.
|
||||
int random() {
|
||||
DateTime dt = ic.getTime()
|
||||
int msec = dt.millisecond
|
||||
ir.setSeed(msec)
|
||||
return ir.get(CacheHandler.MAX_SIZE)
|
||||
}
|
||||
|
||||
// This method resolves a multiple return in one of the methods above,
|
||||
// as per the flag sent along with the list of indices.
|
||||
// 'n' returns the newest, 'o' the oldest and 'r' a random index.
|
||||
int resolve(int index[], char flag) {
|
||||
DateTime dt = null
|
||||
|
||||
if (flag == "n") {
|
||||
int newestItem = 0
|
||||
|
||||
int i = index.arrayLength-1
|
||||
for (int j = 0; j < index.arrayLength; j++) {
|
||||
if (dt == null) {
|
||||
dt = cache[index[i]].timeAdded
|
||||
newestItem = index[i]
|
||||
} else {
|
||||
if (du.before(dt, cache[index[i]].timeAdded)) {
|
||||
dt = cache[index[i]].timeAdded
|
||||
newestItem = index[i]
|
||||
}
|
||||
}
|
||||
i--
|
||||
}
|
||||
|
||||
return newestItem
|
||||
} else if (flag == "o") {
|
||||
int oldestItem = 0
|
||||
|
||||
for (int i = 0; i < index.arrayLength; i++) {
|
||||
if (dt == null) {
|
||||
dt = cache[index[i]].timeAdded
|
||||
oldestItem = index[i]
|
||||
} else {
|
||||
if (du.before(cache[index[i]].timeAdded, dt)) {
|
||||
dt = cache[index[i]].timeAdded
|
||||
oldestItem = index[i]
|
||||
}
|
||||
}
|
||||
}
|
||||
return oldestItem
|
||||
} else if (flag == "r") {
|
||||
dt = ic.getTime()
|
||||
int msec = dt.millisecond
|
||||
ir.setSeed(msec)
|
||||
return ir.get(index.arrayLength)
|
||||
} else {
|
||||
return 0
|
||||
}
|
||||
}
|
||||
}
|
267
dat/cache/difffile-large/10/CacheHandler10_5.dn
vendored
Normal file
267
dat/cache/difffile-large/10/CacheHandler10_5.dn
vendored
Normal file
|
@ -0,0 +1,267 @@
|
|||
/*
|
||||
* EmerGen(e)tic 0.8
|
||||
* Copyright © 2017 Ben Goldsworthy (rumperuu)
|
||||
*
|
||||
* A program to research the utility of genetic algorithms, applied to
|
||||
* 'emergent' sytems.
|
||||
*
|
||||
* This file is part of EmerGen(e)tic.
|
||||
*
|
||||
* IMPROVEMENT(S): License (probably GNU GPLv3 tbh) goes here.
|
||||
*/
|
||||
|
||||
/**
|
||||
** This program serves as the base for each mutated CacheHandler,
|
||||
** providing the functionality that is common to all of them.
|
||||
**/
|
||||
|
||||
/**
|
||||
** @author Ben Goldsworthy (rumperuu) <bgoldsworthy96 @ gmail.com>
|
||||
** @version 1.0
|
||||
**/
|
||||
component provides cache.CacheHandler
|
||||
requires io.Output out,
|
||||
data.IntUtil iu,
|
||||
util.RandomInt ir,
|
||||
time.Calendar ic,
|
||||
time.DateUtil du,
|
||||
io.File {
|
||||
|
||||
static CachedResponse cache[]
|
||||
static int arraySize
|
||||
static Mutex cacheLock
|
||||
|
||||
/**
|
||||
** Returns a requested item from the cache.
|
||||
**
|
||||
** @param resource The request item filepath
|
||||
** @return The item from the cache
|
||||
**/
|
||||
CachedResponse CacheHandler:getCachedResponse(char filePath[]) {
|
||||
// Converts the filepath to an array of bytes for the file.
|
||||
File fd = new File(filePath, File.FILE_ACCESS_READ)
|
||||
char resource[] = fd.read(fd.getSize())
|
||||
fd.close()
|
||||
|
||||
// Returns the item if it's in the cache.
|
||||
mutex(cacheLock) {
|
||||
for (int i = 0; i < arraySize; i++) {
|
||||
if (cache[i].resource == resource) {
|
||||
cache[i].hits++
|
||||
cache[i].lastUsed = ic.getTime()
|
||||
return cache[i]
|
||||
}
|
||||
}
|
||||
}
|
||||
return null
|
||||
}
|
||||
|
||||
/**
|
||||
** Updates the cache if the requested item is not already present.
|
||||
** If the cache is full, the method of determining the item to be
|
||||
** replaced is subject to genetic mutation.
|
||||
**
|
||||
** @param ncr The new item to cache
|
||||
**/
|
||||
void CacheHandler:updateCache(CachedResponse ncr) {
|
||||
mutex(cacheLock) {
|
||||
// If the item is already in the cache, do nothing.
|
||||
for (int i = 0; i < arraySize; i++) {
|
||||
if (cache[i].resource == ncr.resource) {
|
||||
cache[i].response = ncr.response
|
||||
cache[i].resourceAge = ncr.resourceAge
|
||||
|
||||
return
|
||||
}
|
||||
}
|
||||
|
||||
// If the item is not, it must be added.
|
||||
// As `ncr` was created on another component, its details must
|
||||
// be copied to avoid a read-only exception.
|
||||
CachedResponse newEntry = new CachedResponse()
|
||||
newEntry.command = ncr.command
|
||||
newEntry.resource = ncr.resource
|
||||
newEntry.resourceAge = ncr.resourceAge
|
||||
newEntry.mimeType = ncr.mimeType
|
||||
newEntry.response = new byte[](ncr.response)
|
||||
newEntry.contentSize = ncr.contentSize
|
||||
|
||||
// Initialises the cache if there isn't currently one.
|
||||
if (cache == null) {
|
||||
cache = new CachedResponse[CacheHandler.MAX_SIZE]()
|
||||
}
|
||||
|
||||
// If the cache is full, determines which item to replace.
|
||||
if (arraySize == CacheHandler.MAX_SIZE) {
|
||||
int index
|
||||
|
||||
// BEGIN
|
||||
index = 0
|
||||
// END
|
||||
|
||||
cache[index % arraySize] = newEntry
|
||||
//out.println("$(debugMSG) replacing: $(iu.intToString(index))")
|
||||
// Otherwise, appends the item to the end of the cache.
|
||||
} else {
|
||||
cache[arraySize] = newEntry
|
||||
arraySize++
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
** Clears the cache completely.
|
||||
**/
|
||||
void CacheHandler:clearCache() {
|
||||
mutex(cacheLock) {
|
||||
cache = null
|
||||
}
|
||||
}
|
||||
|
||||
/**************************************************************************
|
||||
* What follows are utility functions that a given chromosome of the
|
||||
* `updateCache()` method may or may not call upon.
|
||||
**************************************************************************/
|
||||
|
||||
// This method returns the index of the nth most frequently-used item in
|
||||
// the cache.
|
||||
int nthMostFrequentlyUsed(int n) {
|
||||
int hits[]
|
||||
for (int i = 0; i < arraySize; i++) hits[i] = 0
|
||||
|
||||
int i
|
||||
int j
|
||||
mutex(cacheLock) {
|
||||
for (i = 0; i < arraySize; i++) {
|
||||
for (j = 0; j < arraySize; j++) {
|
||||
if (cache[i].hits == hits[j]) break
|
||||
else if (hits[j] == 0) {
|
||||
hits[j] = cache[i].hits
|
||||
break
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
int k = 0
|
||||
int nthHit = 0
|
||||
for (i = 0; i < j; i++) {
|
||||
if (hits[i] > nthHit) {
|
||||
nthHit = hits[i]
|
||||
k++
|
||||
}
|
||||
if (k == n) break
|
||||
}
|
||||
|
||||
int itemIndices[]
|
||||
|
||||
mutex(cacheLock) {
|
||||
j = 0
|
||||
for (i = 0; i < arraySize; i++) {
|
||||
if (cache[i].hits == nthHit) itemIndices[j++] = i
|
||||
}
|
||||
}
|
||||
|
||||
return resolve(itemIndices, "r")
|
||||
}
|
||||
|
||||
// This method returns the index of the nth most recently-used item in
|
||||
// the cache.
|
||||
int nthMostRecentlyUsed(int n) {
|
||||
DateTime useTimes[]
|
||||
DateTime testTime = ic.getTime()
|
||||
for (int i = 0; i < arraySize; i++) useTimes[i] = testTime
|
||||
|
||||
int i
|
||||
int j
|
||||
mutex(cacheLock) {
|
||||
for (i = 0; i < arraySize; i++) {
|
||||
for (j = 0; j < arraySize; j++) {
|
||||
if (du.equal(cache[i].lastUsed, useTimes[j])) break
|
||||
else if (du.equal(useTimes[j], testTime)) {
|
||||
useTimes[j] = cache[i].lastUsed
|
||||
break
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
int k = 0
|
||||
DateTime nthUseTime = ic.getTime()
|
||||
for (i = 0; i < j; i++) {
|
||||
if (du.after(useTimes[i], nthUseTime)) {
|
||||
nthUseTime = useTimes[i]
|
||||
k++
|
||||
}
|
||||
if (k == n) break
|
||||
}
|
||||
|
||||
int itemIndices[]
|
||||
|
||||
mutex(cacheLock) {
|
||||
j = 0
|
||||
for (i = 0; i < arraySize; i++) {
|
||||
if (du.equal(cache[i].lastUsed, nthUseTime)) itemIndices[j++] = i
|
||||
}
|
||||
}
|
||||
|
||||
return resolve(itemIndices, "r")
|
||||
}
|
||||
|
||||
// This method returns a random index.
|
||||
int random() {
|
||||
DateTime dt = ic.getTime()
|
||||
int msec = dt.millisecond
|
||||
ir.setSeed(msec)
|
||||
return ir.get(CacheHandler.MAX_SIZE)
|
||||
}
|
||||
|
||||
// This method resolves a multiple return in one of the methods above,
|
||||
// as per the flag sent along with the list of indices.
|
||||
// 'n' returns the newest, 'o' the oldest and 'r' a random index.
|
||||
int resolve(int index[], char flag) {
|
||||
DateTime dt = null
|
||||
|
||||
if (flag == "n") {
|
||||
int newestItem = 0
|
||||
|
||||
int i = index.arrayLength-1
|
||||
for (int j = 0; j < index.arrayLength; j++) {
|
||||
if (dt == null) {
|
||||
dt = cache[index[i]].timeAdded
|
||||
newestItem = index[i]
|
||||
} else {
|
||||
if (du.before(dt, cache[index[i]].timeAdded)) {
|
||||
dt = cache[index[i]].timeAdded
|
||||
newestItem = index[i]
|
||||
}
|
||||
}
|
||||
i--
|
||||
}
|
||||
|
||||
return newestItem
|
||||
} else if (flag == "o") {
|
||||
int oldestItem = 0
|
||||
|
||||
for (int i = 0; i < index.arrayLength; i++) {
|
||||
if (dt == null) {
|
||||
dt = cache[index[i]].timeAdded
|
||||
oldestItem = index[i]
|
||||
} else {
|
||||
if (du.before(cache[index[i]].timeAdded, dt)) {
|
||||
dt = cache[index[i]].timeAdded
|
||||
oldestItem = index[i]
|
||||
}
|
||||
}
|
||||
}
|
||||
return oldestItem
|
||||
} else if (flag == "r") {
|
||||
dt = ic.getTime()
|
||||
int msec = dt.millisecond
|
||||
ir.setSeed(msec)
|
||||
return ir.get(index.arrayLength)
|
||||
} else {
|
||||
return 0
|
||||
}
|
||||
}
|
||||
}
|
267
dat/cache/difffile-large/10/CacheHandler10_6.dn
vendored
Normal file
267
dat/cache/difffile-large/10/CacheHandler10_6.dn
vendored
Normal file
|
@ -0,0 +1,267 @@
|
|||
/*
|
||||
* EmerGen(e)tic 0.8
|
||||
* Copyright © 2017 Ben Goldsworthy (rumperuu)
|
||||
*
|
||||
* A program to research the utility of genetic algorithms, applied to
|
||||
* 'emergent' sytems.
|
||||
*
|
||||
* This file is part of EmerGen(e)tic.
|
||||
*
|
||||
* IMPROVEMENT(S): License (probably GNU GPLv3 tbh) goes here.
|
||||
*/
|
||||
|
||||
/**
|
||||
** This program serves as the base for each mutated CacheHandler,
|
||||
** providing the functionality that is common to all of them.
|
||||
**/
|
||||
|
||||
/**
|
||||
** @author Ben Goldsworthy (rumperuu) <bgoldsworthy96 @ gmail.com>
|
||||
** @version 1.0
|
||||
**/
|
||||
component provides cache.CacheHandler
|
||||
requires io.Output out,
|
||||
data.IntUtil iu,
|
||||
util.RandomInt ir,
|
||||
time.Calendar ic,
|
||||
time.DateUtil du,
|
||||
io.File {
|
||||
|
||||
static CachedResponse cache[]
|
||||
static int arraySize
|
||||
static Mutex cacheLock
|
||||
|
||||
/**
|
||||
** Returns a requested item from the cache.
|
||||
**
|
||||
** @param resource The request item filepath
|
||||
** @return The item from the cache
|
||||
**/
|
||||
CachedResponse CacheHandler:getCachedResponse(char filePath[]) {
|
||||
// Converts the filepath to an array of bytes for the file.
|
||||
File fd = new File(filePath, File.FILE_ACCESS_READ)
|
||||
char resource[] = fd.read(fd.getSize())
|
||||
fd.close()
|
||||
|
||||
// Returns the item if it's in the cache.
|
||||
mutex(cacheLock) {
|
||||
for (int i = 0; i < arraySize; i++) {
|
||||
if (cache[i].resource == resource) {
|
||||
cache[i].hits++
|
||||
cache[i].lastUsed = ic.getTime()
|
||||
return cache[i]
|
||||
}
|
||||
}
|
||||
}
|
||||
return null
|
||||
}
|
||||
|
||||
/**
|
||||
** Updates the cache if the requested item is not already present.
|
||||
** If the cache is full, the method of determining the item to be
|
||||
** replaced is subject to genetic mutation.
|
||||
**
|
||||
** @param ncr The new item to cache
|
||||
**/
|
||||
void CacheHandler:updateCache(CachedResponse ncr) {
|
||||
mutex(cacheLock) {
|
||||
// If the item is already in the cache, do nothing.
|
||||
for (int i = 0; i < arraySize; i++) {
|
||||
if (cache[i].resource == ncr.resource) {
|
||||
cache[i].response = ncr.response
|
||||
cache[i].resourceAge = ncr.resourceAge
|
||||
|
||||
return
|
||||
}
|
||||
}
|
||||
|
||||
// If the item is not, it must be added.
|
||||
// As `ncr` was created on another component, its details must
|
||||
// be copied to avoid a read-only exception.
|
||||
CachedResponse newEntry = new CachedResponse()
|
||||
newEntry.command = ncr.command
|
||||
newEntry.resource = ncr.resource
|
||||
newEntry.resourceAge = ncr.resourceAge
|
||||
newEntry.mimeType = ncr.mimeType
|
||||
newEntry.response = new byte[](ncr.response)
|
||||
newEntry.contentSize = ncr.contentSize
|
||||
|
||||
// Initialises the cache if there isn't currently one.
|
||||
if (cache == null) {
|
||||
cache = new CachedResponse[CacheHandler.MAX_SIZE]()
|
||||
}
|
||||
|
||||
// If the cache is full, determines which item to replace.
|
||||
if (arraySize == CacheHandler.MAX_SIZE) {
|
||||
int index
|
||||
|
||||
// BEGIN
|
||||
index = nthMostRecentlyUsed(0)*nthMostFrequentlyUsed(0)
|
||||
// END
|
||||
|
||||
cache[index % arraySize] = newEntry
|
||||
//out.println("$(debugMSG) replacing: $(iu.intToString(index))")
|
||||
// Otherwise, appends the item to the end of the cache.
|
||||
} else {
|
||||
cache[arraySize] = newEntry
|
||||
arraySize++
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
** Clears the cache completely.
|
||||
**/
|
||||
void CacheHandler:clearCache() {
|
||||
mutex(cacheLock) {
|
||||
cache = null
|
||||
}
|
||||
}
|
||||
|
||||
/**************************************************************************
|
||||
* What follows are utility functions that a given chromosome of the
|
||||
* `updateCache()` method may or may not call upon.
|
||||
**************************************************************************/
|
||||
|
||||
// This method returns the index of the nth most frequently-used item in
|
||||
// the cache.
|
||||
int nthMostFrequentlyUsed(int n) {
|
||||
int hits[]
|
||||
for (int i = 0; i < arraySize; i++) hits[i] = 0
|
||||
|
||||
int i
|
||||
int j
|
||||
mutex(cacheLock) {
|
||||
for (i = 0; i < arraySize; i++) {
|
||||
for (j = 0; j < arraySize; j++) {
|
||||
if (cache[i].hits == hits[j]) break
|
||||
else if (hits[j] == 0) {
|
||||
hits[j] = cache[i].hits
|
||||
break
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
int k = 0
|
||||
int nthHit = 0
|
||||
for (i = 0; i < j; i++) {
|
||||
if (hits[i] > nthHit) {
|
||||
nthHit = hits[i]
|
||||
k++
|
||||
}
|
||||
if (k == n) break
|
||||
}
|
||||
|
||||
int itemIndices[]
|
||||
|
||||
mutex(cacheLock) {
|
||||
j = 0
|
||||
for (i = 0; i < arraySize; i++) {
|
||||
if (cache[i].hits == nthHit) itemIndices[j++] = i
|
||||
}
|
||||
}
|
||||
|
||||
return resolve(itemIndices, "r")
|
||||
}
|
||||
|
||||
// This method returns the index of the nth most recently-used item in
|
||||
// the cache.
|
||||
int nthMostRecentlyUsed(int n) {
|
||||
DateTime useTimes[]
|
||||
DateTime testTime = ic.getTime()
|
||||
for (int i = 0; i < arraySize; i++) useTimes[i] = testTime
|
||||
|
||||
int i
|
||||
int j
|
||||
mutex(cacheLock) {
|
||||
for (i = 0; i < arraySize; i++) {
|
||||
for (j = 0; j < arraySize; j++) {
|
||||
if (du.equal(cache[i].lastUsed, useTimes[j])) break
|
||||
else if (du.equal(useTimes[j], testTime)) {
|
||||
useTimes[j] = cache[i].lastUsed
|
||||
break
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
int k = 0
|
||||
DateTime nthUseTime = ic.getTime()
|
||||
for (i = 0; i < j; i++) {
|
||||
if (du.after(useTimes[i], nthUseTime)) {
|
||||
nthUseTime = useTimes[i]
|
||||
k++
|
||||
}
|
||||
if (k == n) break
|
||||
}
|
||||
|
||||
int itemIndices[]
|
||||
|
||||
mutex(cacheLock) {
|
||||
j = 0
|
||||
for (i = 0; i < arraySize; i++) {
|
||||
if (du.equal(cache[i].lastUsed, nthUseTime)) itemIndices[j++] = i
|
||||
}
|
||||
}
|
||||
|
||||
return resolve(itemIndices, "r")
|
||||
}
|
||||
|
||||
// This method returns a random index.
|
||||
int random() {
|
||||
DateTime dt = ic.getTime()
|
||||
int msec = dt.millisecond
|
||||
ir.setSeed(msec)
|
||||
return ir.get(CacheHandler.MAX_SIZE)
|
||||
}
|
||||
|
||||
// This method resolves a multiple return in one of the methods above,
|
||||
// as per the flag sent along with the list of indices.
|
||||
// 'n' returns the newest, 'o' the oldest and 'r' a random index.
|
||||
int resolve(int index[], char flag) {
|
||||
DateTime dt = null
|
||||
|
||||
if (flag == "n") {
|
||||
int newestItem = 0
|
||||
|
||||
int i = index.arrayLength-1
|
||||
for (int j = 0; j < index.arrayLength; j++) {
|
||||
if (dt == null) {
|
||||
dt = cache[index[i]].timeAdded
|
||||
newestItem = index[i]
|
||||
} else {
|
||||
if (du.before(dt, cache[index[i]].timeAdded)) {
|
||||
dt = cache[index[i]].timeAdded
|
||||
newestItem = index[i]
|
||||
}
|
||||
}
|
||||
i--
|
||||
}
|
||||
|
||||
return newestItem
|
||||
} else if (flag == "o") {
|
||||
int oldestItem = 0
|
||||
|
||||
for (int i = 0; i < index.arrayLength; i++) {
|
||||
if (dt == null) {
|
||||
dt = cache[index[i]].timeAdded
|
||||
oldestItem = index[i]
|
||||
} else {
|
||||
if (du.before(cache[index[i]].timeAdded, dt)) {
|
||||
dt = cache[index[i]].timeAdded
|
||||
oldestItem = index[i]
|
||||
}
|
||||
}
|
||||
}
|
||||
return oldestItem
|
||||
} else if (flag == "r") {
|
||||
dt = ic.getTime()
|
||||
int msec = dt.millisecond
|
||||
ir.setSeed(msec)
|
||||
return ir.get(index.arrayLength)
|
||||
} else {
|
||||
return 0
|
||||
}
|
||||
}
|
||||
}
|
267
dat/cache/difffile-large/10/CacheHandler10_7.dn
vendored
Normal file
267
dat/cache/difffile-large/10/CacheHandler10_7.dn
vendored
Normal file
|
@ -0,0 +1,267 @@
|
|||
/*
|
||||
* EmerGen(e)tic 0.8
|
||||
* Copyright © 2017 Ben Goldsworthy (rumperuu)
|
||||
*
|
||||
* A program to research the utility of genetic algorithms, applied to
|
||||
* 'emergent' sytems.
|
||||
*
|
||||
* This file is part of EmerGen(e)tic.
|
||||
*
|
||||
* IMPROVEMENT(S): License (probably GNU GPLv3 tbh) goes here.
|
||||
*/
|
||||
|
||||
/**
|
||||
** This program serves as the base for each mutated CacheHandler,
|
||||
** providing the functionality that is common to all of them.
|
||||
**/
|
||||
|
||||
/**
|
||||
** @author Ben Goldsworthy (rumperuu) <bgoldsworthy96 @ gmail.com>
|
||||
** @version 1.0
|
||||
**/
|
||||
component provides cache.CacheHandler
|
||||
requires io.Output out,
|
||||
data.IntUtil iu,
|
||||
util.RandomInt ir,
|
||||
time.Calendar ic,
|
||||
time.DateUtil du,
|
||||
io.File {
|
||||
|
||||
static CachedResponse cache[]
|
||||
static int arraySize
|
||||
static Mutex cacheLock
|
||||
|
||||
/**
|
||||
** Returns a requested item from the cache.
|
||||
**
|
||||
** @param resource The request item filepath
|
||||
** @return The item from the cache
|
||||
**/
|
||||
CachedResponse CacheHandler:getCachedResponse(char filePath[]) {
|
||||
// Converts the filepath to an array of bytes for the file.
|
||||
File fd = new File(filePath, File.FILE_ACCESS_READ)
|
||||
char resource[] = fd.read(fd.getSize())
|
||||
fd.close()
|
||||
|
||||
// Returns the item if it's in the cache.
|
||||
mutex(cacheLock) {
|
||||
for (int i = 0; i < arraySize; i++) {
|
||||
if (cache[i].resource == resource) {
|
||||
cache[i].hits++
|
||||
cache[i].lastUsed = ic.getTime()
|
||||
return cache[i]
|
||||
}
|
||||
}
|
||||
}
|
||||
return null
|
||||
}
|
||||
|
||||
/**
|
||||
** Updates the cache if the requested item is not already present.
|
||||
** If the cache is full, the method of determining the item to be
|
||||
** replaced is subject to genetic mutation.
|
||||
**
|
||||
** @param ncr The new item to cache
|
||||
**/
|
||||
void CacheHandler:updateCache(CachedResponse ncr) {
|
||||
mutex(cacheLock) {
|
||||
// If the item is already in the cache, do nothing.
|
||||
for (int i = 0; i < arraySize; i++) {
|
||||
if (cache[i].resource == ncr.resource) {
|
||||
cache[i].response = ncr.response
|
||||
cache[i].resourceAge = ncr.resourceAge
|
||||
|
||||
return
|
||||
}
|
||||
}
|
||||
|
||||
// If the item is not, it must be added.
|
||||
// As `ncr` was created on another component, its details must
|
||||
// be copied to avoid a read-only exception.
|
||||
CachedResponse newEntry = new CachedResponse()
|
||||
newEntry.command = ncr.command
|
||||
newEntry.resource = ncr.resource
|
||||
newEntry.resourceAge = ncr.resourceAge
|
||||
newEntry.mimeType = ncr.mimeType
|
||||
newEntry.response = new byte[](ncr.response)
|
||||
newEntry.contentSize = ncr.contentSize
|
||||
|
||||
// Initialises the cache if there isn't currently one.
|
||||
if (cache == null) {
|
||||
cache = new CachedResponse[CacheHandler.MAX_SIZE]()
|
||||
}
|
||||
|
||||
// If the cache is full, determines which item to replace.
|
||||
if (arraySize == CacheHandler.MAX_SIZE) {
|
||||
int index
|
||||
|
||||
// BEGIN
|
||||
index = nthMostFrequentlyUsed(0)/nthMostRecentlyUsed(0)
|
||||
// END
|
||||
|
||||
cache[index % arraySize] = newEntry
|
||||
//out.println("$(debugMSG) replacing: $(iu.intToString(index))")
|
||||
// Otherwise, appends the item to the end of the cache.
|
||||
} else {
|
||||
cache[arraySize] = newEntry
|
||||
arraySize++
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
** Clears the cache completely.
|
||||
**/
|
||||
void CacheHandler:clearCache() {
|
||||
mutex(cacheLock) {
|
||||
cache = null
|
||||
}
|
||||
}
|
||||
|
||||
/**************************************************************************
|
||||
* What follows are utility functions that a given chromosome of the
|
||||
* `updateCache()` method may or may not call upon.
|
||||
**************************************************************************/
|
||||
|
||||
// This method returns the index of the nth most frequently-used item in
|
||||
// the cache.
|
||||
int nthMostFrequentlyUsed(int n) {
|
||||
int hits[]
|
||||
for (int i = 0; i < arraySize; i++) hits[i] = 0
|
||||
|
||||
int i
|
||||
int j
|
||||
mutex(cacheLock) {
|
||||
for (i = 0; i < arraySize; i++) {
|
||||
for (j = 0; j < arraySize; j++) {
|
||||
if (cache[i].hits == hits[j]) break
|
||||
else if (hits[j] == 0) {
|
||||
hits[j] = cache[i].hits
|
||||
break
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
int k = 0
|
||||
int nthHit = 0
|
||||
for (i = 0; i < j; i++) {
|
||||
if (hits[i] > nthHit) {
|
||||
nthHit = hits[i]
|
||||
k++
|
||||
}
|
||||
if (k == n) break
|
||||
}
|
||||
|
||||
int itemIndices[]
|
||||
|
||||
mutex(cacheLock) {
|
||||
j = 0
|
||||
for (i = 0; i < arraySize; i++) {
|
||||
if (cache[i].hits == nthHit) itemIndices[j++] = i
|
||||
}
|
||||
}
|
||||
|
||||
return resolve(itemIndices, "r")
|
||||
}
|
||||
|
||||
// This method returns the index of the nth most recently-used item in
|
||||
// the cache.
|
||||
int nthMostRecentlyUsed(int n) {
|
||||
DateTime useTimes[]
|
||||
DateTime testTime = ic.getTime()
|
||||
for (int i = 0; i < arraySize; i++) useTimes[i] = testTime
|
||||
|
||||
int i
|
||||
int j
|
||||
mutex(cacheLock) {
|
||||
for (i = 0; i < arraySize; i++) {
|
||||
for (j = 0; j < arraySize; j++) {
|
||||
if (du.equal(cache[i].lastUsed, useTimes[j])) break
|
||||
else if (du.equal(useTimes[j], testTime)) {
|
||||
useTimes[j] = cache[i].lastUsed
|
||||
break
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
int k = 0
|
||||
DateTime nthUseTime = ic.getTime()
|
||||
for (i = 0; i < j; i++) {
|
||||
if (du.after(useTimes[i], nthUseTime)) {
|
||||
nthUseTime = useTimes[i]
|
||||
k++
|
||||
}
|
||||
if (k == n) break
|
||||
}
|
||||
|
||||
int itemIndices[]
|
||||
|
||||
mutex(cacheLock) {
|
||||
j = 0
|
||||
for (i = 0; i < arraySize; i++) {
|
||||
if (du.equal(cache[i].lastUsed, nthUseTime)) itemIndices[j++] = i
|
||||
}
|
||||
}
|
||||
|
||||
return resolve(itemIndices, "r")
|
||||
}
|
||||
|
||||
// This method returns a random index.
|
||||
int random() {
|
||||
DateTime dt = ic.getTime()
|
||||
int msec = dt.millisecond
|
||||
ir.setSeed(msec)
|
||||
return ir.get(CacheHandler.MAX_SIZE)
|
||||
}
|
||||
|
||||
// This method resolves a multiple return in one of the methods above,
|
||||
// as per the flag sent along with the list of indices.
|
||||
// 'n' returns the newest, 'o' the oldest and 'r' a random index.
|
||||
int resolve(int index[], char flag) {
|
||||
DateTime dt = null
|
||||
|
||||
if (flag == "n") {
|
||||
int newestItem = 0
|
||||
|
||||
int i = index.arrayLength-1
|
||||
for (int j = 0; j < index.arrayLength; j++) {
|
||||
if (dt == null) {
|
||||
dt = cache[index[i]].timeAdded
|
||||
newestItem = index[i]
|
||||
} else {
|
||||
if (du.before(dt, cache[index[i]].timeAdded)) {
|
||||
dt = cache[index[i]].timeAdded
|
||||
newestItem = index[i]
|
||||
}
|
||||
}
|
||||
i--
|
||||
}
|
||||
|
||||
return newestItem
|
||||
} else if (flag == "o") {
|
||||
int oldestItem = 0
|
||||
|
||||
for (int i = 0; i < index.arrayLength; i++) {
|
||||
if (dt == null) {
|
||||
dt = cache[index[i]].timeAdded
|
||||
oldestItem = index[i]
|
||||
} else {
|
||||
if (du.before(cache[index[i]].timeAdded, dt)) {
|
||||
dt = cache[index[i]].timeAdded
|
||||
oldestItem = index[i]
|
||||
}
|
||||
}
|
||||
}
|
||||
return oldestItem
|
||||
} else if (flag == "r") {
|
||||
dt = ic.getTime()
|
||||
int msec = dt.millisecond
|
||||
ir.setSeed(msec)
|
||||
return ir.get(index.arrayLength)
|
||||
} else {
|
||||
return 0
|
||||
}
|
||||
}
|
||||
}
|
267
dat/cache/difffile-large/10/CacheHandler10_8.dn
vendored
Normal file
267
dat/cache/difffile-large/10/CacheHandler10_8.dn
vendored
Normal file
|
@ -0,0 +1,267 @@
|
|||
/*
|
||||
* EmerGen(e)tic 0.8
|
||||
* Copyright © 2017 Ben Goldsworthy (rumperuu)
|
||||
*
|
||||
* A program to research the utility of genetic algorithms, applied to
|
||||
* 'emergent' sytems.
|
||||
*
|
||||
* This file is part of EmerGen(e)tic.
|
||||
*
|
||||
* IMPROVEMENT(S): License (probably GNU GPLv3 tbh) goes here.
|
||||
*/
|
||||
|
||||
/**
|
||||
** This program serves as the base for each mutated CacheHandler,
|
||||
** providing the functionality that is common to all of them.
|
||||
**/
|
||||
|
||||
/**
|
||||
** @author Ben Goldsworthy (rumperuu) <bgoldsworthy96 @ gmail.com>
|
||||
** @version 1.0
|
||||
**/
|
||||
component provides cache.CacheHandler
|
||||
requires io.Output out,
|
||||
data.IntUtil iu,
|
||||
util.RandomInt ir,
|
||||
time.Calendar ic,
|
||||
time.DateUtil du,
|
||||
io.File {
|
||||
|
||||
static CachedResponse cache[]
|
||||
static int arraySize
|
||||
static Mutex cacheLock
|
||||
|
||||
/**
|
||||
** Returns a requested item from the cache.
|
||||
**
|
||||
** @param resource The request item filepath
|
||||
** @return The item from the cache
|
||||
**/
|
||||
CachedResponse CacheHandler:getCachedResponse(char filePath[]) {
|
||||
// Converts the filepath to an array of bytes for the file.
|
||||
File fd = new File(filePath, File.FILE_ACCESS_READ)
|
||||
char resource[] = fd.read(fd.getSize())
|
||||
fd.close()
|
||||
|
||||
// Returns the item if it's in the cache.
|
||||
mutex(cacheLock) {
|
||||
for (int i = 0; i < arraySize; i++) {
|
||||
if (cache[i].resource == resource) {
|
||||
cache[i].hits++
|
||||
cache[i].lastUsed = ic.getTime()
|
||||
return cache[i]
|
||||
}
|
||||
}
|
||||
}
|
||||
return null
|
||||
}
|
||||
|
||||
/**
|
||||
** Updates the cache if the requested item is not already present.
|
||||
** If the cache is full, the method of determining the item to be
|
||||
** replaced is subject to genetic mutation.
|
||||
**
|
||||
** @param ncr The new item to cache
|
||||
**/
|
||||
void CacheHandler:updateCache(CachedResponse ncr) {
|
||||
mutex(cacheLock) {
|
||||
// If the item is already in the cache, do nothing.
|
||||
for (int i = 0; i < arraySize; i++) {
|
||||
if (cache[i].resource == ncr.resource) {
|
||||
cache[i].response = ncr.response
|
||||
cache[i].resourceAge = ncr.resourceAge
|
||||
|
||||
return
|
||||
}
|
||||
}
|
||||
|
||||
// If the item is not, it must be added.
|
||||
// As `ncr` was created on another component, its details must
|
||||
// be copied to avoid a read-only exception.
|
||||
CachedResponse newEntry = new CachedResponse()
|
||||
newEntry.command = ncr.command
|
||||
newEntry.resource = ncr.resource
|
||||
newEntry.resourceAge = ncr.resourceAge
|
||||
newEntry.mimeType = ncr.mimeType
|
||||
newEntry.response = new byte[](ncr.response)
|
||||
newEntry.contentSize = ncr.contentSize
|
||||
|
||||
// Initialises the cache if there isn't currently one.
|
||||
if (cache == null) {
|
||||
cache = new CachedResponse[CacheHandler.MAX_SIZE]()
|
||||
}
|
||||
|
||||
// If the cache is full, determines which item to replace.
|
||||
if (arraySize == CacheHandler.MAX_SIZE) {
|
||||
int index
|
||||
|
||||
// BEGIN
|
||||
index = 0+2
|
||||
// END
|
||||
|
||||
cache[index % arraySize] = newEntry
|
||||
//out.println("$(debugMSG) replacing: $(iu.intToString(index))")
|
||||
// Otherwise, appends the item to the end of the cache.
|
||||
} else {
|
||||
cache[arraySize] = newEntry
|
||||
arraySize++
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
** Clears the cache completely.
|
||||
**/
|
||||
void CacheHandler:clearCache() {
|
||||
mutex(cacheLock) {
|
||||
cache = null
|
||||
}
|
||||
}
|
||||
|
||||
/**************************************************************************
|
||||
* What follows are utility functions that a given chromosome of the
|
||||
* `updateCache()` method may or may not call upon.
|
||||
**************************************************************************/
|
||||
|
||||
// This method returns the index of the nth most frequently-used item in
|
||||
// the cache.
|
||||
int nthMostFrequentlyUsed(int n) {
|
||||
int hits[]
|
||||
for (int i = 0; i < arraySize; i++) hits[i] = 0
|
||||
|
||||
int i
|
||||
int j
|
||||
mutex(cacheLock) {
|
||||
for (i = 0; i < arraySize; i++) {
|
||||
for (j = 0; j < arraySize; j++) {
|
||||
if (cache[i].hits == hits[j]) break
|
||||
else if (hits[j] == 0) {
|
||||
hits[j] = cache[i].hits
|
||||
break
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
int k = 0
|
||||
int nthHit = 0
|
||||
for (i = 0; i < j; i++) {
|
||||
if (hits[i] > nthHit) {
|
||||
nthHit = hits[i]
|
||||
k++
|
||||
}
|
||||
if (k == n) break
|
||||
}
|
||||
|
||||
int itemIndices[]
|
||||
|
||||
mutex(cacheLock) {
|
||||
j = 0
|
||||
for (i = 0; i < arraySize; i++) {
|
||||
if (cache[i].hits == nthHit) itemIndices[j++] = i
|
||||
}
|
||||
}
|
||||
|
||||
return resolve(itemIndices, "r")
|
||||
}
|
||||
|
||||
// This method returns the index of the nth most recently-used item in
|
||||
// the cache.
|
||||
int nthMostRecentlyUsed(int n) {
|
||||
DateTime useTimes[]
|
||||
DateTime testTime = ic.getTime()
|
||||
for (int i = 0; i < arraySize; i++) useTimes[i] = testTime
|
||||
|
||||
int i
|
||||
int j
|
||||
mutex(cacheLock) {
|
||||
for (i = 0; i < arraySize; i++) {
|
||||
for (j = 0; j < arraySize; j++) {
|
||||
if (du.equal(cache[i].lastUsed, useTimes[j])) break
|
||||
else if (du.equal(useTimes[j], testTime)) {
|
||||
useTimes[j] = cache[i].lastUsed
|
||||
break
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
int k = 0
|
||||
DateTime nthUseTime = ic.getTime()
|
||||
for (i = 0; i < j; i++) {
|
||||
if (du.after(useTimes[i], nthUseTime)) {
|
||||
nthUseTime = useTimes[i]
|
||||
k++
|
||||
}
|
||||
if (k == n) break
|
||||
}
|
||||
|
||||
int itemIndices[]
|
||||
|
||||
mutex(cacheLock) {
|
||||
j = 0
|
||||
for (i = 0; i < arraySize; i++) {
|
||||
if (du.equal(cache[i].lastUsed, nthUseTime)) itemIndices[j++] = i
|
||||
}
|
||||
}
|
||||
|
||||
return resolve(itemIndices, "r")
|
||||
}
|
||||
|
||||
// This method returns a random index.
|
||||
int random() {
|
||||
DateTime dt = ic.getTime()
|
||||
int msec = dt.millisecond
|
||||
ir.setSeed(msec)
|
||||
return ir.get(CacheHandler.MAX_SIZE)
|
||||
}
|
||||
|
||||
// This method resolves a multiple return in one of the methods above,
|
||||
// as per the flag sent along with the list of indices.
|
||||
// 'n' returns the newest, 'o' the oldest and 'r' a random index.
|
||||
int resolve(int index[], char flag) {
|
||||
DateTime dt = null
|
||||
|
||||
if (flag == "n") {
|
||||
int newestItem = 0
|
||||
|
||||
int i = index.arrayLength-1
|
||||
for (int j = 0; j < index.arrayLength; j++) {
|
||||
if (dt == null) {
|
||||
dt = cache[index[i]].timeAdded
|
||||
newestItem = index[i]
|
||||
} else {
|
||||
if (du.before(dt, cache[index[i]].timeAdded)) {
|
||||
dt = cache[index[i]].timeAdded
|
||||
newestItem = index[i]
|
||||
}
|
||||
}
|
||||
i--
|
||||
}
|
||||
|
||||
return newestItem
|
||||
} else if (flag == "o") {
|
||||
int oldestItem = 0
|
||||
|
||||
for (int i = 0; i < index.arrayLength; i++) {
|
||||
if (dt == null) {
|
||||
dt = cache[index[i]].timeAdded
|
||||
oldestItem = index[i]
|
||||
} else {
|
||||
if (du.before(cache[index[i]].timeAdded, dt)) {
|
||||
dt = cache[index[i]].timeAdded
|
||||
oldestItem = index[i]
|
||||
}
|
||||
}
|
||||
}
|
||||
return oldestItem
|
||||
} else if (flag == "r") {
|
||||
dt = ic.getTime()
|
||||
int msec = dt.millisecond
|
||||
ir.setSeed(msec)
|
||||
return ir.get(index.arrayLength)
|
||||
} else {
|
||||
return 0
|
||||
}
|
||||
}
|
||||
}
|
267
dat/cache/difffile-large/10/CacheHandler10_9.dn
vendored
Normal file
267
dat/cache/difffile-large/10/CacheHandler10_9.dn
vendored
Normal file
|
@ -0,0 +1,267 @@
|
|||
/*
|
||||
* EmerGen(e)tic 0.8
|
||||
* Copyright © 2017 Ben Goldsworthy (rumperuu)
|
||||
*
|
||||
* A program to research the utility of genetic algorithms, applied to
|
||||
* 'emergent' sytems.
|
||||
*
|
||||
* This file is part of EmerGen(e)tic.
|
||||
*
|
||||
* IMPROVEMENT(S): License (probably GNU GPLv3 tbh) goes here.
|
||||
*/
|
||||
|
||||
/**
|
||||
** This program serves as the base for each mutated CacheHandler,
|
||||
** providing the functionality that is common to all of them.
|
||||
**/
|
||||
|
||||
/**
|
||||
** @author Ben Goldsworthy (rumperuu) <bgoldsworthy96 @ gmail.com>
|
||||
** @version 1.0
|
||||
**/
|
||||
component provides cache.CacheHandler
|
||||
requires io.Output out,
|
||||
data.IntUtil iu,
|
||||
util.RandomInt ir,
|
||||
time.Calendar ic,
|
||||
time.DateUtil du,
|
||||
io.File {
|
||||
|
||||
static CachedResponse cache[]
|
||||
static int arraySize
|
||||
static Mutex cacheLock
|
||||
|
||||
/**
|
||||
** Returns a requested item from the cache.
|
||||
**
|
||||
** @param resource The request item filepath
|
||||
** @return The item from the cache
|
||||
**/
|
||||
CachedResponse CacheHandler:getCachedResponse(char filePath[]) {
|
||||
// Converts the filepath to an array of bytes for the file.
|
||||
File fd = new File(filePath, File.FILE_ACCESS_READ)
|
||||
char resource[] = fd.read(fd.getSize())
|
||||
fd.close()
|
||||
|
||||
// Returns the item if it's in the cache.
|
||||
mutex(cacheLock) {
|
||||
for (int i = 0; i < arraySize; i++) {
|
||||
if (cache[i].resource == resource) {
|
||||
cache[i].hits++
|
||||
cache[i].lastUsed = ic.getTime()
|
||||
return cache[i]
|
||||
}
|
||||
}
|
||||
}
|
||||
return null
|
||||
}
|
||||
|
||||
/**
|
||||
** Updates the cache if the requested item is not already present.
|
||||
** If the cache is full, the method of determining the item to be
|
||||
** replaced is subject to genetic mutation.
|
||||
**
|
||||
** @param ncr The new item to cache
|
||||
**/
|
||||
void CacheHandler:updateCache(CachedResponse ncr) {
|
||||
mutex(cacheLock) {
|
||||
// If the item is already in the cache, do nothing.
|
||||
for (int i = 0; i < arraySize; i++) {
|
||||
if (cache[i].resource == ncr.resource) {
|
||||
cache[i].response = ncr.response
|
||||
cache[i].resourceAge = ncr.resourceAge
|
||||
|
||||
return
|
||||
}
|
||||
}
|
||||
|
||||
// If the item is not, it must be added.
|
||||
// As `ncr` was created on another component, its details must
|
||||
// be copied to avoid a read-only exception.
|
||||
CachedResponse newEntry = new CachedResponse()
|
||||
newEntry.command = ncr.command
|
||||
newEntry.resource = ncr.resource
|
||||
newEntry.resourceAge = ncr.resourceAge
|
||||
newEntry.mimeType = ncr.mimeType
|
||||
newEntry.response = new byte[](ncr.response)
|
||||
newEntry.contentSize = ncr.contentSize
|
||||
|
||||
// Initialises the cache if there isn't currently one.
|
||||
if (cache == null) {
|
||||
cache = new CachedResponse[CacheHandler.MAX_SIZE]()
|
||||
}
|
||||
|
||||
// If the cache is full, determines which item to replace.
|
||||
if (arraySize == CacheHandler.MAX_SIZE) {
|
||||
int index
|
||||
|
||||
// BEGIN
|
||||
index = 0
|
||||
// END
|
||||
|
||||
cache[index % arraySize] = newEntry
|
||||
//out.println("$(debugMSG) replacing: $(iu.intToString(index))")
|
||||
// Otherwise, appends the item to the end of the cache.
|
||||
} else {
|
||||
cache[arraySize] = newEntry
|
||||
arraySize++
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
** Clears the cache completely.
|
||||
**/
|
||||
void CacheHandler:clearCache() {
|
||||
mutex(cacheLock) {
|
||||
cache = null
|
||||
}
|
||||
}
|
||||
|
||||
/**************************************************************************
|
||||
* What follows are utility functions that a given chromosome of the
|
||||
* `updateCache()` method may or may not call upon.
|
||||
**************************************************************************/
|
||||
|
||||
// This method returns the index of the nth most frequently-used item in
|
||||
// the cache.
|
||||
int nthMostFrequentlyUsed(int n) {
|
||||
int hits[]
|
||||
for (int i = 0; i < arraySize; i++) hits[i] = 0
|
||||
|
||||
int i
|
||||
int j
|
||||
mutex(cacheLock) {
|
||||
for (i = 0; i < arraySize; i++) {
|
||||
for (j = 0; j < arraySize; j++) {
|
||||
if (cache[i].hits == hits[j]) break
|
||||
else if (hits[j] == 0) {
|
||||
hits[j] = cache[i].hits
|
||||
break
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
int k = 0
|
||||
int nthHit = 0
|
||||
for (i = 0; i < j; i++) {
|
||||
if (hits[i] > nthHit) {
|
||||
nthHit = hits[i]
|
||||
k++
|
||||
}
|
||||
if (k == n) break
|
||||
}
|
||||
|
||||
int itemIndices[]
|
||||
|
||||
mutex(cacheLock) {
|
||||
j = 0
|
||||
for (i = 0; i < arraySize; i++) {
|
||||
if (cache[i].hits == nthHit) itemIndices[j++] = i
|
||||
}
|
||||
}
|
||||
|
||||
return resolve(itemIndices, "r")
|
||||
}
|
||||
|
||||
// This method returns the index of the nth most recently-used item in
|
||||
// the cache.
|
||||
int nthMostRecentlyUsed(int n) {
|
||||
DateTime useTimes[]
|
||||
DateTime testTime = ic.getTime()
|
||||
for (int i = 0; i < arraySize; i++) useTimes[i] = testTime
|
||||
|
||||
int i
|
||||
int j
|
||||
mutex(cacheLock) {
|
||||
for (i = 0; i < arraySize; i++) {
|
||||
for (j = 0; j < arraySize; j++) {
|
||||
if (du.equal(cache[i].lastUsed, useTimes[j])) break
|
||||
else if (du.equal(useTimes[j], testTime)) {
|
||||
useTimes[j] = cache[i].lastUsed
|
||||
break
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
int k = 0
|
||||
DateTime nthUseTime = ic.getTime()
|
||||
for (i = 0; i < j; i++) {
|
||||
if (du.after(useTimes[i], nthUseTime)) {
|
||||
nthUseTime = useTimes[i]
|
||||
k++
|
||||
}
|
||||
if (k == n) break
|
||||
}
|
||||
|
||||
int itemIndices[]
|
||||
|
||||
mutex(cacheLock) {
|
||||
j = 0
|
||||
for (i = 0; i < arraySize; i++) {
|
||||
if (du.equal(cache[i].lastUsed, nthUseTime)) itemIndices[j++] = i
|
||||
}
|
||||
}
|
||||
|
||||
return resolve(itemIndices, "r")
|
||||
}
|
||||
|
||||
// This method returns a random index.
|
||||
int random() {
|
||||
DateTime dt = ic.getTime()
|
||||
int msec = dt.millisecond
|
||||
ir.setSeed(msec)
|
||||
return ir.get(CacheHandler.MAX_SIZE)
|
||||
}
|
||||
|
||||
// This method resolves a multiple return in one of the methods above,
|
||||
// as per the flag sent along with the list of indices.
|
||||
// 'n' returns the newest, 'o' the oldest and 'r' a random index.
|
||||
int resolve(int index[], char flag) {
|
||||
DateTime dt = null
|
||||
|
||||
if (flag == "n") {
|
||||
int newestItem = 0
|
||||
|
||||
int i = index.arrayLength-1
|
||||
for (int j = 0; j < index.arrayLength; j++) {
|
||||
if (dt == null) {
|
||||
dt = cache[index[i]].timeAdded
|
||||
newestItem = index[i]
|
||||
} else {
|
||||
if (du.before(dt, cache[index[i]].timeAdded)) {
|
||||
dt = cache[index[i]].timeAdded
|
||||
newestItem = index[i]
|
||||
}
|
||||
}
|
||||
i--
|
||||
}
|
||||
|
||||
return newestItem
|
||||
} else if (flag == "o") {
|
||||
int oldestItem = 0
|
||||
|
||||
for (int i = 0; i < index.arrayLength; i++) {
|
||||
if (dt == null) {
|
||||
dt = cache[index[i]].timeAdded
|
||||
oldestItem = index[i]
|
||||
} else {
|
||||
if (du.before(cache[index[i]].timeAdded, dt)) {
|
||||
dt = cache[index[i]].timeAdded
|
||||
oldestItem = index[i]
|
||||
}
|
||||
}
|
||||
}
|
||||
return oldestItem
|
||||
} else if (flag == "r") {
|
||||
dt = ic.getTime()
|
||||
int msec = dt.millisecond
|
||||
ir.setSeed(msec)
|
||||
return ir.get(index.arrayLength)
|
||||
} else {
|
||||
return 0
|
||||
}
|
||||
}
|
||||
}
|
Reference in a new issue