interval map in C++ - boost

I need to map some intervals (actually these are intervals of addresses) to object ids.
I tried to use boost's interval_map, the example looks very pretty, it easily enumerates all intervals like:
while(it != party.end())
{
interval<ptime>::type when = it->first;
// Who is at the party within the time interval 'when' ?
GuestSetT who = (*it++).second;
cout << when << ": " << who << endl;
}
Which outputs:
----- History of party guests -------------------------
[2008-May-20 19:30:00, 2008-May-20 20:10:00): Harry Mary
[2008-May-20 20:10:00, 2008-May-20 22:15:00): Diana Harry Mary Susan
[2008-May-20 22:15:00, 2008-May-20 23:00:00): Diana Harry Mary Peter Susan
[2008-May-20 23:00:00, 2008-May-21 00:00:00): Diana Peter Susan
[2008-May-21 00:00:00, 2008-May-21 00:30:00): Peter
but it cannot do something like this:
interval<ptime>::type when =
interval<ptime>::closed(
time_from_string("2008-05-20 22:00"),
time_from_string("2008-05-20 22:01"));
GuestSetT who = party[when];
cout << when << ": " << who << endl;
it outputs: error: no match for 'operator[]' in 'party[when]'
it looks strange, since the main function of map is in operator[]
so I cannot get information "who were at the party at a given time"
Is there a ready-to-use solution for this problem?

It's somewhat counter-intuitive, but the () operator is what you're looking for. From the docs, operator() is defined as "Return[ing] the mapped value for a key x. The operator is only available for total maps."
Source: http://www.boost.org/doc/libs/1_54_0/libs/icl/doc/html/boost_icl/function_reference/selection.html

Related

Supercollider: Applying FX to a group seems to affect other groups

I have created separate groups on supercollider for each instrument so that I may apply FX, like echo, to the group and only affect one instrument rather than all instruments playing on that sc server.
On the node tree, the echo synthdef correctly appears in the required group (box) and the FX is applied to the instrument in that group, but ALSO incorrectly applying echo to the nodes in other groups on the same server.
What I have noticed is that it affects all higher number groups and not lower number groups. Applying FX (echo) to group 1 or group 2 also applies the same FX to groups 3,4,5,6,7,8, etc., but applying FX to group 8 does NOT seem to affect groups 1,2,3,4,5,6,& 7.
I have tested this on the following systems/versions....
supercollider version (1:3.10.0+repack-1ubuntu2) on ubuntu (Linux
Optiplex-3010 5.4.0-70-generic #78-Ubuntu SMP x86_64 x86_64 x86_64
GNU/Linux)
supercollider version (1:3.10.0+repack-1+rpi1) on Linux raspberrypi
4.19.75-v7l+ #1270 SMP armv7l GNU/Linux
supercollider version (1:3.8.0~repack-2) on Linux advent
5.0.0-32-generic #34~18.04.2-Ubuntu SMP 2019 i686 i686 i686 GNU/Linux
Is this a known bug? Can I make any manual changes to the supercollider code to resolve this? If not, then it will have to be a separate RPi4 for each instrument, underutilising each RPi and increasing project costs significantly.
Regards to all SCcoders,
Phil
Requested code:
(SynthDef(\echo,{|in, out, length = 1, fb = 0.6, sep = 1|
var input = In.ar(in, 2);
var feedback = LocalIn.ar(2);
var output = LeakDC.ar(feedback*fb + input);
LocalOut.ar(DelayC.ar(output, length, sep));
ReplaceOut.ar(out, output);
}).writeDefFile("/home/phil/Supercollider/syndef");)
I instigate the echo via OSC over tcp/ip, but essentially it would be called by:
/s_new s 'echo' i 1028 i 0 i 8 //in this case 1028 is the node for track 8
//and 8 is the SC group
and adjusted by:
/n_set i 1028 s 'fb' f 0.7
UPDATE: In answer to Fizz's comment about...
how I created groups, initially in the startup.scd file and then clear every time the live performance stops and create every time the live performance starts...
oscStream.Clear();
oscStream << osc::BeginBundleImmediate
<< osc::BeginMessage("/g_freeAll") << 1 << osc::EndMessage
<< osc::BeginMessage("/clearSched") << osc::EndMessage
<< osc::EndBundle;
transmitSocketMelody1.Send(oscStream.Data(),oscStream.Size());
oscStream.Clear();
oscStream << osc::BeginMessage("/g_new") << 1 << 0 << 0
<< osc::EndMessage;
transmitSocketMelody1.Send(oscStream.Data(),oscStream.Size());
oscStream.Clear();
oscStream << osc::BeginMessage("/g_new") << 2 << 0 << 0
<< osc::EndMessage;
transmitSocketMelody1.Send(oscStream.Data(),oscStream.Size());
which is the equivalent of...
s.sendMsg(\g_freeAll, 0);
s.sendMsg(\clearSched);
s.sendMsg(\g_new,1,0,0);
s.sendMsg(\g_new,2,0,0);
Order that I create synthdefs...
The echo synthdef (already shown above) is the first created via the supercollider startup.scd file.
Then the sound synthdefs are created in numerical order Piano1, Piano2, Piano3, etc... also via the supercollider startup.scd file.
s.sendMsg(\b_allocRead, 101,"piano-p-c4.wav"); //buffer 101
SynthDef.new("piano1", {|amp=1, freq|
var output,sig,sig2;
sig=PlayBuf.ar(2, 101, freq/523.25, doneAction:2);
sig=sig*amp;
sig = Splay.ar(sig);
Out.ar(0,sig!2);
}).store;
s.sendMsg(\b_allocRead, 111,"piano-p-c5.wav"); //buffer 111
SynthDef.new("piano2", {|amp=1, freq|
var sig;
sig=PlayBuf.ar(2, 111, freq/523.25, doneAction:2);
sig=sig*amp;
sig = Splay.ar(sig);
Out.ar(0,sig!2);
}).store;
The piano synthdefs are played live from the commands..
oscStream_melody1 << osc::BeginMessage("/s_new")
<< "piano1"
<< NextSCNode << 0 << 1
<< "freq" << OscFrequency[OSCOctave][OSCNote]
<< "amp" << Melody1Amplitude*Melody1Volume
<< osc::EndMessage
transmitSocketMelody1.Send(oscStream_melody1.Data(),oscStream_melody1.Size());
oscStream_melody2 << osc::BeginMessage("/s_new")
<< "piano2"
<< NextSCNode << 0 << 2
<< "freq" << OscFrequency[OSCOctave][OSCNote]
<< "amp" << Melody2Amplitude*Melody2Volume
<< osc::EndMessage;
transmitSocketMelody2.Send(oscStream_melody2.Data(),oscStream_melody2.Size());
which is the equivalent of...
s.sendMsg("/s_new","\piano1",1234, 0, 1,"freq",440,"amp", 0.8)
s.sendMsg("/s_new","\piano2",1235, 0, 2,"freq",880,"amp", 0.7)
echo is turned on, modified and off live thus...
s.sendMsg(\s_new,'echo',1001,0,X); //where X is the group to apply to
s.sendMsg(\n_set, 1001,'fb',0.5,'length',1,'sep',1);
s.sendMsg(\n_free, 1001);
but this turns on the echo for all groups with higher numbers, e.g group2, group3, group4 etc.
Whereas if reverb is set on group2 it affects group3, group4, etc but not group1.
Without seeing any code, this is hard to answer, but I wonder if you are using buses? Your instrument group should output to an echo bus and that bus should output to where you wish the sound to go next - another FX bus or to 0.
You'd want to draw a block diagram of your groups, connecting inputs and outputs. Keep in mind that any FX group can take ins from multiple places, but will only have one output. This means you may end up running the same FX synth more than once, for example, if you want echo on groups A and B, but then you want group B to go to a compressor afterwards.
Make sure to declare every bus, give it a good variable name and remember to pass it as an argument on synth creation.

Getting vehicle ID

I am trying to get vehicle id as follow:
mobility = TraCIMobilityAccess().get(getParentModule());
assert(mobility);
traci = mobility->getCommandInterface();
traciVehicle = mobility->getVehicleCommandInterface();
cout<< mobility->getExternalId();
But it returns an invalid vehicle id. What is wrong?
Please help me to solve this problem. Thanks.
What do you mean by an invalid vehicle id? The way you are getting the identifier is the one used by sumo. If that is the case, can you specify what do you expect as an identifier? (that of omnet which starts from [1]?)
As the id of SUMO and that one apparent in omnet are not the same (the order of creation), you may add the following to get your own id (that matches the one of omnet) :
in the ".h" file of your TraCIDemo11p, add your id:
protected:
int your_id;//added
in the ".c" file of your TraCIDemo11p, affect the index in your id:
if (stage == 0) {
...
your_id = getParentModule()->getIndex();//added
...
next, in the place you want to verify a statement, add this:
EV << "My SUMO id = " << mobility->getExternalId() << endl;
EV << "My VEINS id = " << your_id /*or just : getParentModule()->getIndex()*/<< endl;
I hope this helps.
You can try the FindModlue::findHost() in DemoBaseApplLayer::initialize(int stage) in DemoBaseApplLayer.cc:
EV << FindModule<BaseMobility*>::findHost(getParentModule())->getId() << endl;
It will first return the host module and then use the getId() function to get its id.
For better understanding:
Firstly, you can run the simulation to see the indexing of the whole simulation and it would be like this:
Simulation information in veins
As read from the figure, each objects are assigned to a number, e.g. node[0]has the id 7, besides that, each sub-modules are also assigned with id numbers, e.g.
node[0] id = 7
appl id = 8
nic id = 9
veinsmobility id = 10
All of this ids (7,8,9,10) point to the node[0], which means you can use thoese ids to identify a specific car.
In the default DemoBaseApplLayer.cc, you can find
mac = FindModule<DemoBaseApplLayerToMac1609_4Interface*>::findSubModule(getParentModule());
and
myId = mac->getMACAddress();
in the initialization function void DemoBaseApplLayer::initialize(int stage).
Therefore, you can already use the myId as the vehicle id.
By the way, the reason that you get the 18 and 20 for vehicle id, is that the returned module might just be the host module and the sub-module, e.g. 18 is for the node[*] and the 20 is for its nic sub-module.

How can i separate a full name?

I've to take the right part and clean it after it comparate with the middle part and save if are equal
> #!/usr/bin/env ruby
require 'rubygems'
require 'levenshtein'
require 'csv'
# Extending String class for blank? method
class String
def blank?
self.strip.empty?
end
end
# In
lines = CSV.read('entrada.csv')
lines.each do |line|
id = line[0].upcase.strip
left = line[1].upcase.strip
right = line[2].upcase.strip
eduardo = line[2].upcase.split(' ','de')
line[0] = id
line[1] = left
line[2] = right
line[4] = eduardo[0]+eduardo[1]
distance = Levenshtein.distance left, right
line << 99 if (left.blank? or right.blank?)
line << distance unless (left.blank? or right.blank?)
end
# Out
# counter = 0
CSV.open('salida.csv', 'w') do |csv|
lines.each do |line|
# counter = counter + 1 if line[3] <= 3
csv << line
end
end
# p counter
The middle is the correct the rigth i should correct
Some examples:
Eduardo | Abner | Herrera | Herrera -> Eduardo Herrera
Angel | De | Leon -> Angel De Leon
Maira | Angelina | de | Leon -> Maira De Leon
Marquilla | Gutierrez | Petronilda |De | Leon -> Marquilla Petronilda
First order of business is to come up with some rules. Based on your examples, and Spanish naming customs, here's my stab at the rules.
A name has a forename, paternal surname, and optional maternal surname.
A forename can be multiple words.
A surname can be multiple words linked by a de, y, or e.
So ['Marquilla', 'Gutierrez', 'Petronilda', 'De', 'Leon'] should be { forename: 'Marquilla', paternal_surname: 'Gutierrez', maternal_surname: 'Petronilda de Leon' }
To simplify the process, I'd first join any composite surnames into one field. ['Marquilla', 'Gutierrez', 'Petronilda', 'De', 'Leon'] becomes ['Marquilla', 'Gutierrez', 'Petronilda De Leon']. Watch out for cases like ['Angel', 'De', 'Leon'] in which case the surname is probably De Leon.
Once that's done, figuring out which part is which becomes easier.
name = {}
if parts.length == 1
error?
# The special case of only two parts: forename paternal_surname
elsif parts.length == 2
name = {
forename: parts[0],
paternal_surname: parts[1]
}
# forename paternal_surname maternal_surname
else
# The forename can have multiple parts, so work from the
# end and whatever's left is their forename.
name[:maternal_surname] = parts.pop
name[:paternal_surname] = parts.pop
name[:forename] = parts.join(" ")
end
There's a lot of ambiguity in Spanish naming, so this can only be an educated guess at what their actual name is. You'll probably have to tweak the rules as you learn more about the dataset. For example, I'm pretty sure handling of de is not that simple. For example...
One Leocadia Blanco Álvarez, married to a Pedro Pérez Montilla, may be addressed as Leocadia Blanco de Pérez or as Leocadia Blanco Álvarez de Pérez
In that case ['Marquilla', 'Gutierrez', 'Petronilda', 'De', 'Leon'] becomes ['Marquilla', 'Gutierrez', 'Petronilda', 'De Leon'] which is { forename: 'Marquilla', paternal_surname: 'Gutierrez', maternal_surname: 'Petronilda', married_to: 'Leon' } or 'Marquilla Gutierrez Petronilda who is married to someone whose parental surname is Leon.
Good luck.
I would add more columns to the database, like last_name1, last_name2, last_name3, etc, and make them optional (don't put validations on those attributes). Hope that answers your question!

Generating XML with cdata using Ox?

I need to generate XML using ox but didn't get much help from the documentation. I need to generate XML like this:
<Jobpostings>
<Postings>
<Posting>
<JobTitle><cdata>Programmer Analyst 3-IT</cdata></JobTitle>
<Location><cdata>Romania,Bucharest...</cdata></Location>
<CountryCode><cdata>US</cdata> </CountryCode>
<JobDescription><cdata>class technology to develop.</cdata></JobDescription>
</Posting>
</Postings>
</jobpostings>
I have the data inside the tags as strings in variables like this:
jobtitle = "Programmer Analyst 3-IT" and so on...
I am currently using Nokogiri to generate XML but I need to work on large data, and, for the performance sake I am moving to Ox.
Any ideas on how to do this?
It's pretty simple, you just initialize new elements and append them to other elements. Unfortunately there isn't an XML builder in the Ox library though... Here's an example:
require 'ox'
include Ox
source = Document.new
jobpostings = Element.new('Jobpostings')
source << jobpostings
postings = Element.new('Postings')
jobpostings << postings
posting = Element.new('Posting')
postings << posting
jobtitle = Element.new('JobTitle')
posting << jobtitle
jobtitle << CData.new('Programmer Analyst 3-IT')
location = Element.new('Location')
posting << location
location << CData.new('Romania,Bucharest...')
countrycode = Element.new('CountryCode')
posting << countrycode
countrycode << CData.new('US')
countrycode << ' '
jobdescription = Element.new('JobDescription')
posting << jobdescription
jobdescription << CData.new('class technology to develop.')
puts dump(source)
Returns:
<Jobpostings>
<Postings>
<Posting>
<JobTitle>
<![CDATA[Programmer Analyst 3-IT]]>
</JobTitle>
<Location>
<![CDATA[Romania,Bucharest...]]>
</Location>
<CountryCode>
<![CDATA[US]]> </CountryCode>
<JobDescription>
<![CDATA[class technology to develop.]]>
</JobDescription>
</Posting>
</Postings>
</Jobpostings>

Household mail merge (code golf)

I wrote some mail merge code the other day and although it works I'm a turned off by the code. I'd like to see what it would look like in other languages.
So for the input the routine takes a list of contacts
Jim,Smith,2681 Eagle Peak,,Bellevue,Washington,United States,98004
Erica,Johnson,2681 Eagle Peak,,Bellevue,Washington,United States,98004
Abraham,Johnson,2681 Eagle Peak,,Bellevue,Washington,United States,98004
Marge,Simpson,6388 Lake City Way,,Burnaby,British Columbia,Canada,V5A 3A6
Larry,Lyon,52560 Free Street,,Toronto,Ontario,Canada,M4B 1V7
Ted,Simpson,6388 Lake City Way,,Burnaby,British Columbia,Canada,V5A 3A6
Raoul,Simpson,6388 Lake City Way,,Burnaby,British Columbia,Canada,V5A 3A6
It will then merge lines with the same address and surname into one record. Assume the rows are unsorted). The code should also be flexible enough that fields can be supplied in any order (so it will need to take field indexes as parameters). For a family of two it concatenates both first name fields. For a family of three or more the first name is set to "the" and the lastname is set to "surname family".
Erica and Abraham,Johnson,2681 Eagle Peak,,Bellevue,Washington,United States,98004
Larry,Lyon,52560 Free Street,,Toronto,Ontario,Canada,M4B 1V7
The,Simpson Family,6388 Lake City Way,,Burnaby,British Columbia,Canada,V5A 3A6
Jim,Smith,2681 Eagle Peak,,Bellevue,Washington,United States,98004
My C# implementation of this is:
var source = File.ReadAllLines(#"sample.csv").Select(l => l.Split(','));
var merged = HouseholdMerge(source, 0, 1, new[] {1, 2, 3, 4, 5});
public static IEnumerable<string[]> HouseholdMerge(IEnumerable<string[]> data, int fnIndex, int lnIndex, int[] groupIndexes)
{
Func<string[], string> groupby = fields => String.Join("", fields.Where((f, i) => groupIndexes.Contains(i)));
var groups = data.OrderBy(groupby).GroupBy(groupby);
foreach (var group in groups)
{
string[] result = group.First().ToArray();
if (group.Count() == 2)
{
result[fnIndex] += " and " + group.ElementAt(1)[fnIndex];
}
else if (group.Count() > 2)
{
result[fnIndex] = "The";
result[lnIndex] += " Family";
}
yield return result;
}
}
I don't like how I've had to do the groupby delegate. I'd like if C# had some way to convert a string expression to a delegate. e.g. Func groupby = f => "f[2] + f[3] + f[4] + f[5] + f[1];" I have a feeling something like this can probably be done in Lisp or Python. I look forward to seeing nicer implementation in other languages.
Edit: Where did the community wiki checkbox go? Some mod please fix that.
Ruby — 181 155
Name/surname indexes are in code:a and b. Input data is from ARGF.
a,b=0,1
[*$<].map{|i|i.strip.split ?,}.group_by{|i|i.rotate(a).drop 1}.map{|i,j|k,l,m=j
k[a]+=' and '+l[a]if l
(k[a]='The';k[b]+=' Family')if m
puts k*','}
Python - not golfed
I'm not sure what the order of the rows should be if the indices are not 0 and 1 for the input file
import csv
from collections import defaultdict
class HouseHold(list):
def __init__(self, fn_idx, ln_idx):
self.fn_idx = fn_idx
self.ln_idx = ln_idx
def append(self, item):
self.item = item
list.append(self, item[self.fn_idx])
def get_value(self):
fn_idx = self.fn_idx
ln_idx = self.ln_idx
item = self.item
addr = [j for i,j in enumerate(item) if i not in (fn_idx, ln_idx)]
if len(self) < 3:
fn, ln = " and ".join(self), item[ln_idx]
else:
fn, ln = "The", item[ln_idx]+" Family"
return [fn, ln] + addr
def source(fname):
with open(fname) as in_file:
for item in csv.reader(in_file):
yield item
def household_merge(src, fn_idx, ln_idx, groupby):
res = defaultdict(lambda:HouseHold(fn_idx, ln_idx))
for item in src:
key = tuple(item[x] for x in groupby)
res[key].append(item)
return res.values()
data = household_merge(source("sample.csv"), 0, 1, [1,2,3,4,5,6,7])
with open("result.csv", "w") as out_file:
csv.writer(out_file).writerows(item.get_value() for item in data)
Python - 178 chars
import sys
d={}
for x in sys.stdin:F,c,A=x.partition(',');d[A]=d.get(A,[])+[F]
print"".join([" and ".join(v)+c+A,"The"+c+A.replace(c,' Family,',1)][2<len(v)]for A,v in d.items())
Output
Jim,Smith,2681 Eagle Peak,,Bellevue,Washington,United States,98004
The,Simpson Family,6388 Lake City Way,,Burnaby,British Columbia,Canada,V5A 3A6
Larry,Lyon,52560 Free Street,,Toronto,Ontario,Canada,M4B 1V7
Erica and Abraham,Johnson,2681 Eagle Peak,,Bellevue,Washington,United States,98004
Python 2.6.6 - 287 Characters
This assumes you can hard code a filename (named i). If you want to take input from command line, this goes up ~16 chars.
from itertools import*
for z,g in groupby(sorted([l.split(',')for l in open('i').readlines()],key=lambda x:x[1:]), lambda x:x[2:]):
l=list(g);r=len(l);k=','.join(z);o=l[0]
if r>2:print'The,'+o[1],"Family,"+k,
elif r>1:print o[0],"and",l[1][0]+","+o[1]+","+k,
else:print','.join(o),
Output
Erica and Abraham,Johnson,2681 Eagle Peak,,Bellevue,Washington,United States,98004
Larry,Lyon,52560 Free Street,,Toronto,Ontario,Canada,M4B 1V7
The,Simpson Family,6388 Lake City Way,,Burnaby,British Columbia,Canada,V5A 3A6
Jim,Smith,2681 Eagle Peak,,Bellevue,Washington,United States,98004
I'm sure this could be improved upon, but it is getting late.
Haskell - 341 321
(Changes as per comments).
Unfortunately Haskell has no standard split function which makes this rather long.
Input to stdin, output on stdout.
import List
import Data.Ord
main=interact$unlines.e.lines
s[]=[]
s(',':x)=s x
s l#(x:y)=let(h,i)=break(==k)l in h:(s i)
t[]=[]
t x=tail x
h=head
m=map
k=','
e l=m(t.(>>=(k:)))$(m c$groupBy g$sortBy(comparing t)$m s l)
c(x:[])=x
c(x:y:[])=(h x++" and "++h y):t x
c x="The":((h$t$h x)++" Family"):(t$t$h x)
g a b=t a==t b
Lua, 434 bytes
x,y=1,2 s,p,r,a=string.gsub,pairs,io.read,{}for j,b,c,d,e,f,g,h,i in r('*a'):gmatch('('..('([^,]*),'):rep(7)..'([^,]*))\n')
do k=s(s(s(j,b,''),c,''),'[,%s]','')for l,m in p(a)do if not m.f and (m[y]:match(c) and m[9]==k) then z=1
if m.d then m[x]="The"m[y]=m[y]..' family'm.f=1 else m[x]=m[x].." and "..b m.d=1 end end end if not z then
a[#a+1]={b,c,d,e,f,g,h,i,k} end z=nil end for k,v in p(a)do v[9]=nil print(table.concat(v,','))end

Resources