ContentMolecule.ts 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  1. /**
  2. * Essentially a group of Atoms.
  3. */
  4. class ContentMolecule {
  5. protected atoms : Array<ContentAtom> = [];
  6. public constructor (...atoms : Array<ContentAtom>) {
  7. this.addAtom(...atoms);
  8. }
  9. public addAtom (...atoms : Array<ContentAtom>) {
  10. this.atoms.push(...atoms);
  11. return this;
  12. }
  13. public getAtoms () {
  14. return this.atoms;
  15. }
  16. public findMatches (many : Array<ContentMolecule>) {
  17. let missingAtoms = [...this.atoms];
  18. let chosenOnes = [];
  19. for (let k = 0; k < many.length; k++) {
  20. let vagueMolecule = many[k]
  21. let vagueAtoms = [...vagueMolecule.atoms];
  22. // Check if the ENTIRETY of vague side fits in here.
  23. let cachedAtoms = missingAtoms.slice();
  24. for (let i = vagueAtoms.length - 1; i >= 0; i--) {
  25. let vagueAtom = vagueAtoms[i];
  26. for (let k = cachedAtoms.length - 1; k >= 0; k--) {
  27. let specificAtom = cachedAtoms[k];
  28. if (specificAtom.compareAgainst(vagueAtom)) {
  29. cachedAtoms.splice(k, 1);
  30. vagueAtoms.splice(i, 1);
  31. break;
  32. }
  33. }
  34. }
  35. if (vagueAtoms.length == 0) {
  36. missingAtoms = cachedAtoms;
  37. chosenOnes.push(k);
  38. }
  39. if (missingAtoms.length == 0) {
  40. break;
  41. }
  42. }
  43. if (missingAtoms.length == 0) {
  44. return chosenOnes; // Return best match
  45. } else {
  46. return []; // No match
  47. }
  48. }
  49. }