Skip to content

Commit

Permalink
style: normalize bracket spacing
Browse files Browse the repository at this point in the history
  • Loading branch information
unkn0wn-root committed Jan 22, 2025
1 parent 44c895b commit 96ff85e
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 17 deletions.
37 changes: 26 additions & 11 deletions src/doublyLinkedLists.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,13 @@ export class Node {
timeStamp: Date;
parent?: FreqNode;

constructor(key: string, value: cacheValue, byteSize: number, timeStamp: Date, parent?: FreqNode){
constructor(
key: string,
value: cacheValue,
byteSize: number,
timeStamp: Date,
parent?: FreqNode
) {
this.next = null;
this.prev = null;
this.value = value;
Expand All @@ -34,7 +40,13 @@ export class ValueDoublyLinkedList {
this.tail = null;
}

public addHead(key: string, value: cacheValue, byteSize: number, timeStamp: Date, parent?: FreqNode){
public addHead(
key: string,
value: cacheValue,
byteSize: number,
timeStamp: Date,
parent?: FreqNode
) {
const node = new Node(key, value, byteSize, timeStamp, parent);
if (!this.head) {
this.head = node;
Expand All @@ -47,7 +59,7 @@ export class ValueDoublyLinkedList {
return this.head;
}

public delete(node: Node | null){
public delete(node: Node | null) {
if (!node) {
return;
}
Expand All @@ -69,7 +81,7 @@ export class ValueDoublyLinkedList {

public deleteTail(): Node | null {
if (!this.tail) {
return null; // no nodes to delete
return null;
}

const deleted = this.tail;
Expand Down Expand Up @@ -101,7 +113,7 @@ export class FreqNode {
next: FreqNode | null;
prev: FreqNode | null;

constructor(freqValue: number){
constructor(freqValue: number) {
this.freqValue = freqValue;
this.valList = new ValueDoublyLinkedList();
this.next = null;
Expand All @@ -114,13 +126,13 @@ export class FreqDoublyLinkedList {
head: FreqNode | null;
tail: FreqNode | null;

constructor(){
constructor() {
//head being lowest freq item, tail being highest.
this.head = null;
this.tail = null;
}

public addNewFreq(key: string, value: cacheValue, byteSize: number, timeStamp: Date){
public addNewFreq(key: string, value: cacheValue, byteSize: number, timeStamp: Date) {
if (!this.head) {
this.head = new FreqNode(1);
this.tail = this.head;
Expand All @@ -134,8 +146,11 @@ export class FreqDoublyLinkedList {
return this.head.valList.addHead(key, value, byteSize, timeStamp, this.head);
}

public increaseFreq(node: Node){
if (!node.parent) return;
public increaseFreq(node: Node) {
if (!node.parent) {
return;
}

const { key, value, byteSize, timeStamp, parent } = node;

//is highest freq
Expand Down Expand Up @@ -164,7 +179,7 @@ export class FreqDoublyLinkedList {
this.deleteValNode(this.head.valList.tail)
: undefined;

public deleteValNode(node: Node | null){
public deleteValNode(node: Node | null) {
if (!node || !node.parent) {
return;
}
Expand All @@ -178,7 +193,7 @@ export class FreqDoublyLinkedList {
return node;
}

public delete(freqNode: FreqNode | null){
public delete(freqNode: FreqNode | null) {
if (!freqNode) {
return;
}
Expand Down
10 changes: 5 additions & 5 deletions src/lfu.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ class LFU {
expire: number;
metrics: PerfMetrics;

constructor(expire: number, metrics: PerfMetrics, capacity: number){
constructor(expire: number, metrics: PerfMetrics, capacity: number) {
this.freqList = new FreqDoublyLinkedList();
this.cache = {};
this.length = 0;
Expand All @@ -31,7 +31,7 @@ class LFU {
* @param value
* @returns
*/
public put(key: string, value: cacheValue, byteSize: number){
public put(key: string, value: cacheValue, byteSize: number) {
if (this.cache[key]){
this.metrics.decreaseBytes(this.cache[key].byteSize);
this.metrics.increaseBytes(byteSize);
Expand All @@ -57,7 +57,7 @@ class LFU {
this.metrics.decreaseBytes(deletedNode.byteSize);
}

public get(key: string){
public get(key: string) {
if (!this.cache[key]) return;
//if entry is stale, deletes and exits
const currentTime = new Date();
Expand All @@ -76,7 +76,7 @@ class LFU {
}
}

public delete(key: string){
public delete(key: string) {
const node = this.cache[key];
if (!node) return;

Expand All @@ -88,7 +88,7 @@ class LFU {
return node;
}

public clear(){
public clear() {
this.freqList = new FreqDoublyLinkedList();
this.cache = {};
this.length = 0;
Expand Down
2 changes: 1 addition & 1 deletion src/lru.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ class LRU {
* @param value
* @returns
*/
public put(key: string, value: cacheValue, byteSize: number) {
public put(key: string, value: cacheValue, byteSize: number) {
//if key alreadys exits in cache, replace key value with new value, and move to list head.
if (this.cache[key]){
this.metrics.decreaseBytes(this.cache[key].byteSize);
Expand Down

0 comments on commit 96ff85e

Please sign in to comment.