-
Notifications
You must be signed in to change notification settings - Fork 0
/
Phrases.cs
55 lines (47 loc) · 1.77 KB
/
Phrases.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
using System;
using System.Collections;
using System.Collections.Generic;
namespace Autocomplete
{
public class Phrases : IReadOnlyList<string>
{
private readonly string[] adjectives;
private readonly string[] nouns;
private readonly string[] verbs;
public Phrases(string[] verbs, string[] adjectives, string[] nouns)
{
this.verbs = verbs;
this.adjectives = adjectives;
this.nouns = nouns;
}
// Это называется вычисляемое свойство с геттером.
public virtual int Length => verbs.Length * adjectives.Length * nouns.Length;
// Это называется индексатор c геттером. Он позволяет писать так var x = phrases[i];
public virtual string this[int index]
{
get
{
if (index < 0) throw new IndexOutOfRangeException("index = " + index);
var ni = index % nouns.Length;
var ai = index / nouns.Length % adjectives.Length;
var vi = index / (nouns.Length * adjectives.Length) % verbs.Length;
return verbs[vi] + " " + adjectives[ai] + " " + nouns[ni];
}
}
public IEnumerator<string> GetEnumerator()
{
for (var i = 0; i < Length; i++)
yield return this[i];
}
IEnumerator IEnumerable.GetEnumerator()
{
return GetEnumerator();
}
public int Count => Length;
public override string ToString()
{
return string.Format("Size: {3}. Verbs: {0}, Adjectives: {1}, Nouns: {2}", verbs.Length, adjectives.Length,
nouns.Length, Length);
}
}
}