/
ElementoSinBase.cs
137 lines (124 loc) · 5.33 KB
/
ElementoSinBase.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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
using System;
using System.Collections.Generic;
using System.Text;
using Gabriel.Cat.S.Extension;
namespace Gabriel.Cat.S.Binaris
{
public class ElementoSinBase<TIdTipo, TIdElemento, TId> : IElementoBinarioComplejo
{
const byte NOCOINCIDE = 0x1;
const byte COINCICE = 0x0;
public static readonly ElementoBinario Serializador = ElementoBinario.GetSerializador<ElementoSinBase<TIdTipo, TIdElemento, TId>>();
public bool SinBase { get; set; }
public TIdTipo IdTipo { get; set; }
public TIdElemento IdElemento { get; set; }
public TId Id { get; set; }
public byte[] BytesSinBase { get; set; }
ElementoBinario IElementoBinarioComplejo.Serialitzer => Serializador;
public ElementoSinBase() { }
public ElementoSinBase(TIdTipo idTipo, TIdElemento idElemento, TId id, IElementoBinarioComplejo elementoBase, IElementoBinarioComplejo elementoCompleto) : this(idTipo, idElemento, id, elementoBase.Serialitzer.GetBytes(elementoBase), elementoCompleto.Serialitzer.GetBytes(elementoCompleto))
{ }
public ElementoSinBase(TIdTipo idTipo, TIdElemento idElemento, TId id, byte[] bytesBase, byte[] bytesCompletos)
{
this.Id = id;
this.IdTipo = idTipo;
this.IdElemento = idElemento;
GetBytesSinBase(bytesBase, bytesCompletos);
}
public string Name
{
get
{
return String.Format("{0}_{1}_{2}", Id, IdTipo, IdElemento);
}
}
public byte[] GetBytesCompletos(byte[] bytesBase)
{
if (bytesBase.Length < BytesSinBase.Length - bytesBase.Length)
throw new BaseIncorrectaException();
byte[] bytesCompletos = new byte[bytesBase.Length];
unsafe
{
bytesCompletos.UnsafeMethod((ptrBytesCompletos) =>
{
bytesBase.UnsafeMethod((ptrBytesBase) =>
{
BytesSinBase.UnsafeMethod((ptrBytesSinBase) =>
{
for (int i = 0; i < bytesCompletos.Length; i++)
{
if (*ptrBytesSinBase.PtrArray == NOCOINCIDE)
{
ptrBytesSinBase.PtrArray++;
*ptrBytesCompletos.PtrArray = *ptrBytesSinBase.PtrArray;
}
else
{
*ptrBytesCompletos.PtrArray = *ptrBytesBase.PtrArray;
}
ptrBytesCompletos.PtrArray++;
ptrBytesBase.PtrArray++;
ptrBytesSinBase.PtrArray++;
}
});
});
});
}
return bytesCompletos;
}
public void GetBytesSinBase(byte[] bytesBase, byte[] bytesCompletos)
{//cuando funcione todo lo optimizo
if (bytesBase.Length != bytesCompletos.Length)
throw new BaseIncorrectaException();
int bytesSinCoincidir = 0;
SinBase = true;
for (int i = 0; i < bytesBase.Length; i++)
if (bytesBase[i] != bytesCompletos[i])
bytesSinCoincidir++;
BytesSinBase = new byte[bytesBase.Length + bytesSinCoincidir];
for (int i = 0, j = 0; i < bytesCompletos.Length; i++)
{
if (bytesBase[i] == bytesCompletos[i])
BytesSinBase[j] = COINCICE;
else
{
BytesSinBase[j++] = NOCOINCIDE;
BytesSinBase[j] = bytesCompletos[i];
}
j++;
}
}
public void SetBytesConBase(byte[] bytesCompletos)
{
SinBase = false;
BytesSinBase = bytesCompletos;
}
public override string ToString()
{
return Name;
}
}
public class ElementoSinBase : ElementoSinBase<int, int, int>
{
public ElementoSinBase() { }
public ElementoSinBase(int idTipo, int idElemento, int id, IElementoBinarioComplejo elementoBase, IElementoBinarioComplejo elementoCompleto) : base(idTipo, idElemento, id, elementoBase, elementoCompleto)
{
}
public ElementoSinBase(int idTipo, int idElemento, int id, byte[] bytesBase, byte[] bytesCompletos) : base(idTipo, idElemento, id, bytesBase, bytesCompletos)
{
}
}
public class BaseIncorrectaException : Exception
{ }
public static class ElementoBinarioSinBaseExtension
{
public static T GetCompleto<T, T1, T2, T3>(this ElementoSinBase<T1, T2, T3> elementoSinBase, T tipoCompleto) where T : IElementoBinarioComplejo
{
return (T)tipoCompleto.Serialitzer.GetObject(elementoSinBase.GetBytesCompletos(tipoCompleto.Serialitzer.GetBytes(tipoCompleto)));
}
public static T GetCompleto<T>(this ElementoSinBase elementoSinBase, T tipoCompleto) where T : IElementoBinarioComplejo
{
return ((ElementoSinBase<int, int, int>)elementoSinBase).GetCompleto(tipoCompleto);
}
}
}