拆分重叠范围

时间:2017-09-26 14:50:59

标签: javascript colors split overlapping

我已经实现了一种算法,用于将重叠的现有范围划分为日期/数字范围列表。

算法正在运行,但我想知道你是否可以避免算法的最后一个循环。

有可能吗?

Input Data

0-100(红色) 90-150(绿色) 90-150(蓝色) 140-300(黄色) 170-240(黑色) 350-530(橙色) 50-500(银) 50-60(粉红色)

Output Data

0-49(红色) 50-60(红色,银色,粉色) 61-89(红色,银色) 90-100(红,绿,蓝,银) 101-139(绿,蓝,银) 140-150(绿,蓝,黄,银) 151-169(黄色,银) 170-240(黄色,黑色,银色) 241-300(黄色,银) 301-349(银) 350-500(橙,银) 501-530(橙色)

Javascript代码:

    function splitRanges(original_intervals) {
    
        for (var to = [], from = [], n, i = original_intervals.length; i--;) {
            if (to.indexOf(n = original_intervals[i].to) < 0)
                to.push(n);
            if (from.indexOf(n = original_intervals[i].from) < 0)
                from.push(n);
        }
    
    
        to.sort(function(a, b) {
            return a - b;
        });
        from.sort(function(a, b) {
            return a - b;
        });
    
    
    
    
        var intervals = [];
        while (to.length) {
            var sFrom = from.shift();
            var sTo = 0;
            if (from.length == 0) {
                sTo = (from.push((n = to.shift()) + 1), n);
    
            } else {
                if (from[0] > to[0]) {
                    while (to[0] < from[0]) {
    
                        from.unshift(to[0] + 1);
                        to.shift();
    
                    }
    
                    sTo = from[0] - 1;
                } else {
                    sTo = from[0] - 1;
    
                }
            }
            intervals.push({
                from: sFrom,
                to: sTo,
                colors: []
            });
        }
    
        /***********************Loop that i want remove*/
    
        intervals.forEach(function(item, index) {
            original_intervals.forEach(function(item1, index1) {
                if ((item.from >= item1.from && item.from <= item1.to) || (item.to >= item1.from && item.to <= item1.to))
                    item.colors.push(item1.color);
            });
    
        });
    
        return intervals;
    }
    
    
    
    
    var r1 = [{
    id: 1,
    from: 0,
    to: 100,
	color:'red'
}, {
    id: 2,
    from: 90,
    to: 150,
	color:'green'
}, {
    id: 3,
    from: 90,
    to: 150,
	color:'blue'
}, {
    id: 4,
    from: 140,
    to: 300,
	color:'yellow'
}, {
    id: 5,
    from: 170,
    to: 240,
	color:'black'
}, {
    id: 6,
    from: 350,
    to: 530,
	color:'orange'
}, {
    id: 7,
    from: 50,
    to: 500,
	color:'silver'
}
, {
    id: 8,
    from: 50,
    to: 60,
	color:'pink'
}

];


console.log(splitRanges(r1));
    
    
    

1 个答案:

答案 0 :(得分:0)

你需要一些迭代,至少是获得所有范围点的迭代,然后从中生成一个数组并为每个小间隔获取颜色子集。

public ResponseEntity<?> create(
    @RequestBody AttributeDTO policy,
    BindingResult bindingResult)
{
    logger.debug("CreatePolicy=" + policy);
    ...
var data = [{ from: 0, to: 100, color: 'red' }, { from: 90, to: 150, color: 'green' }, { from: 90, to: 150, color: 'blue' }, { from: 140, to: 300, color: 'yellow' }, { from: 170, to: 240, color: 'black' }, { from: 350, to: 530, color: 'orange' }, { from: 50, to: 500, color: 'silver' }, { from: 50, to: 60, color: 'pink' }],
    ranges = new Set,
    parts,
    result;

data.forEach(({ from, to }) => (ranges.add(from), ranges.add(to)));
parts = [...ranges].sort((a, b) => a - b);
result = parts.slice(1).map(function (a, i, aa) {
    var from = i ? aa[i - 1] : parts[0],
        to = a,
        colors = data.filter(d => d.from <= from && to <= d.to).map(({ color }) => color);
    return { from, to, colors };
});
    
console.log(result);