Ejemplo n.º 1
0
        // You'd think that Unity would be smart enough to NOT use this copy constructor when resolving an object with no parameters.
        // I guess the rule of thumb is DON'T use copy constructors when using an IoC container (or make them private since by default Unity can only see public constructors)
        private MovieList(MovieList toCopy)
            : this()
        {
            if (!ReferenceEquals(this, toCopy))
            {
                _movies = new List <IMovie>(toCopy.Movies);

                UpdateTotals(toCopy);
            }
        }
Ejemplo n.º 2
0
        //----==== PRIVATE ====---------------------------------------------------------

        private void UpdateTotals(MovieList toCopy = null, IMovie movieToAdd = null)
        {
            // "Greedy" method is not used (yet)
            // Keep the list sorted by efficiency so we don't need to always sort the list in the algoritm.

            //_movies.Sort((left, right) =>
            //{
            //	if (left.Efficiency == right.Efficiency)
            //	{
            //		return 0;
            //	}
            //	if (left.Efficiency < right.Efficiency)
            //	{
            //		return -1;
            //	}

            //	return 1;
            //});

            if (toCopy == null)
            {
                // Keep this list sorted by Id so the hashing won't care about the order added.

                //var orderedList = _movies.OrderBy(movie => movie.Id).ToList();

                //_movies.Clear();
                //_movies.AddRange(orderedList);

                _movies.Sort((left, right) =>
                {
                    if (left.Id == right.Id)
                    {
                        return(0);
                    }
                    if (left.Id < right.Id)
                    {
                        return(-1);
                    }

                    return(1);
                });

                //else
                //{
                //	// The movie has not been added yet.
                //	// Since the list is kept sorted, then use insertion instead of sorting the entire list.

                //	var found = _movies.FirstOrDefault(movie => movie.Id >= movieToAdd.Id);

                //	if (found == null)
                //	{
                //		_movies.Add(movieToAdd);
                //	}
                //	else
                //	{
                //		_movies.Insert(_movies.IndexOf(found), movieToAdd);
                //	}
                //}

                _totalCost     = 0;
                _totalEarnings = 0;
                _hashCode      = 19;
                _totalCount    = 0;

                // Only iterate the list once.

                foreach (var item in _movies)
                {
                    // Order does NOT matter for this hash code.
                    // The hash for the following lists will produce the same hash:
                    // 1,2,3,3,3
                    // 1,3,2,3,3
                    // 1,3,3,2,3
                    // 1,3,3,3,2

                    _hashCode = _hashCode * 31 + item.GetHashCode();

                    _totalCount++;
                    _totalCost     += item.Cost;
                    _totalEarnings += item.Earnings;
                }

                _totalEarnings -= (MOVIE_MAX - _totalCount) * MISSING_THEATER_EARNINGS;

                if (_totalEarnings < 0)
                {
                    _totalEarnings = 0;
                }
            }
            else
            {
                // This is to make cloning more efficient.
                // No sorting, and no re-calculating the hashcode.

                _totalCost     = toCopy._totalCost;
                _totalEarnings = toCopy._totalEarnings;
                _hashCode      = toCopy._hashCode;
                _totalCount    = toCopy._totalCount;
            }
        }