PageRenderTime 149ms CodeModel.GetById 142ms app.highlight 5ms RepoModel.GetById 1ms app.codeStats 0ms

/historical/mergetools.py

https://bitbucket.org/lindenlab/apiary/
Python | 97 lines | 41 code | 13 blank | 43 comment | 6 complexity | e1e70932858200d2dd4a695b34302b0f MD5 | raw file
 1#
 2# $LicenseInfo:firstyear=2010&license=mit$
 3# 
 4# Copyright (c) 2010, Linden Research, Inc.
 5# 
 6# Permission is hereby granted, free of charge, to any person obtaining a copy
 7# of this software and associated documentation files (the "Software"), to deal
 8# in the Software without restriction, including without limitation the rights
 9# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10# copies of the Software, and to permit persons to whom the Software is
11# furnished to do so, subject to the following conditions:
12# 
13# The above copyright notice and this permission notice shall be included in
14# all copies or substantial portions of the Software.
15# 
16# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
22# THE SOFTWARE.
23# $/LicenseInfo$
24#
25
26"""mergetools - Merge together sorted sequences
27
28Functions:
29imerge - Return an iterator that produces the merge of the arguments
30merge - Return an array that is the merge of the arguments
31
32In both cases, the arguments can be zero or more iterable objects. Collections,
33iterators, and any object implementing the iterator protocol are acceptable.
34
35If the sequences being merged are not already in sorted order, then the result
36order is not well defined.
37
38Items in the sequences are compared using the < operator. Duplicate items are
39allowed, and will be returned as many times as they appear. If duplicate items
40appear in more than one of the input sequences, then they will appear in the
41output in the order of the input sequences.
42"""
43
44__all__ = [
45        'imerge',
46        'merge',
47    ]
48
49class _Feed(object):
50    def __init__(self, iterable):
51        self._iteration = iter(iterable)
52        self._head = None
53        self.advance()
54    
55    def empty(self):
56        return self._iteration is None
57    
58    def head(self):
59        return self._head
60        
61    def advance(self):
62        try:
63            self._head = self._iteration.next()
64        except StopIteration:
65            self._iteration = None
66            self._head = None
67
68    
69class _IMerge(object):
70    def __init__(self, iterables):
71        self._feeds = map(_Feed, iterables)
72    
73    def __iter__(self):
74        return self
75    
76    def next(self):
77        source = None
78        smallest = None
79        for f in self._feeds:
80            if f.empty():
81                continue
82            if source is None or f.head() < smallest:
83                source = f
84                smallest = f.head()
85        if source is None:
86            raise StopIteration
87        source.advance()
88        return smallest
89        
90
91def imerge(*iterables):
92    """Return an iterator that produces the merge of the arguments"""
93    return _IMerge(iterables)
94
95def merge(*iterables):
96    """Return an array that is the merge of the arguments"""
97    return map(None, _IMerge(iterables))