delta


Delta encoding - strings (Programming logic - no specific language)


Can anyone put the code for delta encoding for strings.
for example, If the initial string is
"Cat and Dogs"
and later the string is
"Cats and Dogs"
I should be able to get the difference between the string versions very efficiently. If anyone can put the code here so that very big paragraphs can be sent with delta encoding with very less data.
I found the answer, I wanted to share it to all.
var oldString = "This is just a sample of string to test the delta encoding. Infact, with my own login.dsfdsfdsfdsfsdfsdfdsfdsfsdfsdfds dsfds fds fddsf";
var newString = "dsfdsThissdf is just a sample of string to test X the dedsfdsflta encoding sdfds decoding. Infact, with my own sadsadsadsad"
var result = "";
var changes = getChanges(oldString, newString);
var string = getOriginal(oldString, changes);
function getChanges(os, ns) {
var addedIndex = false;
var changes = [];
var obj = [];
var oi = 0, ni = 0;
while (oi < oldString.length && ni < newString.length) {
if (newString.charAt(ni) != oldString.charAt(oi)) {
if (!addedIndex) {
obj.push(oi);
obj.push(newString.charAt(ni));
addedIndex = true;
}
else {
obj[1] += newString.charAt(ni);
}
ni++;
}
else {
if (addedIndex) {
changes.push(obj);
obj = [];
addedIndex = false;
}
oi++;
ni++;
}
};
if (addedIndex) {
changes.push(obj);
obj = [];
addedIndex = false;
}
obj = [];
if (ni == newString.length) {
obj.push(-1 * oi);
changes.push(obj);
}
if (oi == oldString.length) {
obj.push(ni);
obj.push(newString.substring(ni));
changes.push(obj);
}
return changes;
}
function getOriginal(os, changes) {
var result = os;
for (var i = changes.length - 1; i >= 0 ; i--) {
if (changes[i][0] < 0) {
result = result.substring(0, -1 * changes[i][0]);
}
else {
result = result.substring(0, changes[i][0]) + changes[i][1] + result.substring(changes[i][0]);
}
};
return result;
}
console.log(string);
console.log(newString);

Related Links

Delta encoding - strings (Programming logic - no specific language)

Categories

HOME
phpword
api
cheerio
c++11
eslint
css3
youtube-data-api-v3
camera
jsf-2.2
spring-data-redis
edge
hugo
limit
azure-container-service
arangodb
fhir
windows-ce
redis-sentinel
shared-libraries
parameter-passing
google-content-api
google-data-studio
foreign-keys
production
jmp
lilypond
undertow
angular-routing
joptionpane
scriptella
jquery-callback
beta
ckfinder
asymptotic-complexity
ntp
joomla2.5
demo
mcustomscrollbar
azure-arm
android-exoplayer
opencmis
dtsearch
launchctl
arq
magic-draw
seq
rackspace-cloud
mapxtreme
winusb
google-cloud-console
signalr.client
expectations
nservicebus5
bizagi
database-relations
cycle2
blitline
linklabel
morton-number
zen-cart
omnifaces
nutiteq
kubuntu
osc
rrule
bcache
appserver
machine-instruction
angular-carousel
grails-domain-class
live555
ray
spring-3
bin
kendo-dataviz
jquery-1.9
android-sdk-2.3
curb
tree-balancing
onmousemove
vfs
extconf.rb
mapping-by-code
jsr286
entity-attribute-value
screen-size
landscape-portrait
maven-1
dot-emacs
binomial-heap
openfaces
duplex
cfwindow
lobo-cobra
int64
contentpresenter
appliance

Resources

Encrypt Message