Example #1
0
        private City[] ConvertToCities(byte[] file, int offset, int count)
        {
            var cities = new City[count];

            for (int i = 0; i < count; i++)
            {
                var city = converterService.ConvertToCity(file, offset);
                cities[i] = city;
                offset   += 96;
            }

            return(cities);
        }
Example #2
0
        public City[] BinarySearchByIpAddress(string ip)
        {
            var intAddress = IpAddressHelpers.IpToUint(ip);

            if (_repository.Db == null)
            {
                throw new Exception("База данных не загружена");
            }

            var firstIndex = 0;
            var lastIndex  = _repository.Db.Header.Records;

            while (firstIndex <= lastIndex)
            {
                int midIndex = firstIndex + (lastIndex - firstIndex) / 2;

                var midRangeFrom = _repository.Db.IpRanges[midIndex, 0];
                var midRangeTo   = _repository.Db.IpRanges[midIndex, 1];

                if (intAddress >= midRangeFrom && intAddress <= midRangeTo)
                {
                    var index = _repository.Db.IpRanges[midIndex, 2];
                    return(new City[] { _converterService.ConvertToCity(_repository.Db.Cities, (int)index * Constants.CitySize) });
                }

                if (intAddress < midRangeFrom)
                {
                    lastIndex = midIndex - 1;
                }
                else
                {
                    firstIndex = midIndex + 1;
                }
            }
            return(new City[] { });
        }