1

I have a task in which user can: 1. Form any number of points any coordinates; 2. Delete points according to a criterion.

I realized: 1. The case of a simple criterion (>, <, =, >= etc.). 2. The case of a composite criterion, which consists of two criteria with OR between them (x>5 OR y<3 , x<=2 OR y=0). I just add what is selected by one query to what was selected by the other. Then I remove duplicates. This is incorrect of course as the program should select only what is necessary at once. Well, but this is more or less satisfactory to me.

I program in C, so there is no possibility to use self modifying code, as far as I know and to the best of my ability.

I decided that I will limit myself to two criteria with OR or AND between them. I can see: >, <, >=, <=, =, !=. That is 6 cases. Then x and y, point ID, distance from origin. 4 cases. Then if I use combinations, that will be already too much cases for me to implement.

But I can't imagine anything but simple providing a special condition for each unique case. Of course, I could select everything according each query. Then use intersection for AND or addition for OR like we do it in discrete mathematics. But I'd like to do it the fastes way: the computer should select only what is necessary.

So for each case I provide if (x > value & y < value) if (x < value & y = value) if (x = value & y =< value)

Etc.

But this seems to be absolutely crazy. Could you recommend anything to me?

Jack
  • 131,802
  • 30
  • 241
  • 343
Kifsif
  • 3,477
  • 10
  • 36
  • 45
  • Revise your tags - theory is not a good tag for your post at all. Also please try to simplify the question. I could not quite understand it now. – Boris Strandjev Jan 06 '13 at 16:20

1 Answers1

0

This is about interpreting custom script code.

How it does work in practice is easy to explain by using objects but rather more difficult without them. Without objects you will need a custom structure that is able to store a predicate, I'll give you a simple example just to clear it a little bit:

union Expression {
  char type;
  int number;
};

int expressionValue(const Point &curPt, const Expression &exp) {
  if (type == TYPE_NUMBER) return exp.number;
  else if (type == TYPE_X) return curPt.x;
  else if (type == TYPE_Y) return curPT.y;
};

In this way you have an union which is able to store an arbitrary kind of value for your expression which maybe a raw number or the coordinate of the current point.

struct Comparison {
  Expression exp1;
  Expression exp2;
  char type;
};

bool evaluateComparison(const Point &curPt, const Comparison &comp) {
  int v1 = expressionValue(pt, exp1);
  int v2 = expressionValue(pt, exp2);

  if (type == '=') return v1 == v2;
  else if (type == '<') return v1 < v2;
  // so on
}

Now you have a data type which is able to store a single comparison, what you need is a way to store a predicate:

struct Predicate {
  char type;
  Comparison comp1;
  Comparison comp2;
};

bool evaluatePredicate(const  Point &curPt, const Predicate &pred) {
  if (pred.type == TYPE_OR) return evaluateComparison(curPt, comp1) || evaluateComparison(curPt, comp2);
  // oothers
};

Of course you can plan whichever kinds of expression or predicates you need (maybe n-ary AND or OR).

The last thing you will need is a simple lexer and parser to turn your input into a predicate which you then evaluate over your points and you are done.

Jack
  • 131,802
  • 30
  • 241
  • 343