Exemplo n.º 1
0
        private void AddImpl(Like like, bool import)
        {
            if (_likee2likers[like.LikeeId] != null)
            {
                var list = _likee2likers[like.LikeeId];
                {
                    if (import)
                    {
                        var rawList = list.GetList();
                        int index   = rawList.CustomBinarySearch(like.LikerId);
                        if (index < 0)
                        {
                            list.Insert(~index, like.LikerId);
                        }
                    }
                    else
                    {
                        if (!list.FullContains(like.LikerId))
                        {
                            list.DelayAdd(like.LikerId);
                        }
                    }
                }
            }
            else
            {
                _likee2likers[like.LikeeId] = DelaySortedList <int> .CreateDefault();

                _likee2likers[like.LikeeId].Load(like.LikerId);
            }

            DelaySortedList <LikeBucket> likes;

            if (_liker2likes[like.LikerId] == null)
            {
                _liker2likes[like.LikerId] = new DelaySortedList <LikeBucket>(BucketIdComparer.Default);
            }

            likes = _liker2likes[like.LikerId];

            LikeBucket bucket = new LikeBucket(like.LikeeId, like.Timestamp.Seconds, 1);

            if (import)
            {
                var rawList = likes.GetList();
                int index   = rawList.BinarySearch(bucket, BucketIdComparer.Default);
                if (index >= 0)
                {
                    rawList[index] += bucket;
                }
                else
                {
                    likes.Insert(~index, bucket);
                }
            }
            else
            {
                likes.UpdateOrAdd(bucket, x => x + bucket);
            }
        }
Exemplo n.º 2
0
        public void Suggest(
            int id,
            Dictionary <int, float> similarity,
            Dictionary <int, List <LikeBucket> > suggested,
            HashSet <int> selfIds,
            MainContext context,
            short cityId,
            short countryId)
        {
            bool curSex = context.Sex.Get(id);

            DelaySortedList <LikeBucket> buckets = _liker2likes[id];

            if (buckets == null)
            {
                return;
            }

            foreach (var likeePair in buckets)
            {
                var likers = _likee2likers[likeePair.LikeeId];
                if (likers == null)
                {
                    continue;
                }

                foreach (var liker in likers)
                {
                    if (curSex != context.Sex.Get(liker))
                    {
                        continue;
                    }

                    if (cityId > 0 && cityId != context.Cities.Get(liker))
                    {
                        continue;
                    }

                    if (countryId > 0 && countryId != context.Countries.Get(liker))
                    {
                        continue;
                    }

                    float current = 0;
                    if (!similarity.TryGetValue(liker, out current))
                    {
                        current = 0;
                    }

                    float      x         = likeePair.Calc();
                    LikeBucket bucketY   = new LikeBucket(likeePair.LikeeId, 0, 0);
                    var        likerList = _liker2likes[liker];
                    bucketY = likerList.Find(bucketY);
                    float y = bucketY.Calc();

                    if (likeePair.TsSum * bucketY.Count == bucketY.TsSum * likeePair.Count)
                    {
                        current += 1.0f;
                    }
                    else
                    {
                        current += 1.0f / Math.Abs(x - y);
                    }
                    similarity[liker] = current;
                }
            }

            for (int i = 0; i < buckets.Count; i++)
            {
                selfIds.Add(buckets[i].LikeeId);
            }

            foreach (var liker in similarity.Keys)
            {
                suggested.Add(liker, _liker2likes[liker].GetList());
            }
        }