location.ts 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. import { Address } from "./address";
  2. class LocationHolder {
  3. public data: Address[];
  4. private address_id: number
  5. constructor () {
  6. this.data = [];
  7. this.address_id = 0;
  8. }
  9. /**
  10. *
  11. * @param {*} value the value to be allocated
  12. * @returns {Number} - the address id
  13. */
  14. allocate (value:any) : number {
  15. const id = this.address_id;
  16. console.log("Allocation address "+ id);
  17. const address = new Address(id, value);
  18. this.data.push(address);
  19. this.address_id += 1;
  20. return id;
  21. }
  22. /**
  23. *
  24. * @param {Number} id
  25. */
  26. deallocate (id: number) : boolean {
  27. const index = this.findIndex(id);
  28. console.log("Deallocation address "+ id);
  29. if(index !== -1) {
  30. this.data.splice(index, 1);
  31. return true;
  32. }
  33. return false;
  34. }
  35. /**
  36. *
  37. * @param {Number} id
  38. * @returns {Address} the address identified by id
  39. */
  40. find (id: number): Address | undefined {
  41. let beg = 0
  42. let end = this.data.length;
  43. console.log("Finding address "+id);
  44. while (beg < end) {
  45. const med = Math.floor((beg + end)/2);
  46. const address = this.getAddressAt(med);
  47. if(address.id === id) {
  48. return address;
  49. } else if (id > address.id) {
  50. beg = med;
  51. } else {
  52. end = med
  53. }
  54. }
  55. return undefined;
  56. }
  57. getAddressAt (pos: number): Address {
  58. return this.data[pos];
  59. }
  60. /**
  61. *
  62. * @param {Number} id address id
  63. * @returns {Number} the index of the address identified by id
  64. */
  65. findIndex (id: number) : number {
  66. let beg = 0
  67. let end = this.data.length;
  68. while (beg < end) {
  69. const med = Math.floor((beg + end)/2);
  70. const address = this.getAddressAt(med);
  71. if(address.id === id) {
  72. return med;
  73. } else if (id > address.id) {
  74. beg = med;
  75. } else {
  76. end = med
  77. }
  78. }
  79. return -1;
  80. }
  81. updateAddress (id: number, value: any): void {
  82. const index = this.findIndex(id);
  83. if(index === -1) {
  84. throw new Error("Invalid address..." + id);
  85. }
  86. this.data[index].value = value;
  87. }
  88. clear () {
  89. for (let i = 0; i < this.data.length; i += 1) {
  90. delete this.data[i];
  91. }
  92. this.data = [];
  93. this.address_id = 0;
  94. }
  95. }
  96. const inner_ref = new LocationHolder();
  97. export const Location = Object.freeze({
  98. allocate: inner_ref.allocate.bind(inner_ref),
  99. deallocate: inner_ref.deallocate.bind(inner_ref),
  100. find: inner_ref.find.bind(inner_ref),
  101. updateAddress: inner_ref.updateAddress.bind(inner_ref),
  102. clear: inner_ref.clear.bind(inner_ref),
  103. size: () => inner_ref.data.length
  104. });