Jump to content

  • Log In with Google      Sign In   
  • Create Account


Member Since 04 Feb 2008
Offline Last Active Today, 01:43 AM

#5276165 They called me crazy!

Posted by fastcall22 on 17 February 2016 - 12:57 PM

But does it support FILE_NOT_FOUND?

#5275858 Data linking varibles

Posted by fastcall22 on 15 February 2016 - 11:22 PM

It is arbitrary. However, the more I think about it, I think it should go toward the selected slider. Just a thought, though.

#5275789 Data linking varibles

Posted by fastcall22 on 15 February 2016 - 12:24 PM

For the list, L=(1, 50, 25, 24), and a selected index, idx=0:

1. Find the sum of the others: sum = 50 + 25 + 24 = 99. You may want to cache these ratios until idx changes.
2. After changing L[idx], find remaining amount: remaining = 100-L[idx]
3. Adjust other values according to their proportion, with rounding: (L[jdx]*remaining)/sum.
4. Add or remove 1 to one of the other sliders to correctly account for rounding corner cases, if the sum doesn’t match 100.

Here’s a quick test in powershell:
foreach ( $z in ((50,25,24), (25,25,25)) ) {
  foreach ( $y in (0,1,50,99,100) ) {
    $remaining = 100-$y;
    $total = $z | measure -sum | select -expand sum;
    $others = $z |% {
      [Math]::floor(($_*$remaining + $total/2)/$total);
    $all = ,$y + $others;
    $sum = $all | measure -sum | select -expand sum;

    $adj = 100-$sum;
    $all[1] += $adj;
    $sum += $adj;

    "sliders=$all sum=$sum"

sliders= ​ 0 ​ 51 ​ 25 ​ 24 sum=100
sliders= ​ 1 ​ 50 ​ 25 ​ 24 sum=100
sliders= 50 ​ 25 ​ 13 ​ 12 sum=100
sliders= 99 ​ ​ 1 ​ ​ 0 ​ ​ 0 sum=100
sliders=100 ​ ​ 0 ​ ​ 0 ​ ​ 0 sum=100

sliders= ​ 0 ​ 34 ​ 33 ​ 33 sum=100
sliders= ​ 1 ​ 33 ​ 33 ​ 33 sum=100
sliders= 50 ​ 16 ​ 17 ​ 17 sum=100
sliders= 99 ​ ​ 1 ​ ​ 0 ​ ​ 0 sum=100
sliders=100 ​ ​ 0 ​ ​ 0 ​ ​ 0 sum=100

#5275603 Using SQL for Templated Data

Posted by fastcall22 on 14 February 2016 - 02:56 AM

Sorry. I only partially answered the question. You may want to check to see if common table expressions are supported by your database engine. Some popular database engines are a bit behind the times in supporting modern SQL features (*cough* MySQL *cough*). In any case, assuming a schema:
create table `prefabs` (
    `id` int primary key not null auto_increment,
    `parent_id` int null,
    `name` varchar(50) not null

create table `prefab_properties` (
    `id` int primary key not null auto_increment,
    `prefab_id` int not null,
    `property_id` int not null,
    `value` varbinary(128) null

create table `properties` (
    `id` int primary key not null auto_increment,
    `name` varchar(100) not null,
    `type` varchar(10) not null /* int, string, color, float, and et. al. */
Then you could use CTEs to do something like:
1. Walk the inheritance tree. The CTE will build a set of all parents for every node.
2. Aggregate non-null properties for every node, and select the first property of that type.

with `tree` as ( /* set of all prefabs’ parents */
    select  p.id        `root_id`,
            p.id        `current_id`,
            p.parent_id `parent_id`,
            1           `level`
    from    prefabs p
    where   :has_prefab_ids is null
    or      p.id in :prefab_ids
    union all
    select  t.root_id,
            t.level + 1
    from    tree t
    join    prefabs p
    on      t.parent_id = p.id
), `tree_props` as ( /* join prefabs in `tree` with their non-null properties */
    select  t.*,
            pp.id `pp_id`
    from    tree `t`
    join    prefab_properties `pp`
    on      t.current_id = pp.prefab_id
    where   pp.value is not null
) select
    _1.root_id `prefab_id`,
    t.current_id `inherited_from`
from ( /* first encountered non-null properties of prefab */
    select  tp.root_id,
            min(tp.level) `min_level`
    from    tree_props `tp`
    group by
) _1
inner join /* rejoin tree_props to obtain property value */
    tree_props `tp`
on  _1.root_id = tp.root_id
and _1.property_id = tp.property_id
and _1.min_level = tp.level
inner join
    prefab_properties pp
on  tp.pp_id = pp.id
Of course, a sane person might consider calculating effective properties in software, rather than rely on a database to do it for them. smile.png

#5275596 Using SQL for Templated Data

Posted by fastcall22 on 13 February 2016 - 09:13 PM

This is usually done with the coalescing function:
with `objects` (`id`,`parent_id`,`name`,`type`,`value`) as ( values
    ( 1, null, 'fruit',     'F', -5 ),
    ( 2, null, 'vegetable', 'B',  7 ),
    ( 3, 1,    'apple',     null, 100 ),
    ( 4, 2,    'carrot',    'X',  null )
) select
    coalesce(child.name, parent.name) `name`,
    coalesce(child.type, parent.type) `type`,
    coalesce(child.value, parent.value) `value`
    `objects` child
left outer join
    `objects` parent
on  child.parent_id = parent.id
This might be incorrect (modern) SQL syntax, but it should get the point across.

#5273245 Warning conversion from size_t to int in x64

Posted by fastcall22 on 29 January 2016 - 01:35 PM

for (int n = 7; n>=0; --n) ...

Just write it like this, lol:
for ( unsigned n = 7; n <= 7; --n )

Problem solved?

#5271582 Passing 2d char array through functions in c

Posted by fastcall22 on 17 January 2016 - 01:23 PM

To explain the differences, a char[8][12] is 96 contiguous bytes, packed tightly with 8 12-byte strings. To iterate through each string, you advance 12 bytes within the array. A char*[8], however, is 8*sizeof(void*) bytes, with pointers that don't necessarily point within a contiguous array.

#5269999 When you realize how dumb a bug is...

Posted by fastcall22 on 08 January 2016 - 12:32 AM

PS> $L

PS> $R

PS> $L -lt $R

PS> 0.576 -lt -4.132

PS> $L.GetType().Name

PS> throw 'omg powershell pls';

#5268930 Reducing byte transfer between C++ and HLSL.

Posted by fastcall22 on 02 January 2016 - 09:17 PM

Yes, it is possible, but...

You'll do more work unpacking the unsigned on the GPU than you would just passing in the other three floats-- by several orders of magnitude.

#5268668 C++ without pointers

Posted by fastcall22 on 31 December 2015 - 09:57 PM

You cannot get very far without using just pointers in C++. Eventually you'll run into object lifetime issues. See C++ Faq Lite on references.

In summary:

1. A reference cannot be re-seated.
2. An object must be stored somewhere, and you'll run into object slicing.
3. If you want the benefits of references without the "danger" of pointer (not implying that references are inherently safe), then look into using smart pointers instead.

inb4 Servant of the Lord's in depth explanation!

#5268559 When you realize how dumb a bug is...

Posted by fastcall22 on 31 December 2015 - 12:20 AM

I can talk all day about the horrors of PHP, but I would just like to share this one that had bit me in the dick when I tried to be overly helpful:
# Web framework and ORM nonsense assumed here:
# require('zomg_ezpz_web_framework.php');
class ORM_Model {
	public static function fetch($id) { return db::query([/*...*/]); }
	public static function fetch_field($id,$field) { return db::query([/*...*/]);}
	public static function find($options) { return db::query([[/*...*/]]); }
	public static $_table_name;
	public static $_fields;

class FoobarModel extends ORM_Model {
	 * getRemainingX
	 * Returns the difference of a Foobar's value_x against its Foobazs' value_y.
	 * The foobar can be specified by numeric id, or by a Foobar object.
	 * @param  mixed  $id_or_Foobar, either numeric or array
	 * @return float  Foobar.value_x - sum(Foobar.Foobaz.value_y)
	public static function getRemainingX($id_or_Foobar){
		# extract or retrieve id and value_x 
		if ( @$id_or_Foobar['id'] ) {
			$Foobar = $id_or_Foobar;
			$id = $Foobar['id'];
			$value_x = $Foobar['value_x'];
		} else {
			$id = $id_or_Foobar;
			$value_x = FoobarModel::fetch_field($id,'value_x');

		# fetch and sum the foobar's foobazs' value_y 
		$q = FoobazModel::find([
			'fields' => 'sum(Foobaz.value_y) `sum`',
			'where'  => ['foobar_id' => $id],
		$y_sum = $q[0]['sum'];

		# return the foobar's remaining value_x
		return $value_x - $y_sum;

class FoobarController extends WebFramework_Controller {
	 * @param   int $foobar_id
	 * @return  rendered web page
	public function remaining_x($foobar_id) {
		# spew out a pretty web page with the foobar's remaining value_x
		$remaining = FoobarModel::getRemainingX($foobar_id);
		$this->prepare_view(['remaining' => $remaining]);

		return $this->render_view();
So, when a user visits example.com/foobar/3/remaining_x, it'll fetch foobar with ID 3, sum its related foobazs' value_y and return a pretty web page with the difference between the foobar's value_x and value_y sum on it.

At least, it should. For some reason, it was returning incorrect results for foobars with ids higher than 9. Any guesses?


#5264472 a good source for learning to write advanced sql queries

Posted by fastcall22 on 01 December 2015 - 02:59 PM

As much as I hate lmgtfy responses, I will have to agree with ChaosEngine.

Part of writing good SQL queries is asking the right kind of questions. There are plenty of examples you can find at your finger tips. Though, I'm ashamed to admit how many times I have needed to look up "mysql update with join", "mysql delete from with alias"-- I always seem to forget the language-specific details.

Most ORMs can translate reasonably well to MySQL, though raw SQL will be needed when the ORM doesn't cut it.

#5264281 Why didn't somebody tell me?

Posted by fastcall22 on 30 November 2015 - 12:46 PM

cd - -> (cd space minus-sign enter) goes back to previous directory

And how about pushd and popd?

#5264056 Best route to take for automatically going through a program

Posted by fastcall22 on 28 November 2015 - 11:33 PM

Games typically have command line arguments that allow you to launch the game in various ways. The goldsrc and source engines (half-life and half-life 2 respectively) have command line options to launch a single player or multiplayer game from the command line. I can't remember what the parameters were exactly, but it probably went something like this:

hl2.exe --console +sv_cheats 1 +developer 2 +map path/to/your/map.bsp

If your editor doesn't have a button to launch the game with your map (as does worldcraft/hammer), then check the game's documentation and see if it has any command line options.

#5264039 Support for unicode identifiers

Posted by fastcall22 on 28 November 2015 - 04:44 PM

Alex, I don't think it is feasible. As an alternative, you could use an AutoHotKey script. A script that would replace the typed character sequence ":肉" with "class", or ":肐" with "function" and etc.

As for library methods, I can only think an additional layer of indirection would work-- a dictionary mapping translations to english.