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
go
localization
gdb
cheerio
angular2-material
yahoo-finance
error-handling
seo
soa
emscripten
crystal-lang
nstableheaderview
elasticsearch-5
mql5
code-generation
shared-libraries
zerobrane
3nf
google-container-registry
soci
boost-icl
google-maps-autocomplete
postscript
hta
pchart
gmock
definitelytyped
moses
slidetoggle
nstimer
spotipy
winpe
sqsh
testcomplete
event-flow
recurring-events
database-comparison
simplecv
sammy.js
classcastexception
nested-lists
slacktextviewcontroller
yowsup
lubridate
openalpr
ideascript
typesafe-config
jvmti
c9.io
hackintosh
ogc
wmp
chronicle-map
xmldocument
pom.xml
ovf
facebook-pixel
configurationmanager
x-ray
page-replacement
flac
trigger.io
usart
pure-css
spring-cloud-feign
file-uri
neon
signalr.client
rspec2
freepbx
aspen
libvlc
inf
tfs-workitem
oracle-spatial
microsoft-reporting
zen-cart
canalyzer
atoi
ms-access-2000
aspectj-maven-plugin
snackbar
matlab-deployment
jquery-forms-plugin
zend-currency
broadband
boost-program-options
administration
deform
appserver
mdichild
named-parameters
paste
xmltodict
ora-06550
eclipse-kepler
embedded-fonts
jeromq
kogrid
nebula
mysql-error-2013
resource-management
multiplexing
infobright
multiplatform
massive
event-receiver
unattended-processing
timertask
aspmenu
wcf-instancing
technical-debt
commercial-application

Resources

Database Users
RDBMS discuss
Database Dev&Adm
javascript
java
csharp
php
android
javascript
java
csharp
php
python
android
jquery
ruby
ios
html
Mobile App
Mobile App
Mobile App