22 Commits

Author SHA1 Message Date
68d242c587 Actually rewrite the dsf implementation.
This rewrite improves the core data structure implementation in two
ways. Firstly, when merging two equivalence classes, we check their
relative sizes, and choose the larger class's canonical element to be
the overall root of the new class tree. This minimises the number of
overlong paths to the root after the merge. Secondly, we defer path
compression until _after_ the two classes are merged, rather than do
it beforehand (via using edsf_canonify as a subroutine) and then have
to do it wastefully again afterwards.

The size-based root selection was what we _used_ to do, and delivers
the better asymptotic performance. I reverted it so that Keen could
track the min of each equivalence class. But since then I've realised
you can have the asymptotic goodness _and_ min-tracking if you store
the minima separately from the main data structure. So now Keen does
that, and other clients don't have to pay the cost.

Similarly, the flip tracking is now a cost that only users of flip
dsfs have to pay, because a normal one doesn't store that information
at all.
2023-04-20 18:42:50 +01:00
c5e253a9f9 Reorganise the dsf API into three kinds of dsf.
This is preparing to separate out the auxiliary functionality, and
perhaps leave space for making more of it in future.

The previous name 'edsf' was too vague: the 'e' stood for 'extended',
and didn't say anything about _how_ it was extended. It's now called a
'flip dsf', since it tracks whether elements in the same class are
flipped relative to each other. More importantly, clients that are
going to use the flip tracking must say so when they allocate the dsf.

And Keen's need to track the minimal element of an equivalence class
is going to become a non-default feature, so there needs to be a new
kind of dsf that specially tracks those, and Keen will have to call it.

While I'm here, I've renamed the three dsf creation functions so that
they start with 'dsf_' like all the rest of the dsf API.
2023-04-20 18:39:41 +01:00
14e1e05510 Introduce a new dsf_equivalent() function.
Not very interesting, but the idiom for checking equivalence via two
calls to dsf_canonify is cumbersome enough to be worth abbreviating.
2023-04-20 18:39:35 +01:00
088fdeee38 Remove conditioned-out dsf diagnostic code.
print_dsf was declared in puzzles.h, but never called, and its
definition was commented out. So it probably wouldn't still have
worked anyway. The other commented-out printfs in that file don't look
very useful either, and they just mean more stuff will need messing
about with as I continue to refactor.
2023-04-20 17:30:03 +01:00
348aac4c85 Remove size parameter from dsf init and copy functions.
Now that the dsf knows its own size internally, there's no need to
tell it again when one is copied or reinitialised.

This makes dsf_init much more about *re*initialising a dsf, since now
dsfs are always allocated using a function that will initialise them
anyway. So I think it deserves a rename.
2023-04-20 17:30:03 +01:00
dad2f35502 Store a size field inside the DSF type.
This permits bounds-checking of all inputs to dsf_canonify and
dsf_merge, so that any out-of-range values will provoke assertion
failure instead of undefined behaviour.
2023-04-20 17:30:01 +01:00
095224d571 Actually make DSF an opaque structure type.
This makes good on all the previous preparatory commits, which I did
separately so that each one individually has a reasonably readable
diff, and all the mechanical changes are separated out from the
rewrites that needed actual thought.

Still no functional change, however: the DSF type wraps nothing but
the same int pointer that 'DSF *' used to store directly.
2023-04-20 17:23:23 +01:00
89c438e149 Declare all dsfs as a dedicated type name 'DSF'.
In this commit, 'DSF' is simply a typedef for 'int', so that the new
declaration form 'DSF *' translates to the same type 'int *' that dsfs
have always had. So all we're doing here is mechanically changing type
declarations throughout the code.
2023-04-20 17:23:21 +01:00
11a8149d67 Use a dedicated copy function to copy dsfs.
Previously we were duplicating the contents of a dsf using straight-up
memcpy. Now there's a dsf_copy function wrapping the same memcpy.

For the moment, this still has to take a size parameter, because the
size isn't stored inside the dsf itself. But once we make a proper
data type, it will be.
2023-04-20 17:21:54 +01:00
bb561ee3b1 Use a dedicated free function to free dsfs.
No functional change: currently, this just wraps the previous sfree
call.
2023-04-20 17:21:12 +01:00
5f5b284c0b Use C99 bool within source modules.
This is the main bulk of this boolification work, but although it's
making the largest actual change, it should also be the least
disruptive to anyone interacting with this code base downstream of me,
because it doesn't modify any interface between modules: all the
inter-module APIs were updated one by one in the previous commits.
This just cleans up the code within each individual source file to use
bool in place of int where I think that makes things clearer.
2018-11-13 21:48:24 +00:00
a550ea0a47 Replace TRUE/FALSE with C99 true/false throughout.
This commit removes the old #defines of TRUE and FALSE from puzzles.h,
and does a mechanical search-and-replace throughout the code to
replace them with the C99 standard lowercase spellings.
2018-11-13 21:48:24 +00:00
20b56788bc Adopt C99 bool in the edsf API.
Now the flag passed to edsf_merge to say whether two items are the
same or opposite is a bool, and so is the flag returned via a pointer
argument from edsf_canonify.

The latter requires client code to be updated to match (otherwise
you'll get a pointer type error), so I've done that update in Loopy,
which is edsf's only current in-tree client.
2018-11-13 21:48:24 +00:00
514bd502be New puzzle! 'Keen', a clone of KenKen.
[originally from svn r8796]
2009-12-27 10:01:23 +00:00
72922b3078 Tweak the semantics of dsf_merge() so that the canonical element of
any equivalence class is always the element with the smallest index.
This is slower (the previous behaviour, suggested by Jonas Koelker,
was to choose the new root element to maximise performance), but
still more than acceptably fast and more useful.

[originally from svn r8792]
2009-12-27 10:01:11 +00:00
df31d4f419 New puzzle: `Filling', a Fillomino implementation by Jonas Koelker.
[originally from svn r7326]
2007-02-25 11:37:05 +00:00
13a9cb0bea James H's Palm-compatibility updates to the latest Loopy changes,
working around bugs in the Palm compiler and removing Palm-
incompatible diagnostics such as fprintf.

[originally from svn r6889]
2006-11-01 13:25:25 +00:00
b7356cd209 r6880 accidentally backed out r6780. That's what I get for accepting
source files from Mike rather than patches, and not adequately
checking the result...

[originally from svn r6882]
[r6780 == f05c25347d66821d928668a7e87dffbf3ffed027]
[r6880 == b9547673c6462bf73e642328300479df6df71d7b]
2006-10-29 09:34:09 +00:00
b9547673c6 Mike Pinna has done some major reworking of the Loopy solver, giving
rise to a new Hard difficulty level.

[originally from svn r6880]
2006-10-28 15:38:53 +00:00
f05c25347d Extra utility function.
[originally from svn r6780]
2006-08-05 16:35:25 +00:00
414330d9ad Cleanups from James H: a few missing statics, a precautionary cast
or two, a debugging fix, a couple of explicit initialisations of
variables that were previously read uninitialised, and a fix for a
whopping great big memory leak in Slant owing to me having
completely forgotten to write free_game().

[originally from svn r6159]
2005-08-03 12:44:51 +00:00
afe80030e4 New puzzle: `Slant', picked from the Japanese-language section of
nikoli.co.jp (which has quite a few puzzles that they don't seem to
have bothered to translate into English).

Minor structural change: the disjoint set forest code used in the
Net solver has come in handy again, so I've moved it out into its
own module dsf.c.

[originally from svn r6155]
2005-08-02 23:16:46 +00:00