DataTable.cs 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. using System;
  2. using UnityEngine;
  3. using UnityEngine.Assertions;
  4. namespace VoxelImporter
  5. {
  6. public class DataTable3<Type>
  7. {
  8. public DataTable3(int reserveX = 0, int reserveY = 0, int reserveZ = 0)
  9. {
  10. reserve = new IntVector3(reserveX, reserveY, reserveZ);
  11. enable = new FlagTable3(reserveX, reserveY, reserveZ);
  12. }
  13. public void Set(int x, int y, int z, Type param)
  14. {
  15. Assert.IsTrue(x >= 0 && y >= 0 && z >= 0);
  16. #region Alloc
  17. reserve = IntVector3.Max(reserve, new IntVector3(x + 1, y + 1, z + 1));
  18. if (table == null)
  19. {
  20. table = new Type[reserve.x][][];
  21. }
  22. if (x >= table.Length)
  23. {
  24. var newTmp = new Type[reserve.x][][];
  25. table.CopyTo(newTmp, 0);
  26. table = newTmp;
  27. }
  28. if(table[x] == null)
  29. {
  30. table[x] = new Type[reserve.y][];
  31. }
  32. if (y >= table[x].Length)
  33. {
  34. var newTmp = new Type[reserve.y][];
  35. table[x].CopyTo(newTmp, 0);
  36. table[x] = newTmp;
  37. }
  38. if (table[x][y] == null)
  39. {
  40. table[x][y] = new Type[reserve.z];
  41. }
  42. if (z >= table[x][y].Length)
  43. {
  44. var newTmp = new Type[reserve.z];
  45. table[x][y].CopyTo(newTmp, 0);
  46. table[x][y] = newTmp;
  47. }
  48. #endregion
  49. table[x][y][z] = param;
  50. enable.Set(x, y, z, true);
  51. }
  52. public void Set(IntVector3 pos, Type param)
  53. {
  54. Set(pos.x, pos.y, pos.z, param);
  55. }
  56. public Type Get(int x, int y, int z)
  57. {
  58. if (!enable.Get(x, y, z)) return default(Type);
  59. return table[x][y][z];
  60. }
  61. public Type Get(IntVector3 pos)
  62. {
  63. return Get(pos.x, pos.y, pos.z);
  64. }
  65. public void Remove(int x, int y, int z)
  66. {
  67. if (!enable.Get(x, y, z)) return;
  68. enable.Set(x, y, z, false);
  69. }
  70. public void Remove(IntVector3 pos)
  71. {
  72. Remove(pos.x, pos.y, pos.z);
  73. }
  74. public void Clear()
  75. {
  76. table = null;
  77. enable.Clear();
  78. }
  79. public bool Contains(int x, int y, int z)
  80. {
  81. return enable.Get(x, y, z);
  82. }
  83. public bool Contains(IntVector3 pos)
  84. {
  85. return enable.Get(pos);
  86. }
  87. public void AllAction(Action<int, int, int, Type> action)
  88. {
  89. if (table == null) return;
  90. enable.AllAction((x, y, z) =>
  91. {
  92. action(x, y, z, table[x][y][z]);
  93. });
  94. }
  95. private IntVector3 reserve;
  96. private Type[][][] table;
  97. private FlagTable3 enable;
  98. }
  99. }