define(function(a,b,c){"use strict";var d=a("./range").Range,e=d.comparePoints,f=function(){this.ranges=[]};(function(){this.comparePoints=e,this.pointIndex=function(a,b,c){for(var d=this.ranges,f=c||0;f<d.length;f++){var g=d[f],h=e(a,g.end);if(!(h>0)){var i=e(a,g.start);return 0===h?b&&0!==i?-f-2:f:i>0||0===i&&!b?f:-f-1}}return-f-1},this.add=function(a){var b=!a.isEmpty(),c=this.pointIndex(a.start,b);c<0&&(c=-c-1);var d=this.pointIndex(a.end,b,c);return d<0?d=-d-1:d++,this.ranges.splice(c,d-c,a)},this.addList=function(a){for(var b=[],c=a.length;c--;)b.push.apply(b,this.add(a[c]));return b},this.substractPoint=function(a){var b=this.pointIndex(a);if(b>=0)return this.ranges.splice(b,1)},this.merge=function(){var a=[],b=this.ranges;b=b.sort(function(a,b){return e(a.start,b.start)});for(var d,c=b[0],f=1;f<b.length;f++){d=c,c=b[f];var g=e(d.end,c.start);g<0||(0!=g||d.isEmpty()||c.isEmpty())&&(e(d.end,c.end)<0&&(d.end.row=c.end.row,d.end.column=c.end.column),b.splice(f,1),a.push(c),c=d,f--)}return this.ranges=b,a},this.contains=function(a,b){return this.pointIndex({row:a,column:b})>=0},this.containsPoint=function(a){return this.pointIndex(a)>=0},this.rangeAtPoint=function(a){var b=this.pointIndex(a);if(b>=0)return this.ranges[b]},this.clipRows=function(a,b){var c=this.ranges;if(c[0].start.row>b||c[c.length-1].start.row<a)return[];var d=this.pointIndex({row:a,column:0});d<0&&(d=-d-1);var e=this.pointIndex({row:b,column:0},d);e<0&&(e=-e-1);for(var f=[],g=d;g<e;g++)f.push(c[g]);return f},this.removeAll=function(){return this.ranges.splice(0,this.ranges.length)},this.attach=function(a){this.session&&this.detach(),this.session=a,this.onChange=this.$onChange.bind(this),this.session.on("change",this.onChange)},this.detach=function(){this.session&&(this.session.removeListener("change",this.onChange),this.session=null)},this.$onChange=function(a){if("insert"==a.action)var b=a.start,c=a.end;else var c=a.start,b=a.end;for(var d=b.row,e=c.row,f=e-d,g=-b.column+c.column,h=this.ranges,i=0,j=h.length;i<j;i++){var k=h[i];if(!(k.end.row<d)){if(k.start.row>d)break;if(k.start.row==d&&k.start.column>=b.column&&(k.start.column==b.column&&this.$insertRight||(k.start.column+=g,k.start.row+=f)),k.end.row==d&&k.end.column>=b.column){if(k.end.column==b.column&&this.$insertRight)continue;k.end.column==b.column&&g>0&&i<j-1&&k.end.column>k.start.column&&k.end.column==h[i+1].start.column&&(k.end.column-=g),k.end.column+=g,k.end.row+=f}}}if(0!=f&&i<j)for(;i<j;i++){var k=h[i];k.start.row+=f,k.end.row+=f}}}).call(f.prototype),b.RangeList=f});