From 427e051f6a1d2b5740b9f3ab0c5e3df9df7494fb Mon Sep 17 00:00:00 2001 From: dreammaster Date: Wed, 17 Apr 2019 05:41:14 +0100 Subject: GLK: GLULXE: astyle formatting --- engines/glk/glulxe/search.cpp | 335 +++++++++++++++++++++--------------------- 1 file changed, 165 insertions(+), 170 deletions(-) (limited to 'engines/glk/glulxe/search.cpp') diff --git a/engines/glk/glulxe/search.cpp b/engines/glk/glulxe/search.cpp index 440da553f0..1bbe8fa027 100644 --- a/engines/glk/glulxe/search.cpp +++ b/engines/glk/glulxe/search.cpp @@ -31,185 +31,180 @@ enum serop { serop_ReturnIndex = 0x04 }; -uint Glulxe::linear_search(uint key, uint keysize, uint start, uint structsize, uint numstructs, - uint keyoffset, uint options) { - unsigned char keybuf[4]; - uint count; - uint ix; - int retindex = ((options & serop_ReturnIndex) != 0); - int zeroterm = ((options & serop_ZeroKeyTerminates) != 0); - - fetchkey(keybuf, key, keysize, options); - - for (count=0; count byte2) - cmp = 1; - } - } - else { - for (ix=0; (!cmp) && ix byte2) - cmp = 1; - } - } - - if (!cmp) { - if (retindex) - return val; - else - return addr; - } - - if (cmp < 0) { - bot = val+1; - } - else { - top = val; - } - } - - if (retindex) - return (uint)-1; - else - return 0; +uint Glulxe::binary_search(uint key, uint keysize, uint start, uint structsize, uint numstructs, + uint keyoffset, uint options) { + byte keybuf[4]; + byte byte1, byte2; + uint top, bot, val, addr; + uint ix; + int retindex = ((options & serop_ReturnIndex) != 0); + + fetchkey(keybuf, key, keysize, options); + + bot = 0; + top = numstructs; + while (bot < top) { + int cmp = 0; + val = (top + bot) / 2; + addr = start + val * structsize; + + if (keysize <= 4) { + for (ix = 0; (!cmp) && ix < keysize; ix++) { + byte1 = Mem1(addr + keyoffset + ix); + byte2 = keybuf[ix]; + if (byte1 < byte2) + cmp = -1; + else if (byte1 > byte2) + cmp = 1; + } + } else { + for (ix = 0; (!cmp) && ix < keysize; ix++) { + byte1 = Mem1(addr + keyoffset + ix); + byte2 = Mem1(key + ix); + if (byte1 < byte2) + cmp = -1; + else if (byte1 > byte2) + cmp = 1; + } + } + + if (!cmp) { + if (retindex) + return val; + else + return addr; + } + + if (cmp < 0) { + bot = val + 1; + } else { + top = val; + } + } + + if (retindex) + return (uint) - 1; + else + return 0; } uint Glulxe::linked_search(uint key, uint keysize, uint start, uint keyoffset, uint nextoffset, uint options) { - unsigned char keybuf[4]; - uint ix; - uint val; - int zeroterm = ((options & serop_ZeroKeyTerminates) != 0); - - fetchkey(keybuf, key, keysize, options); - - while (start != 0) { - int match = true; - if (keysize <= 4) { - for (ix=0; match && ix