[Include] Numeric combinations.
#1

Combinations
This include allows you to use a numeric combinations in a one single variable to store basic data, instead of using two or more ones, so you will be able to save a lot of memory (36 bytes for every var, to be exact).

Usage
This may be confusing at first, but it's simple.
By using this include, you can delete those variables which only store few amount of value, and store all them in a single one.
For example: using per-player vars (MAX_PLAYERS = 100)

PHP код:
new var1[MAX_PLAYERS],
var2[MAX_PLAYERS],
var3[MAX_PLAYERS],
var4[MAX_PLAYERS],
var5[MAX_PLAYERS],
var6[MAX_PLAYERS],
var7[MAX_PLAYERS],
var8[MAX_PLAYERS],
var9[MAX_PLAYERS],
var10[MAX_PLAYERS];
// ...
var1[playerid] = 2;
var2[playerid] = 0;
var3[playerid] = 9;
var4[playerid] = 4;
var5[playerid] = 6;
var6[playerid] = 3;
var7[playerid] = 5;
var8[playerid] = 4;
var9[playerid] = 1;
var10[playerid] = 1;
// ...
printf("var 1: %i"var1[playerid]); // output "var 1: 2"
printf("var 2: %i"var2[playerid]); // output "var 2: 0"
printf("var 3: %i"var3[playerid]); // output "var 3: 9"
printf("var 4: %i"var4[playerid]); // output "var 4: 4"
printf("var 5: %i"var4[playerid]); // output "var 5: 6"
printf("var 6: %i"var4[playerid]); // output "var 6: 3"
printf("var 7: %i"var4[playerid]); // output "var 7: 5"
printf("var 8: %i"var4[playerid]); // output "var 8: 3"
printf("var 9: %i"var4[playerid]); // output "var 9: 1"
printf("var 10: %i"var4[playerid]); // output "var 10: 1" 
It will eat 4,000 bytes ((10 * 100) * 4).
Now look at this, using numeric combinations:
PHP код:
#include <combinations>
// Use defines or something for avoid confusions
#define COMBINATION_VAR_1 (0)
#define COMBINATION_VAR_2 (1)
#define COMBINATION_VAR_3 (2)
#define COMBINATION_VAR_4 (3)
#define COMBINATION_VAR_5 (4)
#define COMBINATION_VAR_6 (5)
#define COMBINATION_VAR_7 (6)
#define COMBINATION_VAR_8 (7)
#define COMBINATION_VAR_9 (8)
#define COMBINATION_VAR_10 (9)
new pCombs[MAX_PLAYERS];
// ...
SetCombination(pCombs[playerid], COMBINATION_VAR_12);
SetCombination(pCombs[playerid], COMBINATION_VAR_20);
SetCombination(pCombs[playerid], COMBINATION_VAR_39);
SetCombination(pCombs[playerid], COMBINATION_VAR_44);
SetCombination(pCombs[playerid], COMBINATION_VAR_56);
SetCombination(pCombs[playerid], COMBINATION_VAR_63);
SetCombination(pCombs[playerid], COMBINATION_VAR_75);
SetCombination(pCombs[playerid], COMBINATION_VAR_84);
SetCombination(pCombs[playerid], COMBINATION_VAR_91);
SetCombination(pCombs[playerid], COMBINATION_VAR_101);
// ...
printf("comb 1: %i"GetCombination(pCombs[playerid], COMBINATION_VAR_1)); // output "comb 1: 2"
printf("comb 2: %i"GetCombination(pCombs[playerid], COMBINATION_VAR_2)); // output "comb 2: 0"
printf("comb 3: %i"GetCombination(pCombs[playerid], COMBINATION_VAR_3)); // output "comb 3: 9"
printf("comb 4: %i"GetCombination(pCombs[playerid], COMBINATION_VAR_4)); // output "comb 4: 4"
printf("comb 5: %i"GetCombination(pCombs[playerid], COMBINATION_VAR_5)); // output "comb 5: 6"
printf("comb 6: %i"GetCombination(pCombs[playerid], COMBINATION_VAR_6)); // output "comb 6: 3"
printf("comb 7: %i"GetCombination(pCombs[playerid], COMBINATION_VAR_7)); // output "comb 7: 5"
printf("comb 8: %i"GetCombination(pCombs[playerid], COMBINATION_VAR_8)); // output "comb 8: 4"
printf("comb 9: %i"GetCombination(pCombs[playerid], COMBINATION_VAR_9)); // output "comb 9: 1"
printf("comb 10: %i"GetCombination(pCombs[playerid], COMBINATION_VAR_10)); // output "comb 10: 1" 
It will eat just 400 bytes (a difference of 3,600 bytes). This may not be a huge difference, but image if you're using a lot of vars that only store, atleast, 0 and 1. It will help you saving a huge amount of memory.

This works by using that one single variable (in this case "pCombs", but you can use any variable) and save a value in one slot (or digit/index - as you want to call it) of the number.

Features
    • You only can save values between 0-9 for every slot.
    • You only can use slots from 0-9 for every variable.
    • You cannot use any tag in the variable.
    • Doesn't support negative values.
    • Support multi-dimensional and char array.
    • Be careful, if numeric combination reachs over 2,147,483,647 (cellmax), you will get wrongs values. To avoid this at all, make sure 9th slot only save values between 0 and 1.
    • This is a good and easy way to save/load player-settings!

Increase/Decrease
Since by this way isn't possible to do ++/--, you must do this:
PHP код:
new val GetCombination(my_varmy_slot) + 1// Increae
SetCombination(my_varmy_slotval);
new 
val GetCombination(my_varmy_slot) - 1// Decrease
SetCombination(my_varmy_slotval);
//
new val GetCombination(my_varmy_slot);
SetCombination(my_varmy_slot, ++val); // Increase
new val GetCombination(my_varmy_slot);
SetCombination(my_varmy_slot, --val); // Decrease 
Benchmarks
Код:
// 10,000 iterations

[Set] Tick: 33ms (0.0033 ms)
[Get] Tick: 24ms (0.0024 ms)

[Set] Tick: 35ms (0.0035 ms)
[Get] Tick: 18ms (0.0018 ms)

[Set] Tick: 31ms (0.0031 ms)
[Get] Tick: 18ms (0.0018 ms)
Download
http://pastebin.com/mkcHuTrZ
Reply
#2

That is a neat piece of coding, will edit my post once I take a deeper look onto it.
+2 reputation.
Reply
#3

Wow, that's cool, it's kinda like rBits but it's kinda better IMHO. Good job.

I don't understand the concept of how it's working, but however it's working
Reply
#4

Nice job. Very simple and and OK for 0-9 ranges. But that's it, 0-9 ranges.

This is why we also have rBits and the similar.
Reply
#5

Quote:
Originally Posted by Jayse
Посмотреть сообщение
Wow, that's cool, it's kinda like rBits but it's kinda better IMHO. Good job.

I don't understand the concept of how it's working, but however it's working
I think that BitFunctions is better because it allows you to edit bits / cells, using fast algorithms and is easy to use.
https://sampforum.blast.hk/showthread.php?tid=591223

I know even mBits which is in a small way faster than the current version BitFunctions, but it is not convenient to operate.
Reply
#6

The idea is good. I'd probably use something like this:
pawn Код:
// SetCombination:
comb[9 - slot] = (1 <= value <= 9) ? 48 + value : 48;

// GetCombination:
if (48 <= (slot = comb[9 - slot]) <= 57) return slot - 48;
instead of those switch statements though.
Reply
#7

rBits looks good I've never tested/used that system so I can't tell you something more!

Quote:
Originally Posted by Konstantinos
Посмотреть сообщение
The idea is good. I'd probably use something like this:
pawn Код:
// SetCombination:
comb[9 - slot] = (1 <= value <= 9) ? 48 + value : 48;

// GetCombination:
if (48 <= (slot = comb[9 - slot]) <= 57) return slot - 48;
instead of those switch statements though.
Do you mean do something like this?
PHP код:
stock SetCombination(&var, slotvalue)
{
    new 
comb[11];
    
format(comb11"%010i", var);
    
comb[slot] = (<= value <= 9) ? 48 value 48;
    return -
1;
}
stock GetCombination(var, slot)
{
    new 
comb[11];
    
format(comb11"%010i", var);
    if(
48 <= (slot comb[slot]) <= 57) return slot 48;
    return 
0;

It's great but always return 0 for me, I don't know if I'm doing it well :P
Reply
#8

Nice trick there, at first I thought it wouldn't work, but it does actually!

Konstantinos did well using ASCII, that's a good way to get rid of the switch statement.
PHP код:
stock SetCombination(&var, slotvalue)
{
    new 
comb[11];
    
format(comb11"%010i", var);
    
comb[slot] = (<= value <= 9) ? 48 value 48;
    var = 
strval(comb);
    return 
1;
}
stock GetCombination(var, slot)
{
    new 
comb[11];
    
format(comb11"%010i", var);
    if(
48 <= (slot comb[slot]) <= 57) return slot 48;
    return 
0;

It should work now.

Good job.
Reply
#9

How I forgot to add "var = strval(comb);"?

About @Konstantinos trick, I've made some benchmarks and it showed this:
Код:
[Set] Konstantinos - 318 ms
[Get] Konstantinos - 193 ms
[Set] Swedky - 291 ms
[Get] Swedky - 174 ms
It's a tiny difference but everything counts.

http://pastebin.com/J5AgC58n
Reply
#10

Quote:
Originally Posted by Swedky
Посмотреть сообщение
It's a tiny difference but everything counts.
You can avoid calling strval and set var directly like:
pawn Код:
var = comb[9 - slot] = ...;
Reply


Forum Jump:


Users browsing this thread: 1 Guest(s)