added Map struct
This commit is contained in:
		
							parent
							
								
									7a274565e5
								
							
						
					
					
						commit
						8f63147dbc
					
				@ -29,25 +29,31 @@ var Struct = {
 | 
				
			|||||||
    }
 | 
					    }
 | 
				
			||||||
  },
 | 
					  },
 | 
				
			||||||
  Insert: {
 | 
					  Insert: {
 | 
				
			||||||
    create: function*( op: Op,
 | 
					    /*{
 | 
				
			||||||
                      content: any,
 | 
					        content: any,
 | 
				
			||||||
                      left: Insert,
 | 
					        left: Id,
 | 
				
			||||||
                      right: Insert,
 | 
					        right: Id,
 | 
				
			||||||
                      parent: List) : Insert {
 | 
					        parent: Id,
 | 
				
			||||||
      op.left = left ? left.id : null;
 | 
					        parentSub: string (optional)
 | 
				
			||||||
 | 
					      }
 | 
				
			||||||
 | 
					    */
 | 
				
			||||||
 | 
					    create: function*( op: Op ) : Insert {
 | 
				
			||||||
 | 
					      if ( op.left === undefined
 | 
				
			||||||
 | 
					        || op.right === undefined
 | 
				
			||||||
 | 
					        || op.parent === undefined ) {
 | 
				
			||||||
 | 
					          throw new Error("You must define left, right, and parent!");
 | 
				
			||||||
 | 
					        }
 | 
				
			||||||
      op.origin = op.left;
 | 
					      op.origin = op.left;
 | 
				
			||||||
      op.right = right ? right.id : null;
 | 
					 | 
				
			||||||
      op.parent = parent.id;
 | 
					 | 
				
			||||||
      op.struct = "Insert";
 | 
					      op.struct = "Insert";
 | 
				
			||||||
      yield* Struct.Operation.create.call(this, op);
 | 
					      yield* Struct.Operation.create.call(this, op);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
      if (left != null) {
 | 
					      if (op.left != null) {
 | 
				
			||||||
        left.right = op.id;
 | 
					        op.left.right = op.id;
 | 
				
			||||||
        yield* this.setOperation(left);
 | 
					        yield* this.setOperation(op.left);
 | 
				
			||||||
      }
 | 
					      }
 | 
				
			||||||
      if (right != null) {
 | 
					      if (op.right != null) {
 | 
				
			||||||
        right.left = op.id;
 | 
					        op.right.left = op.id;
 | 
				
			||||||
        yield* this.setOperation(right);
 | 
					        yield* this.setOperation(op.right);
 | 
				
			||||||
      }
 | 
					      }
 | 
				
			||||||
      return op;
 | 
					      return op;
 | 
				
			||||||
    },
 | 
					    },
 | 
				
			||||||
@ -113,16 +119,42 @@ var Struct = {
 | 
				
			|||||||
          break;
 | 
					          break;
 | 
				
			||||||
        }
 | 
					        }
 | 
				
			||||||
      }
 | 
					      }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
      // reconnect..
 | 
					      // reconnect..
 | 
				
			||||||
      var left = this.getOperation(op.left);
 | 
					      var left = null;
 | 
				
			||||||
      var right = this.getOperation(op.right);
 | 
					      var right = null;
 | 
				
			||||||
      left.right = op.id;
 | 
					      if (op.left != null) {
 | 
				
			||||||
      right.left = op.id;
 | 
					        left = this.getOperation(op.left);
 | 
				
			||||||
 | 
					        left.right = op.id;
 | 
				
			||||||
 | 
					        yield* this.setOperation(left);
 | 
				
			||||||
 | 
					      }
 | 
				
			||||||
 | 
					      if (op.right != null) {
 | 
				
			||||||
 | 
					        right = this.getOperation(op.right);
 | 
				
			||||||
 | 
					        right.left = op.id;
 | 
				
			||||||
 | 
					        yield* this.setOperation(right);
 | 
				
			||||||
 | 
					      }
 | 
				
			||||||
      op.left = left;
 | 
					      op.left = left;
 | 
				
			||||||
      op.right = right;
 | 
					      op.right = right;
 | 
				
			||||||
      yield* this.setOperation(left);
 | 
					 | 
				
			||||||
      yield* this.setOperation(right);
 | 
					 | 
				
			||||||
      yield* this.setOperation(op);
 | 
					      yield* this.setOperation(op);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					      // notify parent
 | 
				
			||||||
 | 
					      var parent = this.getOperation(op.parent);
 | 
				
			||||||
 | 
					      if (op.parentSub != null) {
 | 
				
			||||||
 | 
					        if (right == null) {
 | 
				
			||||||
 | 
					          parent.map[op.parentSub] = op.id;
 | 
				
			||||||
 | 
					          yield* this.setOperation(parent);
 | 
				
			||||||
 | 
					        }
 | 
				
			||||||
 | 
					      } else {
 | 
				
			||||||
 | 
					        if (right == null || left == null) {
 | 
				
			||||||
 | 
					          if (right == null) {
 | 
				
			||||||
 | 
					            parent.end = op.id;
 | 
				
			||||||
 | 
					          }
 | 
				
			||||||
 | 
					          if (left == null) {
 | 
				
			||||||
 | 
					            parent.start = op.id;
 | 
				
			||||||
 | 
					          }
 | 
				
			||||||
 | 
					          yield* this.setOperation(parent);
 | 
				
			||||||
 | 
					        }
 | 
				
			||||||
 | 
					      }
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
  },
 | 
					  },
 | 
				
			||||||
  List: {
 | 
					  List: {
 | 
				
			||||||
@ -166,53 +198,48 @@ var Struct = {
 | 
				
			|||||||
      var o = yield* Struct.List.ref.call(this, op, pos);
 | 
					      var o = yield* Struct.List.ref.call(this, op, pos);
 | 
				
			||||||
      var or = yield* this.getOperation(o.right);
 | 
					      var or = yield* this.getOperation(o.right);
 | 
				
			||||||
      for (var content of contents) {
 | 
					      for (var content of contents) {
 | 
				
			||||||
        o = yield* Struct.Insert.create.call(this, {}, content, o, or, op);
 | 
					        var insert = {
 | 
				
			||||||
 | 
					          left: o,
 | 
				
			||||||
 | 
					          right: or,
 | 
				
			||||||
 | 
					          content: content,
 | 
				
			||||||
 | 
					          parent: op
 | 
				
			||||||
 | 
					        };
 | 
				
			||||||
 | 
					        o = yield* Struct.Insert.create.call(this, insert);
 | 
				
			||||||
      }
 | 
					      }
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
  },
 | 
					  },
 | 
				
			||||||
  Map: {
 | 
					  Map: {
 | 
				
			||||||
    create: function*( op : Op){
 | 
					    create: function*( op : Op ){
 | 
				
			||||||
      op.start = null;
 | 
					      op.map = {};
 | 
				
			||||||
      op.end = null;
 | 
					 | 
				
			||||||
      op.struct = "Map";
 | 
					      op.struct = "Map";
 | 
				
			||||||
      return yield* Struct.Operation.create.call(this, op);
 | 
					      return yield* Struct.Operation.create.call(this, op);
 | 
				
			||||||
    },
 | 
					    },
 | 
				
			||||||
    requiredOps: function(op, ids){
 | 
					    requiredOps: function(op, ids){
 | 
				
			||||||
      if (op.start != null) {
 | 
					      for (var end of op.map) {
 | 
				
			||||||
        ids.push(op.start);
 | 
					        ids.push(end);
 | 
				
			||||||
      }
 | 
					 | 
				
			||||||
      if (op.end != null){
 | 
					 | 
				
			||||||
        ids.push(op.end);
 | 
					 | 
				
			||||||
      }
 | 
					      }
 | 
				
			||||||
      return ids;
 | 
					      return ids;
 | 
				
			||||||
    },
 | 
					    },
 | 
				
			||||||
    execute: function* () {
 | 
					    execute: function* () {
 | 
				
			||||||
      // nop
 | 
					      // nop
 | 
				
			||||||
    },
 | 
					    },
 | 
				
			||||||
    ref: function* (op : Op, pos : number) : Insert {
 | 
					    get: function* (op, name) {
 | 
				
			||||||
      var o = op.start;
 | 
					      return yield* this.getOperation(op.map[name].end);
 | 
				
			||||||
      while ( pos !== 0 || o != null) {
 | 
					 | 
				
			||||||
        o = (yield* this.getOperation(o)).right;
 | 
					 | 
				
			||||||
        pos--;
 | 
					 | 
				
			||||||
      }
 | 
					 | 
				
			||||||
      return (o == null) ? null : yield* this.getOperation(o);
 | 
					 | 
				
			||||||
    },
 | 
					    },
 | 
				
			||||||
    map: function* (o : Op, f : Function) : Array<any> {
 | 
					    set: function* (op, name, value) {
 | 
				
			||||||
      o = o.start;
 | 
					      var end = op.map[name];
 | 
				
			||||||
      var res = [];
 | 
					      if (end == null) {
 | 
				
			||||||
      while ( o != null) {
 | 
					        end = null;
 | 
				
			||||||
        var operation = yield* this.getOperation(o);
 | 
					        op.map[name] = end;
 | 
				
			||||||
        res.push(f(operation.content));
 | 
					 | 
				
			||||||
        o = operation.right;
 | 
					 | 
				
			||||||
      }
 | 
					 | 
				
			||||||
      return res;
 | 
					 | 
				
			||||||
    },
 | 
					 | 
				
			||||||
    insert: function* (op, pos : number, contents : Array<any>) {
 | 
					 | 
				
			||||||
      var o = yield* Struct.List.ref.call(this, op, pos);
 | 
					 | 
				
			||||||
      var or = yield* this.getOperation(o.right);
 | 
					 | 
				
			||||||
      for (var content of contents) {
 | 
					 | 
				
			||||||
        o = yield* Struct.Insert.create.call(this, {}, content, o, or, op);
 | 
					 | 
				
			||||||
      }
 | 
					      }
 | 
				
			||||||
 | 
					      var insert = {
 | 
				
			||||||
 | 
					        left: end,
 | 
				
			||||||
 | 
					        right: null,
 | 
				
			||||||
 | 
					        content: value,
 | 
				
			||||||
 | 
					        parent: op.id,
 | 
				
			||||||
 | 
					        parentSub: name
 | 
				
			||||||
 | 
					      };
 | 
				
			||||||
 | 
					      yield* Struct.Insert.create.call(this, insert);
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
  }
 | 
					  }
 | 
				
			||||||
};
 | 
					};
 | 
				
			||||||
		Loading…
	
	
			
			x
			
			
		
	
		Reference in New Issue
	
	Block a user