All files / src/queue IAgedQueue.ts

83.33% Statements 5/6
100% Branches 0/0
50% Functions 1/2
75% Lines 3/4

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78              3x       3x 42x                                                                                                                                  
/**
 * @param ageA The first age to compare
 * @param ageB The second age to compare
 * @return 0 if same order, positive if ageA after ageB, negative if ageA before ageB
 */
export type AgedCompareFunc = (ageA: number, ageB: number) => number;
 
export const compareDescending = (ageA: number, ageB: number): number => {
  return ageB - ageA;
};
 
export const compareAscending = (ageA: number, ageB: number): number => {
  return ageA - ageB;
};
 
/**
 * Age bits that will very by replacement algorithm
 */
export interface IAged {
  age: number;
}
 
/**
 * A value with age bits that will very by replacement algorithm
 */
export interface IAgedValue<TValue> extends IAged {
  value: TValue;
}
 
/**
 * A data structure for selecting elements to expire in order
 */
export interface IAgedQueue<TKey> {
  /**
   * @param key The key to add
   * @param age The age if explicitly or default if undefined
   */
  addOrReplace(key: TKey, age?: number): void;
 
  /**
   * @return The next key in order or null if there is no key
   */
  next(): TKey | null;
 
  /**
   * @param key The key to delete
   */
  delete(key: TKey): void;
 
  /**
   * @return True if the next key in order is expired and should be removed
   */
  isNextExpired(): boolean;
 
  /**
   * @param key The key we want a default for
   * @return The default age that will very by algorithm
   */
  getInitialAge(key: TKey): number;
 
  /**
   * @param key Advance the age of the specified key
   */
  updateAge(key: TKey): void;
 
  /**
   * @param ageA The first age to compare
   * @param ageB The second age to compare
   * @return 0 if same order, positive if ageA after ageB, negative if ageA before ageB
   */
  compare: AgedCompareFunc;
 
  /**
   * @return The number of keys in the queue
   */
  size(): number;
}