예제 #1
0
        public static int GetPrime(int min)
        {
            Invariants.CheckArgument(min >= 0, nameof(min), "Invalid prime seed");
            for (int index = 0; index < Primes.Length; ++index)
            {
                int prime = Primes[index];
                if (prime >= min)
                {
                    return(prime);
                }
            }

            int candidate = min | 1;

            while (candidate < int.MaxValue)
            {
                if (IsPrime(candidate) && (candidate - 1) % 101 != 0)
                {
                    return(candidate);
                }
                candidate += 2;
            }

            return(min);
        }
예제 #2
0
 /// <summary>
 /// Creates a <see cref="RelativePath"/> instance from a relative
 /// path string (e.g. "foo\\bar\\blah.txt").
 /// </summary>
 public RelativePath(string relativePath)
 {
     Invariants.CheckArgumentNotNull(relativePath, nameof(relativePath));
     Invariants.CheckArgument(!PathHelpers.IsAbsolutePath(relativePath), nameof(relativePath), "Path must be relative.");
     // Empty string is the same as the empty relative path
     _relativePath = relativePath == "" ? null : relativePath;
 }
예제 #3
0
 public FileName(DirectoryName parent, string name)
 {
     Invariants.CheckArgumentNotNull(parent, nameof(parent));
     Invariants.CheckArgumentNotNull(name, nameof(name), "File name is empty");
     Invariants.CheckArgument(PathHelpers.IsFileName(name), nameof(name), "File name contains one or more directory separator");
     _parent = parent;
     _name   = name;
 }
예제 #4
0
 public RelativeDirectoryName(DirectoryName parent, string name)
 {
     Invariants.CheckArgumentNotNull(parent, nameof(parent));
     Invariants.CheckArgumentNotNull(name, nameof(name), "Directory name is empty");
     Invariants.CheckArgument(PathHelpers.IsFileName(name), nameof(name), "Directory name contains one or more directory separator");
     _parent   = parent;
     _name     = name;
     _hashCode = HashCode.Combine(_parent.GetHashCode(), SystemPathComparer.GetHashCode(_name));
 }
예제 #5
0
        public IList <DirectoryEntry> GetDirectoryEntries(FullPath path)
        {
            var entry = FindEntry(path) as DirectoryMock;

            Invariants.CheckArgument(entry != null, nameof(path), "Invalid directory name");
            return(entry.Directories
                   .Select(x => new DirectoryEntry(x.Name, FILE_ATTRIBUTE.FILE_ATTRIBUTE_DIRECTORY))
                   .Concat(entry.Files.Select(x => new DirectoryEntry(x.Name, FILE_ATTRIBUTE.FILE_ATTRIBUTE_NORMAL)))
                   .ToList());
        }
예제 #6
0
 public TValue this[TKey key] {
     get {
         TValue value;
         if (!TryGetValue(key, out value))
         {
             Invariants.CheckArgument(false, nameof(key), "Key not found in table");
         }
         return(value);
     }
     set { UpdateOrAdd(key, value); }
 }
예제 #7
0
        public SlimHashTable(Func <TValue, TKey> keyProvider, int capacity, IEqualityComparer <TKey> comparer, ICollectionGrowthPolicy growthPolicy)
        {
            Invariants.CheckArgumentNotNull(keyProvider, nameof(keyProvider));
            Invariants.CheckArgument(capacity >= 0, nameof(capacity), "Capacity must be positive");
            Invariants.CheckArgumentNotNull(comparer, nameof(comparer));
            Invariants.CheckArgumentNotNull(growthPolicy, nameof(growthPolicy));

            _getKey       = keyProvider;
            _capacity     = capacity;
            _comparer     = comparer;
            _growthPolicy = growthPolicy;

            var length = GetPrimeLength(capacity);

            Initialize(length);
        }
예제 #8
0
        private void AddEntry(TKey key, TValue value, bool updateExistingAllowed)
        {
            var hashCode    = _comparer.GetHashCode(key);
            var bucketIndex = GetBucketIndex(hashCode, _buckets.Length);

            for (var entryIndex = AdjustBucketIndexRead(_buckets[bucketIndex]); entryIndex >= 0;)
            {
                var entry = _entries[entryIndex];
                if (entry.HashCode == hashCode && _comparer.Equals(key, _getKey(entry.Value)))
                {
                    if (!updateExistingAllowed)
                    {
                        Invariants.CheckArgument(false, nameof(key), "Key already exist in table");
                    }
                    _entries[entryIndex].Value = value;
                    return;
                }
                entryIndex = _entries[entryIndex].NextIndex;
            }

            // We need to actually add the value
            AddEntryWorker(value, bucketIndex, hashCode);
        }
예제 #9
0
 public FullPath(string path)
 {
     Invariants.CheckArgument(IsValid(path), nameof(path), "Path must be absolute: \"{0}\".", path);
     _path = path;
 }
예제 #10
0
 public ExponentialGrowthPolicy(double factor)
 {
     Invariants.CheckArgument(factor > 1, nameof(factor), "Factor must be greater than 1");
     _factor = factor;
 }