Created
December 3, 2011 20:15
-
-
Save kscottz/1428025 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * | |
| * * * * * * * * * * Arithmetic Operations * * * * * * * * * * * * * * | |
| * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */ | |
| /* | |
| * Notation: OR == alt // the same | |
| seq + seq: | |
| (a; b; c) + (x; y; z) = (a+x; b+y; c+z) | |
| (a; b; c) + (x; y) = (a+x; b+y; c) | |
| (a; b) + (x; y; z) = (a+x; b+y; z) | |
| seq + alt: we have to promote alt to seq ==> (alt) == (alt; epsilon; epsilon; ...; epsilon) to be of the same size | |
| (a; b; c) + (x | y | z) = (a; b; c) + ((x | y | z); epsilon; epsilon) = (a + (x | y | z); b; c) // a + (x | y | z) is defined in WRD + OR | |
| seq + wrd: we have to promote wrd to seq ===> (wrd) == (wrd; epsilon; epsilon; ...; epsilon) to be of the same size | |
| (a; b; c) + x = (a; b; c) + (x; epsilon; epsilon) = (a + x; b; c) // a + x is WRD + WRD | |
| wrd + seq: we have to promote wrd to seq (again) ===> (wrd) == (wrd; epsilon; epsilon; ...; epsilon) to be of the same size | |
| x + (a; b; c) = (x; epsilon; epsilon) + (a; b; c) = (x + a; b; c) // a + x is WRD + WRD | |
| wrd + alt: we have to convert alt to wrd ===> | |
| x + (a | b | c) = x + (a + b + c) = x + a + b + c // and that's why you should have a function "Lattice sum()" that sums all the elements in a lattice and returns a WRD lattice. | |
| wrd + wrd: cast and sum | |
| alt1 + alt2: | |
| Lattice resultLat = new Lattice(Lattice.OR); | |
| Lattice a2sum = alt2.sum(); // returns a wrd lattice that is the sum of all elements. You don't need two for loops any more | |
| foreach a1 in alt1: | |
| resultLat.vector.add(a1.plusIntInt(a2sum)); | |
| return resultLat; | |
| alt + seq: we have to promote seq to alt ==> (seq) == (seq | epsilon | epsilon | ... | epsilon) | |
| (x | y | z) + (a; b; c) = (x | y | z) + ((a; b; c) | epsilon | epsilon) = (x + (a; b; c) | y | z) // now it is wrd + seq above | |
| to simplify the alt + alt function, you'll need these function for each type pair (should be generated by your script): | |
| Lattice sumIntInt(); // x+y+z | |
| Lattice subIntInt(); // x-y-z | |
| Lattice timesIntInt(); // x*y*z | |
| Lattice divideIntInt(); // x/y/z | |
| */ | |
| //The following functions perform mathematical binary and unary operations: | |
| public Lattice operationTemplate(Lattice secondLat, LatticeOperation op) | |
| { | |
| Lattice retVal; | |
| if (this.type == WRD & secondLat.type == WRD) | |
| { | |
| retVal.word.element = op.process(this.word.element, secondLat.word.element); | |
| } | |
| else if( this.type = WRD & secondLat.type == OR )// do we need the reverse of this? | |
| { | |
| // wrd + alt: we have to convert alt to wrd ===> | |
| // x + (a | b | c) = x + (a + b + c) = x + a + b + c | |
| // and that's why you should have a function "Lattice sum()" | |
| // that sums all the elements in a lattice and returns a WRD lattice. | |
| Iterator<Lattice> itr2 = secondLat.vector.iterator(); | |
| Lattice second = itr2.next(); | |
| if(second != null) | |
| { | |
| retVal = this.operationTemplate(second, op); // do the op for this and the first element of second lattice | |
| } | |
| while ( itr2.hasNext() ) | |
| { | |
| second = itr2.next(); | |
| retVal = retVal.operationTemplate(second, op) | |
| } | |
| } | |
| else if( this.type = OR & secondLat.type == WRD )//this is the reverse of the above | |
| { | |
| Iterator<Lattice> itr1 = this.vector.iterator(); | |
| Lattice first = itr1.next(); | |
| if(first != null) | |
| { | |
| retVal = secondLat.operationTemplate(first, op); // do the op for this and the first element of second lattice | |
| } | |
| while ( itr1.hasNext() ) | |
| { | |
| first = itr1.next(); | |
| retVal = retVal.operationTemplate(first, op) | |
| } | |
| } | |
| else if( this.type = WRD & secondLat.type == SEQ ) // do we need the reverse of this? | |
| { | |
| //wrd + seq: we have to promote wrd to seq (again) ===> (wrd) == (wrd; epsilon; epsilon; ...; epsilon) to be of the same size | |
| //x + (a; b; c) = (x; epsilon; epsilon) + (a; b; c) = (x + a; b; c) // a + x is WRD + WRD | |
| Iterator<Lattice> itr2 = secondLat.vector.iterator(); | |
| Lattice temp itr2.next(); | |
| retVal = this.operationTemplate(temp,op) | |
| while ( itr2.hasNext() ) | |
| { | |
| Lattice second = itr2.next(); | |
| retVal.addElement(second); | |
| } | |
| } | |
| else if( this.type = SEQ & secondLat.type == WRD ) // reverse of above | |
| { | |
| //wrd + seq: we have to promote wrd to seq (again) ===> (wrd) == (wrd; epsilon; epsilon; ...; epsilon) to be of the same size | |
| //x + (a; b; c) = (x; epsilon; epsilon) + (a; b; c) = (x + a; b; c) // a + x is WRD + WRD | |
| Iterator<Lattice> itr1 = this.vector.iterator(); | |
| Lattice temp itr1.next(); | |
| retVal = this.operationTemplate(temp,op) | |
| while ( secondLat.hasNext() ) | |
| { | |
| Lattice first = itr1.next(); | |
| retVal.addElement(first); | |
| } | |
| } | |
| else if( this.type == SEQ & secondLat.type == SEQ ) | |
| { | |
| // seq + seq: | |
| //(a; b; c) + (x; y; z) = (a+x; b+y; c+z) | |
| //(a; b; c) + (x; y) = (a+x; b+y; c) | |
| //(a; b) + (x; y; z) = (a+x; b+y; z) | |
| Iterator<Lattice> itr1 = this.vector.iterator(); | |
| Iterator<Lattice> itr2 = secondLat.vector.iterator(); | |
| while ( itr1.hasNext() | itr2.hasNext() ) | |
| { | |
| Lattice first = itr1.next(); | |
| Lattice second = itr2.next(); | |
| if(first != null & second != null ) | |
| { | |
| retVal.vector.addElement(op.process(first,second)); | |
| } | |
| else if( first == null ) | |
| { | |
| retVal.vector.addElement(second); | |
| } | |
| else if( second == null ) | |
| { | |
| retVal.vector.addElement(first); | |
| } | |
| } | |
| } | |
| else if( this.type == OR & secondLat.type == OR ) | |
| { | |
| // alt1 + alt2: | |
| // Lattice resultLat = new Lattice(Lattice.OR); | |
| // Lattice a2sum = alt2.sum(); // returns a wrd lattice that is the sum of all elements. You don't need two for loops any more | |
| // foreach a1 in alt1: | |
| // resultLat.vector.add(a1.plusIntInt(a2sum)); | |
| // return resultLat; | |
| Lattice sum = this.sumTemplate(secondLat,op); | |
| Iterator<Lattice> itr1 = this.vector.iterator(); | |
| while ( itr1.hasNext() ) | |
| { | |
| Lattice first = itr1.next(); // we get the rhs first value | |
| retVall.vector.add(op.process(first,sum)); | |
| } | |
| } | |
| else if( this.type == OR & secondLat.type == SEQ ) // the first one defines the behavior | |
| { | |
| //alt + seq: we have to promote seq to alt ==> (seq) == (seq | epsilon | epsilon | ... | epsilon) | |
| //(x | y | z) + (a; b; c) = (x | y | z) + ((a; b; c) | epsilon | epsilon) = (x + (a; b; c) | y | z) // now it is wrd + seq above | |
| Iterator<Lattice> itr1 = this.vector.iterator(); | |
| Iterator<Lattice> itr2 = secondLat.vector.iterator(); | |
| Lattice second = itr2.next(); | |
| Lattice first = itr1.next() | |
| retVal.type = OR; | |
| retVal = second.operationTemplate(this, op); //a+(x|y|z) | |
| while( itr2.hasNext() ) | |
| { | |
| second = itr2.next(); | |
| retVal.vector.add( second ); | |
| } | |
| } | |
| else if( this.type ==SEQ & secondLat.type == OR ) // the first one defines the behavior | |
| { | |
| // seq + alt: we have to promote alt to seq ==> (alt) == (alt; epsilon; epsilon; ...; epsilon) to be of the same size | |
| //(a; b; c) + (x | y | z) = (a; b; c) + ((x | y | z); epsilon; epsilon) = (a + (x | y | z); b; c) // a + (x | y | z) is defined in WRD + OR | |
| Iterator<Lattice> itr1 = this.vector.iterator(); | |
| Iterator<Lattice> itr2 = secondLat.vector.iterator(); | |
| Lattice second = itr2.next(); | |
| Lattice first = itr1.next() | |
| retVal.type = SEQ; | |
| retVal = first.operationTemplate(second, op); //a+(x|y|z) | |
| while( itr1.hasNext() ) | |
| { | |
| first = itr1.next(); | |
| retVal.vector.add( first ); | |
| } | |
| } | |
| return retVal; | |
| } | |
| /*****************************************************************************************/ | |
| final public Lattice sumTemplate(Lattice lattice, LatticeOperation op) | |
| { | |
| // apply the operation to each element in the lattice | |
| Lattice retVal; | |
| Iterator<Lattice> iter = lattice.vector.iterator(); | |
| Lattice temp; | |
| while( iter.hasNext() ) | |
| { | |
| temp = iter.next();// this is left to right evaluation | |
| op.process(retVal,temp); | |
| } | |
| return retVal; | |
| } | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment