in java/src/main/java/com/epam/deltix/containers/SortedSqrtDecomposition.java [126:200]
public int addIfNotExist(T x) {
boolean insert = false;
if (needRebuild) {
rebuildSqrt();
}
for (int j = 0; j < numberOfBlocks; ++j) {
if (size != 0 && dataInBegin[j] != null && comparator.compare((T) dataInBegin[j], x) > 0) {
if (j == 0) {
for (int k = endOfBlock[0]; k > 0; k--) {
data[k] = data[k - 1];
}
data[0] = x;
dataInBegin[0] = x;
endOfBlock[0]++;
insert = true;
size++;
if (endOfBlock[0] == (len << 1)) {
needRebuild = true;
}
return 0;
}
for (int k = (len * (j - 1) << 1); k < endOfBlock[j - 1]; k++)
if (comparator.compare((T) data[k], x) > 0) {
for (int l = endOfBlock[j - 1]; l > k; l--) {
data[l] = data[l - 1];
}
data[k] = x;
endOfBlock[j - 1]++;
insert = true;
size++;
if (endOfBlock[j - 1] == (len * j << 1)) needRebuild = true;
return k;
} else if (comparator.compare((T) data[k], x) == 0) {
return -(k + 1);
}
if (!insert) {
data[endOfBlock[j - 1]] = x;
insert = true;
endOfBlock[j - 1]++;
size++;
if (endOfBlock[j - 1] == (len * j << 1)) {
needRebuild = true;
}
return endOfBlock[j - 1] - 1;
}
}
}
if (!insert) {
for (int j = (len * (numberOfBlocks - 1) << 1); j < endOfBlock[numberOfBlocks - 1]; ++j)
if (data[j] != null && comparator.compare((T)data[j], x) > 0) {
for (int l = endOfBlock[numberOfBlocks - 1]; l > j; l--) {
data[l] = data[l - 1];
}
data[j] = x;
if (j == ((len * (numberOfBlocks - 1)) << 1)) dataInBegin[numberOfBlocks - 1] = x;
insert = true;
endOfBlock[numberOfBlocks - 1]++;
size++;
if (endOfBlock[numberOfBlocks - 1] == (len * numberOfBlocks << 1)) needRebuild = true;
return j;
} else if (comparator.compare((T)data[j], x) == 0) {
return -(j + 1);
}
if (!insert) {
size++;
if (endOfBlock[numberOfBlocks - 1] == (len * (numberOfBlocks - 1) << 1))
dataInBegin[numberOfBlocks - 1] = x;
data[endOfBlock[numberOfBlocks - 1]] = x;
endOfBlock[numberOfBlocks - 1]++;
if (endOfBlock[numberOfBlocks - 1] == (len * numberOfBlocks << 1)) needRebuild = true;
return endOfBlock[numberOfBlocks - 1] - 1;
}
}
return NO_ELEMENT;
}