# Path: crumbs/collectionz.py
# class OrderedSet(object):
# 
#     def __init__(self, ordered_set=None):
#         if ordered_set is None:
#             ordered_set = []
#         self._items = ordered_set
# 
#     def check_add(self, item):
#         index = bisect(self._items, item)
#         if index == 0:
#             self._items.insert(0, item)
#             return True
#         else:
#             present = True if item == self._items[index - 1] else False
#             if present:
#                 return False
#             else:
#                 self._items.insert(index, item)
#                 return True
# 
#     def __contains__(self, item):
#         index = bisect(self._items, item)
#         if index == 0:
#             return False
#         return True if item == self._items[index - 1] else False
# 
#     def __len__(self):
#         return len(self._items)
# 
# class KeyedSet(object):
# 
#     def __init__(self, items=None, key=None):
#         if items is None:
#             items = set()
#         elif key:
#             items = set(map(key, items))
#         else:
#             items = set(items)
#         self._items = items
#         self._len = len(self._items)
#         self._key = key
# 
#     def check_add(self, item):
#         item_key = self._key(item) if self._key else item
#         self._items.add(item_key)
#         if len(self._items) > self._len:
#             self._len = len(self._items)
#             return True
#         else:
#             return False
# 
#     def __contains__(self, item):
#         item_key = self._key(item) if self._key else item
#         return item_key in self._items
# 
#     def __len__(self):
#         return len(self._items)
# 
# class RecentlyAddedCache(OrderedDict):
#     def __init__(self, max_size, *args, **kwargs):
# 
#         self.max_size = max_size
#         super(RecentlyAddedCache, self).__init__(*args, **kwargs)
# 
#     def __setitem__(self, key, value, dict_setitem=dict.__setitem__):
#         super(RecentlyAddedCache, self).__setitem__(key, value,
#                                                     dict_setitem=dict_setitem)
#         if len(self) > self.max_size:
#             self.popitem(last=False)


import unittest
from crumbs.collectionz import OrderedSet, KeyedSet, RecentlyAddedCache
        assert not ordered_set.check_add(2)
        assert ordered_set._items == [1, 2, 3, 4, 5, 7, 8, 10]
        assert ordered_set.check_add(0)
        assert ordered_set._items == [0, 1, 2, 3, 4, 5, 7, 8, 10]
        for item in not_in_list:
            assert item not in ordered_set

    def test_unordered_set(self):
        in_set = [1, 2, 3, 4, 5, 8, 10]
        not_in_set = [6, 9, 11, 13]
        keyed_set = KeyedSet(in_set)
        for item in in_set:
            assert item in keyed_set
        assert keyed_set.check_add(7)
        assert keyed_set._items == set([1, 2, 3, 4, 5, 7, 8, 10])
        assert not keyed_set.check_add(2)
        assert keyed_set._items == set([1, 2, 3, 4, 5, 7, 8, 10])
        assert keyed_set.check_add(0)
        assert keyed_set._items == set([0, 1, 2, 3, 4, 5, 7, 8, 10])

        for item in not_in_set:
            assert item not in keyed_set

        #with key
        a = 'a'
        in_set = [(1, a), (2, a), (3, a), (4, a), (5, a),  (8, a), (10, a)]
        not_in_set = [(6, a), (9, a), (11, a), (13, a)]

        def key(item):
            return item[0]

# Path: crumbs/collectionz.py
# class OrderedSet(object):
# 
#     def __init__(self, ordered_set=None):
#         if ordered_set is None:
#             ordered_set = []
#         self._items = ordered_set
# 
#     def check_add(self, item):
#         index = bisect(self._items, item)
#         if index == 0:
#             self._items.insert(0, item)
#             return True
#         else:
#             present = True if item == self._items[index - 1] else False
#             if present:
#                 return False
#             else:
#                 self._items.insert(index, item)
#                 return True
# 
#     def __contains__(self, item):
#         index = bisect(self._items, item)
#         if index == 0:
#             return False
#         return True if item == self._items[index - 1] else False
# 
#     def __len__(self):
#         return len(self._items)
# 
# class KeyedSet(object):
# 
#     def __init__(self, items=None, key=None):
#         if items is None:
#             items = set()
#         elif key:
#             items = set(map(key, items))
#         else:
#             items = set(items)
#         self._items = items
#         self._len = len(self._items)
#         self._key = key
# 
#     def check_add(self, item):
#         item_key = self._key(item) if self._key else item
#         self._items.add(item_key)
#         if len(self._items) > self._len:
#             self._len = len(self._items)
#             return True
#         else:
#             return False
# 
#     def __contains__(self, item):
#         item_key = self._key(item) if self._key else item
#         return item_key in self._items
# 
#     def __len__(self):
#         return len(self._items)
# 
# class RecentlyAddedCache(OrderedDict):
#     def __init__(self, max_size, *args, **kwargs):
# 
#         self.max_size = max_size
#         super(RecentlyAddedCache, self).__init__(*args, **kwargs)
# 
#     def __setitem__(self, key, value, dict_setitem=dict.__setitem__):
#         super(RecentlyAddedCache, self).__setitem__(key, value,
#                                                     dict_setitem=dict_setitem)
#         if len(self) > self.max_size:
#             self.popitem(last=False)


# Path: test/test_collectionz.py
import unittest
from crumbs.collectionz import OrderedSet, KeyedSet, RecentlyAddedCache

        assert not ordered_set.check_add(2)
        assert ordered_set._items == [1, 2, 3, 4, 5, 7, 8, 10]
        assert ordered_set.check_add(0)
        assert ordered_set._items == [0, 1, 2, 3, 4, 5, 7, 8, 10]
        for item in not_in_list:
            assert item not in ordered_set

    def test_unordered_set(self):
        in_set = [1, 2, 3, 4, 5, 8, 10]
        not_in_set = [6, 9, 11, 13]
        keyed_set = KeyedSet(in_set)
        for item in in_set:
            assert item in keyed_set
        assert keyed_set.check_add(7)
        assert keyed_set._items == set([1, 2, 3, 4, 5, 7, 8, 10])
        assert not keyed_set.check_add(2)
        assert keyed_set._items == set([1, 2, 3, 4, 5, 7, 8, 10])
        assert keyed_set.check_add(0)
        assert keyed_set._items == set([0, 1, 2, 3, 4, 5, 7, 8, 10])

        for item in not_in_set:
            assert item not in keyed_set

        #with key
        a = 'a'
        in_set = [(1, a), (2, a), (3, a), (4, a), (5, a),  (8, a), (10, a)]
        not_in_set = [(6, a), (9, a), (11, a), (13, a)]

        def key(item):
            return item[0]

        keyed_set = KeyedSet(in_set, key=key)