ContentMolecule.ts 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  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>, giveRelevant = false) {
  17. let missingAtoms = [...this.atoms];
  18. let chosenOnes = [];
  19. let relevantAtoms : Array<ContentAtom> = [];
  20. for (let k = 0; k < many.length; k++) {
  21. let vagueMolecule = many[k]
  22. let vagueAtoms = [...vagueMolecule.atoms];
  23. // Check if the ENTIRETY of vague side fits in here.
  24. let cachedAtoms = missingAtoms.slice();
  25. for (let i = vagueAtoms.length - 1; i >= 0; i--) {
  26. let vagueAtom = vagueAtoms[i];
  27. for (let k = cachedAtoms.length - 1; k >= 0; k--) {
  28. let specificAtom = cachedAtoms[k];
  29. if (specificAtom.compareAgainst(vagueAtom)) {
  30. cachedAtoms.splice(k, 1);
  31. relevantAtoms.push(...vagueAtoms.splice(i, 1));
  32. break;
  33. }
  34. }
  35. }
  36. if (vagueAtoms.length == 0) {
  37. missingAtoms = cachedAtoms;
  38. chosenOnes.push(k);
  39. }
  40. if (missingAtoms.length == 0) {
  41. break;
  42. }
  43. }
  44. if (giveRelevant) {
  45. return relevantAtoms;
  46. }
  47. if (missingAtoms.length == 0) {
  48. return chosenOnes; // Return best match
  49. } else {
  50. return []; // No match
  51. }
  52. }
  53. }