fork(1) download
  1.  
  2. using System;
  3.  
  4. public class Test
  5. {
  6. public static void Main()
  7. {
  8. //init and print
  9. Console.WriteLine("Init");
  10. SpecialArray<int> test = new SpecialArray<int>(0, 10);
  11. test.Print();
  12.  
  13. //set all and print
  14. Console.WriteLine("Set All to 12");
  15. test.SetAll(12);
  16. test.Print();
  17.  
  18. //set some values and print
  19. Console.WriteLine("Set some values");
  20. test.Set(4, 3);
  21. test.Set(6, 2);
  22. test.Set(0, 0);
  23. test.Print();
  24.  
  25. //set all again and print
  26. Console.WriteLine("Set all to 14");
  27. test.SetAll(14);
  28. test.Print();
  29.  
  30. //set some values again and print
  31. Console.WriteLine("Set some values");
  32. test.Set(4, 4);
  33. test.Set(6, 5);
  34. test.Set(0, 6);
  35. test.Print();
  36.  
  37. //set all again and print
  38. Console.WriteLine("Set all to 16");
  39. test.SetAll(16);
  40. test.Print();
  41.  
  42. //printing tests the Get method works
  43. Console.ReadLine();
  44. }
  45. }
  46.  
  47. public class SpecialValue<T>
  48. {
  49. public bool mode = true;
  50. public bool haslocal = false;
  51. public T local;
  52. }
  53.  
  54. public class SpecialArray<T>
  55. {
  56. SpecialValue<T>[] array;
  57. bool mode;
  58. int size;
  59. T globalTrue;
  60. T globalFalse;
  61.  
  62. public SpecialArray(T globalValue, int size)
  63. {
  64. this.size = size;
  65. mode = true;
  66. globalTrue = globalValue;
  67. array = new SpecialValue<T>[size];
  68. for (int i = 0; i < size; i++)
  69. array[i] = new SpecialValue<T>();
  70. }
  71.  
  72. public T Get(int i)
  73. {
  74. SpecialValue<T> val = array[i];
  75. if (val.mode == mode)
  76. {
  77. if (val.haslocal)
  78. return val.local;
  79. else return val.mode ? globalTrue : globalFalse;
  80. }
  81. else
  82. {
  83. val.mode = mode;
  84. val.haslocal = false;
  85. return val.mode ? globalTrue : globalFalse;
  86. }
  87. }
  88.  
  89. public void Set(int i, T value)
  90. {
  91. SpecialValue<T> val = array[i];
  92. val.haslocal = true;
  93. val.local = value;
  94. }
  95.  
  96. public void SetAll(T value)
  97. {
  98. mode = !mode;
  99. if (mode)
  100. globalTrue = value;
  101. else
  102. globalFalse = value;
  103. }
  104.  
  105. public void Print()
  106. {
  107. for (int i = 0; i < size; i++)
  108. Console.Write(Get(i).ToString() + ", "); //meh good enough
  109. Console.WriteLine();
  110. }
  111. }
  112.  
Success #stdin #stdout 0.03s 24248KB
stdin
Standard input is empty
stdout
Init
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 
Set All to 12
12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 
Set some values
0, 12, 12, 12, 3, 12, 2, 12, 12, 12, 
Set all to 14
14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 
Set some values
6, 14, 14, 14, 4, 14, 5, 14, 14, 14, 
Set all to 16
16, 16, 16, 16, 16, 16, 16, 16, 16, 16,