Example #1
0
        /**
         * An intersection of a type predicate and a predicate with a known type is
         * equivalent to the known type predicate.
         */

        private void RemoveRedundantTypeIntersections(ArrayList newPredicateList, ref bool optimized)
        {
            int knownTypeId = -1;

            for (int i = 0; i < newPredicateList.Count; i++)
            {
                ResourceListPredicate predicate = (ResourceListPredicate)newPredicateList [i];
                if (!(predicate.HasAnyTypePredicate()))
                {
                    int predKnownTypeId = predicate.GetKnownType();
                    if (knownTypeId == -1)
                    {
                        knownTypeId = predKnownTypeId;
                    }
                    else if (predKnownTypeId != knownTypeId)
                    {
                        return;
                    }
                }
            }

            if (knownTypeId != -1)
            {
                for (int i = newPredicateList.Count - 1; i >= 0; i--)
                {
                    if (((ResourceListPredicate)newPredicateList [i]).HasTypePredicate(knownTypeId))
                    {
                        newPredicateList.RemoveAt(i);
                        optimized = true;
                    }
                }
            }
        }
Example #2
0
 internal void Instantiate(bool optimize)
 {
     if (_list == null)
     {
         ResourceListPredicate oldPredicate = _predicate;
         if (optimize)
         {
             _predicate = _predicate.Optimize(_isLive);
         }
         if (MyPalStorage.TraceOperations && !(_predicate is PlainListPredicate))
         {
             if (!Object.ReferenceEquals(oldPredicate, _predicate))
             {
                 Trace.WriteLine("Predicate before optimization: " + oldPredicate.ToString());
             }
             Trace.WriteLine("Instantiating list " + _predicate.ToString());
         }
         bool predicateSortedById = false;
         _list = _predicate.GetMatchingResources(out predicateSortedById);
         if (_isLive && !_handlersAttached)
         {
             SetLive();
         }
         if (_lastComparer != null)
         {
             DoSort(_lastComparer, true);
         }
         else if (predicateSortedById)
         {
             _lastComparer = new ResourceComparer(this, new SortSettings(ResourceProps.Id, true), false);
         }
     }
 }
Example #3
0
        /**
         * Optimizes the predicate to make its calculation more efficient.
         */

        internal override ResourceListPredicate Optimize(bool isLive)
        {
            bool      optimized        = false;
            ArrayList newPredicateList = ArrayListPool.Alloc();

            try
            {
                ExpandIntersections(newPredicateList, ref optimized);
                RemoveEqualSources(newPredicateList, ref optimized);
                RemoveIntersectingUnions(newPredicateList, ref optimized);
                RemoveRedundantTypeIntersections(newPredicateList, ref optimized);
                ResourceListPredicate result = this;
                if (optimized)
                {
                    if (newPredicateList.Count == 1)
                    {
                        result = (ResourceListPredicate)newPredicateList [0];
                    }
                    else
                    {
                        _sourcePredicates = (ResourceListPredicate[])newPredicateList.ToArray(
                            typeof(ResourceListPredicate));
                    }
                }
                return(OptimizeSourcePredicates(result, isLive));
            }
            finally
            {
                ArrayListPool.Dispose(newPredicateList);
            }
        }
Example #4
0
        internal override IntArrayList GetMatchingResources(out bool sortedById)
        {
            sortedById = false;

            IntArrayList result = null;

            for (int i = 0; i < _resTypeIds.Length; i++)
            {
                ResourceListPredicate basePredicate   = new ResourceTypePredicate(_resTypeIds [i]);
                ResourceListPredicate cachedPredicate = MyPalStorage.Storage.GetCachedPredicate(basePredicate);

                bool         tempSortedById = false;
                object       syncObject     = null;
                IntArrayList tempResult;
                if (cachedPredicate != null)
                {
                    tempResult = cachedPredicate.GetSortedMatchingResourcesRef(out syncObject);
                }
                else
                {
                    tempResult = basePredicate.GetMatchingResources(out tempSortedById);
                    syncObject = null;
                }
                if (result == null)
                {
                    if (syncObject == null)
                    {
                        result = tempResult;
                    }
                    else
                    {
                        lock ( syncObject )
                        {
                            result = (IntArrayList)tempResult.Clone();
                        }
                    }
                }
                else
                {
                    if (syncObject == null)
                    {
                        result.AddRange(tempResult);
                    }
                    else
                    {
                        lock ( syncObject )
                        {
                            result.AddRange(tempResult);
                        }
                    }
                }
            }
            if (result == null)
            {
                result = new IntArrayList();
            }
            return(result);
        }
Example #5
0
        protected ResourceListPredicate[] AddPredicate(ResourceListPredicate[] list, ResourceListPredicate pred)
        {
            int len = list.Length;

            ResourceListPredicate[] newList = new ResourceListPredicate [len + 1];
            Array.Copy(list, 0, newList, 0, len);
            newList [len] = pred;
            return(newList);
        }
Example #6
0
        internal virtual ResourceListPredicate Optimize(bool isLive)
        {
            ResourceListPredicate predicate = MyPalStorage.Storage.GetCachedPredicate(this);

            if (predicate != null)
            {
                return(predicate);
            }
            return(this);
        }
Example #7
0
        /**
         * Checks if one of the predicates in the intersection is equal to the
         * specified predicate.
         */

        internal bool ContainsPredicate(ResourceListPredicate pred)
        {
            for (int i = 0; i < _sourcePredicates.Length; i++)
            {
                if (_sourcePredicates [i].Equals(pred))
                {
                    return(true);
                }
            }
            return(false);
        }
Example #8
0
 internal void AddSource(ResourceListPredicate pred)
 {
     lock (this)
     {
         if (_sourcePredicatesSelective != null)
         {
             if (pred.GetSelectionCost() <= _minSelectionCost)
             {
                 _sourcePredicatesSelective = AddPredicate(_sourcePredicatesSelective, pred);
             }
             else
             {
                 _sourcePredicatesFiltering = AddPredicate(_sourcePredicatesFiltering, pred);
             }
         }
         _sourcePredicates = AddPredicate(_sourcePredicates, pred);
     }
 }
Example #9
0
        protected ResourceListPredicate OptimizeSourcePredicates(ResourceListPredicate result, bool isLive)
        {
            if (result == this)
            {
                for (int i = 0; i < _sourcePredicates.Length; i++)
                {
                    _sourcePredicates [i] = _sourcePredicates [i].Optimize(isLive);
                }
            }
            else
            {
                result = result.Optimize(isLive);
            }

            ResourceListPredicate predicate = MyPalStorage.Storage.GetCachedPredicate(result);

            if (predicate != null)
            {
                return(predicate);
            }
            return(result);
        }
Example #10
0
        internal override ResourceListPredicate Optimize(bool isLive)
        {
            ResourceListPredicate result = this;

            if (isLive)
            {
                int plainListCount = 0;
                for (int i = 0; i < _sourcePredicates.Length; i++)
                {
                    PlainListPredicate plainList = _sourcePredicates [i] as PlainListPredicate;
                    if (plainList != null)
                    {
                        plainListCount++;
                        if (plainListCount > 1 || plainList.Count == 0)
                        {
                            result = OptimizePlainLists();
                        }
                    }
                }
            }

            return(OptimizeSourcePredicates(result, isLive));
        }
Example #11
0
 internal override ResourceListPredicate Optimize( bool isLive )
 {
     _lhs = _lhs.Optimize( isLive );
     _rhs = _rhs.Optimize( isLive );
     return base.Optimize( isLive );
 }
Example #12
0
        internal MinusPredicate( ResourceListPredicate lhs, ResourceListPredicate rhs )
		{
            _lhs = lhs;
            _rhs = rhs;
		}
Example #13
0
 internal void AddSource(ResourceListPredicate pred)
 {
     _sourcePredicates = AddPredicate(_sourcePredicates, pred);
 }
Example #14
0
 protected internal ResourceList(ResourceListPredicate predicate, bool live)
 {
     _predicate = predicate;
     _isLive    = live;
 }