Baike.dev
All toolsOnline toolsAI codingTrendingOpen sourceNewsSubmit
Log in
< Back to tools
A

amoeba

> 编程语言
Open source

a Cassowary constraint solving algorithm implements in pure C.

194 stars0 likes0 views
WebsiteGitHub

About

a Cassowary constraint solving algorithm implements in pure C.

Amoeba -- the constraint solving algorithm in pure C

Amoeba is a pure C implement of Cassowary algorithm. It:

  • Uses Clean C, which is the cross set of ANSI C89 and C++, like the Lua language.
  • Is a single-file library. For more single-file library, see the stb project here.
  • Largely impressed by kiwi, the C++ implement of Cassowary algorithm, and the algorithm paper.
  • With a Lua binding.
  • With the same license as the Lua language.

Improvement relative to Kiwi:

  • Single header library that easy to distribution.
  • Lua binding vs. Python binding.
  • Suggest cache makes suggest performance much better than kiwi.
  • Dump/load support improves the performance of building solver.

How To Use

This libary export a constraint solver interface, to solve a constraint problem, you should use it in steps:

  • create a new solver: am_newsolver()
  • create some variables: am_newvariable() binding the am_Num values.
  • create some constraints that may use variables: am_newconstraint().
  • make constraints by construct equation using:
    • am_addterm() add a $a \times variable$ term to constraint equation items.
    • am_setrelation() to specify the equal/greater/less sign in center of equation.
    • am_addconstant() to add a number without variable.
    • am_setstrength() to specify the priority of this constraint in all constraints.
  • after make up a constraint, you could add it into solver by am_add().
  • and you can read out the result of each variable from the binding am_Num.
  • or you can manually specify a new value to variable with am_sugguest().
  • after done, use am_delsolver() to free al memory.

below is a tiny example to demonstrate the steps:

…

Reference

Return value

All functions below that returns int are returning error codes:

  • AM_OK: Operations success.
  • AM_FAILED: The operation is failed, most caused by invalid arguments.
  • AM_UNSATISFIED: Specific constraints added cannot satisfied.
  • AM_UNBOUND: Failed to add constraints because there are unbound variables in it

Dump & Load

Amoeba supports to store whole state of solver into bianry data and to load back in new solver. Notice that the state of loaded solver will be earsed.

To perform a dump or load operations, you should prepare am_Dumper or am_Loader to control the behavior in operations.

  • struct am_Dumper

    A user-defined dump behavior control type. The user must provide reader and var_name function pointers in it. there is a example:

…
  • struct am_Loader

    A user-defined dump behavior control type. The user must provide reader and load_var function pointers in it. there is a example:

…

Routines

  • am_Solver *am_newsolver(am_Allocf *allocf, void *ud);

    Create a new solver with custom memory alloculator, pass NULL for use default ones.

  • void am_resetsolver(am_Solver *solver);

    Remove all constraints from solver, all variables and constraints created are keeped.

  • void am_delsolver(am_Solver *solver);

    Delete a solver and frees all memory it used, after that, all variables/constraints created from this solver are all freed.

  • void am_updatevars(am_Solver *solver);

    Refresh variables' value into it's constrainted value. You could use am_autoupdate() to update all variables automatically when solver changes.

  • void am_autoupdate(am_Solver *solver, int auto_update);

    Set the auto update flags. If setted, all variables will be updated to its latest values right after any changes done to the solver.

  • int am_dump(am_Solver *solver, am_Dumper *d);

    Dump whole solver state (without edits) into binary data.

  • int am_load(am_Solver *solver, am_Loader *l);

    Loads binary data back into solver. Note that all previous states are discarded, but previous vars & constraints are keeped. i.e. the am_resetsolver() called before loaded.

  • am_Id am_newvariable(am_Solver *solver, am_Num *pvalue);

    Create a new variable with a value pvalue binding to it. Returns 0 on error.

    Notice that the pvalue pointer can be retrived in am_dump() or am_varvalue(), so you could put context data here:

    c
    typedef struct MyVarContext {
        am_Num value;
        const char *name;
        /* etc.. */
    } MyVarContext;
    MyVarContext *ctx = new_context();
    am_Id var = am_newvariable(solver, &ctx->value);
    /* retrive the context: */
    MyVarContext *ctx = (MyVarContext*)am_varvalue(var, NULL);
  • void am_delvariable(am_Solver *solver, am_Id var);

    Remove this variable, after that, it cannot be added into further constraints.

  • void am_refcount(am_Solver *solver, am_Id var);

    Retrieve the refcount of the variable.

  • void am_varvalue(am_Solver *solver, am_Id var, am_Num *newvalue);

    Retrieve or reset the value binding of the variable. if newvalue is NULL, Only retrieve the old value, otherwise the newvalue will be setted into var and original value returned.

  • int am_add(am_Constraint *cons);

    Add constraint into the solver it's created from.

  • int am_hasconstraint(am_Constraint *cons);

    Check whether a constraint has been added into the solver or not.

  • void am_remove(am_Constraint *cons);

    Remove added constraint.

  • int am_clearedits(am_Solver *solver);

    Remove all variable suggests from solver.

  • int am_hasedit(am_Solver *solver, am_Id var);

    Check whether a variable has suggested value in the solver.

  • int am_addedit(am_Solver *solver, am_Id var, am_Num strength);

    Prepare to change the value of variables or the strength value if the variable is in edit now.

  • void am_suggest(am_Solver *solver, am_Id var, am_Num value);

    Actually change the value of the variable var.

    After changed, other variable may changed due to the constraints in solver. If you do not want change the strength of suggest (default is AM_MEDIUM), you may call am_suggest() directly without am_addedit().

  • void am_deledit(am_Solver *solver, am_Id var);

    Cancel the modify of variable. The value binding by var will restore to the referred value according the solver.

  • am_Constraint *am_newconstraint(am_Solver *solver, am_Num strength);

    Create a new constraints. Notice that where are one pointer's space can be used to store context data in the head of am_Constraint, example:

    c
    am_Constraint *cons = am_newconstraint(solver, AM_REQUIRED);
    MyContext *ctx = new_my_context();
    ctx->mydata = 1;
    *(MyContext**)cons = ctx;
  • am_Constraint *am_cloneconstraint(am_Constraint *other, am_Num strength);

    Clone a new constraint from the existing one with new strength.

  • void am_resetconstraint(am_Constraint *cons);

    Remove all terms and variables from the constraint, restore it to blank state.

  • void am_delconstraint(am_Constraint *cons);

    Free the constraint. if it's added into solver, remove it first.

  • int am_addterm(am_Constraint *cons, am_Id var, am_Num multiplier);

    Add a term into constraint.

    Example: a constraint like 2*m == x+y, has terms 2*m, 1*x and 1*y. So to makeup this constraint, you could:

    c
    // 2*m == x + y
    am_addterm(c, m, 2.0); // 2*m
    am_setrelation(c, AM_EQUAL); // =
    am_addterm(c, x, 1.0); // x
    am_addterm(c, y, 1.0); // y
  • int am_setrelation(am_Constraint *cons, int relation);

    Set the relations of constraint, could be one of these:

    • AM_EQUAL
    • AM_GREATEQUAL
    • AM_LESSEQUAL

    The terms added before am_setrelation() become the left hand terms of constraints, and the terms adds after call will become the right hand terms of constraints.

  • int am_addconstant(am_Constraint *cons, am_Num constant);

    Add a constant without variable into constraint as a term.

  • int am_setstrength(am_Constraint *cons, am_Num strength);

    Set the strength of a constraint.

  • int am_mergeconstraint(am_Constraint *cons, const am_Constraint *other, am_Num multiplier);

    Merge other constraints into cons, with a multiplier multiples with other.

Issues· 0 open

View all issuesOpen on GitHub

No open issues yet, or sync has not completed.

> Tags

C++cassowary-algorithmconstraint-solving-algorithmlua-languagesingle-header-lib

No comments yet. Be the first to share.

> Details

PublishedAug 1, 2026
UpdatedSep 18, 2026
Category编程语言
PricingOpen source

> Related tools

T
TypeScript
JavaScript 的超集,为前端与全栈提供静态类型
P
Python
通用编程语言,广泛用于 Web、数据与 AI
G
Go
Google 推出的简洁高效系统语言