Memory Layout Choice of N-Dimensional Array - c

I've started writing a library for n-dimensional vectors.
There's a couple of design features that I would like to support in this
library:
The memory layout is continuous. This is so that sharing memory, sending over a network, and saving to file is only a matter of passing a pointer.
Different dimensions can support different element sizes (ie [0][0] may be 1 byte while [1][0] may be 2 bytes).
After drawing out some representations of what the memory layout
would look like for vectors of certain dimensions, I've realized that
there is two ways to layout the structure:
Here's the first way. Both representations are a visualization of
a [2][4][3] array.
---------------------------
| index | 0:0:0 |
| elementSize | |
| elementCount | |
| expansionBoundary | |
| slotCount | |
---------------------------
---------------------------
| index | 1:0:0 |
| elementSize | |
| elementCount | |
| expansionBoundary | |
| slotCount | |
---------------------------
---------------------------
| index | 0:0:0 |
| elementSize | |
| elementCount | |
| expansionBoundary | |
| slotCount | |
---------------------------
---------------------------
| index | 0:1:0 |
| elementSize | |
| elementCount | |
| expansionBoundary | |
| slotCount | |
---------------------------
---------------------------
| index | 0:2:0 |
| elementSize | |
| elementCount | |
| expansionBoundary | |
| slotCount | |
---------------------------
---------------------------
| index | 0:3:0 |
| elementSize | |
| elementCount | |
| expansionBoundary | |
| slotCount | |
---------------------------
---------------------------
| index | 1:0:0 |
| elementSize | |
| elementCount | |
| expansionBoundary | |
| slotCount | |
---------------------------
---------------------------
| index | 1:1:0 |
| elementSize | |
| elementCount | |
| expansionBoundary | |
| slotCount | |
---------------------------
---------------------------
| index | 1:2:0 |
| elementSize | |
| elementCount | |
| expansionBoundary | |
| slotCount | |
---------------------------
---------------------------
| index | 1:3:0 |
| elementSize | |
| elementCount | |
| expansionBoundary | |
| slotCount | |
---------------------------
---------------------------
| Index | 0:0:0 |
| Element | |
---------------------------
---------------------------
| Index | 0:0:1 |
| Element | |
---------------------------
---------------------------
| Index | 0:0:2 |
| Element | |
---------------------------
---------------------------
| Index | 0:1:0 |
| Element | |
---------------------------
---------------------------
| Index | 0:1:1 |
| Element | |
---------------------------
---------------------------
| Index | 0:1:2 |
| Element | |
---------------------------
---------------------------
| Index | 0:2:0 |
| Element | |
---------------------------
---------------------------
| Index | 0:2:1 |
| Element | |
---------------------------
---------------------------
| Index | 0:2:2 |
| Element | |
---------------------------
---------------------------
| Index | 0:3:0 |
| Element | |
---------------------------
---------------------------
| Index | 0:3:1 |
| Element | |
---------------------------
---------------------------
| Index | 0:3:2 |
| Element | |
---------------------------
---------------------------
| Index | 1:0:0 |
| Element | |
---------------------------
---------------------------
| Index | 1:0:1 |
| Element | |
---------------------------
---------------------------
| Index | 1:0:2 |
| Element | |
---------------------------
---------------------------
| Index | 1:1:0 |
| Element | |
---------------------------
---------------------------
| Index | 1:1:1 |
| Element | |
---------------------------
---------------------------
| Index | 1:1:2 |
| Element | |
---------------------------
---------------------------
| Index | 1:2:0 |
| Element | |
---------------------------
---------------------------
| Index | 1:2:1 |
| Element | |
---------------------------
---------------------------
| Index | 1:2:2 |
| Element | |
---------------------------
---------------------------
| Index | 1:3:0 |
| Element | |
---------------------------
---------------------------
| Index | 1:3:1 |
| Element | |
---------------------------
---------------------------
| Index | 1:3:2 |
| Element | |
---------------------------
Here's the second way.
---------------------------
| index | 0:0:0 |
| elementSize | |
| elementCount | |
| expansionBoundary | |
| slotCount | |
---------------------------
---------------------------
| index | 0:0:0 |
| elementSize | |
| elementCount | |
| expansionBoundary | |
| slotCount | |
---------------------------
---------------------------
| Index | 0:0:0 |
| Element | |
---------------------------
---------------------------
| Index | 0:0:1 |
| Element | |
---------------------------
---------------------------
| Index | 0:0:2 |
| Element | |
---------------------------
---------------------------
| index | 0:1:0 |
| elementSize | |
| elementCount | |
| expansionBoundary | |
| slotCount | |
---------------------------
---------------------------
| Index | 0:1:0 |
| Element | |
---------------------------
---------------------------
| Index | 0:1:1 |
| Element | |
---------------------------
---------------------------
| Index | 0:1:2 |
| Element | |
---------------------------
---------------------------
| index | 0:2:0 |
| elementSize | |
| elementCount | |
| expansionBoundary | |
| slotCount | |
---------------------------
---------------------------
| Index | 0:2:0 |
| Element | |
---------------------------
---------------------------
| Index | 0:2:1 |
| Element | |
---------------------------
---------------------------
| Index | 0:2:2 |
| Element | |
---------------------------
---------------------------
| index | 0:3:0 |
| elementSize | |
| elementCount | |
| expansionBoundary | |
| slotCount | |
---------------------------
---------------------------
| Index | 0:3:0 |
| Element | |
---------------------------
---------------------------
| Index | 0:3:1 |
| Element | |
---------------------------
---------------------------
| Index | 0:3:2 |
| Element | |
---------------------------
---------------------------
| index | 1:0:0 |
| elementSize | |
| elementCount | |
| expansionBoundary | |
| slotCount | |
---------------------------
---------------------------
| index | 1:0:0 |
| elementSize | |
| elementCount | |
| expansionBoundary | |
| slotCount | |
---------------------------
---------------------------
| Index | 1:0:0 |
| Element | |
---------------------------
---------------------------
| Index | 1:0:1 |
| Element | |
---------------------------
---------------------------
| Index | 1:0:2 |
| Element | |
---------------------------
---------------------------
| index | 1:1:0 |
| elementSize | |
| elementCount | |
| expansionBoundary | |
| slotCount | |
---------------------------
---------------------------
| Index | 1:1:0 |
| Element | |
---------------------------
---------------------------
| Index | 1:1:1 |
| Element | |
---------------------------
---------------------------
| Index | 1:1:2 |
| Element | |
---------------------------
---------------------------
| index | 1:2:0 |
| elementSize | |
| elementCount | |
| expansionBoundary | |
| slotCount | |
---------------------------
---------------------------
| Index | 1:2:0 |
| Element | |
---------------------------
---------------------------
| Index | 1:2:1 |
| Element | |
---------------------------
---------------------------
| Index | 1:2:2 |
| Element | |
---------------------------
---------------------------
| index | 1:3:0 |
| elementSize | |
| elementCount | |
| expansionBoundary | |
| slotCount | |
---------------------------
---------------------------
| Index | 1:3:0 |
| Element | |
---------------------------
---------------------------
| Index | 1:3:1 |
| Element | |
---------------------------
---------------------------
| Index | 1:3:2 |
| Element | |
---------------------------
Are there certain advantages/disadvantages in choosing one layout over the other?

Related

Alternative piece movement on a 10x10 board in C

I am trying to make the pieces on my board move from left to right and then right to left whenever it moves up a line on the board. I am using 2D arrays to represent the coordinates and pointer arithmetic to manipulate it since we cannot use 2D arrays on the function itself.
while (k <= 11) { //I used K as a marker for the 11 vertical line Im going to make
if (lineOne == 1) {
printf("|");
lineOne = 0;
lineTwo = 1;
k++;
} else if (lineTwo == 1) {
if (row % 2 == 0) { //pointer arithmetic to go through each part of the array
printf("%c%c%c%c", *( * (arrA + row) + col),
*( * (arrB + row) + col),
*( * (arrC + row) + col),
*( * (arrD + row) + col));
col++;
} else if (row % 2 != 0) {
printf("%c%c%c%c", *( * (arrA + row) + col),
*( * (arrB + row) + col),
*( * (arrC + row) + col),
*( * (arrD + row) + col));
col--;
}
lineOne = 1;
lineTwo = 0;
}
}
row++;
divider = 1;
thirdLine = 0;
printf("\n");
These are my initializations
int row = 0, col = 0;
In my head as well, my 2D grid is arranged like this
0
1
2
3
4
5
6
7
8
9
0 1 2 3 4 5 6 7 8 9
This is the output of my code. Dont mind the S, its a bug for another day :(
+----+----+----+----+----+----+----+----+----+----+
|# |S |S |S |S |S |S |S |S |S |
| | | | | | | | | | |
+----+----+----+----+----+----+----+----+----+----+
| | | | | | | | | | |
| | | | | | | | | | |
+----+----+----+----+----+----+----+----+----+----+
| | | | | | | | | | |
| | | | | | | | | | |
+----+----+----+----+----+----+----+----+----+----+
| | | | | | | | | | |
| | | | | | | | | | |
+----+----+----+----+----+----+----+----+----+----+
| | | | | | | | | | |
| | | | | | | | | | |
+----+----+----+----+----+----+----+----+----+----+
| | | | | | | | | | |
| | | | | | | | | | |
+----+----+----+----+----+----+----+----+----+----+
| | | | | | | | | | |
| | | | | | | | | | |
+----+----+----+----+----+----+----+----+----+----+
| | | | | | | | | | |
| | | | | | | | | | |
+----+----+----+----+----+----+----+----+----+----+
| | | | | | | | | | |
| | | | | | | | | | |
+----+----+----+----+----+----+----+----+----+----+
| | | | | | | | | | |
|- | | | | | | | | | |
+----+----+----+----+----+----+----+----+----+----+
a's turn.
a, press <Enter> to roll.
You rolled a 5
95
95
+----+----+----+----+----+----+----+----+----+----+
|# |S |S |S |S |S |S |S |S |S |
| | | | | | | | | | |
+----+----+----+----+----+----+----+----+----+----+
| | | | | | | | | | |
| | | | | | | | | | |
+----+----+----+----+----+----+----+----+----+----+
| | | | | | | | | | |
| | | | | | | | | | |
+----+----+----+----+----+----+----+----+----+----+
| | | | | | | | | | |
| | | | | | | | | | |
+----+----+----+----+----+----+----+----+----+----+
| | | | | | | | | | |
| | | | | | | | | | |
+----+----+----+----+----+----+----+----+----+----+
| | | | | | | | | | |
| | | | | | | | | | |
+----+----+----+----+----+----+----+----+----+----+
| | | | | | | | | | |
| | | | | | | | | | |
+----+----+----+----+----+----+----+----+----+----+
| | | | | | | | | | |
| | | | | | | | | | |
+----+----+----+----+----+----+----+----+----+----+
| | | | | | | | | | |
| | | | | | | | | | |
+----+----+----+----+----+----+----+----+----+----+
| | | | | | | | | | |
| | | | | |a | | | | |
+----+----+----+----+----+----+----+----+----+----+
95
get out
b's turn.
b, press <Enter> to roll.
Here 9 5 are the coordinates I have for that player
As you can see a moved 5 steps to the left when it was supposed to do it from left to right like this
+----+----+----+----+----+----+----+----+----+----+
|# | S| | | | | | | | |
| | | | | | | | | | |
+----+----+----+----+----+----+----+----+----+----+
| | | | | | |l | | | |
| | | | | | | | | | |
+----+----+----+----+----+----+----+----+----+----+
| | | | | | | | | | |
| | | | | | | | | | |
+----+----+----+----+----+----+----+----+----+----+
| |l | S| | | | | | s| |
| | | | | | | | | | |
+----+----+----+----+----+----+----+----+----+----+
| | | | | |L | | | | |
| | | | | | | | | | |
+----+----+----+----+----+----+----+----+----+----+
| | | | | | | | | | |
| | | | | | | | | | |
+----+----+----+----+----+----+----+----+----+----+
| | | |l | | |L |S | | |
| | | | | | | | | | |
+----+----+----+----+----+----+----+----+----+----+
| | | | | | | | | | |
| | | | | | | | | | |
+----+----+----+----+----+----+----+----+----+----+
| | | | | | s| | | | |
| | | | | | | | | | |
+----+----+----+----+----+----+----+----+----+----+
| | s|L | | | | | | | |
|abcd| | | | | | | | | |
+----+----+----+----+----+----+----+----+----+----+
a's turn.
a, press <Enter> to roll.
+----+----+----+----+----+----+----+----+----+----+
|# | S| | | | | | | | |
| | | | | | | | | | |
+----+----+----+----+----+----+----+----+----+----+
| | | | | | |l | | | |
| | | | | | | | | | |
+----+----+----+----+----+----+----+----+----+----+
| | | | | | | | | | |
| | | | | | | | | | |
+----+----+----+----+----+----+----+----+----+----+
| |l | S| | | | | | s| |
| | | | | | | | | | |
+----+----+----+----+----+----+----+----+----+----+
| | | | | |L | | | | |
| | | | | | | | | | |
+----+----+----+----+----+----+----+----+----+----+
| | | | | | | | | | |
| | | | | | | | | | |
+----+----+----+----+----+----+----+----+----+----+
| | | |l | | |L |S | | |
| | | | | | | | | | |
+----+----+----+----+----+----+----+----+----+----+
| | | | | | | | | | |
| | | | | | | | | | |
+----+----+----+----+----+----+----+----+----+----+
| | | | | | s| | | | |
| | | | | | | | | | |
+----+----+----+----+----+----+----+----+----+----+
| | s|L | | | | | | | |
| bcd| | | | a| | | | | |
+----+----+----+----+----+----+----+----+----+----+
a rolls a 4!
b's turn
b, press <Enter> to roll.
I just want my pieces to move alternately for each time they move up a Y-axis.

SQL Server: How to unpivot from pivoted table back to a self referencing table

I've looked at examples from: https://learn.microsoft.com/en-us/sql/t-sql/queries/from-using-pivot-and-unpivot?view=sql-server-ver15 but I couldn't seem to find samples of what I'm trying to do.
I'm wondering if there's a way to unpivot from this:
+----+------------+--------+--------+--------+
| Id | Level0 | Level1 | Level2 | Level3 |
+----+------------+--------+--------+--------+
| 0 | TMI | | | |
+----+------------+--------+--------+--------+
| 1 | TMI | A | | |
+----+------------+--------+--------+--------+
| 2 | TMI | A | B | |
+----+------------+--------+--------+--------+
| 3 | TMI | A | B | C |
+----+------------+--------+--------+--------+
| 4 | TMI | A | B | D |
+----+------------+--------+--------+--------+
Back to self referencing table like this:
+----+-----------+----------+--------+
| Id | LevelName | ParentId | Level |
+----+-----------+----------+--------+
| 0 | TMI | | Level0 |
+----+-----------+----------+--------+
| 1 | A | 0 | Level1 |
+----+-----------+----------+--------+
| 2 | B | 1 | Level2 |
+----+-----------+----------+--------+
| 3 | C | 2 | Level3 |
+----+-----------+----------+--------+
| 4 | D | 2 | Level3 |
+----+-----------+----------+--------+

compare two tables in a query taking so long time

I have a a query where i compare 350 rows with other tables which are having 50000 rows it takes 5 minutes to give result.Any faster way to retrieve values get earlier.
select lower(rtrim(substring(rt_queue, 3, 6))) + " Ticor - " + convert(varchar(4), sort_grp_id) + " " + rtrim(batch_no) + ".prn" as FILENAME from tprt_queue where cycle_date >= (select CYCLE from tb_jpachi_cycle) and batch_no <> 'JOBCTR-' and rt_queue not like '%CN%' and status = 'TINTED' and lower(rtrim(substring(prt_queue, 3, 6))) + " Bicor - " + convert(varchar(4), sort_grp_id) + " " + rtrim(batch_no) + ".prn" not in (
select FILENAME from tb_jpachi_filesprocess ) order by rt_dt
This one contains 100000 rows : select FILENAME from tb_jpachi_filesprocess
Later it will increase day by day.
Here is query plan :
QUERY PLAN FOR STATEMENT 1 (at line 1).
STEP 1
The type of query is EXECUTE.
Executing a newly cached statement (SSQL_ID = 230588804).
QUERY PLAN FOR STATEMENT 1 (at line 0).
STEP 1
The type of query is DECLARE.
QUERY PLAN FOR STATEMENT 2 (at line 1).
Optimized using Serial Mode
STEP 1
The type of query is SELECT.
10 operator(s) under root
|ROOT:EMIT Operator (VA = 10)
|
| |RESTRICT Operator (VA = 9)(0)(0)(0)(0)(9)
| |
| | |SEQUENCER Operator (VA = 8) has 2 children.
| | |
| | | |SCALAR AGGREGATE Operator (VA = 1)
| | | | Evaluate Ungrouped ONCE AGGREGATE.
| | | |
| | | | |SCAN Operator (VA = 0)
| | | | | FROM TABLE
| | | | | tb_jpachi_cycle
| | | | | Table Scan.
| | | | | Forward Scan.
| | | | | Positioning at start of table.
| | | | | Using I/O Size 2 Kbytes for data pages.
| | | | | With LRU Buffer Replacement Strategy for data pages.
| | |
| | | |SQFILTER Operator (VA = 7) has 2 children.
| | | |
| | | | |RESTRICT Operator (VA = 3)(0)(0)(0)(6)(0)
| | | | |
| | | | | |SCAN Operator (VA = 2)
| | | | | | FROM TABLE
| | | | | | tprt_queue
| | | | | | Using Clustered Index.
| | | | | | Index : pk_tprt_queue
| | | | | | Forward Scan.
| | | | | | Positioning at index start.
| | | | | | Using I/O Size 16 Kbytes for index leaf pages.
| | | | | | With LRU Buffer Replacement Strategy for index leaf pages.
| | | | | | Using I/O Size 16 Kbytes for data pages.
| | | | | | With LRU Buffer Replacement Strategy for data pages.
| | | |
| | | | Run subquery 1 (at nesting level 1).
| | | |
| | | | QUERY PLAN FOR SUBQUERY 1 (at nesting level 1 and at line 2).
| | | |
| | | | Correlated Subquery.
| | | | Subquery under an IN predicate.
| | | |
| | | | |SCALAR AGGREGATE Operator (VA = 6)
| | | | | Evaluate Ungrouped ANY AGGREGATE.
| | | | | Scanning only up to the first qualifying row.
| | | | |
| | | | | |RESTRICT Operator (VA = 5)(9)(0)(0)(15)(0)
| | | | | |
| | | | | | |SCAN Operator (VA = 4)
| | | | | | | FROM TABLE
| | | | | | | tb_jpachi_filesprocess
| | | | | | | Table Scan.
| | | | | | | Forward Scan.
| | | | | | | Positioning at start of table.
| | | | | | | Using I/O Size 16 Kbytes for data pages.
| | | | | | | With LRU Buffer Replacement Strategy for data pages.
| | | |
| | | | END OF QUERY PLAN FOR SUBQUERY 1.

Need to update "orderby" column

I have a table test
+----+--+------+--+--+----------+--+--------------+
| ID | | Name | | | orderby | | processgroup |
+----+--+------+--+--+----------+--+--------------+
| 1 | | ABC | | | 10 | | 1 |
| 10 | | DEF | | | 12 | | 1 |
| 15 | | LMN | | | 1 | | 1 |
| 44 | | JKL | | | 4 | | 1 |
| 42 | | XYZ | | | 3 | | 2 |
+----+--+------+--+--+----------+--+--------------+
I want to update the orderby column in the sequence, I am expecting output like
+----+--+------+--+--+----------+--+--------------+
| ID | | Name | | | orderby | | processgroup |
+----+--+------+--+--+----------+--+--------------+
| 1 | | ABC | | | 1 | | 1 |
| 10 | | DEF | | | 2 | | 1 |
| 15 | | LMN | | | 3 | | 1 |
| 44 | | JKL | | | 4 | | 1 |
| 42 | | XYZ | | | 5 | | 1 |
+----+--+------+--+--+----------+--+--------------+
Logic behind this is when we have procesgroup as 1, orderby column should update as 1,2,3,4 and when procesgroup is 2 then update orderby as 5.
This might help you
;WITH CTE AS (
SELECT ROW_NUMBER() OVER (ORDER BY processgroup, ID ) AS SNO, ID FROM TABLE1
)
UPDATE TABLE1 SET TABLE1.orderby= CTE.SNO FROM CTE WHERE TABLE1.ID = CTE.ID

Database queues in Asterisk - timeout not being honored

I setup dynamic queues in Asterisk according to this: http://libryder.com/Ez7llN
The queues connect successfully and all members are recognized but it always rings to voicemail, never trying the next member.
+----+------------+-------------+----------+---------+---------+--------------+----------------+------------------+----------------+--------------------+----------------+---------------+---------------+----------------+----------------+------------------+--------------------+------------------------+-------------------+-------+------------+--------+--------------+----------+-----------+----------------+-------------------+-----------------+----------------+-------------+--------+----------------+-----------+-----------------+----------+-----------------+
| id | name | musiconhold | announce | context | timeout | monitor_join | monitor_format | queue_youarenext | queue_thereare | queue_callswaiting | queue_holdtime | queue_minutes | queue_seconds | queue_lessthan | queue_thankyou | queue_reporthold | announce_frequency | announce_round_seconds | announce_holdtime | retry | wrapuptime | maxlen | servicelevel | strategy | joinempty | leavewhenempty | eventmemberstatus | eventwhencalled | reportholdtime | memberdelay | weight | timeoutrestart | ringinuse | setinterfacevar | autofill | timeoutpriority |
+----+------------+-------------+----------+---------+---------+--------------+----------------+------------------+----------------+--------------------+----------------+---------------+---------------+----------------+----------------+------------------+--------------------+------------------------+-------------------+-------+------------+--------+--------------+----------+-----------+----------------+-------------------+-----------------+----------------+-------------+--------+----------------+-----------+-----------------+----------+-----------------+
| 5 | 4352754426 | NULL | NULL | NULL | 5 | NULL | NULL | NULL | NULL | NULL | NULL | NULL | NULL | NULL | NULL | NULL | NULL | NULL | NULL | NULL | NULL | 0 | NULL | ringall | NULL | NULL | NULL | NULL | NULL | 0 | NULL | NULL | NULL | yes | yes | conf |
+----+------------+-------------+----------+---------+---------+--------------+----------------+------------------+----------------+--------------------+----------------+---------------+---------------+----------------+----------------+------------------+--------------------+------------------------+-------------------+-------+------------+--------+--------------+----------+-----------+----------------+-------------------+-----------------+----------------+-------------+--------+----------------+-----------+-----------------+----------+-----------------+
+----------+------------+------------+---------------------------+---------+--------+---------+
| uniqueid | membername | queue_name | interface | penalty | paused | hunt_id |
+----------+------------+------------+---------------------------+---------+--------+---------+
| 18 | agent1 | 4352754426 | local/4352151050#outbound | NULL | NULL | 5 |
| 19 | agent2 | 4352754426 | local/4352151052#outbound | NULL | NULL | 5 |
| 20 | agent3 | 4352754426 | local/4352151054#outbound | NULL | NULL | 5 |
+----------+------------+------------+---------------------------+---------+--------+---------+
Am I missing some option?
Please try with different ring strategy instead of ringall like random or rrmemory. And please also check timeout field it look too low ?

Resources